[英]Value error when using decision tree classifier
我正在尝试使用 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)
Header=0
应该有什么,谢谢大家
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.