黄色网页视频 I 影音先锋日日狠狠久久 I 秋霞午夜毛片 I 秋霞一二三区 I 国产成人片无码视频 I 国产 精品 自在自线 I av免费观看网站 I 日本精品久久久久中文字幕5 I 91看视频 I 看全色黄大色黄女片18 I 精品不卡一区 I 亚洲最新精品 I 欧美 激情 在线 I 人妻少妇精品久久 I 国产99视频精品免费专区 I 欧美影院 I 欧美精品在欧美一区二区少妇 I av大片网站 I 国产精品黄色片 I 888久久 I 狠狠干最新 I 看看黄色一级片 I 黄色精品久久 I 三级av在线 I 69色综合 I 国产日韩欧美91 I 亚洲精品偷拍 I 激情小说亚洲图片 I 久久国产视频精品 I 国产综合精品一区二区三区 I 色婷婷国产 I 最新成人av在线 I 国产私拍精品 I 日韩成人影音 I 日日夜夜天天综合

隨機(jī)森林算法python實(shí)現(xiàn)

系統(tǒng) 2187 0

隨機(jī)森林算法python實(shí)現(xiàn)

  • 瞎BB
  • 代碼
    • 導(dǎo)入數(shù)據(jù)
    • 切分訓(xùn)練集測(cè)試集
    • 找到最有用的幾個(gè)屬性
    • 根據(jù)上面的代碼更改屬性
    • 參數(shù)組合遍歷找最優(yōu)
    • 隨機(jī)森林
  • 樣本數(shù)據(jù)

瞎BB

1.實(shí)現(xiàn)根據(jù)樣本數(shù)據(jù)(用眼距離distance、最長(zhǎng)持續(xù)用眼時(shí)長(zhǎng)duration、總用眼時(shí)長(zhǎng)total_time、戶(hù)外運(yùn)動(dòng)時(shí)長(zhǎng)outdoor、用眼角度angle、健康環(huán)境光照用眼比例proportion)判別是否需要近視預(yù)警
2.樣本實(shí)在太少,結(jié)果還行,原理都是一樣的

代碼

導(dǎo)入數(shù)據(jù)

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

切分訓(xùn)練集測(cè)試集

            
              
                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
              
              
                )
              
            
          

找到最有用的幾個(gè)屬性

            
              
                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
              
                (
              
              
                )
              
            
          

根據(jù)上面的代碼更改屬性

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

參數(shù)組合遍歷找最優(yōu)

            
              
                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
              
                )
              
              
                #(算法,調(diào)節(jié)參數(shù)(用字典形式),交叉驗(yàn)證次數(shù))
              
              
grid
              
                .
              
              fit
              
                (
              
              data_train
              
                ,
              
               target_train
              
                )
              
              
                #訓(xùn)練集
              
              
grid
              
                .
              
              cv_results_ 
              
                ,
              
               grid
              
                .
              
              best_params_
              
                ,
              
               grid
              
                .
              
              best_score_
              
                #得分,最優(yōu)參數(shù),最優(yōu)得分
              
            
          

隨機(jī)森林

            
              
                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
              
              
                )
              
              
                #交叉驗(yàn)證
              
              
kf 
              
                =
              
               model_selection
              
                .
              
              KFold
              
                (
              
              n_splits
              
                =
              
              
                3
              
              
                )
              
              
scores 
              
                =
              
               model_selection
              
                .
              
              cross_val_score
              
                (
              
              rf
              
                ,
              
               data_train
              
                ,
              
               target_train
              
                ,
              
               cv
              
                =
              
              kf
              
                )
              
              
                print
              
              
                (
              
              scores
              
                .
              
              mean
              
                (
              
              
                )
              
              
                )
              
            
          

樣本數(shù)據(jù)

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

更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

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

【本文對(duì)您有幫助就好】

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

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論