First model with scikit-learn _error

I have this error when "ValueError: could not convert string to float: ‘State-gov’
"
when I run the commands:
from sklearn.neighbors import KNeighborsClassifier

model = KNeighborsClassifier()
model.fit(data, target)

1 Like

Ok I found it…

I created another data frame whit only num values:

df_num=df.select_dtypes(include=[“int64”])
df_num
Thanks

3 Likes

Hello Daiana,

I am having a similar problem in that when I try to run this code:

from sklearn.neighbors import KNeighborsClassifier

model = KNeighborsClassifier()
model.fit(data, target)

I get a ValueError saying “could not convert string to float: ’ Private’”

Where did you put in your solution code to make it work for your problem? I would really appreciate your thoughts!

Thanks!

1 Like

The select_dtypes method does not affect the data in-place. Instead, it returns a new dataframe. In your code, data is still the original dataframe, without filtering, that’s why it still contains strings.

To solve this, you could either create a new dataframe, like @daiana did:

data_numeric = data.select_dtypes(include=[“int64”])

You will then be able to call fit like so:

model.fit(data_numeric, target)

You could also replace your existing dataframe (and the other columns will be lost):

data = data.select_dtypes(include=[“int64”])

And then keep your call to fit as-is.

2 Likes

Nevermind; it’s my fault! I was accessing the wrong dataset. Thanks for the advice!

1 Like

Hi , you can look for all data set in

So no need for this bypass :slight_smile:

2 Likes