欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

决策树 鸢尾花分类 数据挖掘Python

程序员文章站 2024-02-11 16:36:52
...
from sklearn.tree import DecisionTreeClassifier
from sklearn import datasets
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split

iris = datasets.load_iris()
iris_feature = iris.data #特征数据
iris_target = iris.target #分类数据
print (iris_target)
#scikit-learn 已经将花的原名称进行了转换,其中 0, 1, 2 分别代表 Iris Setosa, Iris Versicolour 和 Iris Virginica
feature_train, feature_test, target_train, target_test = train_test_split(iris_feature, iris_target, test_size=0.2,random_state=70)
print("111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111")
print(target_train)

dt_model = DecisionTreeClassifier()
dt_model.fit(feature_train, target_train)
predict_results = dt_model.predict(feature_test)
scores = dt_model.score(feature_test, target_test)

print(predict_results)
print(target_test)
#print(isinstance('abc', Iterable))
print(accuracy_score(predict_results, target_test))
print(scores)

0 1 2分别为鸢尾花种类
决策树 鸢尾花分类 数据挖掘Python