使用决策树分类器时发生值错误

时间:2019-05-21 16:00:29

标签: python jupyter-notebook classification decision-tree

我正在尝试使用Iris数据集运行此决策树分类器,但我不断收到错误消息:ValueError: could not convert string to float: 'sepal_length'

(代码https://drive.google.com/folderview?id=1WzOaIsdgoJaQ-OkExobP7Uo-ljtUoLMw的屏幕截图)

import pandas as pd
from sklearn.tree import DecisionTreeClassifier # Import Decision Tree Classifier
from sklearn.model_selection import train_test_split # Import train_test_split function
from sklearn import metrics

import os
os.chdir('C:\\Users\\michael\\Documents')

col_names = ['sepal length', 'sepal width', 'petal length', 'petal width', 'species']

iris = pd.read_csv("iris.csv", header=None,names=col_names)
iris.head()

feature_cols = ['sepal length', 'sepal width', 'petal length', 'petal width']
X = iris[feature_cols] # Features
y = iris.species # Target variable

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1) # 70% training and 30% test

clf = DecisionTreeClassifier()

# Train Decision Tree Classifier
______> clf = clf.fit(X_train,y_train) --------- error occurs here(could not convert string to float:'sepal_length'...value error

#Predict the response for test dataset
y_pred = clf.predict(X_test)

1 个答案:

答案 0 :(得分:0)

Header = 0,应该在那里,谢谢大家