本文整理汇总了Python中sandbox.util.SparseUtils.SparseUtils.generateSparseBinaryMatrix方法的典型用法代码示例。如果您正苦于以下问题:Python SparseUtils.generateSparseBinaryMatrix方法的具体用法?Python SparseUtils.generateSparseBinaryMatrix怎么用?Python SparseUtils.generateSparseBinaryMatrix使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sandbox.util.SparseUtils.SparseUtils
的用法示例。
在下文中一共展示了SparseUtils.generateSparseBinaryMatrix方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testSampleUsers
# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import generateSparseBinaryMatrix [as 别名]
def testSampleUsers(self):
m = 10
n = 15
r = 5
u = 0.3
w = 1-u
X, U, s, V, wv = SparseUtils.generateSparseBinaryMatrix((m,n), r, w, csarray=True, verbose=True, indsPerRow=200)
k = 50
X2, userInds = Sampling.sampleUsers(X, k)
nptst.assert_array_equal(X.toarray(), X2.toarray())
numRuns = 50
for i in range(numRuns):
m = numpy.random.randint(10, 100)
n = numpy.random.randint(10, 100)
k = numpy.random.randint(10, 100)
X, U, s, V, wv = SparseUtils.generateSparseBinaryMatrix((m,n), r, w, csarray=True, verbose=True, indsPerRow=200)
X2, userInds = Sampling.sampleUsers(X, k)
self.assertEquals(X2.shape[0], min(k, m))
self.assertTrue((X.dot(X.T)!=numpy.zeros((m, m)).all()))
self.assertTrue((X2.toarray() == X.toarray()[userInds, :]).all())
self.assertEquals(X.toarray()[userInds, :].nonzero()[0].shape[0], X2.nnz)
示例2: testAverageRocCurve
# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import generateSparseBinaryMatrix [as 别名]
def testAverageRocCurve(self):
m = 50
n = 20
k = 8
u = 20.0 / m
w = 1 - u
X, U, s, V, wv = SparseUtils.generateSparseBinaryMatrix(
(m, n), k, w, csarray=True, verbose=True, indsPerRow=200
)
fpr, tpr = MCEvaluator.averageRocCurve(X, U, V)
import matplotlib
matplotlib.use("GTK3Agg")
import matplotlib.pyplot as plt
# plt.plot(fpr, tpr)
# plt.show()
# Now try case where we have a training set
folds = 1
testSize = 5
trainTestXs = Sampling.shuffleSplitRows(X, folds, testSize)
trainX, testX = trainTestXs[0]
fpr, tpr = MCEvaluator.averageRocCurve(testX, U, V, trainX=trainX)
示例3: testLocalAucApprox
# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import generateSparseBinaryMatrix [as 别名]
def testLocalAucApprox(self):
m = 100
n = 200
k = 2
X, U, s, V, wv = SparseUtils.generateSparseBinaryMatrix((m, n), k, csarray=True, verbose=True)
w = 1.0
localAuc = MCEvaluator.localAUC(X, U, V, w)
samples = numpy.arange(150, 200, 10)
for i, sampleSize in enumerate(samples):
numAucSamples = sampleSize
localAuc2 = MCEvaluator.localAUCApprox(SparseUtils.getOmegaListPtr(X), U, V, w, numAucSamples)
self.assertAlmostEqual(localAuc2, localAuc, 1)
# Try smaller w
w = 0.5
localAuc = MCEvaluator.localAUC(X, U, V, w)
samples = numpy.arange(50, 200, 10)
for i, sampleSize in enumerate(samples):
numAucSamples = sampleSize
localAuc2 = MCEvaluator.localAUCApprox(SparseUtils.getOmegaListPtr(X), U, V, w, numAucSamples)
self.assertAlmostEqual(localAuc2, localAuc, 1)
示例4: testLocalAUC
# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import generateSparseBinaryMatrix [as 别名]
def testLocalAUC(self):
m = 10
n = 20
k = 2
X, U, s, V, wv = SparseUtils.generateSparseBinaryMatrix((m, n), k, 0.5, verbose=True, csarray=True)
Z = U.dot(V.T)
localAuc = numpy.zeros(m)
for i in range(m):
localAuc[i] = sklearn.metrics.roc_auc_score(numpy.ravel(X[i, :].toarray()), Z[i, :])
localAuc = localAuc.mean()
u = 0.0
localAuc2 = MCEvaluator.localAUC(X, U, V, u)
self.assertEquals(localAuc, localAuc2)
# Now try a large r
w = 1.0
localAuc2 = MCEvaluator.localAUC(X, U, V, w)
self.assertEquals(localAuc2, 0)
示例5: testParallelLearnModel
# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import generateSparseBinaryMatrix [as 别名]
def testParallelLearnModel(self):
numpy.random.seed(21)
m = 500
n = 200
k = 5
X = SparseUtils.generateSparseBinaryMatrix((m, n), k, csarray=True)
from wallhack.rankingexp.DatasetUtils import DatasetUtils
X, U, V = DatasetUtils.syntheticDataset1()
u = 0.1
w = 1-u
eps = 0.05
maxLocalAuc = MaxLocalAUC(k, w, alpha=1.0, eps=eps, stochastic=True)
maxLocalAuc.maxIterations = 3
maxLocalAuc.recordStep = 1
maxLocalAuc.rate = "optimal"
maxLocalAuc.t0 = 2.0
maxLocalAuc.validationUsers = 0.0
maxLocalAuc.numProcesses = 4
os.system('taskset -p 0xffffffff %d' % os.getpid())
print(X.nnz/maxLocalAuc.numAucSamples)
U, V = maxLocalAuc.parallelLearnModel(X)
示例6: testOverfit
# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import generateSparseBinaryMatrix [as 别名]
def testOverfit(self):
"""
See if we can get a zero objective on the hinge loss
"""
m = 10
n = 20
k = 5
u = 0.5
w = 1-u
X = SparseUtils.generateSparseBinaryMatrix((m, n), k, w, csarray=True)
eps = 0.001
k = 10
maxLocalAuc = MaxLocalAUC(k, u, eps=eps, stochastic=True)
maxLocalAuc.rate = "constant"
maxLocalAuc.maxIterations = 500
maxLocalAuc.numProcesses = 1
maxLocalAuc.loss = "hinge"
maxLocalAuc.validationUsers = 0
maxLocalAuc.lmbda = 0
print("Overfit example")
U, V, trainMeasures, testMeasures, iterations, time = maxLocalAuc.learnModel(X, verbose=True)
self.assertAlmostEquals(trainMeasures[-1, 0], 0, 3)
示例7: testModelSelect
# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import generateSparseBinaryMatrix [as 别名]
def testModelSelect(self):
m = 50
n = 50
k = 5
u = 0.5
w = 1-u
X = SparseUtils.generateSparseBinaryMatrix((m, n), k, w)
os.system('taskset -p 0xffffffff %d' % os.getpid())
u = 0.2
lmbda = 0.1
gamma = 0.01
learner = BprRecommender(k, lmbda, gamma)
learner.maxIterations = 2
learner.ks = 2**numpy.arange(3, 5)
learner.lmbdaUsers = 2.0**-numpy.arange(1, 3)
learner.lmbdaPoses = 2.0**-numpy.arange(1, 3)
learner.lmbdaNegs = 2.0**-numpy.arange(1, 3)
learner.gammas = 2.0**-numpy.arange(1, 3)
learner.folds = 2
learner.numProcesses = 1
colProbs = numpy.array(X.sum(1)).ravel()
colProbs /= colProbs.sum()
print(colProbs, colProbs.shape)
learner.modelSelect(X, colProbs=colProbs)
示例8: testF1Atk
# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import generateSparseBinaryMatrix [as 别名]
def testF1Atk(self):
m = 10
n = 5
r = 3
X, U, s, V, wv = SparseUtils.generateSparseBinaryMatrix((m, n), r, 0.5, verbose=True)
import sppy
X = sppy.csarray(X)
orderedItems = MCEvaluator.recommendAtk(U * s, V, n)
self.assertAlmostEquals(
MCEvaluator.f1AtK(X, orderedItems, n, verbose=False), 2 * r / float(n) / (1 + r / float(n))
)
m = 20
n = 50
X, U, s, V, wv = SparseUtils.generateSparseBinaryMatrix((m, n), r, 0.5, verbose=True)
k = 5
orderedItems = MCEvaluator.recommendAtk(U * s, V, k)
precision, scoreInds = MCEvaluator.precisionAtK(X, orderedItems, k, verbose=True)
recall, scoreInds = MCEvaluator.recallAtK(X, orderedItems, k, verbose=True)
f1s = numpy.zeros(m)
for i in range(m):
f1s[i] = 2 * precision[i] * recall[i] / (precision[i] + recall[i])
orderedItems = MCEvaluator.recommendAtk(U * s, V, n)
f1s2, scoreInds = MCEvaluator.f1AtK(X, orderedItems, k, verbose=True)
nptst.assert_array_equal(f1s, f1s2)
# Test case where we get a zero precision or recall
orderedItems[5, :] = -1
precision, scoreInds = MCEvaluator.precisionAtK(X, orderedItems, k, verbose=True)
recall, scoreInds = MCEvaluator.recallAtK(X, orderedItems, k, verbose=True)
f1s = numpy.zeros(m)
for i in range(m):
if precision[i] + recall[i] != 0:
f1s[i] = 2 * precision[i] * recall[i] / (precision[i] + recall[i])
f1s2, scoreInds = MCEvaluator.f1AtK(X, orderedItems, k, verbose=True)
nptst.assert_array_equal(f1s, f1s2)
示例9: __init__
# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import generateSparseBinaryMatrix [as 别名]
def __init__(self):
numpy.random.seed(21)
# Create a low rank matrix
self.m = 1000
self.n = 5000
self.k = 10
self.X = SparseUtils.generateSparseBinaryMatrix((self.m, self.n), self.k, csarray=True)
示例10: __init__
# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import generateSparseBinaryMatrix [as 别名]
def __init__(self):
numpy.random.seed(21)
#Create a low rank matrix
m = 500
n = 200
self.k = 8
self.X = SparseUtils.generateSparseBinaryMatrix((m, n), self.k, csarray=True)
print(self.X)
示例11: testSampleUsers2
# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import generateSparseBinaryMatrix [as 别名]
def testSampleUsers2(self):
m = 10
n = 15
r = 5
u = 0.3
w = 1-u
X, U, s, V, wv = SparseUtils.generateSparseBinaryMatrix((m,n), r, w, csarray=True, verbose=True, indsPerRow=200)
k = X.nnz+100
X2, userInds = Sampling.sampleUsers2(X, k)
nptst.assert_array_equal(X.toarray(), X2.toarray())
#Test pruning of cols
k = 500
m = 100
n = 500
u = 0.1
w = 1 - u
X, U, s, V, wv = SparseUtils.generateSparseBinaryMatrix((m,n), r, w, csarray=True, verbose=True, indsPerRow=200)
numpy.random.seed(21)
X2, userInds = Sampling.sampleUsers2(X, k, prune=True)
nnz1 = X2.nnz
self.assertTrue((X2.sum(0)!=0).all())
numpy.random.seed(21)
X2, userInds = Sampling.sampleUsers2(X, k, prune=False)
nnz2 = X2.nnz
self.assertEquals(nnz1, nnz2)
numRuns = 50
for i in range(numRuns):
m = numpy.random.randint(10, 100)
n = numpy.random.randint(10, 100)
k = 500
X, U, s, V, wv = SparseUtils.generateSparseBinaryMatrix((m,n), r, w, csarray=True, verbose=True, indsPerRow=200)
X2, userInds = Sampling.sampleUsers2(X, k)
self.assertTrue((X.dot(X.T)!=numpy.zeros((m, m)).all()))
self.assertTrue((X2.toarray() == X.toarray()[userInds, :]).all())
self.assertEquals(X.toarray()[userInds, :].nonzero()[0].shape[0], X2.nnz)
示例12: testScale
# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import generateSparseBinaryMatrix [as 别名]
def testScale(self):
"""
Look at the scales of the unnormalised gradients.
"""
m = 100
n = 400
k = 3
X = SparseUtils.generateSparseBinaryMatrix((m, n), k, csarray=True)
w = 0.1
eps = 0.001
learner = MaxAUCTanh(k, w)
learner.normalise = False
learner.lmbdaU = 1.0
learner.lmbdaV = 1.0
learner.rho = 1.0
learner.numAucSamples = 100
indPtr, colInds = SparseUtils.getOmegaListPtr(X)
r = numpy.random.rand(m)
U = numpy.random.rand(X.shape[0], k)
V = numpy.random.rand(X.shape[1], k)
gi = numpy.random.rand(m)
gi /= gi.sum()
gp = numpy.random.rand(n)
gp /= gp.sum()
gq = numpy.random.rand(n)
gq /= gq.sum()
permutedRowInds = numpy.array(numpy.random.permutation(m), numpy.uint32)
permutedColInds = numpy.array(numpy.random.permutation(n), numpy.uint32)
maxLocalAuc = MaxLocalAUC(k, w)
normGp, normGq = maxLocalAuc.computeNormGpq(indPtr, colInds, gp, gq, m)
normDui = 0
for i in range(m):
du = learner.derivativeUi(indPtr, colInds, U, V, r, gi, gp, gq, i)
normDui += numpy.linalg.norm(du)
normDui /= float(m)
print(normDui)
normDvi = 0
for i in range(n):
dv = learner.derivativeVi(indPtr, colInds, U, V, r, gi, gp, gq, i)
normDvi += numpy.linalg.norm(dv)
normDvi /= float(n)
print(normDvi)
示例13: setUp
# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import generateSparseBinaryMatrix [as 别名]
def setUp(self):
numpy.set_printoptions(precision=3, suppress=True)
numpy.random.seed(21)
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
self.m = 30
self.n = 20
k = 5
u = 0.1
w = 1-u
self.X = SparseUtils.generateSparseBinaryMatrix((self.m, self.n), k, w, csarray=True)
self.X.prune()
示例14: profileShuffleSplitRows
# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import generateSparseBinaryMatrix [as 别名]
def profileShuffleSplitRows(self):
m = 10000
n = 5000
k = 5
u = 0.1
w = 1-u
X, U, s, V = SparseUtils.generateSparseBinaryMatrix((m,n), k, w, csarray=True, verbose=True, indsPerRow=200)
k2 = 10
testSize = 2
ProfileUtils.profile('Sampling.shuffleSplitRows(X, k2, testSize)', globals(), locals())
示例15: profileSampleUsers
# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import generateSparseBinaryMatrix [as 别名]
def profileSampleUsers(self):
m = 10000
n = 50000
k = 5
u = 0.01
w = 1-u
X, U, s, V, wv = SparseUtils.generateSparseBinaryMatrix((m,n), k, w, csarray=True, verbose=True, indsPerRow=200)
print(X.nnz)
k2 = 100000
ProfileUtils.profile('Sampling.sampleUsers2(X, k2)', globals(), locals())