一、预测概率直方图
我们可以通过绘制直方图来查看模型的预测概率的分布。
直方图以样本的预测概率分箱后的结果为横坐标,每个箱中的样本数量为纵坐标绘制一个图像。
具体代码实现为:
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression as LR
import matplotlib.pyplot as plt
data = load_breast_cancer()
X = data.data
print(X.shape)
y = data.target
Xtrain, Xtest, Ytrain, Ytest = train_test_split(X, y, test_size=0.3, random_state=420)
fig, ax1 = plt.subplots(figsize=(8, 6))
estimators = [GaussianNB().fit(Xtrain, Ytrain)
, LR(solver='lbfgs', max_iter=5000, multi_class='auto').fit(Xtrain,Ytrain)
, SVC(kernel='rbf', probability=True).fit(Xtrain,Ytrain)
]
name = ['GaussianNB', 'LogisticRegression', 'SVC']
for i, estimator in enumerate(estimators):
proba = estimator.predict_proba(Xtest)[:,0]
ax1.hist(proba
, bins=10
, label = name[i]
, histtype = "step" #直方图设置为透明色
, lw = 2 #直方图柱子描边的粗细
, density = True
)
ax1.set_xlabel("Distribution of