正如我希望只使用numpy
和scipy
(我不想用scikit-learn
),我想知道如何在一個巨大的SciPy的csc_matrix
執行行的L2正常化(2000000 X 500,000)。該操作必須消耗盡可能少的內存,因爲它必須適合內存。L2正常化
我至今是:
import scipy.sparse as sp
tf_idf_matrix = sp.lil_matrix((n_docs, n_terms), dtype=np.float16)
# ... perform several operations and fill up the matrix
tf_idf_matrix = tf_idf_matrix/l2_norm(tf_idf_matrix)
# l2_norm() is what I want
def l2_norm(sparse_matrix):
pass
只需添加:如果其他人不反對scikit-learn,它是從sklearn.preprocessing import normalize;正常化(tf_idf_matrix)'。來自sklearn開發者的無恥插件。 –