python实现cart算法鸢尾花数据集并可视化
时间: 2023-07-24 15:41:22 浏览: 109
python可视化代码集合(以鸢尾花为例)
好的,以下是Python实现CART算法鸢尾花数据集并可视化的代码:
```python
import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
# 加载鸢尾花数据集
iris = load_iris()
X = iris.data
y = iris.target
feature_names = iris.feature_names
target_names = iris.target_names
# 将数据集随机分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1)
# 创建决策树分类器
clf = DecisionTreeClassifier(criterion="gini")
# 拟合训练集
clf.fit(X_train, y_train)
# 可视化决策树
fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(4,4), dpi=300)
tree.plot_tree(clf, feature_names=feature_names, class_names=target_names, filled=True)
plt.show()
```
最后,运行代码即可生成一棵决策树,并可视化展示。
阅读全文