python矩阵优化
我的输入有一个形状:python矩阵优化,python,math,Python,Math,我的输入有一个形状:(9480,1) 以下是我目前的职能: def f(X): ordered_max_scorers = np.sort(np.max(X, axis=2), axis=1) cd_mat2 = np.array([[0] * len(X)] * len(X), dtype="float64") for ind1, score1 in enumerate(ordered_max_scorers): for ind2, score2 in
(9480,1)
以下是我目前的职能:
def f(X):
ordered_max_scorers = np.sort(np.max(X, axis=2), axis=1)
cd_mat2 = np.array([[0] * len(X)] * len(X), dtype="float64")
for ind1, score1 in enumerate(ordered_max_scorers):
for ind2, score2 in enumerate(ordered_max_scorers[:ind1]):
cognitive_diversity = np.sum((score1 - score2)**2)**(1/2)
cd_mat2[ind1][ind2] = cognitive_diversity
cd_mat2[ind2][ind1] = cognitive_diversity
return cd_mat2
我正试图优化这个函数,使它运行得更快:它被对象A中的对象B调用一次。每次A运行时,B都会被调用几百次(大约20多次),如果这有什么区别的话
我试着用numpy替换:
cognitive_diversity = np.sum((score1 - score2) ** 2) ** (1/2)
与:
但这似乎减慢了代码的速度。
我还尝试使用numba进行优化:
@autojit
def temp(x, y):
return np.sum((score1 - score2) ** 2) ** (1/2)
再次用于认知多样性
cognitive_diversity = temp(score1, score2)
但这也大大降低了代码的速度
如果有人对加速有任何建议,或者对如何正确地重新编写循环以加速它有任何建议,那将是惊人的
编辑
谢谢你的帮助。我用它想出了一个稍微不同的解决方案
#Std jit optimization
@jit()
def cd_matrix_jit(oms,l_oms,l_oms2,cd_mat2):
for ind1 in range(l_oms):
for ind2 in range(l_oms):
d=0.0
for i in range(l_oms2):
d = d + (oms[ind1,i]-oms[ind2,i])**2
cd_mat2[ind1,ind2] = d**(1/2)
return cd_mat2
首先是风格上的一些改进:
np.array([[0] * len(X)] * len(X), dtype="float64")
len(X)
是9,X.shape[0]
。所以这个表达式最好写为
np.zeros((X.shape[0], X.shape[0]))
cd_mat2[ind1, ind2]
i、 e.0的9x9数组(浮动默认数据类型)
可以写成
np.zeros((X.shape[0], X.shape[0]))
cd_mat2[ind1, ind2]
在
您试图通过使用上(或下)三角形来避免重复计算。对于numpy
,这可能不值得。如果可以使用一个表达式而不是循环执行整个计算,则即使某些值重复,也会更快
比如:
cd_mat2 = np.sqrt(np.sum((ordered_max_scorers[:,None] - ordered_max_scorers)**2), axis=1))
一个样本,
X
,最好小于(9480,1),可以让我们在备选方案中复制您的计算。对于供其他人阅读的代码,请使用4个空格缩进;操作符周围的空白也很有用!你有没有测量过代码慢的地方?以timeit为例?主要的减速是由于认知多样性的重复计算。我只是不确定是否有一种方法不象我那样重复,或者是否有更好的方法来优化认知多样性的计算谢谢你的帮助。我最终测试了另一组解决方案,并最终使用了一个cuda解决方案,用于更大的阵列,另外还使用了来自numba的@Guvectoriation
cd_mat2 = np.sqrt(np.sum((ordered_max_scorers[:,None] - ordered_max_scorers)**2), axis=1))