lv 2_ 결측치 보간법과 랜덤포레스트로 따릉이 데이터 예측하기

lv 2_ 결측치 보간법과 랜덤포레스트로 따릉이 데이터 예측하기

1. 전처리

1) 결측치 대체 평균

: 원하는 피쳐의 결측치를 해당 피쳐의 평균값으로 대체하는 코드

: df.fillna({'칼럼명':int(df['칼럼명'].mean)}, inplace=True)

Ex ) fillna() 를 이용해 결측치를 각 피쳐의 평균값으로 대체하는 코드

- 결측치가 있는 피쳐 살펴보기

→ print(train.isnull().sum())

- fillna()를 이용해 결측치를 각 피쳐의 평균값으로 대체하기

→ train.fillna({'hour_bef_temperature':int(train['hour_bef_temperature'].mean())}, inplace=True)

train.fillna({'hour_bef_precipitation':int(train['hour_bef_precipitation'].mean())}, inplace=True)

train.fillna({'hour_bef_windspeed':int(train['hour_bef_windspeed'].mean())}, inplace=True)

train.fillna({'hour_bef_humidity':int(train['hour_bef_humidity'].mean())}, inplace=True)

train.fillna({'hour_bef_visibility':int(train['hour_bef_visibility'].mean())}, inplace=True)

train.fillna({'hour_bef_ozone':int(train['hour_bef_ozone'].mean())}, inplace=True)

train.fillna({'hour_bef_pm10':int(train['hour_bef_pm10'].mean())}, inplace=True)

train.fillna({'hour_bef_pm2.5':int(train['hour_bef_pm2.5'].mean())}, inplace=True)

2) 결측치 대체 보간법

: 피쳐의 정보성을 강조하기 위해 보간법을 사용해 결측치를 채우기

: Python pandas 의 interpolate() method 를 사용해 구현

: df.interpolate(inplace=True)

Ex) 결측치가 있는 피쳐를 살펴본 후, 각 평균치로 대체하기

- 결측치가 있는 피쳐 살펴보기

→ print(train.isnull().sum())

- 결측치 보간법으로 대체하기

→ train.interpolate(inplace=True)

2. 모델링

1) 랜덤 포레스트 개념과 선언

- 랜덤 포레스트란?

: 여러 개의 의사결정나무를 만들어서 이들의 평균으로 예측의 성능을 높이는 방법

- 주어진 하나의 데이터로부터 여러 개의 랜덤 데이터셋을 추출해, 각 데이터셋을 통해 모델을 여러개 만들 수 있다

- 랜덤포레스트회귀 모델 선언 방법

: from sklearn.ensemble import RandomForestRegressor

model = RandomForestRegressor()

Ex) sklearn.ensemble 로부터 RandomForestRegressor 를 불러와 모델을 선언하는 코드

→ from sklearn.ensemble import RandomForestRegressor

model = RandomForestRegressor()

2) 랜덤포레스트를 평가척도에 맞게 학습

: 랜덤포레스트 모듈의 옵션 중 criterion 옵션을 통해 어떤 평가척도를 기준으로 훈련할 것인지 정할 수 있다.

: model = RandomForestRegressor(criterion = 'mse')

Ex) # count 피쳐를 제외한 X_train df 를 생성하는 코드를 아래에 작성

→ X_train = train.drop(['count'], axis=1)

Y_train = train['count']

# count 피쳐만을 가진 Y_train df 를 생성하는 코드를 아래에 작성

→ model = RandomForestRegressor(criterion = 'mse')

model.fit(X_train, Y_train)

3. 튜닝

1) 랜덤포레스트 변수중요도 확인

: fit() 으로 모델이 학습되고 나면 feature_importances_ 속성(attribute) 으로 변수의 중요도를 파악할 수 있다.

여기서 변수의 중요도란 예측변수를 결정할 때 각 피쳐가 얼마나 중요한 역할을 하는지에 대한 척도이다.

따라서 변수의 중요도가 낮다면 해당 피쳐를 제거함으로써 모델의 성능을 높일 수 있다.

: model.feature_importances_

Ex) 랜덤포레스트모델 예측변수의 중요도를 출력하는 코드

→ model.feature_importances_

array([0.02570311, 0.5910564 , 0.18363637, 0.01817667, 0.02735183, 0.03565568, 0.03198511,

0.03468067, 0.03121435, 0.02053981])

2) 변수 제거

: 변수중요도가 낮은 피쳐를 파악하고 나면 차례대로 하나씩 피쳐를 제거하면서 모델을 새로 훈련할 수 있다.

: id 피쳐는 예측에 의미가 없는 피쳐이다. id 와 count 를 drop 한 X_train_1 훈련 df 을 새로 생성한다.

예측을 할 때 test 는 훈련 셋과 동일한 피쳐를 가져야 하므로 동일하게 피쳐를 drop 한 test_1 df 를 생성해준다.

< 각 모델로 예측한 예측값들을 submission 에 저장한 후, 리더보드에 제출해 점수를 비교하기 >

# X_train 에서 drop 할 피쳐의 경우의 수 대로 3개의 X_train 을 생성하세요.

