我一直试图重塑我的矩阵:
数组(<320000x799928类型的“”稀疏矩阵,包含2929143个存储在压缩稀疏行format>,dtype=object中的元素)
我想把它输入到一个神经网络中。这些经典的转换都不起作用。我尝试过重塑、扁平、.todense和.toarray
知道这里会发生什么事吗?
发布于 2022-06-19 15:46:02
显示为:
array([<320000x799928 sparse matrix of type '<class 'numpy.float64'>' with 2929143 stored elements in Compressed Sparse Row format>], dtype=object)是单个元素(shape (1,)) numpy数组,对象dtype。元素是稀疏矩阵,但数组本身不是。
从一个小型稀疏矩阵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,)这已经是一个一维数组了,但不是数字数组。
我可以通过以下方式访问该元素:
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并将toarray (或todense)应用于它:
In [106]: arr[0].toarray()
Out[106]:
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])todense将制造一个np.matrix。
一旦它是一个ndarray,它就可以被扁平
In [107]: arr[0].toarray().ravel()
Out[107]: array([1., 0., 0., 0., 1., 0., 0., 0., 1.])稀疏矩阵本身可以被重塑为1行矩阵。但只要它是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有一个返回一维数组的属性:
In [115]: arr[0].todense().A1
Out[115]: array([1., 0., 0., 0., 1., 0., 0., 0., 1.])内存
但在使用toarray (或todense)时要格外小心。对于这些维度,数组对于大多数内存来说都太大了:
In [118]: 320000*799928*8/1e9
Out[118]: 2047.81568它作为稀疏矩阵工作,因为只有一小部分值是非零的。
In [119]: 2929143/(320000*799928)
Out[119]: 1.1442994713274194e-05https://stackoverflow.com/questions/72675985
复制相似问题