决策树实现鸢尾花三分类

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/haishu_zheng/article/details/81452690

一、 数据集

Iris(鸢尾花)数据集是多重变量分析的数据集。
数据集包含150行数据,分为3类,每类50行数据。
每行数据包含4个属性:Sepal Length(花萼长度)、Sepal Width(花萼宽度)、Petal Length(花瓣长度)和Petal Width(花瓣宽度)。可通过这4个属性预测鸢尾花卉属于三个种类(Setosa,Versicolour,Virginica)中的哪一类。

样本数据局部截图:

Iris.png

完整的样本数据请自行下载Iris.csv

二、算法代码

from sklearn.tree import DecisionTreeClassifier
from sklearn import datasets
from sklearn.cross_validation import train_test_split
from sklearn.metrics import accuracy_score

iris = datasets.load_iris()
iris_feature = iris.data
iris_target = iris.target

feature_train, feature_test, target_train, target_test = train_test_split(iris_feature, iris_target, test_size=0.33,
                                                                          random_state=56)
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 (accuracy_score(predict_results, target_test))
print (scores)

运行结果:

[2 1 2 2 2 2 2 0 0 2 1 0 0 0 2 0 0 0 2 1 0 2 1 1 0 2 2 1 1 1 2 2 2 0 0 0 2
 2 2 0 1 2 2 2 1 1 2 2 1 0]
[2 1 1 2 2 2 2 0 0 2 1 0 0 0 1 0 0 0 2 1 0 2 1 1 0 2 2 1 1 1 2 2 1 0 0 0 2
 2 2 0 1 2 2 1 1 1 2 2 1 0]
0.92
0.92


机器学习交流QQ群:658291732
TopCoder & Codeforces & AtCoder交流QQ群:648202993
更多内容请关注微信公众号
wechat_public_header.jpg

猜你喜欢

转载自blog.csdn.net/haishu_zheng/article/details/81452690