我尝试了以下代码,但此错误一直在发生
数据集的链接在下面的链接中
ValueError
--->第18行ds1_model.fit(X,y)
ValueError:无法将字符串转换为float:'Iris-setosa'
import pandas as pd
from sklearn.metrics import mean_absolute_error
from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import train_test_split
url = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/iris.csv'
ds1 = pd.read_csv(url)
ds1.columns = (['SepalLength' , 'SepalWidth' , 'PetalLength' , 'PetalWidth' , 'ClassLabel'])
ds1_filtered=ds1.dropna(axis=0)
y = ds1_filtered.ClassLabel
ds1_features = ['SepalLength' , 'SepalWidth' , 'PetalLength' , 'PetalWidth']
X = ds1_filtered[ds1_features]
ds1_model = DecisionTreeRegressor()
ds1_model.fit(X, y)
PredictedClassLabel = ds1_model.predict(X)
mean_absolute_error(y, PredictedClassLabel)
train_X, val_X, train_y, val_y = train_test_split(X, y, random_state = 0)
ds1_model = DecisionTreeRegressor()
ds1_model.fit(train_X, train_y)
predicitions = ds1_model.predict(val_X)
print(mean_absolute_error(val_y, predictions))
您能帮忙建议或解释如何解决此问题吗?
DataSet Link
最佳答案
顾名思义,虹膜数据集是一种分类,而不是回归分类。因此,ClassLabel
都不是要使用的正确模型,DecisionTreeRegressor
也不是正确的指标。
您应该使用mean_absolute_error
和DecisionTreeClassifier
代替:
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
iris = load_iris()
clf = DecisionTreeClassifier()
train_X, val_X, train_y, val_y = train_test_split(iris.data, iris.label, random_state = 0)
clf.fit(train_X, train_Y)
pred = clf.predict(val_X)
print(accuracy_score(val_y, pred))
使用所述数据集的scikit-learn decision tree classification tutorial可以为您提供更多建议。
关于python - Python DecisionTreeRegressor,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58737970/