Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从scipy矩阵中删除行_Python_Numpy_Scipy - Fatal编程技术网

Python 从scipy矩阵中删除行

Python 从scipy矩阵中删除行,python,numpy,scipy,Python,Numpy,Scipy,我有一个scipy稀疏矩阵data和一个整数n,它与我要删除的data中的一行关联。要删除此行,我尝试了以下方法: data = sparse.csr_matrix(np.delete(np.array(data),n, axis=0)) 但是,这产生了以下错误: Traceback (most recent call last): File "...", line 260, in <module> X_labeled = sparse.csr_matrix(np.de

我有一个scipy稀疏矩阵
data
和一个整数
n
,它与我要删除的
data
中的一行关联。要删除此行,我尝试了以下方法:

data = sparse.csr_matrix(np.delete(np.array(data),n, axis=0))
但是,这产生了以下错误:

Traceback (most recent call last):
  File "...", line 260, in <module>
    X_labeled = sparse.csr_matrix(np.delete(np.array(X_labeled),n, axis=0))
  File "/anaconda3/lib/python3.6/site-packages/scipy/sparse/compressed.py", line 79, in __init__
    self._set_self(self.__class__(coo_matrix(arg1, dtype=dtype)))
  File "/anaconda3/lib/python3.6/site-packages/scipy/sparse/coo.py", line 177, in __init__
    self.row, self.col = M.nonzero()
SystemError: <built-in method nonzero of numpy.ndarray object at 0x113c883f0> returned a result with an error set
Traceback (most recent call last):
  File "...", line 261, in <module>
    X_labeled = np.delete(X_labeled.toarray(),n, axis=0)
  File "/anaconda3/lib/python3.6/site-packages/numpy/lib/function_base.py", line 4839, in delete
    "size %i" % (obj, axis, N))
IndexError: index 86 is out of bounds for axis 0 with size 4
我得到这个错误:

Traceback (most recent call last):
  File "...", line 260, in <module>
    X_labeled = sparse.csr_matrix(np.delete(np.array(X_labeled),n, axis=0))
  File "/anaconda3/lib/python3.6/site-packages/scipy/sparse/compressed.py", line 79, in __init__
    self._set_self(self.__class__(coo_matrix(arg1, dtype=dtype)))
  File "/anaconda3/lib/python3.6/site-packages/scipy/sparse/coo.py", line 177, in __init__
    self.row, self.col = M.nonzero()
SystemError: <built-in method nonzero of numpy.ndarray object at 0x113c883f0> returned a result with an error set
Traceback (most recent call last):
  File "...", line 261, in <module>
    X_labeled = np.delete(X_labeled.toarray(),n, axis=0)
  File "/anaconda3/lib/python3.6/site-packages/numpy/lib/function_base.py", line 4839, in delete
    "size %i" % (obj, axis, N))
IndexError: index 86 is out of bounds for axis 0 with size 4
我明白了:

<class 'scipy.sparse.csr.csr_matrix'>
(4, 2740)
(4, 2740)

(4, 2740)
(4, 2740)

将稀疏矩阵转换为密集矩阵的正确方法是使用
toarray
,而不是
np.array(…)

delete
适用于此正确数组:

In [414]: data = sparse.csr_matrix(np.delete(M.toarray(),1, axis=0))
In [415]: data
Out[415]: 
<2x3 sparse matrix of type '<class 'numpy.float64'>'
    with 2 stored elements in Compressed Sparse Row format>
In [416]: data.A
Out[416]: 
array([[ 1.,  0.,  0.],
       [ 0.,  0.,  1.]])

你的数据看起来像什么?用这个简单的数据运行你的行是有效的:
data=np.array([1,2,3,4,5,6])
。将稀疏矩阵转换为密集数组的正确方法是
data.toarray()
为什么不将
数据
索引到除要删除的一个之外的所有行索引?我想你是指
todense()
方法。。无论如何,这不是关于矩阵的。
todense
构成了一个
np.matrix
toarray
构成了一个
np.ndarray
(有一个
.a
捷径)。
In [411]: M.toarray()
Out[411]: 
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])
In [414]: data = sparse.csr_matrix(np.delete(M.toarray(),1, axis=0))
In [415]: data
Out[415]: 
<2x3 sparse matrix of type '<class 'numpy.float64'>'
    with 2 stored elements in Compressed Sparse Row format>
In [416]: data.A
Out[416]: 
array([[ 1.,  0.,  0.],
       [ 0.,  0.,  1.]])
In [417]: M[[0,2],:]
Out[417]: 
<2x3 sparse matrix of type '<class 'numpy.float64'>'
    with 2 stored elements in Compressed Sparse Row format>
In [418]: _.A
Out[418]: 
array([[ 1.,  0.,  0.],
       [ 0.,  0.,  1.]])
In [420]: M[np.array([True,False,True]),:].A
Out[420]: 
array([[ 1.,  0.,  0.],
       [ 0.,  0.,  1.]])
In [421]: mask=np.ones((3,),bool)
In [422]: mask[1]=False
In [423]: M[mask,:].A