本文整理汇总了Python中scipy.sparse.random方法的典型用法代码示例。如果您正苦于以下问题:Python sparse.random方法的具体用法?Python sparse.random怎么用?Python sparse.random使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.sparse
的用法示例。
在下文中一共展示了sparse.random方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_0201_sparse_matmul
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import random [as 别名]
def test_0201_sparse_matmul(self):
""" The testbed for checking different ways of invoking matrix-matrix multiplications """
return
for n in [50, 100, 200, 400, 800, 1600, 3200]:
print()
for dens in [0.001, 0.002, 0.004, 0.008, 0.016, 0.032, 0.064, 0.128, 0.256]:
asp = sprs.random(n, n, format='csr', density=dens)
bsp = sprs.random(n, n, format='csr', density=dens)
t1 = timer()
cmat1 = np.dot(asp, bsp)
t2 = timer(); ts =t2-t1; #print('runtime sparse ', ts)
adn = asp.toarray()
bdn = bsp.toarray()
t1 = t2
cmat2 = np.dot(adn, bdn)
t2 = timer(); td =t2-t1; #print('runtime dense ', td)
t1 = t2
print('dens, ratio {:5d}, {:.6f} {:.6f} {:.6f} {:.6f}'.format(n, dens, td, ts, td/ts))
示例2: build_dataset
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import random [as 别名]
def build_dataset(n_samples=50, n_features=200, n_targets=1, sparse_X=False):
"""Build samples and observation for linear regression problem."""
random_state = np.random.RandomState(0)
if n_targets > 1:
w = random_state.randn(n_features, n_targets)
else:
w = random_state.randn(n_features)
if sparse_X:
X = sparse.random(n_samples, n_features, density=0.5, format='csc',
random_state=random_state)
else:
X = np.asfortranarray(random_state.randn(n_samples, n_features))
y = X.dot(w)
return X, y
示例3: testCopytoExecution
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import random [as 别名]
def testCopytoExecution(self):
a = ones((2, 3), chunk_size=1)
b = tensor([3, -1, 3], chunk_size=2)
copyto(a, b, where=b > 1)
res = self.executor.execute_tensor(a, concat=True)[0]
expected = np.array([[3, 1, 3], [3, 1, 3]])
np.testing.assert_equal(res, expected)
a = ones((2, 3), chunk_size=1)
b = tensor(np.asfortranarray(np.random.rand(2, 3)), chunk_size=2)
copyto(b, a)
res = self.executor.execute_tensor(b, concat=True)[0]
expected = np.asfortranarray(np.ones((2, 3)))
np.testing.assert_array_equal(res, expected)
self.assertTrue(res.flags['F_CONTIGUOUS'])
self.assertFalse(res.flags['C_CONTIGUOUS'])
示例4: testAstypeExecution
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import random [as 别名]
def testAstypeExecution(self):
raw = np.random.random((10, 5))
arr = tensor(raw, chunk_size=3)
arr2 = arr.astype('i8')
res = self.executor.execute_tensor(arr2, concat=True)
np.testing.assert_array_equal(res[0], raw.astype('i8'))
raw = sps.random(10, 5, density=.2)
arr = tensor(raw, chunk_size=3)
arr2 = arr.astype('i8')
res = self.executor.execute_tensor(arr2, concat=True)
self.assertTrue(np.array_equal(res[0].toarray(), raw.astype('i8').toarray()))
raw = np.asfortranarray(np.random.random((10, 5)))
arr = tensor(raw, chunk_size=3)
arr2 = arr.astype('i8', order='C')
res = self.executor.execute_tensor(arr2, concat=True)[0]
np.testing.assert_array_equal(res, raw.astype('i8'))
self.assertTrue(res.flags['C_CONTIGUOUS'])
self.assertFalse(res.flags['F_CONTIGUOUS'])
示例5: testWhereExecution
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import random [as 别名]
def testWhereExecution(self):
raw_cond = np.random.randint(0, 2, size=(4, 4), dtype='?')
raw_x = np.random.rand(4, 1)
raw_y = np.random.rand(4, 4)
cond, x, y = tensor(raw_cond, chunk_size=2), tensor(raw_x, chunk_size=2), tensor(raw_y, chunk_size=2)
arr = where(cond, x, y)
res = self.executor.execute_tensor(arr, concat=True)
self.assertTrue(np.array_equal(res[0], np.where(raw_cond, raw_x, raw_y)))
raw_cond = sps.csr_matrix(np.random.randint(0, 2, size=(4, 4), dtype='?'))
raw_x = sps.random(4, 1, density=.1)
raw_y = sps.random(4, 4, density=.1)
cond, x, y = tensor(raw_cond, chunk_size=2), tensor(raw_x, chunk_size=2), tensor(raw_y, chunk_size=2)
arr = where(cond, x, y)
res = self.executor.execute_tensor(arr, concat=True)[0]
self.assertTrue(np.array_equal(res.toarray(),
np.where(raw_cond.toarray(), raw_x.toarray(), raw_y.toarray())))
示例6: testArgwhereExecution
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import random [as 别名]
def testArgwhereExecution(self):
x = arange(6, chunk_size=2).reshape(2, 3)
t = argwhere(x > 1)
res = self.executor.execute_tensor(t, concat=True)[0]
expected = np.argwhere(np.arange(6).reshape(2, 3) > 1)
np.testing.assert_array_equal(res, expected)
data = np.asfortranarray(np.random.rand(10, 20))
x = tensor(data, chunk_size=10)
t = argwhere(x > 0.5)
res = self.executor.execute_tensor(t, concat=True)[0]
expected = np.argwhere(data > 0.5)
np.testing.assert_array_equal(res, expected)
self.assertTrue(res.flags['F_CONTIGUOUS'])
self.assertFalse(res.flags['C_CONTIGUOUS'])
示例7: testSortIndicesExecution
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import random [as 别名]
def testSortIndicesExecution(self):
# only 1 chunk when axis = -1
raw = np.random.rand(100, 10)
x = tensor(raw, chunk_size=10)
r = sort(x, return_index=True)
sr, si = self.executor.execute_tensors(r)
np.testing.assert_array_equal(sr, np.take_along_axis(raw, si, axis=-1))
x = tensor(raw, chunk_size=(22, 4))
r = sort(x, return_index=True)
sr, si = self.executor.execute_tensors(r)
np.testing.assert_array_equal(sr, np.take_along_axis(raw, si, axis=-1))
raw = np.random.rand(100)
x = tensor(raw, chunk_size=23)
r = sort(x, axis=0, return_index=True)
sr, si = self.executor.execute_tensors(r)
np.testing.assert_array_equal(sr, raw[si])
示例8: testArgsort
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import random [as 别名]
def testArgsort(self):
# only 1 chunk when axis = -1
raw = np.random.rand(100, 10)
x = tensor(raw, chunk_size=10)
xa = argsort(x)
r = self.executor.execute_tensor(xa, concat=True)[0]
np.testing.assert_array_equal(np.sort(raw), np.take_along_axis(raw, r, axis=-1))
x = tensor(raw, chunk_size=(22, 4))
xa = argsort(x)
r = self.executor.execute_tensor(xa, concat=True)[0]
np.testing.assert_array_equal(np.sort(raw), np.take_along_axis(raw, r, axis=-1))
raw = np.random.rand(100)
x = tensor(raw, chunk_size=23)
xa = argsort(x, axis=0)
r = self.executor.execute_tensor(xa, concat=True)[0]
np.testing.assert_array_equal(np.sort(raw, axis=0), raw[r])
示例9: testShape
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import random [as 别名]
def testShape(self):
raw = np.random.RandomState(0).rand(4, 3)
x = mt.tensor(raw, chunk_size=2)
s = shape(x)
ctx, executor = self._create_test_context(self.executor)
with ctx:
result = executor.execute_tensors(s)
self.assertSequenceEqual(result, (4, 3))
s = shape(x[x > .5])
result = executor.execute_tensors(s)
expected = np.shape(raw[raw > .5])
self.assertSequenceEqual(result, expected)
s = shape(0)
result = executor.execute_tensors(s)
expected = np.shape(0)
self.assertSequenceEqual(result, expected)
示例10: testTakeExecution
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import random [as 别名]
def testTakeExecution(self):
data = np.random.rand(10, 20, 30)
t = tensor(data, chunk_size=10)
a = t.take([4, 1, 2, 6, 200])
res = self.executor.execute_tensor(a, concat=True)[0]
expected = np.take(data, [4, 1, 2, 6, 200])
np.testing.assert_array_equal(res, expected)
a = take(t, [5, 19, 2, 13], axis=1)
res = self.executor.execute_tensor(a, concat=True)[0]
expected = np.take(data, [5, 19, 2, 13], axis=1)
np.testing.assert_array_equal(res, expected)
with self.assertRaises(ValueError):
take(t, [1, 3, 4], out=tensor(np.random.rand(4)))
out = tensor([1, 2, 3, 4])
a = take(t, [4, 19, 2, 8], out=out)
res = self.executor.execute_tensor(out, concat=True)[0]
expected = np.take(data, [4, 19, 2, 8])
np.testing.assert_array_equal(res, expected)
示例11: testHStackExecution
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import random [as 别名]
def testHStackExecution(self):
a_data = np.random.rand(10)
b_data = np.random.rand(20)
a = tensor(a_data, chunk_size=4)
b = tensor(b_data, chunk_size=4)
c = hstack([a, b])
res = self.executor.execute_tensor(c, concat=True)[0]
expected = np.hstack([a_data, b_data])
self.assertTrue(np.array_equal(res, expected))
a_data = np.random.rand(10, 20)
b_data = np.random.rand(10, 5)
a = tensor(a_data, chunk_size=3)
b = tensor(b_data, chunk_size=4)
c = hstack([a, b])
res = self.executor.execute_tensor(c, concat=True)[0]
expected = np.hstack([a_data, b_data])
self.assertTrue(np.array_equal(res, expected))
示例12: testVStackExecution
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import random [as 别名]
def testVStackExecution(self):
a_data = np.random.rand(10)
b_data = np.random.rand(10)
a = tensor(a_data, chunk_size=4)
b = tensor(b_data, chunk_size=4)
c = vstack([a, b])
res = self.executor.execute_tensor(c, concat=True)[0]
expected = np.vstack([a_data, b_data])
self.assertTrue(np.array_equal(res, expected))
a_data = np.random.rand(10, 20)
b_data = np.random.rand(5, 20)
a = tensor(a_data, chunk_size=3)
b = tensor(b_data, chunk_size=4)
c = vstack([a, b])
res = self.executor.execute_tensor(c, concat=True)[0]
expected = np.vstack([a_data, b_data])
self.assertTrue(np.array_equal(res, expected))
示例13: testDStackExecution
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import random [as 别名]
def testDStackExecution(self):
a_data = np.random.rand(10)
b_data = np.random.rand(10)
a = tensor(a_data, chunk_size=4)
b = tensor(b_data, chunk_size=4)
c = dstack([a, b])
res = self.executor.execute_tensor(c, concat=True)[0]
expected = np.dstack([a_data, b_data])
self.assertTrue(np.array_equal(res, expected))
a_data = np.random.rand(10, 20)
b_data = np.random.rand(10, 20)
a = tensor(a_data, chunk_size=3)
b = tensor(b_data, chunk_size=4)
c = dstack([a, b])
res = self.executor.execute_tensor(c, concat=True)[0]
expected = np.dstack([a_data, b_data])
self.assertTrue(np.array_equal(res, expected))
示例14: testColumnStackExecution
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import random [as 别名]
def testColumnStackExecution(self):
a_data = np.array((1, 2, 3))
b_data = np.array((2, 3, 4))
a = tensor(a_data, chunk_size=1)
b = tensor(b_data, chunk_size=2)
c = column_stack((a, b))
res = self.executor.execute_tensor(c, concat=True)[0]
expected = np.column_stack((a_data, b_data))
np.testing.assert_equal(res, expected)
a_data = np.random.rand(4, 2, 3)
b_data = np.random.rand(4, 2, 3)
a = tensor(a_data, chunk_size=1)
b = tensor(b_data, chunk_size=2)
c = column_stack((a, b))
res = self.executor.execute_tensor(c, concat=True)[0]
expected = np.column_stack((a_data, b_data))
np.testing.assert_equal(res, expected)
示例15: testAllAnyExecution
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import random [as 别名]
def testAllAnyExecution(self):
raw1 = np.zeros((10, 15))
raw2 = np.ones((10, 15))
raw3 = np.array([[True, False, True, False], [True, True, True, True],
[False, False, False, False], [False, True, False, True]])
arr1 = tensor(raw1, chunk_size=3)
arr2 = tensor(raw2, chunk_size=3)
arr3 = tensor(raw3, chunk_size=4)
self.assertFalse(self.executor.execute_tensor(arr1.all())[0])
self.assertTrue(self.executor.execute_tensor(arr2.all())[0])
self.assertFalse(self.executor.execute_tensor(arr1.any())[0])
self.assertTrue(self.executor.execute_tensor(arr1.any()))
np.testing.assert_array_equal(raw3.all(axis=1),
self.executor.execute_tensor(arr3.all(axis=1))[0])
np.testing.assert_array_equal(raw3.any(axis=0),
self.executor.execute_tensor(arr3.any(axis=0))[0])
raw = sps.random(10, 10, density=.5) > .5
arr = tensor(raw, chunk_size=3)
self.assertEqual(raw.A.all(), self.executor.execute_tensor(arr.all())[0])
self.assertEqual(raw.A.any(), self.executor.execute_tensor(arr.any())[0])