蟒蛇:numpy的名单阵列和vstack [英] python: numpy list to array and vstack

查看:115
本文介绍了蟒蛇:numpy的名单阵列和vstack的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

from scipy.io.wavfile import read
filepath = glob.glob('*.wav')
rates = []
datas = []
for fp in filepath:
    rate, data = read(fp)
    rates.append(rate)
    datas.append(data)

我得到一个列表'数据'是:

I get a list 'datas' which is :

[array([0, 0, 0, ..., 0, 0, 0], dtype=int16), array([0, 0, 0, ..., 0, 0, 1], dtype=int16), array([0, 0, 0, ..., 0, 0, 0], dtype=int16),..., array([0, 0, 0, ..., 0, 0, 0], dtype=int16)]

我用

new_array = numpy.vstack([datas])

要获得new_array:

to get the new_array :

[[array([0, 0, 0, ..., 0, 0, 0], dtype=int16)
  array([0, 0, 0, ..., 0, 0, 1], dtype=int16)
  array([0, 0, 0, ..., 0, 0, 0], dtype=int16)
  ...
  array([0, 0, 0, ..., 0, 0, 0], dtype=int16)]]

但我真的preFER一个是:

But I really prefer one is :

(array([[ 0,  0,  0, ...,  0,  0,  0],
   [ 0,  0,  0, ...,  0,  0,  1],
   [ 0,  0,  0, ...,  0,  0,  0],
   ...,        
   [ 0,  0,  0, ...,  0,  0,  0]], dtype=int16)

我应该使用哪种功能?

Which function should I use?

感谢。

推荐答案

我下面的作品,所以 DATAS 的任何元素是不平坦的阵列喜欢你的问题建议,潜在的行具有不同的长度(这竟然是这个原因,见注释),或者你正在使用具有与 vstack ? (虽然我认为这是不可能的)

The following works for me, so either the elements of datas are not flat arrays like your question suggests, the potential rows have different lengths (this turned out to be the reason, see comments), or perhaps you are using an older version that has a problem with a 1-dimensional object in vstack? (although I think that is unlikely)

In [14]: datas = [np.asarray([0, 0, 0, 0, 0, 0]), np.asarray([0, 0, 0, 0, 0, 1])]

In [15]: datas
Out[15]: [array([0, 0, 0, 0, 0, 0]), array([0, 0, 0, 0, 0, 1])]

In [16]: datas[0].shape
Out[16]: (6,)

In [17]: np.vstack(datas)
Out[17]:
array([[0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 1]])

这篇关于蟒蛇:numpy的名单阵列和vstack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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