gpt4 book ai didi

python - 决策树: Probability of prediction inversely proportional in python

转载 作者:行者123 更新时间:2023-11-30 09:05:05 24 4
gpt4 key购买 nike

我想创建与决策树中每个类别成反比的预测概率。类似于所描述的here第 9 页 4.1 中的公式。我该如何引用我的代码来做到这一点:

import numpy as np
import pandas as pd
from sklearn.cross_validation import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn import tree
url="https://archive.ics.uci.edu/ml/machine-learning-databases/abalone/abalone.data"
c=pd.read_csv(url, header=None)
X = c.values[:,1:8]
Y = c.values[:,0]
X_train, X_test, y_train, y_test = train_test_split( X, Y, test_size = 0.3, random_state = 100)
clf_entropy = DecisionTreeClassifier(criterion = "entropy", random_state = 100,
max_depth=3, min_samples_leaf=5)
clf_entropy.fit(X_train, y_train)
probs = clf_entropy.predict_proba(X_test)
probs

目标是将零概率替换为小的非零值并对概率进行归一化以使其成为分布。然后选择标签,使得选择的概率成反比与当前树的预测成比例。 enter image description here

最佳答案

上述方程可以通过以下代码片段实现。

def inverse_prob(model_probs):
model_probs[model_probs == 0 ] = 1e-5
inverse = 1/model_probs
return inverse/inverse.sum(axis=0)

只要给定的概率分布中有零值,就添加一个小值 1e-5。

关于python - 决策树: Probability of prediction inversely proportional in python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53936310/

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