Python으로 배우는 트리 기반 Machine Learning
Elie Kawerk
Data Scientist
이해하기 쉽습니다.
해석하기 쉽습니다.
사용하기 편리합니다.
유연성: 비선형 의존 관계를 표현할 수 있습니다.
전처리: 특성의 표준화 또는 정규화가 불필요합니다.
분류: 직교 결정 경계만 생성할 수 있습니다.
훈련 세트의 작은 변화에 민감합니다.
높은 분산: 제약 없는 CART는 훈련 세트에 과적합될 수 있습니다.
해결책: 앙상블 학습.
동일한 데이터셋으로 여러 모델을 학습합니다.
각 모델이 예측을 수행합니다.
메타 모델: 개별 모델의 예측을 집계합니다.
최종 예측: 더 강건하고 오류에 덜 취약합니다.
최적 결과: 각 모델이 서로 다른 방식으로 강점을 가집니다.

이진 분류 작업.
$N$개의 분류기가 예측을 수행합니다: $P_1$, $P_2$, ..., $P_N$ (단, $P_i$ = 0 또는 1).
메타 모델 예측: 하드 투표.

# Import functions to compute accuracy and split data
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
# Import models, including VotingClassifier meta-model
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier as KNN
from sklearn.ensemble import VotingClassifier
# Set seed for reproducibility
SEED = 1
# Split data into 70% train and 30% test X_train, X_test, y_train, y_test = train_test_split(X, y, test_size= 0.3, random_state= SEED) # Instantiate individual classifiers lr = LogisticRegression(random_state=SEED) knn = KNN() dt = DecisionTreeClassifier(random_state=SEED)# Define a list called classifier that contains the tuples (classifier_name, classifier) classifiers = [('Logistic Regression', lr), ('K Nearest Neighbours', knn), ('Classification Tree', dt)]
# Iterate over the defined list of tuples containing the classifiers
for clf_name, clf in classifiers:
#fit clf to the training set
clf.fit(X_train, y_train)
# Predict the labels of the test set
y_pred = clf.predict(X_test)
# Evaluate the accuracy of clf on the test set
print('{:s} : {:.3f}'.format(clf_name, accuracy_score(y_test, y_pred)))
Logistic Regression: 0.947
K Nearest Neighbours: 0.930
Classification Tree: 0.930
# Instantiate a VotingClassifier 'vc'
vc = VotingClassifier(estimators=classifiers)
# Fit 'vc' to the traing set and predict test set labels
vc.fit(X_train, y_train)
y_pred = vc.predict(X_test)
# Evaluate the test-set accuracy of 'vc'
print('Voting Classifier: {.3f}'.format(accuracy_score(y_test, y_pred)))
Voting Classifier: 0.953
Python으로 배우는 트리 기반 Machine Learning