简体   繁体   English

将 function 应用于 numpy 矩阵列表

[英]Apply function to list of numpy matrices

I am working on python.我正在研究 python。 So, I got a main matrix lets call it "X".所以,我得到了一个主矩阵,我们称之为“X”。 Then I got a list of other 5000 matrices which is called Z. I want to apply between X and each matrix within Z the sklearn.metrics.pairwise.cosine_similarity function and take the average within matrix.然后我得到了一个名为 Z 的其他 5000 个矩阵的列表。我想在 X 和 Z 内的每个矩阵之间应用 sklearn.metrics.pairwise.cosine_similarity function 并取矩阵内的平均值。 I do not want to do a for loop as it takes a ton of time.我不想做一个 for 循环,因为它需要大量时间。 Is there a way, without multiprocessing to do this without the for loop?有没有办法在没有 for 循环的情况下进行多处理?

from sklearn.metrics.pairwise import cosine_similarity
Z = [numpy.random.rand(7,12),numpy.random.rand(7,12),numpy.random.rand(7,12)] #in my example X contains 5000 matrices, here only 3
X = numpy.random.rand(7,12)

#the loop I want to get rid of
cosine_similarities = []
for matrix in Z:
    cosine_similarities.append(cosine_similarity(X, matrix).mean())

Here is a way to avoid loops.这是一种避免循环的方法。 If all matrices in Z are of same shape, you can simply stack them and find cosine similarity and then split them:如果Z中的所有矩阵都具有相同的形状,您可以简单地将它们堆叠并找到余弦相似度,然后将它们拆分:

Y = np.vstack(Z)
cosine_similarities  = sklearn.metrics.pairwise.cosine_similarity(X, Y).reshape(len(Z),X.shape[0],-1)

it will return an array that each row is the cosine similarity of X and elements of Z .它将返回一个数组,其中每一行是X的余弦相似度和Z的元素。 And if you want to take average of that:如果你想取平均值:

cosine_similarities = cosine_similarities.mean((1,2))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM