我想编写一个函数来规范大型稀疏矩阵的行(这样它们的总和为1)。
from pylab import * import scipy.sparse as sp def normalize(W): z = W.sum(0) z[z < 1e-6] = 1e-6 return W / z[None,:] w = (rand(10,10)<0.1)*rand(10,10) w = sp.csr_matrix(w) w = normalize(w)
但是,这给出了以下异常:
File "/usr/lib/python2.6/dist-packages/scipy/sparse/base.py", line 325, in __div__ return self.__truediv__(other) File "/usr/lib/python2.6/dist-packages/scipy/sparse/compressed.py", line 230, in __truediv__ raise NotImplementedError
这已在scikit-learn sklearn.preprocessing.normalize中实现。
from sklearn.preprocessing import normalize w_normalized = normalize(w, norm='l1', axis=1)
axis=1应该按行axis=0归一化,按列归一化。使用可选参数copy=False来修改矩阵。
axis=1
axis=0
copy=False