Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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稀疏矩阵对numpy数组进行列_堆栈?_Python_Python 2.7_Python 3.x_Numpy_Scipy - Fatal编程技术网

Python 如何使用scipy稀疏矩阵对numpy数组进行列_堆栈?

Python 如何使用scipy稀疏矩阵对numpy数组进行列_堆栈?,python,python-2.7,python-3.x,numpy,scipy,Python,Python 2.7,Python 3.x,Numpy,Scipy,我有以下矩阵: A.toarray() array([[0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], ..., [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0]], dtype=int64) type(A)

我有以下矩阵:

A.toarray()

array([[0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       ..., 
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]], dtype=int64)

type(A)

scipy.sparse.csr.csr_matrix

A.shape
(878049, 942)
矩阵B:

B

array([2248, 2248, 2248, ...,    0,    0,    0])

type(B)

numpy.ndarray

B.shape

(878049,)
我想在C中列stack
A
B
,我尝试了以下方法:

C =  sparse.column_stack([A,B])
然后:

我的问题是如何保存尺寸。因此,知道如何对它们进行列堆栈吗

更新

我尝试了以下方法:

#Sorry for the name
C =  np.vstack(( A.A.T, B)).T
我得到:

array([[   0,    0,    0, ...,    0,    6],
       [   0,    0,    0, ...,    0,    6],
       [   0,    0,    0, ...,    0,    6],
       ..., 
       [   0,    0,    0, ...,    0,    1],
       [   0,    0,    0, ...,    0,    1],
       [   0,    0,    0, ...,    0,    1]], dtype=int64)

这是列堆栈的正确方法吗?

您是否尝试了以下方法

C=np.vstack((A.T,B)).T
对于样本值:

A = array([[1, 2, 3], [4, 5, 6]])
>>>> A.shape
(2, 3)
B = array([7, 8])
>>> B.shape
(2,)
C=np.vstack((A.T,B)).T
>>> C.shape
(2, 4)
如果A是稀疏矩阵,并且希望将输出保持为稀疏,则可以执行以下操作:

C=np.vstack((A.A.T,B)).T
D=csr_matrix((C))
2个问题

  • 没有稀疏的.column\u堆栈
  • 您正在混合稀疏矩阵和密集数组
2个较小的示例:

In [129]: A=sparse.csr_matrix([[1,0,0],[0,1,0]])
In [130]: B=np.array([1,2])
使用
np.column\u stack
会给出错误:

In [131]: np.column_stack((A,B))
... 
ValueError: all the input array dimensions except for the concatenation axis must match exactly
但如果我首先将
A
转换为一个数组,column\u stack就可以了:

In [132]: np.column_stack((A.A, B))
Out[132]: 
array([[1, 0, 0, 1],
       [0, 1, 0, 2]])
串联的等效项

In [133]: np.concatenate((A.A, B[:,None]), axis=1)
Out[133]: 
array([[1, 0, 0, 1],
       [0, 1, 0, 2]])
有一个
sparse.hstack
。为此,我还需要将
B
转换为稀疏矩阵。转置之所以有效,是因为它现在是一个矩阵(与一维数组相反):

[134]中的
:sparse.hstack((A,sparse.csr_矩阵(B.T))
出[134]:
在[135]中:
出[135]:
数组([[1,0,0,1],
[0,1,0,2]],dtype=int32)

我尝试了这个,但是我得到了:
ValueError:除了连接轴之外,所有的输入数组维度都必须完全匹配
而且
A
是一个稀疏矩阵。在调用
C=csr\u矩阵((C))之前,这难道不是一个巨大的非稀疏版本的
C
?@madpysicast确实如此。你在哪里找到了稀疏的。列\u堆栈?有
np.column\u stack
,但不是稀疏版本。下面所有的答案都是非稀疏的。看看这个答案。它看起来正是你想要的。我投票决定以重复的方式结束。可能是重复的
In [133]: np.concatenate((A.A, B[:,None]), axis=1)
Out[133]: 
array([[1, 0, 0, 1],
       [0, 1, 0, 2]])
In [134]: sparse.hstack((A,sparse.csr_matrix(B).T))
Out[134]: 
<2x4 sparse matrix of type '<class 'numpy.int32'>'
    with 4 stored elements in COOrdinate format>
In [135]: _.A
Out[135]: 
array([[1, 0, 0, 1],
       [0, 1, 0, 2]], dtype=int32)