不知道該矩陣你問在這裏,但假設你有變量中的兩個原始陣列,
Ri = [ 1, 2, 0, 0, 3, 4]; Rj = [ 0, 1, 0, 3, 5, 2]
這裏是你如何構建共同額定等級和計算餘弦相似,
import numpy as np
Rip = np.array([ i if j != 0 else 0 for i,j in zip(Ri,Rj) ])
Rjp = np.array([ j if i != 0 else 0 for i,j in zip(Ri,Rj) ])
如果你不想明確地使用for語句,你可以使用地圖,
Rip = map(lambda x,y: 0 if y == 0 else x, Ri, Rj)
Rjp = map(lambda x,y: 0 if x == 0 else y, Ri, Rj)
然後餘弦相似度可以與Rip
這些明確的(或密集)的陳述和Rjp
cos_sim = float(np.dot(Rip, Rjp))/np.sqrt(np.dot(Rip,Rip) * np.dot(Rjp,Rjp))
計算如果你不希望明確存儲完整陣列可以使用scipy.sparse
存儲載體不如疏單行(列)矩陣。請注意,如果你這樣做,np.dot
將不再起作用,你應該使用sparse matrices的dot
方法。
from scipy.sparse import csr_matrix
# make single column/row sparse matrix reps of Rip
row = np.array([ i for (i,x) in enumerate(Rip) if x != 0 ])
col = np.zeros(row.size, dtype=np.int32)
dat = np.array([ x for (i,x) in enumerate(Rip) if x != 0 ])
Rip_col_mat = csr_matrix((dat,(row,col)))
Rip_row_mat = csr_matrix((dat,(col,row)))
# make single column/row sparse matrix reps of Rjp
row = np.array([ i for (i,x) in enumerate(Rjp) if x != 0 ])
col = np.zeros(row.size, dtype=np.int32)
dat = np.array([ x for (i,x) in enumerate(Rjp) if x != 0 ])
Rjp_col_mat = csr_matrix((dat,(row,col)))
Rjp_row_mat = csr_matrix((dat,(col,row)))
我們計算餘弦相似,我們可以做的,
inner = Rip_row_mat.dot(Rjp_col_mat).data
Rip_m = np.sqrt(Rip_row_mat.dot(Rip_col_mat).data)
Rjp_m = np.sqrt(Rjp_row_mat.dot(Rjp_col_mat).data)
cos_sim = inner/(Rip_m * Rjp_m)
謝謝您的回答。我想知道是否有任何方法可以避免顯式循環? –
在某些級別,任何數組操作都將以某種方式遍歷元素。如果您不希望列表解析中的「顯式」for循環,可以使用'map'。 – Gabriel