如何在python

时间:2019-03-22 14:03:49

标签: python arrays numpy append

我有两个数组XYX的大小为(5000,2351),Y的大小为(2351,)。我对Y使用了重塑函数以获取大小(1,2351)。然后,我将附加函数用于X,而得到的大小为(5001,2351)(117552351,)。

Y = Y.reshape(1,-1)
X = np.append(X,Y)

哪里有问题?

2 个答案:

答案 0 :(得分:0)

您可以使用np.concatenate

X = np.arange(0,15).reshape(5,3)
Y = np.arange(0,3)
Y = Y.reshape(1,-1)

np.concatenate([X,Y])

产量:

array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11],
       [12, 13, 14],
       [ 0,  1,  2]])

答案 1 :(得分:0)

来自https://docs.scipy.org/doc/numpy-1.14.2/reference/generated/numpy.append.html

如果轴为“无”,则输出为平坦数组 您应该使用axis = 0,或者最好使用vstack()

numpy.append(arr, values, axis=None)[source]
Append values to the end of an array.

Parameters: 
arr : array_like

Values are appended to a copy of this array.

values : array_like

These values are appended to a copy of arr. It must be of the correct shape (the same shape as arr, excluding axis). If axis is not specified, values can be any shape and will be flattened before use.

axis : int, optional

The axis along which values are appended. If axis is not given, both arr and values are flattened before use.

Returns:    
append : ndarray

A copy of arr with values appended to axis. Note that append does not occur in-place: a new array is allocated and filled. If axis is None, out is a flattened array.