Search code examples
pythondecision-treevalueerror

Value Error: could not convert string to float: 'good'


I am trying to fit a decision tree model with the training dataset. But finding this error

credit_df=pd.read_csv('credit.csv')
credit_df.head()

[! dataframe]1

X = credit_df.drop("default" , axis=1)
Y=credit_df.pop("default")
from sklearn.model_selection import train_test_split

X_train, X_test, train_labels, test_labels = train_test_split(X, y, test_size=.30, random_state=1)

dt_model = DecisionTreeClassifier(criterion = 'gini' )
dt_model.fit(X_train, train_labels)

error message


Solution

  • I tried the code below and now the error is fixed. There were some object data types and i converted them into categorical values

    for feature in credit_df.columns: 
        if credit_df[feature].dtype == 'object': 
            credit_df[feature] = pd.Categorical(credit_df[feature]).codes