You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

sklearn决策树分类器仅预测最频繁类别?如何指定预测True的概率?

Understanding Decision Tree Leaf Class Labels & Getting True Class Probabilities

Hey there, let's clear up what's going on with your decision tree visualization and how to get the True class probabilities you need without flipping your target variable.

Why All Leaves Show Class=False (or Class=True After Flipping)

The Class label shown in decision tree visualizations is the majority class within that node/leaf by default. Since your original PP variable has ~66% False/0 samples, every split ends up with False being the most common class in each leaf—hence the Class=False label. When you flipped to PP2 where True becomes the majority (~66%), the same logic applies, so leaves show Class=True. This is just the default behavior of sklearn's tree visualization, not a problem with your model's ability to predict the True class.

How to Get True Class Probabilities (No Need to Flip Labels)

Your model is already capable of predicting the probability of the True class—you just need to use the right method:

  • Use tree_clf.predict_proba(X): This returns a 2D array where each row corresponds to a sample. The first column is the probability of the False class, and the second column is the probability of the True class. You can confirm the order of classes by checking tree_clf.classes_ (it will be array(['False', 'True'], dtype=object) since you cast PP to string).
  • For example, to get the True probability for a single sample:
    true_prob = tree_clf.predict_proba(sample)[0][1]
    

Improve Visualization to Show True Class Proportions

If you want your tree visualization to clearly display the True class distribution (instead of just the majority class), adjust the plot_tree parameters:

from sklearn.tree import plot_tree
import matplotlib.pyplot as plt

plt.figure(figsize=(20, 10))
plot_tree(
    tree_clf,
    feature_names=predictors,
    class_names=tree_clf.classes_,  # Show both class names
    proportion=True,  # Display class proportions instead of raw counts
    filled=True,
    rounded=True,
    impurity=False  # Optional: Hide impurity if not needed
)
plt.show()

This will show the percentage of False and True samples in each node/leaf, so you can easily see how the model splits data relative to the True class.

Bonus: Handling Imbalanced Data (Optional)

Since your dataset is slightly imbalanced (66% False / 34% True), you might want to make the model more sensitive to the True class during training. Add class_weight='balanced' to your DecisionTreeClassifier initialization:

tree_clf = DecisionTreeClassifier(
    max_depth=3,
    random_state=42,
    min_samples_leaf=500,
    class_weight='balanced'
)

This adjusts the model's loss function to give more weight to the minority class (True), which can help improve predictions for that class if it's business-critical.


内容的提问来源于stack exchange,提问作者opt135

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:54:24