Noobmaster6999
Noobmaster6999

Reputation: 11

I keep getting this error: y should be a 1d array, got an array of shape (576, 8) instead

im trying to test and train my data using the MLPRegressor, but always end up with the same error for the line "classifier.fit(xtrain, ttrain)", I'm relatively new to python so this has had me stuck for a while now

iv tried to use ravel() and reshaping it but neither has worked anyone got any good advice?

data = pd.read_excel("data.xlsx")
print(data)
from sklearn import preprocessing
from sklearn.preprocessing import MinMaxScaler

inputs = data.values[:,:8].astype(float)

#Normalize the inputs
scaler = MinMaxScaler()
scaled = scaler.fit_transform(inputs)

print(inputs.ptp(axis=0))
print(scaled.ptp(axis=0))

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.read_excel("ENB2012_data.xlsx")
x = df.iloc[:,1:2].values
y = df.iloc[:,2].values
df

from sklearn.neural_network import MLPRegressor
regressor = MLPRegressor(max_iter=5000)
regressor.fit(inputs, scaled)
outputs = regressor.predict(inputs)
print("MLP Regressor: \n", outputs)

from sklearn.metrics import mean_absolute_error
regressor = MLPRegressor(max_iter=5000)
regressor.fit(inputs, scaled)
outputs = regressor.predict(inputs)
print(mean_absolute_error(outputs, scaled))

from numpy.lib.shape_base import split
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score


from sklearn.base import ClassifierMixin
#split the data to training and testing
xtrain, xtest, ttrain, ttest = train_test_split(outputs, scaled)
#train the classifiers
classifier = SVC(gamma = "auto")
classifier.fit(xtrain, ttrain)
ytrain = classifier(xtrain)
ytest = classifier.predict(xtest)   

Edit:

ValueError                                Traceback (most recent call 
last)
<ipython-input-229-62670b265a4b> in <module>()
      4 #train the classifiers
      5 classifier = SVC(gamma = "auto")
----> 6 classifier.fit(xtrain, ttrain)
      7 ytrain = classifier(xtrain)
      8 ytest = classifier.predict(xtest)

4 frames
/usr/local/lib/python3.7/dist-packages/sklearn/utils/validation.py in 
column_or_1d(y, warn)
   1037 
   1038     raise ValueError(
   1039         "y should be a 1d array, got an array of shape {} 
instead.".format(shape)
   1040     )
   1041 

ValueError: y should be a 1d array, got an array of shape (576, 8) 
instead.

Upvotes: 1

Views: 1053

Answers (1)

Jesse H.
Jesse H.

Reputation: 733

While we need more context to answer your question, you should start with using np.shape(input) to check the shape of your input variable.

You should check after this line:

inputs = data.values[:,:8].astype(float)
# Check with np.shape
print(np.shape(input))

# Expected output:
...we need to know this

You want a 1-dimensional array for this. Here are a few examples,

# This is a 3x1 shape (1-dimensional).
np.shape([0, 1, 2])

# This is a 3x3 shape (multi-dimensional).
np.shape([[0, 1, 2],[3, 4, 5],[6, 7, 8]])

Upvotes: 1

Related Questions