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

隨機森林算法python實現

系統 1895 0

隨機森林算法python實現

  • 瞎BB
  • 代碼
    • 導入數據
    • 切分訓練集測試集
    • 找到最有用的幾個屬性
    • 根據上面的代碼更改屬性
    • 參數組合遍歷找最優
    • 隨機森林
  • 樣本數據

瞎BB

1.實現根據樣本數據(用眼距離distance、最長持續用眼時長duration、總用眼時長total_time、戶外運動時長outdoor、用眼角度angle、健康環境光照用眼比例proportion)判別是否需要近視預警
2.樣本實在太少,結果還行,原理都是一樣的

代碼

導入數據

            
              
                import
              
               pandas
patients 
              
                =
              
               pandas
              
                .
              
              read_csv
              
                (
              
              
                "data.csv"
              
              
                )
              
              
patients
              
                .
              
              head
              
                (
              
              
                5
              
              
                )
              
            
          

切分訓練集測試集

            
              
                from
              
               sklearn
              
                .
              
              model_selection 
              
                import
              
               train_test_split
patients_data
              
                =
              
              patients
              
                .
              
              loc
              
                [
              
              
                :
              
              
                ,
              
              
                'distance'
              
              
                :
              
              
                'proportion'
              
              
                ]
              
              
patients_target
              
                =
              
              patients
              
                .
              
              loc
              
                [
              
              
                :
              
              
                ,
              
              
                'warning'
              
              
                ]
              
              
data_train
              
                ,
              
              data_test
              
                ,
              
              target_train
              
                ,
              
              target_test
              
                =
              
              train_test_split
              
                (
              
              patients_data
              
                ,
              
              patients_target
              
                ,
              
              test_size
              
                =
              
              
                0.1
              
              
                ,
              
              random_state
              
                =
              
              
                42
              
              
                )
              
            
          

找到最有用的幾個屬性

            
              
                import
              
               numpy 
              
                as
              
               np

              
                from
              
               sklearn
              
                .
              
              feature_selection 
              
                import
              
               SelectKBest
              
                ,
              
               f_classif

              
                import
              
               matplotlib
              
                .
              
              pyplot 
              
                as
              
               plt
predictors 
              
                =
              
              
                [
              
              
                "distance"
              
              
                ,
              
              
                "duration"
              
              
                ,
              
              
                "total_time"
              
              
                ,
              
              
                "outdoor"
              
              
                ,
              
              
                "angle"
              
              
                ,
              
              
                "proportion"
              
              
                ]
              
              

selector 
              
                =
              
               SelectKBest
              
                (
              
              f_classif
              
                ,
              
               k
              
                =
              
              
                5
              
              
                )
              
              
selector
              
                .
              
              fit
              
                (
              
              data_train
              
                ,
              
               target_train
              
                )
              
              

scores 
              
                =
              
              
                -
              
              np
              
                .
              
              log10
              
                (
              
              selector
              
                .
              
              pvalues_
              
                )
              
              

plt
              
                .
              
              bar
              
                (
              
              
                range
              
              
                (
              
              
                len
              
              
                (
              
              predictors
              
                )
              
              
                )
              
              
                ,
              
               scores
              
                )
              
              
plt
              
                .
              
              xticks
              
                (
              
              
                range
              
              
                (
              
              
                len
              
              
                (
              
              predictors
              
                )
              
              
                )
              
              
                ,
              
               predictors
              
                ,
              
               rotation
              
                =
              
              
                'vertical'
              
              
                )
              
              
plt
              
                .
              
              show
              
                (
              
              
                )
              
            
          

根據上面的代碼更改屬性

            
              predictors_best 
              
                =
              
              
                [
              
              
                "distance"
              
              
                ,
              
              
                "total_time"
              
              
                ,
              
              
                "angle"
              
              
                ,
              
              
                "proportion"
              
              
                ]
              
              
data_train 
              
                =
              
               data_train
              
                [
              
              predictors_best
              
                ]
              
              
data_test 
              
                =
              
               data_test
              
                [
              
              predictors_best
              
                ]
              
            
          

參數組合遍歷找最優

            
              
                from
              
               sklearn
              
                .
              
              model_selection 
              
                import
              
               GridSearchCV
