在Python中给出两个矩阵A
和B
,我想找到两个矩阵中行之间的相关性。矩阵的长度为5 * 7.
我想找到A
和B
中每一行之间的相关性,并平均相关性:
A = data_All_Features_rating1000_topk_nr ;
B = data_All_Features_rating1000_leastk_nr ;
corr_1 = corrcoeff(A[0,:],B[0,:]])
corr_2 = corrcoeff(A[0,:],B[1,:]])
corr_3 = corrcoeff(A[0,:],B[2,:]])
corr_4 = corrcoeff(A[0,:],B[3,:]])
corr_5 = corrcoeff(A[0,:],B[4,:]])
corr_6 = corrcoeff(A[1,:],B[1,:]])
corr_7 = corrcoeff(A[1,:],B[2,:]])
corr_8 = corrcoeff(A[1,:],B[3,:]])
corr_9 = corrcoeff(A[1,:],B[4,:]])
corr_10 = corrcoeff(A[2,:],B[2,:]])
corr_11 = corrcoeff(A[2,:],B[3,:]])
corr_12 = corrcoeff(A[2,:],B[4,:]])
corr_13 = corrcoeff(A[3,:],B[3,:]])
corr_14 = corrcoeff(A[3,:],B[4,:]])
corr_14 = corrcoeff(A[4,:],B[4,:]])
corravg = avg(corr_1,corr_2,...,corr_14).
这就是我的所作所为:
topk = 5
corr_res = []
p = 0 ;
for i in range(0,topk):
for j in range(i,topk):
a = data_All_Features_rating1000_topk_nr[i,:]
b = data_All_Features_rating1000_leastk_nr[j,:]
tmp = np.corrcoef(a,b)
print tmp[0,1]
corr_res = corr_res.extend(tmp[0,1])
我收到此错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-159-ab1d737eed71> in <module>()
22 tmp = np.corrcoef(a,b)
23 print tmp[0,1]
---> 24 corr_res = corr_res.extend(tmp[0,1])
25 # print p+1
26 # print corr_res
TypeError: 'numpy.float64' object is not iterable
答案 0 :(得分:2)
在python中执行矩阵运算的有效方法是使用NumPy库。正确的相关性计算可以是用户numpy.correlate函数。要计算所有行组合之间的相关性,您可以使用
import numpy as np
A = np.array([[1, 2, 3, 4], [2, 3, 5, 6], [1,3,4,5], [7,8,2,3]])
B = np.array([[1, 2, 3, 4], [3, 5, 6, 2], [3,2,4,1], [9,8,2,1]])
corr = []
for i in xrange(len(A)):
for j in xrange(len(B)-i):
corr.extend(np.correlate(A[i], B[j+i]))
corr_avg = np.average(corr)
print corr_avg
print " ".join(map(str, corr))
更新
而不是
print tmp[0,1]
corr_res = corr_res.extend(tmp[0,1])
尝试
print tmp[0,0]
corr_res.append(tmp[0,0])
list
方法extend
在输入上获取可迭代对象(如其他列表,元组,...)。如果您想要添加list
标量值,则应使用append
方法。