CatBoostとMLコンテスト

データ分析と基本モデル





前書き

この記事は、タンザニアの水源問題に対処するためにDrivenDataが公開しコンテストのデータに基づいています。 





コンテストの情報は、タンザニアの水資源省がTaarifaと呼ばれるオープンソースプラットフォームを使用して入手しました。タンザニアは東アフリカ最大の国で、人口は約6000万人です。人口の半分はきれいな水を利用できず、人口の2/3は衛生状態が悪いことに苦しんでいます。貧しい家では、家族はしばしば水ポンプから水を得るために徒歩で何時間も費やさなければなりません。 





タンザニアの淡水問題に取り組むために、数十億ドルの対外援助が提供されています。しかし、タンザニア政府は今日までこの問題を解決することができていません。ウォーターポンプの大部分は完全に故障しているか、実際には機能していません。残りは大規模な修理が必要です。タンザニアの水資源省はTaarifaに同意し、彼らは自分たちの仕事をどのように達成するかについてコミュニティのヒントを得ることを期待してコンテストを開始しました。





データ

データには、ウォーターポンプに関連する多くの特性(機能)があり、水があるポイントの地理的位置、それらを構築および管理する組織、および地域、地方自治体の領域に関するいくつかのデータに関連する情報があります。支払いの種類と数に関する情報もあります。 





給水ポイントは、サービス可能非機能的サービス可能分けられますが修理が必要ですコンテストの目標は、給水ポイントの機能を予測するモデルを構築することです。

データには59,400行と40列が含まれています。ターゲットラベルは別のファイルに含まれています。

この競争に使用されるメトリックは分類率であり、予測されたクラスがテストセット内の実際のクラスと一致する行のパーセンテージを計算します。最大値は1、最小値は0です。目標は分類率を最大化することです。





データ解析

データテーブルのフィールドの説明:





  • amount_tsh-総静的水頭(給水ポイントで利用可能な水の量)





  • date_recorded — 





  • funder — 





  • gps_height — 





  • installer — 





  • longitude — GPS ()





  • latitude — GPS ()





  • wpt_name — ,





  • num_private — 





  • basin — 





  • subvillage — 





  • region — 





  • region_code —  ()





  • district_code — () 





  • lga — 





  • ward —





  • population — 





  • public_meeting — /





  • recorded_by —





  • scheme_management — 





  • scheme_name — 





  • permit — 





  • construction_year — 





  • extraction_type — 





  • extraction_type_group — 





  • extraction_type_class — 





  • management — 





  • management_group — 





  • payment — 





  • payment_type — 





  • water_quality — 





  • quality_group — 





  • quantity — 





  • quantity_group — 





  • source — 





  • source_type — 





  • source_class — 





  • waterpoint_type — 





  • waterpoint_type_group — 





    ,  —  :





    , . :





    • (under-sampling)





    • , (over-sampling)





    •  —  (SMOTE)





    • ,





    .





    , .





    .





    , scheme_name, , .





    / . permit, installer funder.





    .





    , . , (quantity_group).





    , , . . , . , , , .





    ? , quality_group.





    , , . .





    quality_group .





     —  (waterpoint_type_group).





    , other . ? , .





     —  , , , 80- . 





    . , . , 500 .





    Danida —  , , . RWSSP ( ), Dhv . , , , . , , , . , , .





    , , , . , .





     —  . .





    , . , .





     —  .





    , , , , - .





    0 . , amount_tsh (label = 0). amount_tsh. , 500 .





, .





  • installer , . . .





  • , 71 (0,95 ), «other».





  • funder.  — 98.





  • . . , . : scheme_management, quantity_group, water_quality, payment_type, extraction_type, waterpoint_type_group, region_code.





  • latitude longitude region_code.





  • subvillage scheme_name.





  • public_meeting permit .





  • subvillage, public_meeting, scheme_name, permit, , . , , .





  • scheme_management, quantity_group, water_quality, region_code, payment_type, extraction_type, waterpoint_type_group, date_recorded, recorded_by , , .





. , , CatBoost. .





, . .





def fit_model(train_pool, test_pool, **kwargs):
    model = CatBoostClassifier(
        max_ctr_complexity=5,
        task_type='CPU',
        iterations=10000,
        eval_metric='AUC',
        od_type='Iter',
        od_wait=500,
        **kwargs
    )return model.fit(
        train_pool,
        eval_set=test_pool,
        verbose=1000,
        plot=False,
        use_best_model=True)
      
      



AUC, , .





.  — 





def classification_rate(y, y_pred):
    return np.sum(y==y_pred)/len(y)
      
      



,  —  . OOF (Out-of-Fold). ; . , .





def get_oof(n_folds, x_train, y, x_test, cat_features, seeds):    ntrain = x_train.shape[0]
    ntest = x_test.shape[0]  
        
    oof_train = np.zeros((len(seeds), ntrain, 3))
    oof_test = np.zeros((ntest, 3))
    oof_test_skf = np.empty((len(seeds), n_folds, ntest, 3))    test_pool = Pool(data=x_test, cat_features=cat_features) 
    models = {}    for iseed, seed in enumerate(seeds):
        kf = StratifiedKFold(
            n_splits=n_folds,
            shuffle=True,
            random_state=seed)          
        for i, (train_index, test_index) in enumerate(kf.split(x_train, y)):
            print(f'\nSeed {seed}, Fold {i}')
            x_tr = x_train.iloc[train_index, :]
            y_tr = y[train_index]
            x_te = x_train.iloc[test_index, :]
            y_te = y[test_index]
            train_pool = Pool(data=x_tr, label=y_tr, cat_features=cat_features)
            valid_pool = Pool(data=x_te, label=y_te, cat_features=cat_features)model = fit_model(
                train_pool, valid_pool,
                loss_function='MultiClass',
                random_seed=seed
            )
            oof_train[iseed, test_index, :] = model.predict_proba(x_te)
            oof_test_skf[iseed, i, :, :] = model.predict_proba(x_test)
            models[(seed, i)] = modeloof_test[:, :] = oof_test_skf.mean(axis=1).mean(axis=0)
    oof_train = oof_train.mean(axis=0)
    return oof_train, oof_test, models
      
      



,  —  seeds.





折り目の1つの学習曲線
 

, .





, ().





:





balanced accuracy: 0.6703822994494413
classification rate: 0.8198316498316498
      
      



.





, -5 0,005 , , .





,  —  . , , .





balanced accuracy: 0.6549535670689709
classification rate: 0.8108249158249158
      
      



.





:





  • ;





  • ;





  • CatBoost, ;





  • OOF-;





  • .





データを準備し、モデルを作成するための適切なツールを選択するための適切なアプローチは、追加の機能生成がなくても優れた結果をもたらすことができます。





宿題として、新しい機能を追加し、最適なモデルパラメータを選択し、他のライブラリを使用して勾配を高め、結果のモデルからアンサンブルを構築することをお勧めします。





記事のコードはここで見ることができます








All Articles