0
votes

When I used to run this code I have this error. I have tried to solve it by others method but they are not sophisticated. The dataset looks like this:

[table of data]

And my code:

from sklearn.ensemble import RandomForestRegressor

regressor = RandomForestRegressor(n_estimators = 10, random_state = 0)
regressor.fit(df_train, y_train) 

Error trace:

File "C:\Users\Acer 15\Anaconda3\lib\site-packages\sklearn\utils\validation.py", line 433, in check_array array = np.array(array, dtype=dtype, order=order, copy=copy) ValueError: could not convert string to float: '15ML'

1
What is the error? And could you somehow format your input data in the question section rather posting an image? - Bussller
File "C:\Users\Acer 15\Anaconda3\lib\site-packages\sklearn\utils\validation.py", line 433, in check_array array = np.array(array, dtype=dtype, order=order, copy=copy) ValueError: could not convert string to float: '15ML' - faisal zahoor
What're the other methods that you tried that are not sophisticated? - Bussller
Error is due to 15ML being a string with numeric as well as string characters not been able to be converted to float. You need to process the data-set to make it work. Is your data-set in DB? or pandas dataframe? - Bussller
pandas dataframe - faisal zahoor

1 Answers

1
votes

To replace 15ML to 15 in pandas dataframe,

df['Quantity'].replace('15ML','15')

I have assumed the column name for 15ML column is Quantity. You should replace with the actual column name that you've. If you want to access by location, you can also use

df.ix[:,4].replace('15ML','15')

I have counted including the index column in the image. Actual location may vary according to how you load the data.