利用Jupyter Notebook对数据运用决策树算法实现可视化的详细教程,具体到代码
时间: 2024-03-25 13:41:19 浏览: 208
好的,以下是具体的代码实现:
```python
# 导入必要的库
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import tree
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.tree import export_graphviz
import graphviz
# 导入数据集
iris = load_iris()
# 将数据集转换为DataFrame
iris_df = pd.DataFrame(data=np.c_[iris['data'], iris['target']], columns=iris['feature_names'] + ['target'])
iris_df.head()
# 数据集分割
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=42)
# 构建决策树模型
clf = tree.DecisionTreeClassifier(max_depth=3)
clf.fit(X_train, y_train)
# 可视化决策树
dot_data = tree.export_graphviz(clf, out_file=None, filled=True,
feature_names=iris.feature_names,
class_names=iris.target_names)
graph = graphviz.Source(dot_data)
graph
# 模型评估
y_pred = clf.predict(X_test)
accuracy_score(y_test, y_pred)
```
希望以上代码能帮到你。
阅读全文