I am trying to train a logistic regression model to recognize handwritten English letters. In my test data I have 74880 images. Each image has 784 pixels. The labels correspond to the place in the English alphabet. For example, A is 1, B is 2 and so on. In total there are 26 classes.

In order to optimize the model I decided to one-hot encode the labels. This means for an image with the label 23 (the letter W) after encoding the label will become: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0]. However, when encoding the labels I receive this weird error: ValueError: y should be a 1d array, got an array of shape (74880, 26) instead. This error does not occur when using another model like multilayer perceptron. Weird fact: sometimes I receive (37440, 26) instead of the (74880, 26) in my error after running the same exact code again.

Anyone has an explanation? Thanks in advance.

Here is the source code:

import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelBinarizer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn.model_selection import GridSearchCV
from sklearn.preprocessing import StandardScaler
from sklearn.neural_network import MLPClassifier

def binarize(y_train, y_val, y_test):
    one_hot = LabelBinarizer()
    Y_train = one_hot.fit_transform(y_train)
    Y_val   = one_hot.transform(y_val)
    Y_test = one_hot.transform(y_test)
    return Y_train, Y_val, Y_test

def lgr(X_train, X_val, X_test, Y_train, Y_val, Y_test):
    lgr = LogisticRegression(random_state=999, verbose=2)

    parameters = {
    'solver': ['sag'],
    'max_iter': [10]
    clf = GridSearchCV(lgr, parameters, n_jobs=-1, cv=2, verbose=2)
    clf.fit(X_train, Y_train)
    print(grid_result.best_score_, grid_result.best_params_)
    # Y_pred = lgr.predict(X_val)
    # acc = accuracy_score(Y_val, Y_pred)
    # print(acc)

def main():
    # loading trainingset
    with np.load('training-dataset.npz') as data:
        img = data['x']
        lbl = data['y']

    # train 60% validation 20% test 20% split
    X_train, X_test, y_train, y_test = train_test_split(img, lbl, test_size=0.2, random_state=999)
    X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.25, random_state=999)

    scaler = StandardScaler()
    X_train = scaler.fit_transform(X_train)

    X_val = scaler.transform(X_val)
    X_test = scaler.transform(X_test)

    # one-hot encoding
    Y_train, Y_val, Y_test = binarize(y_train, y_val, y_test)

    lgr(X_train, X_val, X_test, Y_train, Y_val, Y_test)

if __name__ == '__main__':
New contributor
jxpython is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.