【问题标题】:Error: Header of CSV file is engaged decision tree calculation in scikit-learn Library错误:CSV 文件的标题在 scikit-learn 库中参与决策树计算
【发布时间】:2021-07-18 01:45:25
【问题描述】:

我运行以下代码表单,通过 scikit-learn 库创建决策树。

import numpy as np
from sklearn.model_selection import train_test_split
import os
from sklearn.tree import export_graphviz
import graphviz

#progress 1
path="/mnt/d/TestDecisionTree/datasets"
os.chdir(path)
os.getcwd()

#progress 2
dataset=np.loadtxt("internetlogit.csv", delimiter=",")
x=dataset[:,0:5]
y=dataset[:,5]

#progress 3
from sklearn.tree import DecisionTreeRegressor
X_train, X_test, y_train, y_test = train_test_split(x, y)
tree = DecisionTreeRegressor().fit(X_train,y_train)

#progress 4
print("Training set accuracy: {:.3f}".format(tree.score(X_train, y_train)))
print("Test set accuracy: {:.3f}".format(tree.score(X_test, y_test)))

#progress 5
dtree = tree.predict(x)
print(dtree)

#progress 6
percentageerror_tree=((y-dtree)/dtree)*100
percentageerror_tree

#progress 7
np.mean(percentageerror_tree)

#progress 8
export_graphviz(tree,out_file="result/tree.dot")

with open("result/tree.dot") as f:
    dot_graph = f.read()

graphviz.Source(dot_graph)

我的示例数据是 internetlogit.csv 文件中的以下数据集

age,gender,webpages,videohours,income,usage
36,0,32,0.061388889,6021,0
33,0,49,8.516666667,10239,1
46,1,22,0,1374,0
53,0,16,2.762222222,5376,0
27,1,30,0,1393,0
21,1,23,2.641111111,4866,0
42,0,30,0,1673,0
...

但我在“进度 2”中收到此错误。

ValueError: could not convert string to float: 'age'

这意味着 CSV 文件的标题参与决策树计算。但是,不应该是这样的。我该如何解决这个问题?

感谢您的帮助。

【问题讨论】:

    标签: python python-3.x scikit-learn decision-tree


    【解决方案1】:

    pandas 将是最简单的直接修复,例如:

    import pandas as pd
    from io import StringIO
    
    csv_file = StringIO("""
    age,gender,webpages,videohours,income,usage
    36,0,32,0.061388889,6021,0
    33,0,49,8.516666667,10239,1
    46,1,22,0,1374,0
    53,0,16,2.762222222,5376,0
    27,1,30,0,1393,0
    21,1,23,2.641111111,4866,0
    42,0,30,0,1673,0
    """)
    
    df = pd.read_csv(csv_file)
    
    y = df["usage"]
    x = df.drop(["usage"], axis=1)
    
    from sklearn.tree import DecisionTreeRegressor
    from sklearn.model_selection import train_test_split
    
    X_train, X_test, y_train, y_test = train_test_split(x, y)
    tree = DecisionTreeRegressor().fit(X_train,y_train)
    tree.fit(X_train, y_train)
    print(tree)
    

    ValueError 出现在发布代码的第 13 行,因为列名不能解释为字符串。

    如果您不想使用 pandas,也可以将 skiprows 传递给 np.loadtxt

    dataset = np.loadtxt(csv_file, delimiter=",", skiprows=2)
    x = dataset[:,0:5]
    y = dataset[:,5]
    

    【讨论】:

      猜你喜欢
      • 2016-12-31
      • 2018-08-16
      • 2017-05-21
      • 2017-02-23
      • 2020-04-05
      • 1970-01-01
      • 2015-03-05
      • 2017-03-26
      相关资源
      最近更新 更多