์ต์ข ๊ฒฐ์ ์์ ํ๋ ๋ณดํ
์ต์ข ๊ฒฐ์ ์์ ์ํํธ๋ณดํ (ํ๋ฅ ์ ํ๊ท ๊ฐ(?))
๋๋ค ํฌ๋ ์คํธ Random Forest : ๊ฐ์ ์๊ณ ๋ฆฌ์ฆ์ผ๋ก ๊ตฌํํ๋ ๋ฐฐ๊น
(Bagging)์ ๋ํ์ ์ธ ๋ฐฉ๋ฒ.
- ์์๋ธ ๋ฐฉ๋ฒ ์ค์์ ๋น๊ต์ ์๋๊ฐ ๋น ๋ฅด๋ฉฐ ๋ค์ํ ์์ญ์์ ๋์ ์ฑ๋ฅ์ ๋ณด์ฌ์ฃผ๊ณ ์๋ค. โ ๋ถํธ์คํธ๋ํ์ ์ฌ๋ฌ ๊ฐ์ ์์ ๋ฐ์ดํฐ ์
์ ์ค์ฒฉ์ ํ์ฉํด์ ๋ง๋๋ ๊ฒ.
- ๋๋ค ํฌ๋ ์คํธ๋ ๊ฒฐ์ ๋๋ฌด๋ฅผ ๊ธฐ๋ณธ์ผ๋ก ํจ โ ๋ถํธ์คํธ๋ํ์ผ๋ก ์ํ๋ง๋ ๋ฐ์ดํฐ๋ง๋ค ๊ฒฐ์ ๋๋ฌด๊ฐ ์์ธกํ ๊ฒฐ๊ณผ๋ฅผ ์ํํธ๋ณดํ
์ผ๋ก ์ต์ข
์์ธก ๊ฒฐ๋ก ์ ์ป์
IMU ์ผ์๋ฅผ ํ์ฉํด์ ์ฌ๋์ ํ๋์ ์ธ์ํ๋ ์คํ
ํฐ์ ์๋ ๊ฐ์๋/์์ด๋ก ์ผ์ ์ฌ์ฉ
๋ฐ์ดํฐ์ ๊ณต์ ๊ฒฝ๋ก : https://archive.ics.uci.edu/ml/datasets/human+activity+recognition+using+smartphones
๋ฐ์ดํฐ์ ํน์ฑ
- ๊ฐ์๋๊ณ๋ก๋ถํฐ 3์ถ ๊ฐ์๋(์ด ๊ฐ์๋) ๋ฐ ์ถ์ ๋ ์ ์ฒด ๊ฐ์๋
- ์์ด๋ก ์ค์ฝํ์ 3์ถ ๊ฐ์๋
- ์๊ฐ ๋ฐ ์ฃผํ์ ์์ญ ๋ณ์๊ฐ ํฌํจ๋ 561 ๊ธฐ๋ฅ ๋ฒกํฐ
- ํ๋ ๋ผ๋ฒจ
- ์คํ์ ์ํํ ๋์์ ์๋ณ์
๋ฐ์ดํฐ์ ํด๋์ค : Walking Upstairs, Standing, Walking Downstairs, Sitting, Laying, Walking
์๊ฐ ์์ญ์ ๋ฐ์ดํฐ๋ฅผ ์ง์ ์ฌ์ฉํ๋ ๊ฒ์ ์ด๋ ต๋ค.
- ์๊ฐ์์ญ ๋ฐ์ดํฐ๋ฅผ ๋จธ์ ๋ฌ๋์ ์ ์ฉํ๊ธฐ ์ํด ์ฌ๋ฌ ํต๊ณ์ ๋ฐ์ดํฐ๋ก ๋ณํํจ
- ์๊ฐ ์์ญ์ ํ๊ท , ๋ถ์ฐ, ํผํฌ, ์ค๊ฐ ๊ฐ, ์ฃผํ์ ์์ญ์ ํ๊ท , ๋ถ์ฐ ๋ฑ์ผ๋ก ๋ณํํ ์์น๋ฅผ ๊ฐ์ง๊ณ ์๋ค
์ผ์ ์ ํธ โ ํน์ง์ถ์ถ โ ๋ชจ๋ธํ์ต โ ํ๋์ถ๋ก
# ๋ฐ์ดํฐ ์ฝ๊ธฐ
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
url = 'https://raw.githubusercontent.com/PinkWink/ML_tutorial/master/dataset/HAR_dataset/features.txt'
feature_name_df = pd.read_csv(url, sep='\s+', header=None,
names=['column_index', 'column_name'])
# sep='\s+' : ๊ธธ์ด๊ฐ ์ ํด์ง์ง ์์ ๊ณต๋ฐฑ์ด ๊ตฌ๋ถ์์ธ ๊ฒฝ์ฐ์๋ \s+ ์ ๊ท์(regular expression) ๋ฌธ์์ด์ ์ฌ์ฉ
# ์ฐธ๊ณ : https://datascienceschool.net/01%20python/04.02%20%EB%8D%B0%EC%9D%B4%ED%84%B0%20%EC%9E%85%EC%B6%9C%EB%A0%A5.html
# names= : column์ด๋ฆ ์ค์
feature_name_df.head()
# ํน์ฑ๋ง 561๊ฐ
len(feature_name_df)
feature_name = feature_name_df.iloc[:, 1].values.tolist()
feature_name[:10]
# X ๋ฐ์ดํฐ ์ฝ๊ธฐ
X_train_url = 'https://raw.githubusercontent.com/PinkWink/ML_tutorial/master/dataset/HAR_dataset/train/X_train.txt'
X_test_url = 'https://raw.githubusercontent.com/PinkWink/ML_tutorial/master/dataset/HAR_dataset/test/X_test.txt'
X_train = pd.read_csv(X_test_url, sep='\s+', header=None)
X_test = pd.read_csv(X_test_url, sep='\s+', header=None)
X_train.columns = feature_name
X_test.columns = feature_name
X_train.head()
X_train.info()
X_test.head()
X_test.info()
# y ๋ฐ์ดํฐ ์ฝ๊ธฐ
y_train_url = 'https://raw.githubusercontent.com/PinkWink/ML_tutorial/master/dataset/HAR_dataset/train/y_train.txt'
y_test_url = 'https://raw.githubusercontent.com/PinkWink/ML_tutorial/master/dataset/HAR_dataset/test/y_test.txt'
y_train = pd.read_csv(y_test_url, sep='\s+', header=None, names = ['action'])
y_test = pd.read_csv(y_test_url, sep='\s+', header=None, names=['action'])
X_train.shape, X_test.shape, y_train.shape, y_test.shape
# ๊ฐ ์ก์
๋ณ ๋ฐ์ดํฐ์ ์
# 1. Walking
# 2. WalkingUpstairs
# 3. WalkingDownstairs
# 4. Sitting
# 5. Standing
# 6. Laying
y_train['action'].value_counts()
# ๊ฒฐ์ ๋๋ฌด
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
dt_clf = DecisionTreeClassifier(random_state=13, max_depth=4)
dt_clf.fit(X_train, y_train)
pred = dt_clf.predict(X_test)
accuracy_score(y_test, pred)
# max_depth๋ฅผ ๋ค์ํ๊ฒ ํ๊ธฐ ์ํด GridSearchCV ์ด์ฉ
from sklearn.model_selection import GridSearchCV
params = {'max_depth' : [6, 8, 10, 12, 16, 20, 24]}
grid_cv = GridSearchCV(dt_clf, param_grid=params, scoring='accuracy',
cv = 5, return_train_score=True) # cv data๋ฅผ 5๊ฐ๋ก ๋๋(?)
grid_cv.fit(X_train, y_train)
# max_depth 16์ด ์ข๋ค๊ณ ํจ(?) ์๋ฃ์ ๋ค๋ฆ;;;
grid_cv.best_score_
grid_cv.best_params_
# max_depth ๋ณ๋ก ํ๋ก ์ฑ๋ฅ์ ์ ๋ฆฌ(test๋ฐ์ดํฐ๋ ์์ง ์ํจ, ์ฌ๊ธฐ๋ ๋ฐ์ดํฐ ๋๋๊ฑฐ๋ฅผ ํด๋ณธ๊ฒ)
cv_result_df = pd.DataFrame(grid_cv.cv_results_)
cv_result_df[['param_max_depth', 'mean_test_score', 'mean_train_score']]
# ์ค์ test ๋ฐ์ดํฐ์์์ ๊ฒฐ๊ณผ
max_depths = [6, 8, 10, 12, 16, 20, 24]
for depth in max_depths:
dt_clf = DecisionTreeClassifier(max_depth=depth, random_state=156)
dt_clf.fit(X_train, y_train)
pred = dt_clf.predict(X_test)
accuracy = accuracy_score(y_test, pred)
print('Max_Depth = ', depth, ', Accuracy = ', accuracy)
# ๋ฒ ์คํธ ๋ชจ๋ธ ๊ฒฐ๊ณผ
best_dt_clf = grid_cv.best_estimator_
pred1 = best_dt_clf.predict(X_test)
accuracy_score(y_test, pred1)
# ๋๋คํฌ๋ ์คํธ ์ ์ฉ
from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import RandomForestClassifier
params = {
'max_depth' : [6, 8, 10],
'n_estimators' : [50, 100, 200], # n_estimators : ์ฌ์ฉํ tree์
'min_samples_leaf' : [8, 12], # min_samples_leaf : ๊ฐ์ฅ ๋ง์ง๋ง ์(ํญ๋ชฉ)์ ์ต์ ๋ฐ์ดํฐ ๊ฐ์
'min_samples_split' : [8, 12] # min_samples_split : ๋ถํ ๊ธฐ์ค ์ต์ ๋ฐ์ดํฐ
}
rf_clf = RandomForestClassifier(random_state=13, n_jobs=-1) # n_jobs : ์ฌ์ฉํ cpu ์ฝ์ด ์
grid_cv = GridSearchCV(rf_clf, param_grid=params, cv = 2, n_jobs=-1)
grid_cv.fit(X_train, y_train)
# ๊ฒฐ๊ณผ ์ ๋ฆฌ๋ฅผ ์ํ ์์
cv_results_df = pd.DataFrame(grid_cv.cv_results_)
cv_result_df.columns
cv_result_df.head()
# ์ฑ๋ฅ์ด ์ข์
target_col = ['rank_test_score', 'mean_test_score', 'param_n_estimators', 'param_max_depth']
cv_results_df[target_col].sort_values('rank_test_score').head()
# best ๋ชจ๋ธ
grid_cv.best_params_
grid_cv.best_score_
# test ๋ฐ์ดํฐ์ ์ ์ฉ
rf_clf_best = grid_cv.best_estimator_
rf_clf_best.fit(X_train, y_train)
pred1 = rf_clf_best.predict(X_test)
pred1
accuracy_score(y_test, pred1)
# ์ค์ ํน์ฑ ํ์ธ
# ์ํฅ๋ ฅ ๋์ feature๋ค ์ค 20๊ฐ๋ง ๊ฐ์ ธ์ค๊ธฐ
best_cols_values = rf_clf_best.feature_importances_
best_cols = pd.Series(best_cols_values, index=X_train.columns)
best_cols_values, best_cols
# ๊ฐ ํน์ฑ๋ค์ ์ค์๋๊ฐ ๊ฐ๋ณ์ ์ผ๋ก ๋์ง ์๋ค.
top20_cols = best_cols.sort_values(ascending=False)[:20]
top20_cols
# ์ฃผ์ ํน์ฑ ๊ด์ฐฐ
import seaborn as sns
plt.figure(figsize=(8, 8))
sns.barplot(x=top20_cols, y=top20_cols.index)
plt.show()
# ์ฃผ์ 20๊ฐ ํน์ฑ
top20_cols.index
# 20๊ฐ์ ํน์ฑ๋ง ๊ฐ์ง๊ณ ๋ค์ ์ฑ๋ฅ ํ์ธ
X_train_re = X_train[top20_cols.index]
X_test_re = X_test[top20_cols.index]
# 561๊ฐ์ ํน์ฑ๋ณด๋ค 20๊ฐ์ ํน์ฑ๋ง ๋ณด๋ฉด ์ฐ์ฐ์๋๊ฐ ์ ๋ง ๋น ๋ฅผ ๊ฒ์ด๋ค.
# accuracy๋ ์ฝ๊ฐ ๋จ์ด์ง๋๋ผ๋
rf_clf_best_re = grid_cv.best_estimator_
rf_clf_best_re.fit(X_train_re, y_train.values.reshape(-1,))
pred1_re = rf_clf_best_re.predict(X_test_re)
accuracy_score(y_test, pred1_re)
๋ง์ด ์ด๋ ต...ใ ใ
๐ป ์ถ์ฒ : ์ ๋ก๋ฒ ์ด์ค ๋ฐ์ดํฐ ์ทจ์ ์ค์ฟจ