欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

python實(shí)現(xiàn)LOOCV并畫ROC曲線

系統(tǒng) 2188 0

以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ì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦?。?!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 高清人人天天夜夜曰狠狠狠狠 | 精品一区二区三区的国产在线观看 | 日本欧美国产精品第一页久久 | 香蕉久久久久久 | 美日韩一区二区 | 五月婷婷在线观看视频 | 一区二区三区四区免费 | 欧美成年 | 国产乱色精品成人免费视频 | 精品伊人久久久 | 国产青青| 国产精品久久久久久久久久久久 | 久在线视频 | 日韩高清一区二区 | 波多野结衣全部系列在线观看 | 成人av一区二区三区 | 国产不卡视频在线播放 | 91精品视频在线播放 | 2018天天干夜夜操 | 三上悠亚2022最新新作番号 | 多女多p多杂交视频在线观看 | 日产一一到六区麻豆 | 国产视频网 | 污的网站在线观看 | 一区二区国产在线观看 | 久久精品伊人 | 色哦色哦哦色天天综合 | 奇米影视8888狠狠狠狠 | 午夜刺激视频 | dydog net| 久久精品欧美一区二区三区不卡 | 亚洲一区在线观看视频 | 欧美国产日韩一区二区三区 | 亚洲精品久久AV无码蜜桃 | 久久久久久精 | 日韩精品真人荷官无码 | 精品久久久久国产免费 | 国产精品99久久 | 亚洲 欧美 中文字幕 | 宅男lu666噜噜噜在线观看 | 亚洲综合一二三区 |