监督学习算法2.4-不确定度估计

#不确定度估计
import mglearn
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.datasets import load_breast_cancer
from sklearn.datasets import make_moons
from sklearn.datasets import make_blobs
from sklearn.datasets import make_circles
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.svm import LinearSVC
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.neural_network import MLPClassifier
from numpy.core.umath_tests import inner1d
from mpl_toolkits.mplot3d import Axes3D,axes3d

x,y = make_circles(noise=0.25,factor=0.5,random_state=1)
y_named = np.array(['blue','red'])[y]
x_train,x_test,y_train_named,y_test_named,y_train,y_test = train_test_split(x,y_named,y,random_state=0)
gbrt = GradientBoostingClassifier(random_state=0)
gbrt.fit(x_train,y_train_named)

print('x_test.shape:{}'.format(x_test.shape))
print('decision function shape:{}'.format(gbrt.decision_function(x_test).shape))
print('decision function:{}'.format(gbrt.decision_function(x_test)[:6]))
print('thresholded decision function:{}'.format(gbrt.decision_function(x_test) > 0))
print('prediction:{}'.format(gbrt.predict(x_test)))

x_test.shape:(25, 2)
decision function shape:(25,)
decision function:[ 4.13592629 -1.7016989 -3.95106099 -3.62599351 4.28986668 3.66166106]
thresholded decision function:[ True False False False True True False True True True False True
True False True False False False True True True True True False
False]
prediction:[‘red’ ‘blue’ ‘blue’ ‘blue’ ‘red’ ‘red’ ‘blue’ ‘red’ ‘red’ ‘red’ ‘blue’
‘red’ ‘red’ ‘blue’ ‘red’ ‘blue’ ‘blue’ ‘blue’ ‘red’ ‘red’ ‘red’ ‘red’
‘red’ ‘blue’ ‘blue’]

greater_zero = (gbrt.decision_function(x_test) > 0).astype(int)
pred = gbrt. classes_[greater_zero]
print('pred is equal to predictions:{}'.format(np.all(pred == gbrt.predict(x_test))))
decision_function = gbrt.decision_function(x_test)
print("decision function minimum:{:.2f} maximum:{:.2f}".format(np.min(decision_function),np.max(decision_function)))

pred is equal to predictions:True
decision function minimum:-7.69 maximum:4.29

fig,axes = plt.subplots(1,2,figsize=(13,5))
mglearn.tools.plot_2d_separator(gbrt,x,ax=axes[0],alpha=.4,fill=True,cm=mglearn.cm2)
scores_image = mglearn.tools.plot_2d_scores(gbrt,x,ax=axes[1],alpha=.4,cm=mglearn.ReBl)
for ax in axes:
    mglearn.discrete_scatter(x_test[:,0],x_test[:,1],y_test,markers='^',ax=ax)
    mglearn.discrete_scatter(x_train[:,0],x_train[:,1],y_train,markers='o',ax=ax)
    ax.set_xlabel('feature 0')
    ax.set_ylabel('feature 1')
cbar = plt.colorbar(scores_image,ax=axes.tolist())
axes[0].legend(['test class 0','test class 1','train class 0','train calss 1'],ncol=4,loc=(.1,1.1))

在这里插入图片描述

print('shape of probabilities:{}'.format(gbrt.predict_proba(x_test).shape))
print('predicted probabilities:{}'.format(gbrt.predict_proba(x_test)[:6]))

shape of probabilities:(25, 2)
predicted probabilities:[[0.01573626 0.98426374]
[0.84575649 0.15424351]
[0.98112869 0.01887131]
[0.97406775 0.02593225]
[0.01352142 0.98647858]
[0.02504637 0.97495363]]

fig,axes = plt.subplots(1,2,figsize=(13,5))
mglearn.tools.plot_2d_separator(gbrt,x,ax=axes[0],alpha=.4,fill=True,cm=mglearn.cm2)
scores_image = mglearn.tools.plot_2d_scores(gbrt,x,ax=axes[1],alpha=.5,cm=mglearn.ReBl,function='predict_proba')
for ax in axes:
    mglearn.discrete_scatter(x_test[:,0],x_test[:,1],y_test,markers='^',ax=ax)
    mglearn.discrete_scatter(x_train[:,0],x_train[:,1],y_train,markers='o',ax=ax)
    ax.set_xlabel('feature 0')
    ax.set_ylabel('feature 1')
