지도학습 파이프라인

Python으로 설계하는 Machine Learning 워크플로

Dr. Chris Anagnostopoulos

Honorary Associate Professor

레이블된 데이터

  • 특징 변수(축약: X)
  • 레이블 또는 클래스(축약: y)
credit_scoring.head(4)
  checking_status  duration  ...  foreign_worker class
0            '<0'         6  ...             yes  good
1      '0<=X<200'        48  ...             yes   bad
2   'no checking'        12  ...             yes  good
3            '<0'        42  ...             yes  good
Python으로 설계하는 Machine Learning 워크플로

특징 공학

  • 대부분의 분류기는 수치형 특징을 기대함
  • 문자열 열을 숫자로 변환 필요

sklearn.preprocessingLabelEncoder로 전처리:

le = LabelEncoder()
le.fit_transform(credit_scoring['checking_status'])[:4]
array([1, 0, 3, 1])
Python으로 설계하는 Machine Learning 워크플로

모델 학습

  • .fit(features, labels)
  • .predict(features)
features, labels = credit_scoring.drop('class', 1), credit_scoring['class']

model_nb = GaussianNB() model_nb.fit(features, labels) model_nb.predict(features.head(5))
['good' 'bad' 'good' 'bad' 'good']

처음 5개 예시에 대해 정확도 60%.

Python으로 설계하는 Machine Learning 워크플로

모델 선택

  • .fit()은 주어진 모델의 파라미터를 최적화합니다
  • 다른 모델은 어떨까요?

AdaBoostClassifier가 처음 5개 데이터에서 GaussianNB보다 우수:

model_ab = AdaBoostClassifier()
model_ab.fit(features, labels)
model_ab.predict(features.head(5))
numpy.array(labels[0:5])
['good' 'bad' 'good' 'good' 'bad']
['good' 'bad' 'good' 'good' 'bad']
Python으로 설계하는 Machine Learning 워크플로

성능 평가

표본이 클수록 정확도 추정이 더 안정적입니다:

from sklearn.metrics import accuracy_score
accuracy_score(labels, model_nb.predict(features)) # naive bayes
0.706
accuracy_score(labels, model_ab.predict(features)) # adaboost
0.802

이 계산의 문제는 무엇일까요?

Python으로 설계하는 Machine Learning 워크플로

과적합과 데이터 분할

과적합(Overfitting): 모델은 학습한 데이터에서 미지의 데이터보다 항상 더 잘 동작합니다.

X_train, y_train으로 학습하고 X_test, y_test로 정확도 평가:

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

GaussianNB().fit(X_train, y_train).predict(X_test)
Python으로 설계하는 Machine Learning 워크플로

표준 지도학습 워크플로: 특징 공학, 학습/테스트 분할, 모델 평가, 모델 선택. 단, 현실 문제에서는 이 표준 파이프라인만으로는 부족할 때가 있습니다.

Python으로 설계하는 Machine Learning 워크플로

이 강의에서 다룰 내용

  1. 파이프라인을 확장 가능하게 튜닝하는 법
  2. 도메인 전문가를 참여시켜 예측의 관련성 보장
  3. 시간이 지나도 성능을 유지하는 방법
  4. 레이블이 부족할 때 모델 학습하기
Python으로 설계하는 Machine Learning 워크플로

주택담보대출 위기를 막을 수 있었을까요?

Python으로 설계하는 Machine Learning 워크플로

Preparing Video For Download...