本文整理汇总了Python中sandbox.util.Util.Util.argmaxN方法的典型用法代码示例。如果您正苦于以下问题:Python Util.argmaxN方法的具体用法?Python Util.argmaxN怎么用?Python Util.argmaxN使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sandbox.util.Util.Util
的用法示例。
在下文中一共展示了Util.argmaxN方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: recommendAtk
# 需要导入模块: from sandbox.util.Util import Util [as 别名]
# 或者: from sandbox.util.Util.Util import argmaxN [as 别名]
def recommendAtk(U, V, k, blockSize=1000, omegaList=None, verbose=False):
"""
Compute the matrix Z = U V^T and then find the k largest indices for each row.
"""
blocksize = 1000
numBlocks = int(ceil(U.shape[0]/float(blocksize)))
orderedItems = numpy.zeros((U.shape[0], k), numpy.int32)
scores = numpy.zeros((U.shape[0], k), numpy.float)
for j in range(numBlocks):
logging.debug("Block " + str(j) + " of " + str(numBlocks))
endInd = min(U.shape[0], (j+1)*blocksize)
UV = U[j*blocksize:endInd, :].dot(V.T)
orderedItems[j*blocksize:endInd, :] = Util.argmaxN(UV, k)
rowInds = numpy.repeat(numpy.arange(endInd-j*blocksize), k)
colInds = orderedItems[j*blocksize:endInd, :].flatten()
scores[j*blocksize:endInd, :] = numpy.reshape(UV[rowInds, colInds], (endInd-j*blocksize,k))
#orderedItems[j*blocksize:endInd, :] = Util.argmaxN2d(scores, k)
#Now delete items in omegaList if given
if omegaList != None:
for i in range(j*blocksize, endInd):
nonTrainItems = orderedItems[i, :][numpy.logical_not(numpy.in1d(orderedItems[i, :], omegaList[i]))]
orderedItems[i, 0:nonTrainItems.shape[0]] = nonTrainItems
orderedItems[i, nonTrainItems.shape[0]:] = -1
if verbose:
return orderedItems, scores
else:
return orderedItems
示例2: testArgMaxN
# 需要导入模块: from sandbox.util.Util import Util [as 别名]
# 或者: from sandbox.util.Util.Util import argmaxN [as 别名]
def testArgMaxN(self):
numRuns = 10
for j in range(numRuns):
m = numpy.random.randint(5, 50)
a = numpy.random.rand(m)
inds = numpy.flipud(numpy.argsort(a))
for i in range(m):
nptst.assert_array_equal(Util.argmaxN(a, i), inds[0:i])
#Now test a 2D array
for j in range(numRuns):
m = numpy.random.randint(5, 50)
n = numpy.random.randint(5, 50)
A = numpy.random.rand(m, n)
inds = numpy.argsort(-A, axis=1)
for i in range(n):
nptst.assert_array_equal(Util.argmaxN(A, i), inds[:, 0:i])
示例3: testRecommendAtk
# 需要导入模块: from sandbox.util.Util import Util [as 别名]
# 或者: from sandbox.util.Util.Util import argmaxN [as 别名]
def testRecommendAtk(self):
m = 20
n = 50
r = 3
X, U, s, V, wv = SparseUtils.generateSparseBinaryMatrix((m, n), r, 0.5, verbose=True)
import sppy
X = sppy.csarray(X)
k = 10
orderedItems, scores = MCEvaluator.recommendAtk(U, V, k, verbose=True)
# Now do it manually
Z = U.dot(V.T)
orderedItems2 = Util.argmaxN(Z, k)
scores2 = numpy.fliplr(numpy.sort(Z, 1))[:, 0:k]
nptst.assert_array_equal(orderedItems, orderedItems2)
nptst.assert_array_equal(scores, scores2)
# Test case where we have a set of training indices to remove
# Let's create a random omegaList
omegaList = []
for i in range(m):
omegaList.append(numpy.random.permutation(n)[0:5])
orderedItems = MCEvaluator.recommendAtk(U, V, k, omegaList=omegaList)
orderedItems2 = MCEvaluator.recommendAtk(U, V, k)
# print(omegaList)
# print(orderedItems)
# print(orderedItems2)
for i in range(m):
items = numpy.intersect1d(omegaList[i], orderedItems[i, :])
self.assertEquals(items.shape[0], 0)
items = numpy.union1d(omegaList[i], orderedItems[i, :])
items = numpy.intersect1d(items, orderedItems2[i, :])
nptst.assert_array_equal(items, numpy.sort(orderedItems2[i, :]))