简体   繁体   English

稀疏 CSR 矩阵为 1-dim

[英]sparse CSR matrix into 1-dim

I have been trying to reshape my matrix:我一直在尝试重塑我的矩阵:

array([<320000x799928 sparse matrix of type '<class 'numpy.float64'>' with 2929143 stored elements in Compressed Sparse Row format>], dtype=object)数组([<320000x799928 类型的稀疏矩阵 '<class 'numpy.float64'>' 具有 2929143 个以压缩稀疏行格式存储的元素>],dtype=object)

into a 1 dim matrix as I want to feed it into a neural network.到一个 1 暗矩阵,因为我想将它输入到神经网络中。 None of the classic transformations work.经典的转换都不起作用。 I tried reshaping, flattening, .todense, and .toarray我尝试了重塑、展平、.todense 和 .toarray

Any idea what could be going on here?知道这里会发生什么吗?

Something that displays as:显示为:

array([<320000x799928 sparse matrix of type '<class 'numpy.float64'>' with 2929143 stored elements in Compressed Sparse Row format>], dtype=object)

is a single element (shape (1,)) numpy array, object dtype.是单个元素(形状(1,)) numpy数组,对象 dtype。 The element is a sparse matrix, but the array itself is not.元素是稀疏矩阵,但数组本身不是。

Starting with a small sparse matrix A , I can make an array that displays like yours:从一个小的稀疏矩阵A开始,我可以制作一个像你一样显示的数组:

In [101]: arr = np.array([A])

In [102]: arr
Out[102]: 
array([<3x3 sparse matrix of type '<class 'numpy.float64'>'
        with 3 stored elements in Compressed Sparse Row format>],
      dtype=object)

In [103]: arr.shape
Out[103]: (1,)

This is a 1d array already - but not numeric.这已经是一个一维数组 - 但不是数字。

I can access that element with:我可以通过以下方式访问该元素:

In [104]: arr[0]
Out[104]: 
<3x3 sparse matrix of type '<class 'numpy.float64'>'
    with 3 stored elements in Compressed Sparse Row format>

In [105]: print(arr[0])
  (0, 0)    1.0
  (1, 1)    1.0
  (2, 2)    1.0

And apply toarray (or todense ) to it:并将toarray (或todense )应用于它:

In [106]: arr[0].toarray()
Out[106]: 
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])

todense will make a np.matrix . todense将制作一个np.matrix

Once it's a ndarray it can be flattened一旦它是一个ndarray ,它就可以被展平

In [107]: arr[0].toarray().ravel()
Out[107]: array([1., 0., 0., 0., 1., 0., 0., 0., 1.])

The sparse matrix itself can be reshaped to a 1 row matrix.稀疏矩阵本身可以重塑为 1 行矩阵。 But as long as it's sparse it has to remain 2d.但只要它是sparse的,它就必须保持 2d。

In [109]: arr[0].reshape(1,9)
Out[109]: 
<1x9 sparse matrix of type '<class 'numpy.float64'>'
    with 3 stored elements in COOrdinate format>
In [110]: arr[0].reshape(1,9).A
Out[110]: array([[1., 0., 0., 0., 1., 0., 0., 0., 1.]])

np.matrix has a property that returns a raveled 1d array: np.matrix有一个属性,它返回一个 raveled 1d 数组:

In [115]: arr[0].todense().A1
Out[115]: array([1., 0., 0., 0., 1., 0., 0., 0., 1.])

memory记忆

But big caution about using toarray (or todense ).但是对于使用toarray (或todense )非常谨慎。 With those dimensions the array will be too big for most memory:对于这些尺寸,数组对于大多数内存来说太大了:

In [118]: 320000*799928*8/1e9
Out[118]: 2047.81568

It works as a sparse matrix because only a small fraction of the values are nonzero它作为一个稀疏矩阵工作,因为只有一小部分值是非零的

In [119]: 2929143/(320000*799928)
Out[119]: 1.1442994713274194e-05

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM