gpt4 book ai didi

Python 决策树回归器

转载 作者:行者123 更新时间:2023-11-30 09:40:57 25 4
gpt4 key购买 nike

我尝试了以下代码,但出现了此错误

数据集的链接位于下面的链接

ValueError---> line 18 ds1_model.fit(X, y)

ValueError: could not convert string to 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 都不是正确的指标。

您应该使用 DecisionTreeClassifieraccuracy_score 来代替:

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 决策树回归器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58737970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com