1-18 Random Forest 계산 사례

codingart(66)
Published in
#kr
Words
471
Reading
3 min
Listen
Play
7y

랜덤 포레스트(Random Forrests) 머신러닝 기법은 Classification 성능이라든지 또는 확장성(Scalability)과 쉬운 사용법으로 인해 지난 10년간에 걸쳐 각광을 받았다. 랜덤포레스트 기법은 직관적으로 Decision Tree들로 이루어진 앙상블이라고 볼 수 있다. 즉 랜덤포레스트는 높은 변동성을 가지는 여러 Decision Tree들의 결과를 대상으로 평균을 냄으로 인해 보다 로버스틱하면서 Overfitting에 덜 민감한 머신 러닝 모델을 구축할 수 있게 된다.
랜덤포레스트 알고리듬은 다음과 같이 4단계로 구성이 될 수 있다.

  1. 사이즈가 n 인 랜덤한 시작용 샘플을 뽑는다.
  2. 시작용 샘플들로부터 Decision Tree를 키워나간다.
    • 각 Tree 마디(node)에서 시작용 샘플들의 replacement 없이
      랜덤하게 d 개의 특징들을 선택한다.
    • 이 특징을 사용하면서 IG(Information Gain)를 최대화 할 수 있도록 Tree를 마디(node)에서 분리시킨다.
      gini impurity 나 entropy 둘 중에 하나를 택하면 된다
  3. 1,2 단계를 k번(estimator의 수) 반복하여 앙상블을 구성해야 한다.
  4. 다수결 원칙에 의해 class 라벨을 할당한 각 Tree의 예측 값을 합하도록 한다.

noname01.png

DecisionTree Classifier 결과와 RandomForestClassifier 결과를 비교해 보자.

noname02.png

경계에서 Classification 결과는 Decision Tree 가 보다 민감해 보인다.
#Random_forest_01

from sklearn import version as sklearn_version
from sklearn import datasets
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from matplotlib.colors import ListedColormap
import matplotlib.pyplot as plt
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier

#Loading the Iris dataset from scikit-learn.

iris = datasets.load_iris()
X = iris.data[:, [2, 3]]
y = iris.target

print('Class labels:', np.unique(y))

#Splitting data into 70% training and 30% test data:

X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=1, stratify=y)

print('Labels counts in y:', np.bincount(y))
print('Labels counts in y_train:', np.bincount(y_train))
print('Labels counts in y_test:', np.bincount(y_test))

def plot_decision_regions(X, y, classifier, test_idx=None, resolution=0.02):

# setup marker generator and color map
markers = ('s', 'x', 'o', '^', 'v')
colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan')
cmap = ListedColormap(colors[:len(np.unique(y))])

# plot the decision surface
x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1
x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution),
                       np.arange(x2_min, x2_max, resolution))
Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T)
Z = Z.reshape(xx1.shape)
plt.contourf(xx1, xx2, Z, alpha=0.3, cmap=cmap)
plt.xlim(xx1.min(), xx1.max())
plt.ylim(xx2.min(), xx2.max())

for idx, cl in enumerate(np.unique(y)):
    plt.scatter(x=X[y == cl, 0], 
                y=X[y == cl, 1],
                alpha=0.8, 
                c=colors[idx],
                marker=markers[idx], 
                label=cl, 
                edgecolor='black')

#highlight test samples
if test_idx:
    #plot all samples
    X_test, y_test = X[test_idx, :], y[test_idx]

    plt.scatter(X_test[:, 0],
                X_test[:, 1],
                c='',
                edgecolor='black',
                alpha=1.0,
                linewidth=1,
                marker='o',
                s=100, 
                label='test set')

#Decision tree learning

tree = DecisionTreeClassifier(criterion='gini',
max_depth=4,
random_state=1)
tree.fit(X_train, y_train)
y_pred = tree.predict(X_test)
print('\nDecision Tree')
print('Misclassified samples: %d' % (y_test != y_pred).sum())
print('Accuracy: %.2f' % accuracy_score(y_test, y_pred))
print('Accuracy: %.2f' % tree.score(X_test, y_test))

X_combined = np.vstack((X_train, X_test))
y_combined = np.hstack((y_train, y_test))
plot_decision_regions(X_combined, y_combined,
classifier=tree, test_idx=range(105, 150))

plt.xlabel('petal length [cm]')
plt.ylabel('petal width [cm]')
plt.legend(loc='upper left')
plt.tight_layout()
plt.show()

#Combining weak to strong learners via random forests

forest = RandomForestClassifier(criterion='entropy',n_estimators=25,
max_depth=None,random_state=1, n_jobs=2)
forest.fit(X_train, y_train)
y_pred = tree.predict(X_test)
print('\nRandom Forests')
print('Misclassified samples: %d' % (y_test != y_pred).sum())
print('Accuracy: %.2f' % accuracy_score(y_test, y_pred))
print('Accuracy: %.2f' % tree.score(X_test, y_test))

plot_decision_regions(X_combined, y_combined,
classifier=forest, test_idx=range(105, 150))

plt.xlabel('petal length [cm]')
plt.ylabel('petal width [cm]')
plt.legend(loc='upper left')
plt.tight_layout()
plt.show()