gpt4 book ai didi

python - 加入两个numpy矩阵

转载 作者:太空狗 更新时间:2023-10-29 22:13:48 25 4
gpt4 key购买 nike

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

[[0]         [1]               [[0][1]
[1] + [0] = [1][0]
[4] [1] [4][1]
[0]] [1]] [0][1]]

例如,使用这些矩阵:

>>type(X)
>>type(Y)
>>X.shape
>>Y.shape
<class 'numpy.matrixlib.defmatrix.matrix'>
<class 'numpy.matrixlib.defmatrix.matrix'>
(53, 1)
(53, 1)

我已经尝试过 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

最佳答案

从回溯来看,您似乎已经完成了 from scipy.sparse import * 或类似的操作,因此 numpy.hstack 遮蔽了scipy.sparse.hstacknumpy.hstack 工作正常:

>>> X = np.matrix([[0, 1, 4, 0]]).T
>>> Y = np.matrix([[1, 0, 1, 1]]).T
>>> np.hstack([X, Y])
matrix([[0, 1],
[1, 0],
[4, 1],
[0, 1]])

关于python - 加入两个numpy矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12246680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com