决策树

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score
from sklearn import tree

df = pd.read_csv("datas/iris_data.csv")
df.head()
image.png
# 定义x和y
x = df.drop(["target", "label"], axis=1)
y = df["label"]

# 模型训练
tree_classifier = tree.DecisionTreeClassifier(criterion="entropy", min_samples_leaf=5)
tree_classifier.fit(x, y)

# 预测和准确率 0.9733333333333334
y_predict = tree_classifier.predict(x)
accuracy_score(y, y_predict)

# 画决策树
plt.figure(figsize=(20, 17))
tree.plot_tree(tree_classifier,filled="True", feature_names=["entropy", "sepal width", "petal length", "petal length"], 
               class_names=["setosa", "versicolor", "virginica"])
image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。