连接/合并MX1 numpy数组和MXN numpy数组 [英] concatenate/ combine MX1 numpy array with MXN numpy array

查看:82
本文介绍了连接/合并MX1 numpy数组和MXN numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

required_time_stamps contains 5911 time stamps
time_based_mfcc_feature contains 5911 samples each having 20 mfcc features.

因此,如果您要查看time_based_mfcc_feature
它看起来像:

So if you were to look at time_based_mfcc_feature
it will look like :

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'>

< type 'numpy.ndarray'>

print required_time_stamps.shape  

(5911,)

print type(time_based_mfcc_feature)

<输入'numpy.ndarray'>

< type 'numpy.ndarray'>

print time_based_mfcc_feature.shape  

(5911,20)

我想将两者结合起来,这样我就可以了:

I want to combine these two so that I will have :

在R中,我可以简单地完成

In R, I can simply do

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中实现呢?

How would I do achieve this in python ?

这样最终数据将如下所示:

So that the final data would look like this :

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到time 5911仅是required_time_stamps中包含的值.
我尝试过:

Where these time1 to time 5911 are simply the values contained in the required_time_stamps.
I tried :

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

我还查看了以下内容:将多个一维数组与一维数组串联但我认为这是另外一回事.

I also looked at : Numpy concatenate 2D arrays with 1D array but I think it is something else.

目标是将这些数据逐行馈送到keras神经网络. 我也有与5911时间戳相对应的5911标签,我将在以后进行连接.

Goal is to feed this data to a keras neural network, row by row.
I also have 5911 labels corresponding to the 5911 time stamps, which I will concatenate later.

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

UPDATE: Based on the links in comments I tried,

>>> 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]])

在此示例中,堆叠有效,但不知道为什么相同的逻辑对我不起作用.

For this example the stacking works , but no clue why the same logic is not working for me.

更新:我能够通过遵循cmaher的建议来解决:

UPDATE : I was able to solve by follwing cmaher's suggestion:

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

但是,只有当它们具有相同的尺寸时,这才是正确的. 在大多数情况下,我最终得到形状为(8400,)的数组A和形状为(8399,21)的数组B.

however this is true only if both have same dimension. In most cases I am ending up with Array A having shape (8400,) and Array B having shape (8399, 21).

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

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.

在更新时发生更新错误: 目前当我做A = A[:B.shape[0],:] 在哪里 A = new_labels_np_array B = time_based_mfcc_feature

UPDATE ERROR WHILE SLICINg: Currently When I do A = A[:B.shape[0],:] where 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`

推荐答案

由于您已经在线程

Since you've found already an answer for the first part of your question in the thread numpy-concatenate-2d-arrays-with-1d-array, I'll address the second question:

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

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.

您可以像对对列表进行切片一样对numpy数组进行切片.因此,沿着轴0将2D数组B修剪为A的大小.

You can slice a numpy array like you would slice a list. So to trim a 2D-array B to the size of A along axis 0.

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

这将修剪数组的末尾.如果要在开始时进行修剪,即丢弃不适合形状的前几行,而不是最后一行:

This trims the end of the array. If you want to trim at the beginning, i.e. throw away the first few rows that don't fit into shape instead of the last:

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

编辑:您的注释意味着您事先不知道哪个数组更长.在这种情况下:

Your comment implies that you don't know in advance which of the arrays is longer. In that case:

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:,:]

这篇关于连接/合并MX1 numpy数组和MXN numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