使用scikit-learn在python中进行机器学习

时间:2019-07-05 08:14:28

标签: python

我制作了有关汽车信息的数据库,包括,汽车制造商,里程,年份,价格,如下所示:

[[('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

1 个答案:

答案 0 :(得分:1)

LabelEncoder仅对目标类起作用,因此您不应将x传递给它(请参阅here)。而且似乎您为目标类使用了错误的索引:  y.append([item[4]])应该是y.append([item[0]])