Scipy的csr_matrix按值排序?

问题描述

因此,scikit-learn的DBSCAN接受稀疏矩阵,并且如果矩阵不是csr_matrix格式,则将其转换为此类。我想解析一个csr_matrix,但随后收到此警告:

效率警告:预先计算的稀疏输入未按数据排序。

如何创建数据排序的csr_matrix?如果我初始化数据排序的矩阵,则矩阵会自动对其进行索引排序:

>>> from scipy.sparse import csr_matrix
>>> x = csr_matrix(([1,2,3],[[3,1],[5,1]]))
>>> print(x)
  (1,1)    3
  (2,2)    2
  (3,5)    1

我知道csr_matrix有一个has_sorted_indices标志,但是我不确定如何使用它。即使我将其设置为false,矩阵仍然会按索引排序。

编辑:我尝试过sorted_indices,但似乎没有任何改变。我不确定sorted_indices的概念是否正确?是否应该对每行数据从低到高排序?

>>> from scipy.sparse import csr_matrix
>>> x = csr_matrix(([7,3,5,1,6,2],[[0,[0,1]]),shape=(3,2))
>>> print(x)
  (0,0)    7
  (0,1)    1
  (1,0)    3
  (1,1)    6
  (2,0)    5
  (2,1)    2
>>> x.has_sorted_indices = False
>>> x.sort_indices()
>>> print(x)
  (0,1)    2

我想要什么(这有可能还是没有?)

  (0,1)    1
  (0,0)    7
  (1,1)    2
  (2,0)    5

基本上我需要这个来返回True:

out_of_order = graph.data[:-1] > graph.data[1:]
line_change = np.unique(graph.indptr[1:-1] - 1)
line_change = line_change[line_change < out_of_order.shape[0]]
return (out_of_order.sum() == out_of_order[line_change].sum())

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)