본문 바로가기
Informations

Logistic regression 하이퍼 파라메터 설정하기

by 지구별 이용자 2023. 6. 22.

https://www.kaggle.com/code/funxexcel/p2-logistic-regression-hyperparameter-tuning

 

P2 : Logistic Regression - hyperparameter tuning

Explore and run machine learning code with Kaggle Notebooks | Using data from Breast Cancer Wisconsin (Diagnostic) Data Set

www.kaggle.com

 

요약

1) solver, 비용 함수를 최소화하는 매개변수 가중치를 찾는 모델들

https://towardsdatascience.com/dont-sweat-the-solver-stuff-aea7cddc3451

 

Don’t Sweat the Solver Stuff

Tips for Better Logistic Regression Models in Scikit-Learn

towardsdatascience.com

newton-cg : 속도느림. 헤시안행렬 사용

lbfgs : 그래디언트 평가를 통해 2차 미분 행렬을 근사함. 메모리 절약(기본솔버)

liblinear : 대규모 분류용 라이브러리. 좌표하강 알고리즘 사용. 한번에 한 방향으로 최소값 이동.

sag :이전 그래디언트 값의 무작위 샘플을 사용하는 그래디언트 접근법의 변형. 빠름.

saga : L1정규화 가능. 일반적으로 빠름.

 

2) penalty, 패널티를 주는 방법. 모델의 종류라고 할 수 있다. L1(Lasso), L2(Ridge), 엘라스틱

라쏘는 계수크기의 절대값을 손실 함수에 패널티로 추가.

릿지는 계수의 제곱크기를 추가.

엘라스틱은 둘다. 

 

3) C

양의실수여야 함. 정규화 강도의 역수! 값이 작을수록 더 강하게 정규화

 

4) max_iter

솔버가 수렴하는 최대 반복 횟수

 

 

모델 선언

logModel = LogisticRegression()

튜닝할 하이퍼파라메터 영역

param_grid = [    
    {'penalty' : ['l1', 'l2', 'elasticnet', 'none'],
    'C' : np.logspace(-4, 4, 20),
    'solver' : ['lbfgs','newton-cg','liblinear','sag','saga'],
    'max_iter' : [100, 1000,2500, 5000]
    }

그리드 서치 import 하고 모델 학습하기

from sklearn.model_selection import GridSearchCV
clf = GridSearchCV(logModel, param_grid = param_grid, cv = 3, verbose=True, n_jobs=-1)
best_clf = clf.fit(X,y)

 

결과보기 

best_clf.best_params_

{'C': 0.23357214690901212,
 'max_iter': 100,
 'penalty': 'l2',
 'solver': 'newton-cg'}

 

'Informations' 카테고리의 다른 글

종양학 관련 미국 대학원  (0) 2023.06.04
배워야 할 것들  (0) 2023.06.04