以sklearn中的iris數(shù)據(jù)為例
用的是Adaboost算法
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 4 21:17:19 2019
@author: ZQQ
"""
import
numpy
as
np
from
sklearn
.
ensemble
import
AdaBoostClassifier
from
sklearn
.
tree
import
DecisionTreeClassifier
from
sklearn
.
model_selection
import
LeaveOneOut
from
sklearn
import
datasets
iris
=
datasets
.
load_iris
(
)
X
=
iris
.
data
y
=
iris
.
target
##變?yōu)?分類
X
,
y
=
X
[
y
!=
2
]
,
y
[
y
!=
2
]
# 這個(gè)地方可以加上上一篇博客的隨機(jī)打亂數(shù)據(jù)操作
loo
=
LeaveOneOut
(
)
loo
.
get_n_splits
(
X
)
print
(
"交叉驗(yàn)證次數(shù):"
,
loo
.
get_n_splits
(
X
)
)
# 輸出為100,--->進(jìn)行100折,也就是留一
y_pred
=
[
]
for
train_index
,
test_index
in
loo
.
split
(
X
)
:
#print("train:", train_index, "TEST:", test_index) # 索引
X_train
,
X_test
=
X
[
train_index
]
,
X
[
test_index
]
y_train
,
y_test
=
y
[
train_index
]
,
y
[
test_index
]
#print(X_train, X_test, y_train, y_test)
# 調(diào)用、訓(xùn)練模型
model_bdt
=
AdaBoostClassifier
(
DecisionTreeClassifier
(
max_depth
=
2
)
,
algorithm
=
"SAMME"
,
n_estimators
=
10
)
model_bdt
.
fit
(
X_train
,
y_train
)
# 預(yù)測(cè)
x_test_pred
=
model_bdt
.
predict
(
X_test
)
#print(x_test_pred)
y_pred
.
append
(
x_test_pred
)
# 當(dāng)前預(yù)測(cè)值添加到列表中
from
sklearn
.
metrics
import
roc_curve
,
auc
y_pred
=
np
.
array
(
y_pred
)
# list to array
fpr
,
tpr
,
threshold
=
roc_curve
(
y
,
y_pred
)
#計(jì)算真正率和假正率
roc_auc
=
auc
(
fpr
,
tpr
)
# 計(jì)算auc的值
import
matplotlib
.
pyplot
as
plt
lw
=
2
# 定義線條寬度
plt
.
figure
(
figsize
=
(
8
,
5
)
)
plt
.
plot
(
fpr
,
tpr
,
color
=
'darkorange'
,
lw
=
lw
,
label
=
'ROC curve (area = %0.2f)'
%
roc_auc
)
###假正率為橫坐標(biāo),真正率為縱坐標(biāo)做曲線
plt
.
plot
(
[
0
,
1
]
,
[
0
,
1
]
,
color
=
'navy'
,
lw
=
lw
,
linestyle
=
'--'
)
plt
.
xlim
(
[
0.0
,
1.0
]
)
plt
.
ylim
(
[
0.0
,
1.05
]
)
plt
.
xlabel
(
'False Positive Rate'
)
plt
.
ylabel
(
'True Positive Rate'
)
plt
.
title
(
'Receiver operating characteristic example'
)
plt
.
legend
(
loc
=
"lower right"
)
plt
.
savefig
(
'loocv.png'
,
dpi
=
600
)
# 以600大批保存圖片
plt
.
show
(
)
當(dāng)然還有其他風(fēng)格的代碼,實(shí)現(xiàn)的功能是相同的。
參考官網(wǎng)教程:
https://scikit-learn.org/stable/auto_examples/model_selection/plot_roc.html#sphx-glr-auto-examples-model-selection-plot-roc-py
https://scikit-learn.org/stable/modules/generated/sklearn.metrics.roc_auc_score.html#sklearn.metrics.roc_auc_score
https://my.oschina.net/u/3702502/blog/1841599
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
