python - numpy csr 矩阵 "mean"函数是否对所有矩阵求平均值?我怎样才能删除某个值?

标签 python numpy matrix mean

我有一个 numpy csr 矩阵,我想得到它的意思,但它包含很多零,因为我消除了主对角线上及其下方的所有值,只取上三角值,现在我的 csr转换为数组时的矩阵如下所示:

   0.          0.          0.          0.          0.          0.          0.
   0.          0.          0.          0.          0.          0.          0.
   0.          0.          0.          0.          0.63646664  0.34827262
   0.24316454  0.1362165   0.63646664  0.15762204  0.31692202  0.12114576
   0.35917146

据我所知,为了使 csr 矩阵正常工作并显示如下内容,这些零很重要:

(0,5) 0.5790418
(3,10) 0.578210
(5,20) 0.912370
(67,5) 0.1093109

我看到 csr 矩阵有它自己的 mean function ,但这是否意味着函数考虑了所有零,因此除以数组中包括零的元素数?因为我只需要非零值的平均值。我的矩阵包含多个向量之间的相似性,更像是一个类似这样的矩阵列表:

[[ 0.          0.63646664  0.48492084  0.42134077  0.14366401  0.10909745
   0.06172853  0.08116201  0.19100626  0.14517247  0.23814955  0.1899649
   0.20181049  0.25663533  0.21003358  0.10436352  0.2038447   1.
   0.63646664  0.34827262  0.24316454  0.1362165   0.63646664  0.15762204
   0.31692202  0.12114576  0.35917146]
 [ 0.          0.          0.58644824  0.4977052   0.15953415  0.46110612
   0.42580993  0.3236768   0.48874263  0.44671607  0.59153001  0.57868948
   0.27357541  0.51645488  0.43317846  0.50985032  0.37317457  0.63646664
   1.          0.51529235  0.56963948  0.51218525  1.          0.38345582
   0.55396192  0.32287605  0.46700191]
 [ 0.          0.          0.          0.6089113   0.53873289  0.3367261
   0.29264493  0.13232082  0.43288206  0.80079927  0.37842518  0.33658945
   0.61990095  0.54372307  0.49982101  0.23555037  0.39283379  0.48492084
   0.58644824  0.64524906  0.31279271  0.39476181  0.58644824  0.39028705
   0.43856802  0.32296735  0.5541861 ]]

那么我怎样才能只对非零值取平均值呢?

我的另一个问题是如何删除所有等于某个值的值,正如我在上面指出的那样,我可能必须将某个值变为零?但是我该怎么做呢?例如,我想去掉所有等于或大于 1.0 的值? 这是我到目前为止制作矩阵的代码:

vectorized_words = parse.csr_matrix(vectorize_words(nostopwords,glove_dict))

#calculating the distance/similarity between each vector in the matrix
cos_similiarity = cosine_similarity(vectorized_words, dense_output=False)
# since there are duplicates like (5,0) and (0,5) which we should remove, I use scipy's triu function
coo_cossim = cos_similiarity.tocoo()
vector_similarities = sparse.triu(coo_cossim, k = 1).tocsr()

最佳答案

是的,csr_matrix.mean() 在计算平均值时确实包括所有零。举个简单的例子:

from scipy.sparse import csr_matrix

m = csr_matrix(([1,1], ([2,3],[3,3])), shape=(5,5))
m.toarray()

# returns:
array([[0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 0, 1, 0],
       [0, 0, 0, 1, 0],
       [0, 0, 0, 0, 0]], dtype=int32)

# test the mean method
m.mean(), m.mean(axis=0), m.mean(axis=1)

# returns:
0.080000000000000002,
matrix([[ 0. ,  0. ,  0. ,  0.4,  0. ]]),
matrix([[ 0. ],
        [ 0. ],
        [ 0.2],
        [ 0.2],
        [ 0. ]])

如果您需要执行不包含零的计算,则必须使用其他方法构建结果。不过这并不难做到:

nonzero_mean = m.sum() / m.count_nonzero()

关于python - numpy csr 矩阵 "mean"函数是否对所有矩阵求平均值?我怎样才能删除某个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43427615/

相关文章:

python - python/numpy 中矩阵的条件和

python - 如何在 pandas 中按列名称连接列?

python - Numpy:用同一行中其他元素的最大值替换一行中的每个元素

java - 如何使用java中的colt库求解线性方程组

numpy - 通过在 Numpy 或类似工具中进行平均来缩小 3D 矩阵

python - 使用 argparse 打开两个文件

python - 在*初始化后更改 defaultdict 的默认返回值

Python:如何沿新轴将多个数组堆叠在一起

python - systemd : `airflow.pid` vs `airflow-monitor.pid` 的 Airflow

python - Numpy:根据另一个数组中的索引将值放入 1-of-n 数组中