Skip to content Skip to sidebar Skip to footer

"valueerror: Could Not Convert String To Float" Error In Scikit-learn

I'm running the following script: import numpy as np import matplotlib.pyplot as plt import pandas as pd from sklearn.preprocessing import OneHotEncoder dataset = pd.read_csv('data

Solution 1:

It is categorical_features=3 that hurts you. You cannot use categorical_features with string data. Remove this option, and luck will be with you. Also, you probably need fit_transform, not fit as such.

onehotencoder = OneHotEncoder(handle_unknown='ignore')
transformed = onehotencoder.fit_transform(X[:, [3]]).toarray()
X1 = np.concatenate([X[:, :2], transformed, X[:, 4:]], axis=1)
#array([[165349.2, 136897.8, 0.0, '0.0, 1.0, 192261.83],#       [162597.7, 151377.59, 1.0, 0.0, 0.0, 191792.06],#       [153441.51, 101145.55, 0.0, 1.0, 0.0, 191050.39],#       [144372.41, 118671.85, 0.0, 0.0, 1.0, 182901.99],#       [142107.34, 91391.77, 0.0, 1.0, 0.0, 166187.94']])

Solution 2:

Try this:

from sklearn.compose import ColumnTransformer, make_column_transformer
from sklearn.preprocessing import OneHotEncoder

columntransformer = make_column_transformer(
(OneHotEncoder(categories='auto'), [3]),
    remainder='passthrough')


X = columntransformer.fit_transform(X)
X = X.astype(float)

Post a Comment for ""valueerror: Could Not Convert String To Float" Error In Scikit-learn"