gpt4 book ai didi

python - 决策树 - 边缘/分支很轻,不可见

转载 作者:行者123 更新时间:2023-12-04 12:35:26 24 4
gpt4 key购买 nike

我正在使用经典的 Titanic 数据集来构建决策树。但是,我不确定几乎看不见的边缘或分支出了什么问题。

这是构建决策树的代码

    # Plant a new pruned tree 

ideal_dt = DecisionTreeClassifier(random_state=6, ccp_alpha=optimal_alpha)
ideal_dt = ideal_dt.fit(X_train, y_train)

# Plot the confusion matrix
plot_confusion_matrix(ideal_dt,X_test,y_test,display_labels=['Not Survived','Survived'])
plt.grid(False);

# Plot the tree
plt.figure(figsize=(200,180))
plot_tree(ideal_dt,filled=True,rounded=True, fontsize=120, class_names=labels,feature_names=data_features.columns);

print('\nIdeal Decision Tree')
# Training Score
print('Training Set Accuracy:',ideal_dt.score(X_train,y_train))
# Testing Score
print('Testing Set Accuracy:',ideal_dt.score(X_test,y_test))

enter image description here

这是设置:
# Basic Import
import pandas as pd
import numpy as np
import seaborn as sns
import random
import matplotlib.pyplot as plt

# Hypothesis Testing
from scipy.stats import ttest_ind, ttest_rel, ttest_1samp


# Machine Learning Import
import sklearn as skl
from sklearn import datasets

# Preprocessing
from sklearn.preprocessing import LabelEncoder

from sklearn.model_selection import train_test_split, cross_val_score

# Linear Regression
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Ridge

# KNN Classification
from sklearn.neighbors import KNeighborsClassifier, KNeighborsRegressor
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import scale
from sklearn.metrics import confusion_matrix
from sklearn.metrics import plot_confusion_matrix
from sklearn.metrics import f1_score
from sklearn.decomposition import PCA
from sklearn.model_selection import GridSearchCV

# K-means clustering
from sklearn.cluster import KMeans

# Logistic Regression
from sklearn.linear_model import LogisticRegression

# Decision Tree
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import DecisionTreeRegressor
from sklearn.tree import plot_tree
from sklearn.model_selection import cross_val_score

# Database Import
import sqlite3
from sqlite3 import Error

# Measure Performance
from sklearn.metrics import make_scorer, accuracy_score, r2_score, mean_squared_error
import sklearn.metrics as skm
from sklearn.metrics import classification_report


from sklearn.tree import DecisionTreeClassifier
# plt.style.use('seaborn-notebook')
## inline figures
%matplotlib inline
plt.style.use('seaborn')

## just to make sure few warnings are not shown
import warnings
warnings.filterwarnings("ignore")

我试过注释掉 plt.style.use('seaborn')但没有用。任何建议,将不胜感激

最佳答案

plot_tree()返回艺术家列表(Annotations 的列表)。您可以循环访问箭头并更改其属性。引用 https://matplotlib.org/api/_as_gen/matplotlib.patches.FancyArrowPatch.html#matplotlib.patches.FancyArrowPatch获取您可以更改的属性列表。

我不知道为什么箭头没有出现在你的情况下,但我会从它们的颜色和宽度开始。

from matplotlib import pyplot as plt
from sklearn.datasets import load_iris
from sklearn import tree
clf = tree.DecisionTreeClassifier(random_state=0)
iris = load_iris()
clf = clf.fit(iris.data, iris.target)
fig, ax = plt.subplots(figsize=(10,10))
out = tree.plot_tree(clf)
for o in out:
arrow = o.arrow_patch
if arrow is not None:
arrow.set_edgecolor('red')
arrow.set_linewidth(3)

enter image description here

关于python - 决策树 - 边缘/分支很轻,不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62318367/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com