[英]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.