如何限制在决策树分类器的特征重要性图上绘制的特征数量?

问题描述 投票:0回答:1

我正在评估我的决策树分类器,并且试图绘制功能的重要性。该图形可以正确打印,但可以打印所有(80多个)功能,从而产生非常混乱的视觉效果。我试图弄清楚如何将绘图按重要性顺序限制为仅重要的变量。

指向数据集的链接,您可以下载到您的工作目录,名为(“ File):https://github.com/Arsik36/Python

最小可复制代码:

   import pandas as pd
        import matplotlib.pyplot as plt
        import seaborn as sns
        from sklearn.model_selection import train_test_split  
        from sklearn.tree import DecisionTreeClassifier

        file = 'file.xlsx'
        my_df = pd.read_excel(file)

        # Determining response variable
        my_df_target = my_df.loc[ :, 'Outcome']

        # Determining explanatory variables
        my_df_data = my_df.drop('Outcome', axis = 1)

        # Declaring train_test_split with stratification
        X_train, X_test, y_train, y_test = train_test_split(my_df_data,
                                                            my_df_target,
                                                            test_size = 0.25,
                                                            random_state = 331,
                                                            stratify = my_df_target)

    # Declaring class weight
    weight = {0: 455, 1:1831}

    # Instantiating Decision Tree Classifier
    decision_tree = DecisionTreeClassifier(max_depth = 5,
                                           min_samples_leaf = 25,
                                           class_weight = weight,
                                           random_state = 331)

    # Fitting the training data
    decision_tree_fit = decision_tree.fit(X_train, y_train)

    # Predicting on the test data
    decision_tree_pred = decision_tree_fit.predict(X_test)

# Declaring the number of features in the X_train data
n_features = X_train.shape[1]

# Setting the plot window
figsize = plt.subplots(figsize = (12, 9))

# Specifying the contents of the plot
plt.barh(range(n_features), decision_tree_fit.feature_importances_, align = 'center')
plt.yticks(pd.np.arange(n_features), X_train.columns)
plt.xlabel("The degree of importance")
plt.ylabel("Feature")

我试图将当前输出限制为仅重要的功能:enter image description here

python machine-learning decision-tree
1个回答
0
投票

您需要修改所有绘图代码以删除低重要性功能,请尝试此操作(未经测试):

# Setting the plot window
figsize = plt.subplots(figsize = (12, 9))

featues_mask = tree.feature_importances_> 0.005

# Specifying the contents of the plot
plt.barh(range(sum(featues_mask)), tree.feature_importances_[featues_mask], align = 'center')
plt.yticks(pd.np.arange(sum(featues_mask)), X_train.columns[featues_mask])
plt.xlabel("The degree of importance")
plt.ylabel("Feature")
© www.soinside.com 2019 - 2024. All rights reserved.