close

[Solved] RandomForestClassfier.fit(): Value: could not convert string to float

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error RandomForestClassfier.fit(): Value: could not convert string to float in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How RandomForestClassfier.fit(): Value: could not convert string to float Error Occurs?

Today I get the following error RandomForestClassfier.fit(): Value: could not convert string to float in python.

How To Solve RandomForestClassfier.fit(): Value: could not convert string to float Error ?

  1. How To Solve RandomForestClassfier.fit(): Value: could not convert string to float Error ?

    To Solve RandomForestClassfier.fit(): Value: could not convert string to float Error I selected OneHotEncoder that binarize all the strings. It is quite effective but if you have a lot different strings the matrix will grow very quickly and memory will be required.

  2. RandomForestClassfier.fit(): Value: could not convert string to float

    To Solve RandomForestClassfier.fit(): Value: could not convert string to float Error I selected OneHotEncoder that binarize all the strings. It is quite effective but if you have a lot different strings the matrix will grow very quickly and memory will be required.

Solution 1

You have to do some encoding before using fit. As it was told fit() does not accept Strings but you solve this.

There are several classes that can be used :

  • LabelEncoder : turn your string into incremental value
  • OneHotEncoder : use One-of-K algorithm to transform your String into integer

Personally I have post almost the same question on StackOverflow some time ago. I wanted to have a scalable solution but didn’t get any answer. I selected OneHotEncoder that binarize all the strings. It is quite effective but if you have a lot different strings the matrix will grow very quickly and memory will be required.

Solution 2

LabelEncoding worked for me (basically you’ve to encode your data feature-wise) (mydata is a 2d array of string datatype):

myData=np.genfromtxt(filecsv, delimiter=",", dtype ="|a20" ,skip_header=1);

from sklearn import preprocessing
le = preprocessing.LabelEncoder()
for i in range(*NUMBER OF FEATURES*):
    myData[:,i] = le.fit_transform(myData[:,i])

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read