X_train_1 = train.drop(['drop 할 피쳐'], axis=1)

# 각 train 에 따라 동일하게 피쳐를 drop 한 test 셋들을 생성하세요.

test_1 = test.drop(['drop 할 피쳐'], axis = 1)

# 각 X_train 에 대해 모델 훈련을 해주세요.

model_input_var1 = RandomForestRegressor(criterion = 'mse')

model_input_var1.fit(X_train_1, Y_train)

# 각 모델로 test 셋들을 예측해주세요.

y_pred_1 = model_input_var1.predict(test_1)

# 각 결과들을 submission 파일로 저장해주세요.

submission_1 = pd.read_csv('data/submission.csv')

submission_1['count'] = y_pred_1

submission_1.to_csv('sub_1.csv', index = False)

Ex)

# X_train 에서 drop 할 피쳐의 경우에 수 대로 3개의 X_train 을 생성하세요.

X_train_1 = train.drop(['count','id'], axis=1)

X_train_2 = train.drop(['count', 'id', 'hour_bef_windspeed'], axis=1)

X_train_3 = train.drop(['count', 'id', 'hour_bef_windspeed', 'hour_bef_pm2.5'], axis=1)

# 각 train 에 따라 동일하게 피쳐를 drop 한 test 셋들을 생성하세요.

test_1 = test.drop(['id'], axis=1)

test_2 = test.drop(['id', 'hour_bef_windspeed'], axis=1)

test_3 = test.drop(['id', 'hour_bef_windspeed', 'hour_bef_pm2.5'], axis=1)

# 각 X_train에 대해 모델 훈련을 해주세요.

model_input_var1 = RandomForestRegressor(criterion = 'mse')

model_input_var1.fit(X_train_1, Y_train)

model_input_var2 = RandomForestRegressor(criterion = 'mse')

model_input_var2.fit(X_train_2, Y_train)

model_input_var3 = RandomForestRegressor(criterion = 'mse')

model_input_var3.fit(X_train_3, Y_train)

# 각 모델로 test 셋들을 예측해주세요.

y_pred_1 = model_input_var1.predict(test_1)

y_pred_2 = model_input_var2.predict(test_2)

y_pred_3 = model_input_var3.predict(test_3)

# 각 결과들을 submission 파일로 저장해주세요.

submission_1 = pd.read_csv('data/submission.csv')

submission_2 = pd.read_csv('data/submission.csv')

submission_3 = pd.read_csv('data/submission.csv')

submission_1['count'] = y_pred_1

submission_2['count'] = y_pred_2

submission_3['count'] = y_pred_3

submission_1.to_csv('sub_1.csv',index=False)

submission_2.to_csv('sub_2.csv',index=False)

submission_3.to_csv('sub_3.csv',index=False)

3) 하이퍼파라미터 / gfidsearch 개념

: 하이퍼파라미터 튜닝은 정지규칙 값들을 설정하는 것을 의미한다.

: 의사결정나무에는 정지규칙(stopping criteria) 이라는 개념이 있는데, 그 중 4가지 정지 규칙에 대해 알아보자

1. 최대깊이 (max_depth)

최대로 내려갈 수 있는 depth / 뿌리 노드로부터 내려갈 수 있는 깊이를 지정하며 작을수록 트리는 작아지게 된다.

2. 최소 노드크기(min_samples_split)

노드를 분할하기 위한 데이터 수 / 해당 노드에 이 값보다 적은 확률변수 수가 있다면 stop. 작을수록 트리는 커짐.

3. 최소 향상도(min_impurity_decrease)

노드를 분할하기 위한 최소 향상도 / 향상도가 설정값 이하라면 더 이상 분할하지 않는다. 작을수록 트리는 커짐.

4. 비용복잡도(Cost-complexity)

트리가 커지는 것에 대해 패널티 계수를 설정해서 불순도와 트리가 커지는 것에 대해 복잡도를 계산하는 것이다.

: 이와 같은 정지규칙들을 종합적으로 고려해 최적의 조건값을 설정할 수 있고, 이를 하이퍼파라미터 튜닝이라고 한다.

: 하이퍼파라미터 튜닝에는 다양한 방법론이 있다. 그 중 Best 성능을 나타내는 GridSearch는 완전 탐색(Exhaustive Search) 을 사용한다. 가능한 모든 조합 중에서 가장 우수한 조합을 찾아주지만, 완전 탐색이기 때문에 Best 조합을 찾을 때까지 시간이 매우 오래 걸린다는 단점이 있다.

4) Gridsearch 구현

- GridSearchCV 모듈로 완전탐색 하이퍼파라미터 튜닝을 구현하기

: from sklearn.model_selection import GridSearchCV

model = RandomForestRegressor(criterion = 'mse', random_state=2020)

params = {'n_estimators': [200, 300, 500], 'max_features': [5, 6, 8], 'min_samples_leaf': [1, 3, 5]}

greedy_CV = GridSearchCV(model, param_grid=params, cv = 3, n_jobs = -1)

greedy_CV.fit(X_train, Y_train)

from http://trisha227.tistory.com/14 by ccl(A) rewrite - 2021-11-05 01:26:58