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

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

系統(tǒng) 1757 0

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

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

瞎BB

1.實(shí)現(xiàn)根據(jù)樣本數(shù)據(jù)(用眼距離distance、最長持續(xù)用眼時(shí)長duration、總用眼時(shí)長total_time、戶外運(yùn)動(dòng)時(shí)長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)練集測試集

            
              
                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號聯(lián)系: 360901061

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

【本文對您有幫助就好】

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

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 博野县| 新田县| 延寿县| 崇义县| 荆门市| 大埔县| 南乐县| 砀山县| 博客| 田阳县| 二连浩特市| 凭祥市| 健康| 万安县| 镇远县| 辉南县| 长寿区| 扎囊县| 山西省| 庆元县| 辰溪县| 阳春市| 东方市| 临湘市| 灌阳县| 内江市| 甘孜县| 长顺县| 璧山县| 茶陵县| 微山县| 高阳县| 景泰县| 沙雅县| 镇安县| 桓台县| 成安县| 蕲春县| 乌恰县| 垦利县| 新营市|