tree_param_grid 
              
                =
              
              
                {
              
              
                'min_samples_split'
              
              
                :
              
              
                list
              
              
                (
              
              
                (
              
              
                2
              
              
                ,
              
              
                3
              
              
                ,
              
              
                4
              
              
                )
              
              
                )
              
              
                ,
              
              
                'n_estimators'
              
              
                :
              
              
                list
              
              
                (
              
              
                (
              
              
                3
              
              
                ,
              
              
                5
              
              
                ,
              
              
                10
              
              
                ,
              
              
                15
              
              
                ,
              
              
                20
              
              
                ,
              
              
                25
              
              
                ,
              
              
                30
              
              
                ,
              
              
                35
              
              
                ,
              
              
                40
              
              
                ,
              
              
                45
              
              
                ,
              
              
                50
              
              
                )
              
              
                )
              
              
                }
              
              
grid 
              
                =
              
               GridSearchCV
              
                (
              
              RandomForestClassifier
              
                (
              
              
                )
              
              
                ,
              
              param_grid
              
                =
              
              tree_param_grid
              
                ,
              
               cv
              
                =
              
              kf
              
                )
              
              
                #(算法,調節參數(用字典形式),交叉驗證次數)
              
              
grid
              
                .
              
              fit
              
                (
              
              data_train
              
                ,
              
               target_train
              
                )
              
              
                #訓練集
              
              
grid
              
                .
              
              cv_results_ 
              
                ,
              
               grid
              
                .
              
              best_params_
              
                ,
              
               grid
              
                .
              
              best_score_
              
                #得分,最優參數,最優得分
              
            
          

隨機森林

            
              
                from
              
               sklearn 
              
                import
              
               model_selection

              
                from
              
               sklearn
              
                .
              
              ensemble 
              
                import
              
               RandomForestClassifier
rf 
              
                =
              
               RandomForestClassifier
              
                (
              
              random_state
              
                =
              
              
                1
              
              
                ,
              
               n_estimators
              
                =
              
              
                35
              
              
                ,
              
               min_samples_split
              
                =
              
              
                2
              
              
                ,
              
               min_samples_leaf
              
                =
              
              
                2
              
              
                )
              
              
                #交叉驗證
              
              
kf 
              
                =
              
               model_selection
              
                .
              
              KFold
              
                (
              
              n_splits
              
                =
              
              
                3
              
              
                )
              
              
scores 
              
                =
              
               model_selection
              
                .
              
              cross_val_score
              
                (
              
              rf
              
                ,
              
               data_train
              
                ,
              
               target_train
              
                ,
              
               cv
              
                =
              
              kf
              
                )
              
              
                print
              
              
                (
              
              scores
              
                .
              
              mean
              
                (
              
              
                )
              
              
                )
              
            
          

樣本數據

sample distance duration total_time outdoor angle proportion warning(1 yes 0 no)
1 20 72 344 148 11 81 1
2 34 68 263 135 7 50 1
3 25 98 357 32 12 64 1
4 37 65 291 157 8 89 0
5 34 151 162 169 18 63 1
6 30 178 259 146 32 50 1
7 20 35 134 37 23 68 0
8 39 111 169 87 4 52 0
9 22 44 265 136 14 76 1
10 39 151 219 140 2 55 0
11 21 179 184 64 18 60 1
12 25 41 241 71 16 72 1
13 18 171 286 131 35 89 1
14 32 33 236 102 29 50 1
15 20 133 226 124 17 81 1
16 17 148 236 66 32 75 1
17 34 111 214 57 5 88 0
18 24 85 163 155 14 32 1
19 32 165 276 146 33 52 1
20 25 124 359 171 33 70 0
21 31 51 167 47 25 47 0
22 31 63 352 58 22 44 1
23 16 58 164 45 13 73 0
24 29 37 326 104 33 68 1
25 34 47 197 59 5 66 0
26 36 123 185 165 26 70 0
27 25 126 171 45 23 33 1
28 31 84 98 37 30 51 1
29 30 92 153 114 14 48 0
30 29 178 278 146 27 45 1

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

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

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

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 满城县| 安化县| 民勤县| 江源县| 昌平区| 克山县| 临猗县| 邻水| 九寨沟县| 孟津县| 扶风县| 准格尔旗| 嘉荫县| 前郭尔| 大田县| 明水县| 双流县| 蒙自县| 虎林市| 蒲江县| 靖宇县| 乐平市| 三原县| 喀什市| 泸溪县| 石棉县| 门源| 平武县| 湘潭县| 铜鼓县| 大化| 湾仔区| 旬邑县| 霸州市| 新乡县| 承德市| 永平县| 浙江省| 阳原县| 二手房| 阜城县|