ValueError: could not convert string to float: when running model


hello all,

I’m new to Python, hope someone can shed some light on this error and how python works.
I’m getting the error below when i run, Y_train) where Electronic is a name of a column in my dataframe. I get it with other categorical columns as well.

ValueError: could not convert string to float: 'Electronic’

Does this mean I have to convert all my predictors to numeric values before I can run any model in Python?

Will a model ever run without converting predictors to numerical values?




Hi @rich_fl,

Most machine learning models cannot deal with categorical variables. Which means that they can use only integers or float values. So if you have a variable (or a feature) which has multiple categories, you would need to convert them into numbers.

For instance, if you have a dataset with the following columns, MyField1 and MyFiled2 , the first variable is categorical.


So to convert it into numerical form, we can apply one hot encoding. it will look like this (image below).


You can refer this article below to understand this concept in detail:



Model will accept only numerical values. so you have to convert the sting value to numeic or do encoding for string value