LoginSignup
1
2

More than 5 years have passed since last update.

BalancedBaggingClassifierのestimatorsからcoefficient, interceptの値(参考)を抽出する方法

Posted at

不均衡データにおける係数の算出

単一のLogisticRegressionを実行した場合、下記attributeを指定することで各説明変数の係数と切片を求めることができる。

lr = LogisticRegression()
lr.fit(X,y)
lr.coef_, lr.intercept_

しかし、不均衡データにおいてBalancedBaggingClassifierを使った場合、係数と切片を取得するattributeが存在しないため、以下の要領で算出する必要がある。

import pandas as pd
Import numpy as np
from sklearn.linear_model import LogisticRegression
from imblearn.ensemble import BalanceBaggingClassifier

# base_estimatorでLogistic Regressionを設定
lr_bbc = BlanceBaggingClassifier(base_estimator=LogisticRegression())
lr_bbc.fit(X, [str(s) for s in y])

# Pipelineオブジェクトの表示
lr_bbc.estimatrors_

[Pipeline(memory=None,
steps=[('sampler', RandomUnderSampler(random_state=96484896, ratio='not minority',
replacement=False, return_indices=False,
sampling_strategy='not minority')), ('classifier', LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
intercept_scaling=1, max_iter=100, multi_class='warn',
n_jobs=None, penalty='l2', random_state=53827939, solver='warn',
tol=0.0001, verbose=0, warm_start=False))])…

Baggingされた各ステップから係数を抽出

for文のリスト内包表記でPipelineオブジェクトの中から”classifier”というkeyのオブジェクトを指定。
各オブジェクトに対してcoef_, intercept_を抽出し、平均をとることで参考値としての係数を算出する。

lr_coef = np.mean([lr.named_steps["classifier"].coef_[0] for lr in lr_bbc.estimators_],axis=0)
lr_intercept = np.mean([lr.named_steps["classifier"].intercept_[0] for lr in lr_bbc.estimators_],axis=0)

おまけ:GridsearchCVを実行した際の係数

GridsearchCVにおいて選択されたモデルに対する係数は下記で算出可能

params_lr = {“C”:[1,10,100]}
lr = LogisticRegression()
gr_lr = Gridsearch(lr,params,cv=5)
gr_lr.fit(X,y)
gr_lr.best_estimator_.coef_

以上

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2