【问题标题】:sklearn decision tree - could not convert string to floatsklearn 决策树 - 无法将字符串转换为浮点数
【发布时间】:2021-06-03 23:56:11
【问题描述】:

我有一个包含来自 truecar.com 的汽车信息的 CSV 文件,我想用这些数据预测汽车的价格,但我收到了错误消息。这是回溯:

File "x\Python39\lib\site-packages\numpy\core\_asarray.py", line 102, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: could not convert string to float: 'exterior_color'

代码:

import csv
from sklearn import tree

x = [] 
y = [] 

with open('x', 'r') as csv_file:
    data = csv.reader(csv_file)
    for line in data:
        x.append(line[0:-2])
        y.append(line[-1])

    # print(x)
    # print(y)

clf = tree.DecisionTreeClassifier()
clf = clf.fit(x, y)

【问题讨论】:

    标签: python scikit-learn


    【解决方案1】:

    DecisionTreeClassifierfit 方法在X 参数(documentation) 中采用浮点数组。

    我建议您对非数字变量进行一次热编码。我建议您阅读一些有关这种方法的文章,该方法将一列分类数据转换为多列布尔值。

    Passing categorical data to Sklearn Decision Tree

    Why One-Hot Encode Data in Machine Learning?

    【讨论】:

      猜你喜欢
      • 2018-02-15
      • 2020-05-25
      • 2018-09-20
      • 2019-02-06
      • 2019-02-08
      • 2020-08-26
      • 2020-05-05
      • 2021-10-10
      • 1970-01-01
      相关资源
      最近更新 更多