如何连接下面两个矩阵?

2024-06-29 00:12:18 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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)

我试过了:

^{pr2}$

不管我得到了什么,我都很感激能帮助我克服这种情况:

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

Tags: inpyfornpline矩阵file形状
1条回答
网友
1楼 · 发布于 2024-06-29 00:12:18

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 the dimensions of the result. For example, if axis=0 it will be the first dimension 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 The stacked 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)

输出:

^{pr2}$

相关问题 更多 >