Issue
I wrote this program, but i get error:
ValueError: could not convert string to float
My code:
import sqlite3
from sklearn import tree
database = sqlite3.connect("database.db")
c = database.cursor()
x = []
y = []
for row in c.execute('SELECT * FROM cars'):
x.append(row[0])
y.append(row[1])
clf = tree.DecisionTreeClassifier()
ml = clf.fit(x,y)
edit: I read this link (https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.LabelEncoder.html) but i do not know how to use label encoder in this app - please help me to use label encoder or solve this problem
Solution
An example how to use LabelEncoder
properly:
I created a y
list which contains some strings:
dummy_y = ['label1', 'label1', 'label2', 'label3', 'label77']
We want to encode each element in the list to a numeric value, LabelEncoder
maps each string value to a corresponding numerical value.
First import the LabelEncoder
class & create an instance of it:
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
Now we can fit_transform
our dummy list which means that the instance will first map each string to a value with fit
and then will actually apply it with transform
:
encoded_dummy_y = le.fit_transform(dummy_y)
will return:
array([0, 0, 1, 2, 3])
Where 0
corresponds to label1
, 1
to label2
etc.
Answered By - ImSo3K
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.