相关疑难解决方法(0)

加入两个numpy矩阵

如果你有两个numpy矩阵,你如何将它们合并为一个?它们应该水平连接,以便

[[0]         [1]               [[0][1]
 [1]     +   [0]         =      [1][0]
 [4]         [1]                [4][1]
 [0]]        [1]]               [0][1]]
Run Code Online (Sandbox Code Playgroud)

例如,使用这些矩阵:

>>type(X)
>>type(Y)
>>X.shape
>>Y.shape
<class 'numpy.matrixlib.defmatrix.matrix'>
<class 'numpy.matrixlib.defmatrix.matrix'>
(53, 1)
(53, 1)
Run Code Online (Sandbox Code Playgroud)

我试过hstack但得到一个错误:

>>Z = hstack([X,Y])

Traceback (most recent call last):
  File "labels.py", line 85, in <module>
    Z = hstack([X, Y])
  File "C:\Python27\lib\site-packages\scipy\sparse\construct.py", line 263, in h
stack
    return bmat([blocks], format=format, dtype=dtype)
  File "C:\Python27\lib\site-packages\scipy\sparse\construct.py", line 329, in b
mat
    raise ValueError('blocks must have rank 2')
ValueError: blocks must have rank 2
Run Code Online (Sandbox Code Playgroud)

python numpy matrix scipy multidimensional-array

6
推荐指数
1
解决办法
7362
查看次数

scipy.sparse.hstack(([1],[2])) - >"ValueError:blocks必须是2-D".为什么?

scipy.sparse.hstack((1, [2]))并且scipy.sparse.hstack((1, [2]))工作得很好,但不是scipy.sparse.hstack(([1], [2])).为什么会这样?

以下是我系统上发生的情况:


C:\Anaconda>python
Python 2.7.10 |Anaconda 2.3.0 (64-bit)| (default, May 28 2015, 16:44:52) [MSC v.
1500 64 bit (AMD64)] on win32
>>> import scipy.sparse
>>> scipy.sparse.hstack((1, [2]))
<1x2 sparse matrix of type '<type 'numpy.int32'>'
        with 2 stored elements in COOrdinate format>
>>> scipy.sparse.hstack((1, 2))
<1x2 sparse matrix of type '<type 'numpy.int32'>'
        with 2 stored elements in COOrdinate format>
>>> scipy.sparse.hstack(([1], [2]))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module> …
Run Code Online (Sandbox Code Playgroud)

python scipy sparse-matrix

5
推荐指数
2
解决办法
6244
查看次数