计算稀疏矩阵的相似性

Jim*_*Lin 6 python numpy classification scipy sparse-matrix

我正在使用Python与numpy,scipy和scikit-learn模块.

我想用非常大的稀疏矩阵对数组进行分类.(100,000*100,000)

矩阵中的值等于0或1.我唯一拥有的是value = 1的索引.

a = [1,3,5,7,9] 
b = [2,4,6,8,10]
Run Code Online (Sandbox Code Playgroud)

意思是

a = [0,1,0,1,0,1,0,1,0,1,0]
b = [0,0,1,0,1,0,1,0,1,0,1]
Run Code Online (Sandbox Code Playgroud)

如何在scipy中将索引数组更改为稀疏数组?

如何快速对这些阵列进行分类?

非常感谢你.

Sau*_*tro 4

如果您选择稀疏,coo_matrix您可以通过以下索引来创建它:

from scipy.sparse import coo_matrix
import scipy
nrows = 100000
ncols = 100000
row = scipy.array([1,3,5,7,9])
col = scipy.array([2,4,6,8,10])
values = scipy.ones(col.size)
m = coo_matrix((values, (row,col)), shape=(nrows, ncols), dtype=float)
Run Code Online (Sandbox Code Playgroud)