【问题标题】:how to refer to scipy sparse matrix columns?如何引用 scipy 稀疏矩阵列?
【发布时间】:2017-10-23 01:56:42
【问题描述】:

我需要参考具体的scipy稀疏矩阵列

例如,在 pandas 中我会写:

data_sims.columns[1]

data_sims 是 csr scipy 矩阵。 如果我写 data_sims[:,j],那么我按列获取所有行,但我不能引用特定列?怎么做才好>?

for i in tqdm(range(0, data_sims.shape[0])):
     for j in range(1,data_sims.shape[1]):
        user = data_sims[i].data
        product = data_sims[:,j].data

data_sims 只有用户的 id 行和列名 data_sims 是 array([ 1.00000000e+00, 3.30000000e+01, 4.20000000e+01, ..., 1.96620000e+04, 1.96720000e+04, 1.96950000e+04]) –

我只想引用列,例如 getcol(2) 给了我 col2 中所有值的数组,但是是否可以只引用 col2 而不是获取 col2 的值? data_sims.columns[2] –

【问题讨论】:

  • 你能展示一个小样本矩阵吗?不清楚你需要什么以及为什么data_sims[:,j] 不行
  • data_sims 只有用户的 id 行和列名 data_sims 是 ',具有 1257 个压缩稀疏行格式的存储元素> array([ 1.00000000e +00, 3.30000000e+01, 4.20000000e+01, ..., 1.96620000e+04, 1.96720000e+04, 1.96950000e+04])
  • 请编辑您的问题并修改相关信息。

标签: python-3.x numpy scipy sparse-matrix


【解决方案1】:

演示:

In [20]: from scipy import sparse as sp

In [21]: M = sp.random(20, 5, .2, 'csr')

In [22]: M
Out[22]:
<20x5 sparse matrix of type '<class 'numpy.float64'>'
        with 20 stored elements in Compressed Sparse Row format>

In [23]: M.A
Out[23]:
array([[ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.28107916,  0.        ,  0.        ],
       [ 0.87837137,  0.13842525,  0.        ,  0.        ,  0.23325649],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.52736337],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.04542009,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.63677513,  0.        ,  0.        ],
       [ 0.63231093,  0.62618467,  0.        ,  0.06950421,  0.        ],
       [ 0.        ,  0.43227768,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.5629196 ,  0.        ,  0.        ,  0.89888461,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.72068086,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.39975165,  0.47361848,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.62760683,  0.        ,  0.59258286],
       [ 0.        ,  0.91076085,  0.        ,  0.        ,  0.47079545]])

In [24]: for i in M[:, 2]:
    ...:     if i > 0:
    ...:         print(i)
    ...:
  (0, 0)        0.281079161053
  (0, 0)        0.636775129263
  (0, 0)        0.720680860082
  (0, 0)        0.399751651175
  (0, 0)        0.627606833131

你也可以这样做:

In [37]: M[:, 2].A
Out[37]:
array([[ 0.        ],
       [ 0.28107916],
       [ 0.        ],
       [ 0.        ],
       [ 0.        ],
       [ 0.        ],
       [ 0.        ],
       [ 0.        ],
       [ 0.63677513],
       [ 0.        ],
       [ 0.        ],
       [ 0.        ],
       [ 0.        ],
       [ 0.        ],
       [ 0.72068086],
       [ 0.39975165],
       [ 0.        ],
       [ 0.        ],
       [ 0.62760683],
       [ 0.        ]])

In [38]: M[:, 2].A.ravel()
Out[38]:
array([ 0.        ,  0.28107916,  0.        ,  0.        ,  0.        ,  0.        ,  0.        ,  0.        ,  0.63677513,  0
.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.72068086,  0.39975165,  0.        ,  0.        ,  0.62760683,  0.        ])

【讨论】:

  • 非常感谢,但我只想参考该列,例如 getcol(2) 给了我 col2 中所有值的数组,但是否可以只参考 col2 而不是获取值col2 的? data_sims.columns[2]
  • @IvanShelonik,是的,有可能 - 使用 Pandas SparseDataFrame ;-)
  • 谢谢。但是 PandasSparseDataframe 的工作并不像我想要的那么好(或者我错过了一些东西),而且它现在非常不稳定。我可以通过linkedin 加你并告诉你我想做什么吗?也许你会得到我真正的建议,用俄语解释起来会容易得多。我脑子里太乱了
猜你喜欢
  • 2012-01-15
  • 1970-01-01
  • 1970-01-01
  • 2017-01-19
  • 2012-05-15
  • 2017-03-26
  • 2017-03-31
  • 2023-04-10
  • 2021-10-30
相关资源
最近更新 更多