本文整理汇总了Python中sandbox.util.SparseUtils.SparseUtils.sparseMatrix方法的典型用法代码示例。如果您正苦于以下问题:Python SparseUtils.sparseMatrix方法的具体用法?Python SparseUtils.sparseMatrix怎么用?Python SparseUtils.sparseMatrix使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sandbox.util.SparseUtils.SparseUtils
的用法示例。
在下文中一共展示了SparseUtils.sparseMatrix方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testSparseMatrix
# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import sparseMatrix [as 别名]
def testSparseMatrix(self):
m = 10
n = 15
A = numpy.random.rand(m, n)
rowInds, colInds = A.nonzero()
vals = A[rowInds, colInds]
X = SparseUtils.sparseMatrix(vals, rowInds, colInds, A.shape, "scipy", storagetype="col")
self.assertTrue(X.dtype==A.dtype)
self.assertTrue(X.shape==A.shape)
self.assertTrue(type(X)== scipy.sparse.csc_matrix)
nptst.assert_array_equal(X.toarray(), A)
X = SparseUtils.sparseMatrix(vals, rowInds, colInds, A.shape, "scipy", storagetype="row")
self.assertTrue(X.dtype==A.dtype)
self.assertTrue(X.shape==A.shape)
self.assertTrue(type(X)== scipy.sparse.csr_matrix)
nptst.assert_array_equal(X.toarray(), A)
X = SparseUtils.sparseMatrix(vals, rowInds, colInds, A.shape, "csarray", storagetype="col")
self.assertTrue(X.dtype==A.dtype)
self.assertTrue(X.shape==A.shape)
self.assertTrue(type(X)== sppy.csarray)
self.assertTrue(X.storagetype=="col")
nptst.assert_array_equal(X.toarray(), A)
X = SparseUtils.sparseMatrix(vals, rowInds, colInds, A.shape, "csarray", storagetype="row")
self.assertTrue(X.dtype==A.dtype)
self.assertTrue(X.shape==A.shape)
self.assertTrue(type(X)== sppy.csarray)
self.assertTrue(X.storagetype=="row")
nptst.assert_array_equal(X.toarray(), A)
示例2: shuffleSplitRows
# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import sparseMatrix [as 别名]
def shuffleSplitRows(X, k, testSize, numRows=None, csarray=True, rowMajor=True, colProbs=None):
"""
Take a sparse binary matrix and create k number of train-test splits
in which the test split contains at most testSize elements and the train
split contains the remaining elements from X for each row. The splits are
computed randomly. Returns sppy.csarray objects by default.
:param colProbs: This is the probability of choosing the corresponding column/item. If None, we assume uniform probabilities.
"""
if csarray:
mattype = "csarray"
else:
mattype = "scipy"
if rowMajor:
storagetype = "row"
else:
storagetype = "col"
if numRows == None:
numRows = X.shape[0]
outputRows = False
else:
outputRows = True
trainTestXList = []
omegaList = SparseUtils.getOmegaList(X)
m, n = X.shape
for i in range(k):
trainInd = 0
testInd = 0
trainRowInds = numpy.zeros(X.nnz, numpy.int32)
trainColInds = numpy.zeros(X.nnz, numpy.int32)
testRowInds = numpy.zeros(X.shape[0]*testSize, numpy.int32)
testColInds = numpy.zeros(X.shape[0]*testSize, numpy.int32)
rowSample = numpy.sort(numpy.random.choice(m, numRows, replace=False))
for j in range(m):
if j in rowSample:
if colProbs == None:
inds = numpy.random.permutation(omegaList[j].shape[0])
else:
probs = colProbs[omegaList[j]]
probs /= probs.sum()
inds = numpy.random.choice(omegaList[j].shape[0], omegaList[j].shape[0], p=probs, replace=False)
trainInds = inds[testSize:]
testInds = inds[0:testSize]
else:
trainInds = numpy.arange(omegaList[j].shape[0])
testInds = numpy.array([], numpy.int)
trainRowInds[trainInd:trainInd+trainInds.shape[0]] = numpy.ones(trainInds.shape[0], dtype=numpy.uint)*j
trainColInds[trainInd:trainInd+trainInds.shape[0]] = omegaList[j][trainInds]
trainInd += trainInds.shape[0]
testRowInds[testInd:testInd+testInds.shape[0]] = numpy.ones(testInds.shape[0], dtype=numpy.uint)*j
testColInds[testInd:testInd+testInds.shape[0]] = omegaList[j][testInds]
testInd += testInds.shape[0]
trainRowInds = trainRowInds[0:trainInd]
trainColInds = trainColInds[0:trainInd]
testRowInds = testRowInds[0:testInd]
testColInds = testColInds[0:testInd]
trainX = SparseUtils.sparseMatrix(numpy.ones(trainRowInds.shape[0], numpy.int), trainRowInds, trainColInds, X.shape, mattype, storagetype)
testX = SparseUtils.sparseMatrix(numpy.ones(testRowInds.shape[0], numpy.int), testRowInds, testColInds, X.shape, mattype, storagetype)
if not outputRows:
trainTestXList.append((trainX, testX))
else:
trainTestXList.append((trainX, testX, rowSample))
return trainTestXList