cbar = plt.colorbar(scores_image,ax=axes.tolist())
axes[0].legend(['test class 0','test class 1','train class 0','train calss 1'],ncol=4,loc=(.1,1.1))

在这里插入图片描述

iris = load_iris()
x_train,x_test,y_train,y_test = train_test_split(iris.data,iris.target,random_state=42)
gbrt = GradientBoostingClassifier(learning_rate=0.01,random_state=0)
gbrt.fit(x_train,y_train)
print('decision function shape:{}'.format(gbrt.decision_function(x_test).shape))
print('decision function:{}'.format(gbrt.decision_function(x_test)[:6,:]))
print('argmax of decision function:{}'.format(np.argmax(gbrt.decision_function(x_test),axis=1)))
print('predictions:{}'.format(gbrt.predict(x_test)))

decision function shape:(38, 3)
decision function:[[-0.52931069 1.46560359 -0.50448467]
[ 1.51154215 -0.49561142 -0.50310736]
[-0.52379401 -0.4676268 1.51953786]
[-0.52931069 1.46560359 -0.50448467]
[-0.53107259 1.28190451 0.21510024]
[ 1.51154215 -0.49561142 -0.50310736]]
argmax of decision function:[1 0 2 1 1 0 1 2 1 1 2 0 0 0 0 1 2 1 1 2 0 2 0 2 2 2 2 2 0 0 0 0 1 0 0 2 1
0]
predictions:[1 0 2 1 1 0 1 2 1 1 2 0 0 0 0 1 2 1 1 2 0 2 0 2 2 2 2 2 0 0 0 0 1 0 0 2 1
0]

print('predicted probabilities:{}'.format(gbrt.predict_proba(x_test)[:6]))
print('sum:{}'.format(gbrt.predict_proba(x_test)[:6].sum(axis=1)))
print('argmax of predicted probabilities:{}'.format(np.argmax(gbrt.predict_proba(x_test),axis=1)))
print('predictions:{}'.format(gbrt.predict(x_test)))

predicted probabilities:[[0.10664722 0.7840248 0.10932798]
[0.78880668 0.10599243 0.10520089]
[0.10231173 0.10822274 0.78946553]
[0.10664722 0.7840248 0.10932798]
[0.10825347 0.66344934 0.22829719]
[0.78880668 0.10599243 0.10520089]]
sum:[1. 1. 1. 1. 1. 1.]
argmax of predicted probabilities:[1 0 2 1 1 0 1 2 1 1 2 0 0 0 0 1 2 1 1 2 0 2 0 2 2 2 2 2 0 0 0 0 1 0 0 2 1
0]
predictions:[1 0 2 1 1 0 1 2 1 1 2 0 0 0 0 1 2 1 1 2 0 2 0 2 2 2 2 2 0 0 0 0 1 0 0 2 1
0]

logreg = LogisticRegression()
named_target = iris.target_names[y_train]
logreg.fit(x_train,named_target)
print('unique classes in training data:{}'.format(logreg.classes_))
print('predictions:{}'.format(logreg.predict(x_test)[:10]))
argmax_dec_func = np.argmax(logreg.decision_function(x_test),axis=1)
print('argmax of decision function:{}'.format(argmax_dec_func[:10]))
print('argmax combined with classes_:{}'.format(logreg.classes_[argmax_dec_func][:10]))

unique classes in training data:[‘setosa’ ‘versicolor’ ‘virginica’]
predictions:[‘versicolor’ ‘setosa’ ‘virginica’ ‘versicolor’ ‘versicolor’ ‘setosa’
‘versicolor’ ‘virginica’ ‘versicolor’ ‘versicolor’]
argmax of decision function:[1 0 2 1 1 0 1 2 1 1]
argmax combined with classes_:[‘versicolor’ ‘setosa’ ‘virginica’ ‘versicolor’ ‘versicolor’ ‘setosa’
‘versicolor’ ‘virginica’ ‘versicolor’ ‘versicolor’]

发布了65 篇原创文章 · 获赞 13 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/heroybc/article/details/102964363