“Setting an array element with a sequence” numpy error

社会主义新天地 提交于 2019-12-11 04:14:51

问题


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

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:

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:

What am I doing wrong?

Code:

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)

回答1:


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)

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

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

This will make X_train a normal 2D array.



来源:https://stackoverflow.com/questions/46990022/setting-an-array-element-with-a-sequence-numpy-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!