본문 바로가기

여러가지/Python

섹션 11. 랜덤 포레스트 회귀 (Random Forest Intuition)

※ 여러 요소의 데이터 집합(고차원 데이터 집합)에 적합하다.

 

● URL : https://colab.research.google.com/drive/1pcUf3XXy4dipc39HRCiwoAZpppPT9JzW

 

앙상블 학습

: 여러 알고리즘이나 같은 알고리즘을 여러개 가져와 조합하여 기존보다 높은 성능을 가진 것으로 만드는 행위

 

Step 1. Importing the libraries

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

 

Step 2. Importing the dataset

dataset = pd.read_csv('Position_Salaries.csv')
X = dataset.iloc[:, 1:-1].values
y = dataset.iloc[:, -1].values

 

Step 3. Training the Random Forest Regression model on the whole dataset

- n_estimators : 트리의 수

from sklearn.ensemble import RandomForestRegressor
regressor = RandomForestRegressor(n_estimators = 10, random_state = 0)
regressor.fit(X, y)

 

Step 4. Predicting a new result

regressor.predict([[6.5]])

 

Step 5. Visualising the Random Forest Regression results (higher resolution)

X_grid = np.arange(min(X), max(X), 0.01)
X_grid = X_grid.reshape((len(X_grid), 1))
plt.scatter(X, y, color = 'red')
plt.plot(X_grid, regressor.predict(X_grid), color = 'blue')
plt.title('Truth or Bluff (Random Forest Regression)')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()

(급여 수준 사이에 계단 두개)

의사 결정 트리 회귀보다 트리가 더 많기 때문에 특성이 더 많이 분할되기 때문이다.