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)
<类型 'numpy.ndarray'>
print required_time_stamps.shape
(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仅是required_time_stamps中包含的数值。
我尝试过:
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]])
对于这个示例,堆叠功能是有效的,但我不知道为什么相同的逻辑对我不起作用。
更新:我按照cmaher的建议解决了问题:
mfcc_features_with_times= np.hstack((required_time_stamps[:,None],time_based_mfcc_feature))
然而,这只有在两者具有相同的尺寸时才为真。在大多数情况下,我得到的 Array A 的形状为 (8400,),而 Array B 的形状为 (8399, 21)。
如何截断/删除 A 的最后几行,使 A 和 B 的形状都变成 (8399,) 和 (8399, 21) 呢?请给予建议。
更新错误,当切片时:当我执行 A = A[:B.shape[0],:]
时,
其中
A = new_labels_np_array
B = 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`