我有coo_matrix
a
形状(40106, 2048)
和列numpy数组b
形状(40106,)
。
我想要做的是简单地连接矩阵和数组(即结果数据结构将具有形状(40106, 2049)
)。
我尝试使用hstack
,如下所示
concat = hstack([a, b])
但是我收到以下错误:
File "/Users/usr/anaconda/lib/python3.5/site-packages/scipy/sparse/construct.py", line 464, in hstack
return bmat([blocks], format=format, dtype=dtype)
File "/Users/usr/anaconda/lib/python3.5/site-packages/scipy/sparse/construct.py", line 581, in bmat
'row dimensions' % i)
ValueError: blocks[0,:] has incompatible row dimensions
由于a
和b
具有相同的行数,因此我无法理解尺寸不匹配的原因。
答案 0 :(得分:1)
将第二个数组1D
转换为2D
,然后使用hstack
-
hstack([A,B[:,None]])
示例运行 -
In [86]: from scipy.sparse import coo_matrix, hstack
# Sample inputs as a coo_matrix and an array
In [87]: A = coo_matrix([[1, 2, 0], [3, 0, 4]])
...: B = np.array([5, 6])
...:
# Use proposed solution
In [88]: out = hstack([A,B[:,None]])
# Print the dense version to visually verify
In [89]: out.toarray()
Out[89]:
array([[1, 2, 0, 5],
[3, 0, 4, 6]])
答案 1 :(得分:1)
我认为是sparse.hstack
。转换为矩阵时,b
为(1,40106)
。尝试将其转换为正确的稀疏矩阵,然后再将其传递给hstack
。 hstack
将作业传递给bmat
,最终加入所有输入矩阵的coo
属性,从而制作新矩阵
In [66]: from scipy import sparse
In [67]: A = sparse.coo_matrix(np.eye(3))
In [68]: b = np.ones(3)
In [69]: sparse.hstack((A,b))
....
ValueError: blocks[0,:] has incompatible row dimensions
In [70]: B=sparse.coo_matrix(b)
In [71]: B
Out[71]:
<1x3 sparse matrix of type '<class 'numpy.float64'>'
with 3 stored elements in COOrdinate format>
In [72]: sparse.hstack((A,B.T))
Out[72]:
<3x4 sparse matrix of type '<class 'numpy.float64'>'
with 6 stored elements in COOrdinate format>
In [73]: _.A
Out[73]:
array([[ 1., 0., 0., 1.],
[ 0., 1., 0., 1.],
[ 0., 0., 1., 1.]])
这也有效(如在Divakar的回答中):
In [74]: sparse.hstack((A,b[:,None]))
Out[74]:
<3x4 sparse matrix of type '<class 'numpy.float64'>'
with 6 stored elements in COOrdinate format>
我的hastack
确实:
return bmat([blocks], format=format, dtype=dtype)
所以直接调用bmat也可以使用
In [93]: sparse.bmat([[A, B.T]])
Out[93]:
<3x4 sparse matrix of type '<class 'numpy.float64'>'
with 6 stored elements in COOrdinate format>
sparse.bmat([A, B.T])
会产生blocks must be 2d
错误。