我编写了这段代码来创建一个对称矩阵m * m(A),其中m = 1682.这段代码在Jupyter上网本(Ipython)上运行,速度非常慢。考虑到真实矩阵m = 1000万,有谁知道如何提高性能?
其他细节:
电影是一个矩阵(numpy数组)
def make_A(R, rx, movies): m = R.shape[1] A = np.zeros((m,m)) for i in range(m): for j in range(i,m): if i!= j: ri = np.linalg.norm(R[:,i], ord=None) rj = np.linalg.norm(R[:,j], ord=None) mov1 = movies[i] mov2 = movies[j] temp = rx[(rx[:,1]==mov1) | (rx[:,1]==mov2)] temp = temp[:,0] y = np.bincount(temp.T) ii = np.nonzero(y)[0] temp = np.vstack((ii, y[ii])).T temp = temp[temp[:,1]==2] Uij = temp.shape[0] A[i,j]=ri*rj+Uij A=A + A.T - np.diag(A.diagonal()) return A