将MX1 numpy数组与MXN numpy数组连接/合并

2024-06-17 17:10:37 发布

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

required_time_stamps包含5911个时间戳
time_based_mfcc_feature包含5911个样本,每个样本具有20个mfcc特征。在

所以如果你看time_based_mfcc_feature
它看起来像:

row1    val2 val3  ... val 20  
row2    val2 val3  ... val 20  
row3    val2 val3  ... val 20
.  
.  
.  
row5911  val2 val3  ... val 20  


print type(required_time_stamps)  

< type 'numpy.ndarray'>

^{pr2}$

(5911年)

print type(time_based_mfcc_feature)

< type 'numpy.ndarray'>

print time_based_mfcc_feature.shape  

(5911,20)

我想把这两者结合起来,这样我就能:

在R,我可以简单地

time_based_mfcc_feature<-as.data.frame(time_based_mfcc_feature) 
required_time_stamps<-as.data.frame(required_time_stamps)  

new_dataframe <- merge(required_time_stamps,time_based_mfcc_feature)  
View(new_dataframe)

如何在python中实现这一点?在

最终数据如下:

time1   row1    val2 val3  ... val 20  
time2   row2    val2 val3  ... val 20  
time3   row3    val2 val3  ... val 20
.  
.  
.  
time5911 row5911  val2 val3  ... val 20    

其中,time1到time5911只是所需的时间戳中包含的值。
我试过了:

mfcc_features_with_times= np.hstack((required_time_stamps,time_based_mfcc_feature))

但是得到了这个错误

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-41-ce462d805743> in <module>()
----> 1 mfcc_features_with_times= np.hstack((required_time_stamps,time_based_mfcc_feature))

/usr/local/lib/python2.7/dist-packages/numpy/core/shape_base.pyc in hstack(tup)
    289     # As a special case, dimension 0 of 1-dimensional arrays is "horizontal"
    290     if arrs and arrs[0].ndim == 1:
--> 291         return _nx.concatenate(arrs, 0)
    292     else:
    293         return _nx.concatenate(arrs, 1)

ValueError: all the input arrays must have same number of dimensions

然后我试着转置:

t = required_time_stamps.transpose  
mfcc_features_with_times= np.hstack((t,time_based_mfcc_feature))  

但同样的错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-43-47cddb391d3f> in <module>()
----> 1 mfcc_features_with_times= np.hstack((t,time_based_mfcc_feature))

/usr/local/lib/python2.7/dist-packages/numpy/core/shape_base.pyc in hstack(tup)
    289     # As a special case, dimension 0 of 1-dimensional arrays is "horizontal"
    290     if arrs and arrs[0].ndim == 1:
--> 291         return _nx.concatenate(arrs, 0)
    292     else:
    293         return _nx.concatenate(arrs, 1)

ValueError: all the input arrays must have same number of dimensions

我还看了:Numpy concatenate 2D arrays with 1D array但我认为是另外一回事。在

目标是将这些数据逐行输入keras神经网络。
我还有5911个对应于5911时间戳的标签,稍后我将连接这些标签。在

更新: 根据我试过的评论中的链接

>>> a = np.array([[1,2,3], [2,3,4]])
>>> a
array([[1, 2, 3],
       [2, 3, 4]])
>>> b = np.array([[1,2,3,0], [2,3,4,0]])
>>> b
array([[1, 2, 3, 0],
       [2, 3, 4, 0]])
>>> c= np.hstack((a,b))
>>> c
array([[1, 2, 3, 1, 2, 3, 0],
       [2, 3, 4, 2, 3, 4, 0]])

在这个例子中,堆叠是可行的,但是不知道为什么同样的逻辑对我不起作用。在

最新消息:我按照克马尔的建议解决了这个问题:

mfcc_features_with_times= np.hstack((required_time_stamps[:,None],time_based_mfcc_feature))

然而,只有当两者具有相同的维度时,这才是正确的。 在大多数情况下,我的结果是数组A的形状(8400,)和数组B的形状(8399,21)。在

如何截断/删除A的最后几行,使A和B具有相同的形状 (8399,)和(8399,21)。 请告知。在

切片时发生更新错误: 现在我做A = A[:B.shape[0],:] 哪里 A = new_labels_np_arrayB = time_based_mfcc_feature

` 64     if len(new_labels_np_array) > len(time_based_mfcc_feature):
---> 65         new_labels_np_array = new_labels_np_array[:time_based_mfcc_feature.shape[0],:]
     66     elif len(time_based_mfcc_feature)>len(new_labels_np_array):
     67         time_based_mfcc_feature = time_based_mfcc_feature[:,new_labels_np_array.shape[0],:]

IndexError: too many indices for array`

Tags: newtimenprequiredvalarrayfeaturebased
1条回答
网友
1楼 · 发布于 2024-06-17 17:10:37

既然您已经在线程numpy-concatenate-2d-arrays-with-1d-array中找到了问题第一部分的答案,那么我将解决第二个问题:

How do I truncate/delete the last few rows of A so that both A and B have same shapes like (8399,) and (8399, 21) . Please advise.

您可以像分割slice a list那样对numpy数组进行切片。所以要沿轴0将2D数组B裁剪为A的大小。在

B = B[:A.shape[0],:]

这将修剪阵列的末端。如果要在开始时修剪,即丢弃不适合形状的前几行,而不是最后几行:

^{pr2}$

编辑:您的注释暗示您事先不知道哪个数组更长。在这种情况下:

trim = min(A.shape[0], B.shape[0])
A = A[:trim]
B = B[:trim,:] 

或分别

trim = min(A.shape[0], B.shape[0])
A = A[-trim:]
B = B[-trim:,:]

相关问题 更多 >