모두 종합해 보기
이전 연습 문제에서 만든 거리 기반 학습 이상치 탐지 파이프라인에 더해, 이번에는 one-class SVM을 사용한 특성 기반 학습도 지원하려고 합니다. 두 가지 특성을 추출하겠습니다. 첫째, 문자열 길이, 둘째, 문자열의 첫 글자를 Chapter 1에서 소개한 LabelEncoder() 함수로 숫자 인코딩한 값입니다. 공정한 비교를 위해 이상치 점수를 AUC로 평가할 예정이에요. 다음이 이미 import되어 있습니다: LabelEncoder(), roc_auc_score()는 auc()로, 그리고 OneClassSVM. 데이터는 proteins라는 pandas 데이터 프레임으로 제공되며 label과 seq 두 열과 IMMUNE SYSTEM, VIRUS 두 클래스가 있습니다. 학습된 LoF 감지기는 lof_detector로 제공됩니다.
이 연습은 강의의 일부입니다
Python으로 설계하는 Machine Learning 워크플로
연습 안내
- 문자열
s에 대해len(s)는 길이를 반환합니다. 이를seq열에 적용해 새 열len을 만드세요. - 문자열
s에 대해list(s)는 문자 목록을 반환합니다. 이를 사용해 각 시퀀스의 첫 글자를 추출하고,LabelEncoder()로 인코딩하세요. - LoF 점수는
negative_outlier_factor_속성에 있습니다. 해당 점수의 AUC를 계산하세요. len과first두 열만 가진 데이터 프레임에 1-class SVM을 학습하세요. 점수를 추출하고, LoF 점수와 SVM 점수를 모두 AUC로 평가하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Create a feature that contains the length of the string
proteins['len'] = proteins['seq'].apply(____)
# Create a feature encoding the first letter of the string
proteins['first'] = ____.____(
proteins['seq'].apply(____))
# Extract scores from the fitted LoF object, compute its AUC
scores_lof = lof_detector.____
print(____(proteins['label']==____, scores_lof))
# Fit a 1-class SVM, extract its scores, and compute its AUC
svm = ____.____(proteins[['len', 'first']])
scores_svm = svm.____(proteins[['len', 'first']])
print(____(proteins['label']==____, scores_svm))