[英]machine learning in python with scikit-learn
我制作了有关汽车信息的数据库,包括,汽车制造商,里程,年份,价格,如下所示:
[[('Volkswagen Polo', 82000, 2010, 43000)], [('Porsche 911', 2500, 2018, 349000)], [('Volvo S60', 89000, 2015, 98000)], [('BMW 1', 127467, 2012, 97000)]
我正在学习机器学习,我想使用决策树。
我想获取汽车制造商,行驶里程和年份并预测价格。 我尝试了很多方法,每次遇到错误时,我都会尝试。 例如:
ValueError: could not convert string to float: 'Volkswagen Polo'
or
ValueError: Found array with dim 3. Estimator expected <= 2.
or
TypeError: fit() takes 2 positional arguments but 3 were given
我试过下面的代码:
cursor = cnx.cursor()
cursor.execute('SELECT * FROM cars_2')
my_result = cursor.fetchall()
x = []
y = []
for item in my_result:
x.append([item[1:4]])
y.append([item[4]])
le = preprocessing.LabelEncoder()
le.fit(x, y)
要么
cursor = cnx.cursor()
cursor.execute('SELECT * FROM cars_2')
my_result = cursor.fetchall()
x = []
y = []
for item in my_result:
x.append([item[1:4]])
y.append([item[4]])
clf = tree.DecisionTreeClassifier()
clf = clf.fit(x, y
LabelEncoder
仅在目标类上运行,因此您不应将x
传递给它(请参阅此处 )。 而且似乎您为目标类使用了错误的索引: y.append([item[4]])
应该是y.append([item[0]])
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.