python 列表转化成数组_将列表转换为NumPy数组

当尝试将包含不同大小数组的列表转换为NumPy数组时,会得到一个形状为(2000,)的数组,而不是期望的(2000, 88200)。问题在于列表中的每个元素都是不同长度的数组。解决方案是通过填充零使所有数组具有相同的长度,然后转换为NumPy数组。" 126150859,10245277,Python urllib库详解与应用,"['Python', '网络编程', 'HTTP', '爬虫']
部署运行你感兴趣的模型镜像

I have a list that consits of 2000 rows and 88200 columns:

testlist = list(split_audio_to_parts(audio, self.sample_rate, self.audio_index))

debugging output of testlist gives

[array([-0.00683594, -0.00689697, -0.00708008, ..., 0. ,

0. , 0. ]), array([-0.01287842, -0.01269531, -0.01257324, ..., 0. ,

0. , 0. ]), array([0.02288818, 0.01940918, 0.01409912, ..., 0. , 0. ,

0. ]), array([0.00772095, 0.00671387, 0.00695801, ..., 0. , 0. ,

0. ]),

and so on.

split_audio_to_parts is a function:

def split_audio_to_parts(x, sample_rate, audio_index):

for i, row in audio_index.iterrows():

x_part = x[int(row['start_samples']):int(row['end_samples'])]

yield x_part

When I try to convert it to numpy array using samples = np.array(testlist) or samples = np.asarray(testlist), it gives me array of shape (2000,), although debugging shows that testlist consits of 2000 entries with 88200 positions. Why so? I'm using 64bit numpy and 64bit Python 3.6.5.

解决方案

The problem is testlist is a list of different size arrays. For example checkout this code:

>>>import numpy as np

>>>import random

>>>random.seed(3240324324)

>>> y=[np.array(list(range(random.randint(1,3)))) for _ in range(3)]

>>> y

[array([0, 1, 2]), array([0, 1, 2]), array([0])]

>>> np.array(y)

array([array([0, 1, 2]), array([0, 1, 2]), array([0])], dtype=object)

>>> np.array(y).shape

(3,)

and the array would be of object type instead of float. the only way for this to work is having same sized arrays.

If you really need to stuff these rows somehow into an array you can pad with zeros, for example:

>>> size = y[max(enumerate(y),key=lambda k:k[1].shape)[0]].shape[0]

>>> z=[np.append(x,np.zeros(size-x.shape[0])) for x in y]

>>> z

[array([ 0., 1., 2.]), array([0, 1, 2]), array([0, 0, 0])]

>>>np.array(z).shape

(3, 3)

but you would have to decide how you do this padding.

您可能感兴趣的与本文相关的镜像

GPT-SoVITS

GPT-SoVITS

AI应用

GPT-SoVITS 是一个开源的文本到语音(TTS)和语音转换模型,它结合了 GPT 的生成能力和 SoVITS 的语音转换技术。该项目以其强大的声音克隆能力而闻名,仅需少量语音样本(如5秒)即可实现高质量的即时语音合成,也可通过更长的音频(如1分钟)进行微调以获得更逼真的效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值