본문 바로가기

여러가지/Python

섹션 10. 의사 결정 트리 회귀 (Decision Tree Intuition)

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

 

● URL : https://colab.research.google.com/drive/1-Gm8bfp7_8h4rs39JUa2GoaryumOuPRm#scrollTo=pLVaXoYVU_Uy

 

CART

: 범주화 회귀 나무, 즉, 범주화 나무(Classification Trees), 회귀 나무(Regression Trees) 모두 포괄

- 범주화 나무(Classification Trees): 범주 예측

- 회귀 나무(Regression Trees) : 연속적 수치값 예측

(+) 정보 엔트로피

 

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 Decision Tree Regression model on the whole dataset

from sklearn.tree import DecisionTreeRegressor
regressor = DecisionTreeRegressor(random_state = 0)
regressor.fit(X, y)

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

(1) random_state = 0 : 무작위성에 관여하는 모든 함수들이 항상 같은 순서로 같은 값을 생성하게 되어 일관된 결과 보장

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

Step 4. Predicting a new result

regressor.predict([[6.5]])

 

Step 5. Visualising the Decision Tree 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 (Decision Tree Regression)')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()

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