“Setting an array element with a sequence” numpy e

2019-08-10 22:20发布

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

Beginning

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:

Transformation

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: 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条回答
神经病院院长
2楼-- · 2019-08-10 22:49

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.

查看更多
登录 后发表回答