2

When I use model.predict(), the following error occurs,

ValueError: could not convert string to float:

One-hot encoding was used in my code as follows,

features_df = pd.get_dummies(df, columns=['garage_type', 'city'])

The predict code used is given below.

sample = [["attached"], ["Richardport"]]
predictedvalue = model.predict(sample)'

Any suggestions to solve this error? Thank you.

Krishi H
  • 526
  • 8
  • 26
Vincent Teo
  • 43
  • 1
  • 9

2 Answers2

0

The reason why you are getting an error is because you are sending strings to your model, while the model is expecting a vector of floats which is to be of the same shape as that of your training data.

From the code, it is clear that you are using one hot encoding before you use your data for training. You need to convert your sample also into the same one hot encoded vector format to get the answer.

secretive
  • 2,032
  • 7
  • 16
  • can you assist with the suggested codes ? I have this way but still returns error : Sample = X_train[['attached','Richardport']].values and X.astype('str') – Vincent Teo May 27 '19 at 05:22
0

You'll need to convert your sample to separate columns also, That means, you have to check the new dummy columns, and convert your sample to a boolean list L, where L[i]=True if column[i]=='attached' or column[i]=='Richardport' and False for all other list elements, before calling the .predict(L) method.

This response might be helpful:

How to use Pandas get_dummies on predict data?

Nadhem Maaloul
  • 433
  • 5
  • 11