日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python實現LOOCV并畫ROC曲線

系統 2270 0

以sklearn中的iris數據為例
用的是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


              
                ##變為2分類
              
              
X
              
                ,
              
               y 
              
                =
              
               X
              
                [
              
              y 
              
                !=
              
              
                2
              
              
                ]
              
              
                ,
              
               y
              
                [
              
              y 
              
                !=
              
              
                2
              
              
                ]
              
              
                # 這個地方可以加上上一篇博客的隨機打亂數據操作
              
              
loo 
              
                =
              
               LeaveOneOut
              
                (
              
              
                )
              
              
loo
              
                .
              
              get_n_splits
              
                (
              
              X
              
                )
              
              
                print
              
              
                (
              
              
                "交叉驗證次數:"
              
              
                ,
              
              loo
              
                .
              
              get_n_splits
              
                (
              
              X
              
                )
              
              
                )
              
              
                # 輸出為100,--->進行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)
              
              
                # 調用、訓練模型
              
              
    model_bdt 
              
                =
              
               AdaBoostClassifier
              
                (
              
              DecisionTreeClassifier
              
                (
              
              max_depth 
              
                =
              
              
                2
              
              
                )
              
              
                ,
              
               algorithm 
              
                =
              
              
                "SAMME"
              
              
                ,
              
               n_estimators 
              
                =
              
              
                10
              
              
                )
              
              
    model_bdt
              
                .
              
              fit
              
                (
              
              X_train
              
                ,
              
               y_train
              
                )
              
              
                # 預測
              
              
    x_test_pred 
              
                =
              
               model_bdt
              
                .
              
              predict
              
                (
              
              X_test
              
                )
              
              
                #print(x_test_pred)
              
              
    y_pred
              
                .
              
              append
              
                (
              
              x_test_pred
              
                )
              
              
                # 當前預測值添加到列表中
              
              
                from
              
               sklearn
              
                .
              
              metrics 
              
                import
              
               roc_curve
              
                ,
              
               auc

y_pred 
              
                =
              
               np
              
                .
              
              array
              
                (
              
              y_pred
              
                )
              
              
                # list to array
              
              
fpr
              
                ,
              
               tpr
              
                ,
              
               threshold 
              
                =
              
               roc_curve
              
                (
              
              y
              
                ,
              
               y_pred
              
                )
              
              
                #計算真正率和假正率
              
              
roc_auc 
              
                =
              
               auc
              
                (
              
              fpr
              
                ,
              
               tpr
              
                )
              
              
                # 計算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
              
                )
              
              
                ###假正率為橫坐標,真正率為縱坐標做曲線
              
              
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
              
                (
              
              
                )
              
            
          

當然還有其他風格的代碼,實現的功能是相同的。

參考官網教程:
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


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

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

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 乐安县| 天全县| 沂源县| 平昌县| 资中县| 南康市| 调兵山市| 吕梁市| 昂仁县| 舞阳县| 临海市| 呼玛县| 台中县| 剑阁县| 杭锦后旗| 灵武市| 亳州市| 丹寨县| 丰县| 织金县| 宁晋县| 洛阳市| 陇川县| 遵化市| 九龙城区| 社旗县| 县级市| 乌拉特前旗| 宿州市| 宽城| 同心县| 井研县| 饶河县| 湟源县| 沧源| 北海市| 政和县| 舒兰市| 河池市| 平凉市| 越西县|