“设置具有序列的数组元素”。 numpy错误 [英] "Setting an array element with a sequence" numpy error

查看:74
本文介绍了“设置具有序列的数组元素”。 numpy错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事一个涉及必须使用以下形式的预处理数据的项目。

I'm working on a project that involves having to work with preprocessed data in the following form.

上面也给出了数据说明。目的是预测书面数字是否与所述数字的音频匹配。首先,我将形式(N,13)的口述数组转换为时间轴上的均值,如下所示:

Data explanation has been given above too. The goal is to predict whether a written digit matches the audio of said digit or not. First I transform the spoken arrays of form (N,13) to the means over the time axis as such:

这会为语音中的每个数组创建一致的长度(1,13)。为了在简单的香草算法中对此进行测试,我将两个数组压缩在一起,以便我们创建一个格式为(45000,2)的数组,当我将其插入LogisticRegression类的fit函数时,会引发以下错误:

This creates a consistent length of (1,13) for every array within spoken. In order to test this in a simple vanilla algorithm I zip the two arrays together such that we create an array of the form (45000, 2), when I insert this into the fit function of the LogisticRegression class it throws me the following error:

我在做什么错了?

代码:

import numpy as np
from sklearn.linear_model import LogisticRegression

match = np.load("/srv/digits/match_train.npy")
spoken = np.load("/srv/digits/spoken_train.npy")
written = np.load("/srv/digits/written_train.npy")
print(match.shape, spoken.shape, written.shape)
print(spoken[0].shape, spoken[1].shape)

def features(signal, function):
    new = np.copy(signal)
    for i in range(len(signal)):
        new[i] = function(new[i], axis=0)
    return new

spokenMean = features(spoken, np.mean)
print(spokenMean.shape, spokenMean[0])

result = np.array(list(zip(spokenMean,written)))
print(result.shape)

X_train, X_val, y_train, y_val = train_test_split(result, match, test_size = 
0.33, random_state = 123)
model = LogisticRegression()
print(X_train.shape, y_train.shape)
model.fit(X_train, y_train)
yh_val = model.predict(X_val)


推荐答案

spokenMean 类型为 object ,即它是一维数组,其中包含较小的一维数组。您需要做的第一件事是将其转换为2D浮点数组。这对我有用:

The spokenMean type is object, i.e. it's a 1D array which holds smaller 1D arrays. The first thing you need to do is to convert it to a 2D float array. This worked for me:

spokenMean = features(spoken, np.mean)
spokenMean = np.vstack(spokenMean[:]).astype(np.float32)

然后,列表( zip(...))不是连接两个数组的正确方法。而是调用 np.concatenate

Then, list(zip(...)) is not the right way to concatenate the two arrays. Instead, call np.concatenate:

result = np.concatenate([spokenMean, written], axis=1)

这将使 X_train 普通的2D数组。

This will make X_train a normal 2D array.

这篇关于“设置具有序列的数组元素”。 numpy错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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