gpt4 book ai didi

python - 如何连接以下两个矩阵?

转载 作者:太空宇宙 更新时间:2023-11-04 10:05:14 25 4
gpt4 key购买 nike

你好我有下面的矩阵叫做 tfidf2,这个矩阵的形状是(11159, 1985) 它有 11159 行和 1985 列,我想将一个新矩阵连接到这个矩阵,称为 datesNumpy 的矩阵具有 (11159, 12) 的形状,它们具有相同的行数所以是可能的为了连接它,称为 tfidf3 的新矩阵的形状应该是 (11159,1997),

import numpy as np
tfidf2 = tdf.transform(list_cluster)
print("Shape tfidf2",tfidf2.shape)
listAux=[]
for l in listMonth:
listAux.append([int(y) for y in l])
datesNumpy=np.array([np.array(xi) for xi in listAux])
print("Shape datesNumpy",datesNumpy.shape)

我试过:

tfidf3=np.stack((tfidf2, datesNumpy), axis=-1)

无论我得到什么,我感谢支持以克服这种情况:

Shape tfidf2 (11159, 1985)
Shape datesNumpy (11159, 12)
Traceback (most recent call last):
File "Main.py", line 235, in <module>
tfidf3=np.stack((tfidf2, datesNumpy), axis=-1)
File "/usr/local/lib/python3.5/dist-packages/numpy/core/shape_base.py", line 339, in stack
raise ValueError('all input arrays must have the same shape')
ValueError: all input arrays must have the same shape

在收到来自这里的反馈后,我尝试了:

tfidf3=np.concatenate([tfidf2, datesNumpy], axis=1)

但是我得到了:

Traceback (most recent call last):
File "Main.py", line 235, in <module>
tfidf3=np.concatenate([tfidf2, datesNumpy], axis=1)
ValueError: zero-dimensional arrays cannot be concatenated

最佳答案

numpy.stack(arrays, axis=0)

Join a sequence of arrays along a new axis.

The axis parameter specifies the index of the new axis in thedimensions of the result. For example, if axis=0 it will be the firstdimension and if axis=-1 it will be the last dimension.

Parameters:

arrays : sequence of array_like Each array must have the same shape.

axis : int, optional The axis in the result array along which the input arrays are stacked.

Returns:

stacked : ndarray Thestacked array has one more dimension than the input arrays.

根据文档必须具有相同的形状。

你必须concatenate

例子:

tfidf2 = np.zeros((11159, 1985))
datesNumpy = np.ones((11159, 12))

tfidf3=np.concatenate([tfidf2, datesNumpy], axis=1)
print(tfidf3.shape)

输出:

(11159, 1997)

关于python - 如何连接以下两个矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41349326/

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