How to convert string to float, dtype='numeric' is not compatible with arrays of bytes/strings.Convert your data to numeric values explicitly instead

66 views Asked by At
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn import preprocessing

cols = ['country', 'population', 'median_age']
col_types = {'country': str, 'population': int, 'median_age': int}
database = pd.read_csv('dataset.csv', dtype=col_types)

le = preprocessing.LabelEncoder()
database['country'] = le.fit_transform(database['country'])

X = (database[['country']])
y = database.drop(columns=['country'])

model = DecisionTreeClassifier()
model.fit(X.values, y.values)
prevision = model.predict([['Italy']])
print(prevision)

I'm trying to code a program with machine learning, which with the name of a nation as only input, returns the median age and how many inhabitants that nation has. The problem is that fit and predict functions require a float, but my input is a string, so I tried to convert it using label encoder but I received this error:

dtype='numeric' is not compatible with arrays of bytes/strings. 
Convert your data to numeric values explicitly instead

How to resolve this?

0

There are 0 answers