TypeError: float() argument must be a string or a number, not ‘LabelEncoder’

Mj Cheruiyot
Nov 11, 2020

Still working on my Logistic Regression problem and as I trained my model, error appears;

TypeError: float() argument must be a string or a number, not ‘LabelEncoder’

This is my code;

from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
subscription.poutcome = le.fit(subscription.poutcome)subscription.day_of_week= le.fit(subscription.day_of_week)subscription.month = le.fit(subscription.month)
#splitting the dataset for trainingX = subscription.drop(['y'],axis=1)y = subscription['y']from sklearn.model_selection import train_test_splitX_train, X_test, y_train,y_test = train_test_split(X, y, test_size = 0.2, random_state =10)
#fit model
from sklearn.linear_model import LogisticRegressionlogreg = LogisticRegression()logreg.fit(X_train, y_train)TypeError: float() argument must be a string or a number, not 'LabelEncoder'

The main reason this happened is cause the values in the columns were fit to the label encoder and not transformed.

The right way to have done this was;

from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
subscription.poutcome = le.fit_transform(subscription.poutcome)subscription.day_of_week= le.fit_transform(subscription.day_of_week)subscription.month = le.fit_transform(subscription.month)

--

--

Mj Cheruiyot

Analytics Engineer/ Data Enthusiast/ Lover of Technology