Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

getting shape errors for .score method from sklearn

df = pd.read_csv('../input/etu-ai-club-competition-2/train.csv')
df.shape
(750000,77)

X = df.drop(columns = 'Target')
y = df['Target']


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25)
model = MLPRegressor(hidden_layer_sizes = 60, activation = "relu", solver = "adam")
model

model.fit(X_train, y_train)

pr = model.predict(X_test)
pr.shape
(187500,)

model.score(y_test, pr)

ValueError: Expected 2D array, got 1D array instead:
array=[-120.79511811 -394.11307519 -449.59524477 ... -432.46130084 -492.81440014
 -753.02016315].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

Just started getting into ml. I dont really understand why I need to have a 2d array to get score or how do I convert mine into one. I did try to reshape it as said in the error but when I do that I get the messages ValueError: X has 1 features, but MLPRegressor is expecting 76 features as input. and ValueError: X has 187500 features, but MLPRegressor is expecting 76 features as input. for reshaping into (-1, 1) and (1, -1) respectively.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The correct way to call the score method would be:

model.score(X_test, y_test)

Internally, it first computes the predictions and then passes the predictions to a scoring function.

If you want to pass the predictions directly, you need to use one of the scoring functions in the metrics package, as explained here:

https://scikit-learn.org/0.15/modules/model_evaluation.html

Note: you might also want to have a look at the example code in the MLPRegressor documentation:

https://scikit-learn.org/stable/modules/generated/sklearn.neural_network.MLPRegressor.html

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading