决策树

2023-01-09  本文已影响0人  y_7539
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
上一篇 下一篇

猜你喜欢

热点阅读