Search code examples
pythondata-sciencelogistic-regression

python code error. ValueError: y should be a 1d array, got an array of shape (56000, 10) instead


import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.linear_model import LogisticRegression
logistic_regression = LogisticRegression(random_state = 10)
logistic_regression.fit(X_train, y_train)
y_pred_logistic_regression = logistic_regression.predict(X_test)
print(y_pred_logistic_regression.shape)

ValueError: y should be a 1d array, got an array of shape (56000, 10)instead


Solution

  • Your y_train array is out of shape, assuming you are working with one hot encoded classes, the solution would be to convert it into a class index array:

    logistic_regression.fit(X_train, y_train.argmax(axis=1))
    

    example:

    >> y_train
    array([[0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
           [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],
           [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],
           [1., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
    # after conversion:
    >> y_train.argmax(axis=1)
    array([2, 3, 4, 0])