์์๋ธ(Ensemble) ๋ชจ๋ธ
์ฌ๋ฌ ๊ฐ์ ๋ถ๋ฅ๊ธฐ(Classifier)๋ฅผ ์์ฑํ๊ณ , ๊ฐ ์์ธก ๊ฒฐ๊ณผ๋ฅผ ๊ฒฐํฉํ์ฌ ์์ธก๋ ฅ์ ํฅ์์ํค๋ ๋ชจ๋ธ
์ฅ์ : ๋จ์ผ ๋ชจ๋ธ(Decision Tree)์ ๋นํด์ ๋ถ๋ฅ ์ฑ๋ฅ ์ฐ์
๋จ์ผ Tree vs ์์๋ธ ๋ชจ๋ธ
Decision tree | Random Forest |
์์ฑ๋ ๋ถ๋ฅ๋ชจ๋ธ์ ๊ฒ์ ๋ฐ์ดํฐ์ ์ ์ฉํ์ฌ ๋ชฉํ๋ณ์ ์์ธก | ๋์ผํ ํ๋์ ๋ฐ์ดํฐ ์งํฉ์์ ์์๋ณต์ ์ํ๋ง์ ํตํด ์ฌ๋ฌ ๊ฐ์ ํ๋ จ์ฉ ๋ฐ์ดํฐ๋ฅผ ์์ฑ |
์์ฑ๋ ๋ถ๋ฅ๋ชจ๋ธ์ ๊ฒ์ ๋ฐ์ดํฐ์ ์ ์ฉํ์ฌ ๋ชฉํ๋ณ์ ์์ธก | ์ฌ๋ฌ ๋ฒ์ ํ์ต์ ํตํด ์ฌ๋ฌ ๊ฐ์ ํธ๋ฆฌ ์์ฑํ๊ณ , ์ด๋ฅผ ๊ฒฐํฉํ์ฌ ์ต์ข ์ ์ผ๋ก ๋ชฉํ๋ณ์ ์์ธก |
์์ฑ๋ ๋ถ๋ฅ๋ชจ๋ธ์ ๊ฒ์ ๋ฐ์ดํฐ์ ์ ์ฉํ์ฌ ๋ชฉํ๋ณ์ ์์ธก | ๋ถ๋ฅ๋ชจ๋ธ์ ๊ฒ์ ๋ฐ์ดํฐ์ ์ ์ฉ |
์์๋ธ(Ensemble) ์๊ณ ๋ฆฌ์ฆ
์ข ๋ฅ : ๋ฐฐ๊น (Bagging), ๋ถ์คํ (Boosting)
์์๋ธ ํ์ต ๋ชจ๋ธ ์์ฑ ์ ์ฐจ :
1. ์ ์ฒด ๋ฐ์ดํฐ์์ ํ๋ จ ์งํฉ ์์ฑ
2. ๊ฐ ํ๋ จ์งํฉ ๋ชจ๋ธ ํ์ต
3. ํ์ต๋ ๋ชจ๋ธ ์์๋ธ ๋์ถ
* ๋จ์ : ๋ชจ๋ธ ๊ฒฐ๊ณผ์ ํด์์ด ์ด๋ ต๊ณ , ์์ธก ์๊ฐ ๋ง์ด ์์
์์๋ธ ๋ชจ๋ธ ํ์ต ์๊ณ ๋ฆฌ์ฆ ๋น๊ต
๋ฐฐ๊น (Bagging) | ๋ถ์คํ (Boosting) | |
๊ณตํต์ | ์ ์ฒด ๋ฐ์ดํฐ ์งํฉ์ผ๋ก๋ถํฐ ๋ณต์ ๋๋ค ์ํ๋ง(bootstrap) ์ผ๋ก ํ๋ จ ์งํฉ ์์ฑ | |
์ฐจ์ด์ | ๋ณ๋ ฌํ์ต : ๊ฐ ๋ชจ๋ธ์ ๊ฒฐ๊ณผ ๋ฅผ ์กฐํฉํ์ฌ ํฌํ ๊ฒฐ์ | ์์ฐจํ์ต : ํ์ฌ ๋ชจ๋ธ ๊ฐ์ค์น -> ๋ค์ ๋ชจ๋ธ ์ ๋ฌ |
ํน์ง | ๊ท ์ผํ ํ๋ฅ ๋ถํฌ์ ์ํด์ ํ๋ จ ์งํฉ ์์ฑ | ๋ถ๋ฅํ๊ธฐ ์ด๋ ค์ด ํ๋ จ ์งํฉ ์์ฑ |
๊ฐ์ | ๊ณผ๋์ ํฉ์ ๊ฐํจ | ๋์ ์ ํ๋ |
์ฝ์ | ํน์ ์์ญ ์ ํ๋ ๋ฎ์ | Outlier ์ทจ์ฝ |
R ํจํค์ง | randomForest | XGboost |
์์๋ธ ํ์ต ์๊ณ ๋ฆฌ์ฆ
* Bagging์ ์ผ๋ฐ์ ์ธ ๋ชจ๋ธ์ ๋ง๋๋๋ฐ ์ง์ค, Boosting์ ๋ง์ถ๊ธฐ ์ด๋ ค์ด ๋ฌธ์ ๋ฅผ ๋ง์ถ๋๋ฐ ์ด์ ์ ๋ง์ถค ์์๋ธ ํ์ต ๋ฐฉ๋ฒ
1) ๋ถํธ์คํธ๋ฉ(Bootstrap)
์์๋ธ ๋ชจ๋ธ์์ ์๋์ ๋ฐ์ดํฐ ์ ์ผ๋ก๋ถํฐ ๋ณต์ ์ถ์ถ๋ฐฉ์์ผ๋ก ํ๋ จ ๋ฐ์ดํฐ ์ ์ ์์ฑํ๋ ๊ธฐ๋ฒ
๋ฐ์ดํฐ์ ์์ ์์์ ์ผ๋ก ๋๋ฆฌ๊ณ , ๋ฐ์ดํฐ์ ์ ๋ถํฌ๊ฐ ๊ณ ๋ฅด์ง ์์ ๋ ๊ณ ๋ฅด๊ฒ ๋ง๋๋ ํจ๊ณผ
2) ๋ฐฐ๊น ์๊ณ ๋ฆฌ์ฆ
Bagging : Bootstrap Aggregating(“์ฃผ๋จธ๋ ํตํฉ”)
๋ถํธ์คํธ๋ฉ์ ํตํด์ ์กฐ๊ธ์ฉ ์๋ก ๋ค๋ฅธ ํ๋ จ ๋ฐ์ดํฐ๋ฅผ ์์ฑํ์ฌ ๋ชจ๋ธ (ํ๋ จ ๋ ํธ๋ฆฌ)์ ์์ฑํ๊ณ , ๊ฒฐ๊ณผ๋ฅผ ๊ฒฐํฉ(aggregating) ์ํค๋ ๋ฐฉ๋ฒ
3) ๋ถ์คํ (boosting) ์๊ณ ๋ฆฌ์ฆ
์๋ชป ๋ถ๋ฅ๋ ๊ฐ์ฒด๋ค์ ์ง์คํ์ฌ ์๋ก์ด ๋ถ๋ฅ๊ท์น์ ์์ฑํ๋ ๋จ๊ณ๋ฅผ ๋ฐ๋ณตํ๋ ์๊ณ ๋ฆฌ์ฆ(์์ฐจ์ ํ์ต)
์ฝํ ์์ธก๋ชจํ๋ค์ ๊ฒฐํฉํ์ฌ ๊ฐํ ์์ธก๋ชจํ์ ๋ง๋๋ ์๊ณ ๋ฆฌ์ฆ
์ค ๋ถ๋ฅ๋ ๊ฐ์ฒด๋ ๋์ ๊ฐ์ค์น, ์ ๋ถ๋ฅ๋ ๊ฐ์ฒด๋ ๋ฎ์ ๊ฐ์ค์น๋ฅผ ์ ์ฉํ์ฌ ์์ธก๋ชจํ์ ์ ํ๋๋ฅผ ํฅ์์ํค๊ธฐ ์ํ ๋ฐฉ๋ฒ
4) RandomForest
๋ฐฐ๊น ์๊ณ ๋ฆฌ์ฆ๊ณผ ์ ์ฌํจ(์์๋ธ ๋ชจ๋ธ)
ํ๊ท๋ถ์, ๋ถ๋ฅ๋ถ์ ๋ชจ๋ ๊ฐ๋ฅ
๋ณ๋ ํ๋(์ค์ผ์ผ ์กฐ์ ) ๊ณผ์ ์์
๋จ์ผ tree ๋ชจ๋ธ ๋จ์ ๋ณด์(์ฑ๋ฅ, ๊ณผ๋์ ํฉ)
๋์ฉ๋ ๋ฐ์ดํฐ ์ ์ผ๋ก ์ฒ๋ฆฌ์๊ฐ ์ฆ๊ฐ(๋จ์ )
๋ฉํฐ์ฝ์ด ํ๋ก์ธ์ค ์ด์ฉ ๋ณ๋ ฌ์ฒ๋ฆฌ ๊ฐ๋ฅ
์ฐจ์ด์
๋ฐฐ๊น : ์ํ ๋ณต์ ์ถ์ถ ์ ๋ชจ๋ ๋ ๋ฆฝ๋ณ์(์ค๋ช ๋ณ์) ์ฌ์ฉ
๋๋คํฌ๋ ์คํธ : a๊ฐ ๋ ๋ฆฝ๋ณ์(์ค๋ช ๋ณ์)๋ง์ผ๋ก ๋ณต์ ์ถ์ถ
์ค๋ช ๋ณ์ ๊ฐ์ : sqrt(์ค๋ช ๋ณ์ ๊ฐ์) (์: 15๊ฐ ๋ณ์๋ผ๋ฉด 4๊ฐ ์ ๋)
์ค๋ช ๋ณ์๊ฐ ๋ง์ ๊ฒฝ์ฐ ๋ณ์๊ฐ ์๊ด์ฑ์ด ๋์ ๋ณ์๊ฐ ์์ผ ํ๋ฅ ์ ๊ฑฐ
5) XGBoost ์๊ณ ๋ฆฌ์ฆ
์ฌ๋ฌ ๊ฐ์ ๊ฒฐ์ ํธ๋ฆฌ๋ฅผ ์์์ ์ผ๋ก ํ์ตํ๋ ๋ฐฉ์. ์์๋ธ ํ์ต๋ฐฉ๋ฒ(๋ถ์คํ ์ ํ
์์ฐจ์ ํ์ต ๋ฐฉ๋ฒ : ์ฝํ ๋ถ๋ฅ๊ธฐ๋ฅผ ๊ฐํ ๋ถ๋ฅ๊ธฐ๋ก ์์ฑ. ๋ถ๋ฅ์ ํ๋ ์ฐ์, Outlier ์ทจ์ฝ
[์ค์ต]
RandomForest
from sklearn.ensemble import RandomForestClassifier #์ง์ ๋ณ์
from sklearn.datasets import load_wine #3๊ฐ class
from sklearn.metrics import confusion_matrix, accuracy_score, classification_report #model ํ๊ฐ๋๊ตฌ
1. dataset load
wine = load_wine()
x๋ณ์ ์ด๋ฆ
x_names = wine.feature_names
x_names
len(x_names) #13
X, y = load_wine(return_X_y = True)
2. model ์์ฑ
model = RandomForestClassifier().fit(X = X, y = y) #๊ธฐ๋ณธ ํ๋ผ๋ฏธํฐ
3. test set ์์ฑ
import numpy as np
idx = np.random.choice(a = len(X), size = 100, replace = False)
idx
X_test, y_test = X[idx], y[idx]
X_test.shape #(100, 13)
y_test.shape #(100,)
4. model ํ๊ฐ
y_pred = model.predict(X = X_test)
con_mat = confusion_matrix(y_test, y_pred)
print(con_mat)
[[33 0 0]
[ 0 43 0]
[ 0 0 24]]
acc = accuracy_score(y_test, y_pred)
print(acc) #1.0
5. ์ค์๋ณ์ ์๊ฐํ (y์ ์ํฅ๋ ฅ์ด ๊ฐ์ฅ ํฐ ์ค์๋ณ์ ์ ์ )
dir(model) #model์์ ํธ์ถ๊ฐ๋ฅํ methodํ์ธ
model.feature_importances_ #proline
array([0.11749764, 0.02167489, 0.0126863 , 0.02941608, 0.02915404,
0.05885703, 0.17041459, 0.01035685, 0.01737543, 0.15155433,
0.08574004, 0.1037657 , 0.19150708])
x_names[-1] #'proline'
x_names[6] #'flavanoids'
๊ฐ๋ก๋ง๋ ์ฐจํธ
import matplotlib.pyplot as plt
plt.barh(range(13), width = model.feature_importances_)
13๊ฐ์ ๋๊ธ๋ง๋ค x๋ณ์ ์ด๋ฆ์ ๋ถ์ด๊ธฐ
plt.yticks(range(13), x_names)
plt.xlabel('feature_importances')
plt.show()
RandomForest GridSearch
1. RandomForest
2. GridSearch : best parameters
from sklearn.ensemble import RandomForestClassifier #model
from sklearn.datasets import load_digits #10๊ฐ class
from sklearn.model_selection import GridSearchCV #best params
1. dataset load
X, y = load_digits(return_X_y=True)
2. model ์์ฑ : default params
model = RandomForestClassifier().fit(X = X, y = y)
n_estimators : int, default=100 : ๊ฒฐ์ ํธ๋ฆฌ ๊ฐ์
criterion : {"gini", "entropy"}, default="gini" : ์ค์๋ณ์ ์ ์ ๊ธฐ์ค
max_depth : int, default=None : ์ต๋ ํธ๋ฆฌ ๊น์ด
min_samples_split : int or float, default=2
min_samples_leaf : int or float, default=1
max_features : {"auto", "sqrt", "log2"} : ์ต๋ ์ฌ์ฉํ x๋ณ์ ๊ฐ์
model.score(X = X, y = y)
3. GridSearch model
parmas = {'n_estimators' : [100, 150, 200],
'criterion' : ["gini", "entropy"],
'max_depth' : [None, 3, 5, 7],
'min_samples_leaf' : [1, 2, 3],
'max_features' : ["auto", "sqrt"]} #dictํ
grid_model = GridSearchCV(model, param_grid=parmas,
scoring='accuracy', cv = 5, n_jobs= -1)
model2 = grid_model.fit(X = X, y = y)
dir(model2)
'''
'best_params_',
'best_score_',
'''
model2.best_score_ #0.9438130609718354
print('best params : ', model2.best_params_)
best params : {'criterion': 'gini',
'max_depth': None,
'max_features': 'sqrt',
'min_samples_leaf': 1,
'n_estimators': 150}
new model
new_model = RandomForestClassifier(
max_features='sqrt',n_estimators=150).fit(X = X, y = y)
new_model.score(X = X, y = y) #1.0
XGBoost_test
pip install xgboost
from xgboost import XGBClassifier #๋ถ๋ฅ Tree
from xgboost import plot_importance #์ค์๋ณ์ ์๊ฐํ
from sklearn.datasets import make_blobs #dataset : ๋ฐฉ์ธ. ์ ์ ๋์ฉ
from sklearn.model_selection import train_test_split #split๋๊ตฌ
from sklearn.metrics import accuracy_score, classification_report
1. dataset load
X, y = make_blobs(n_samples=2000, n_features=4, centers=2,
cluster_std=2.5, random_state=123)
n_samples : ํ๋ณธ(=๋ฐฉ์ธ)์ ๊ฐ์ (๊ธฐ๋ณธ : 100)
n_features = x๋ณ์ ๊ฐ์
centers = y๋ณ์ class(=๋ฐฉ์ธ์ ์ ํ) ๊ฐ์
cluster_std = ํด๋ฌ์คํฐ ๊ฐ ํ์คํธ์ฐจ(๋
ธ์ด์ฆ ๋ฐ์), ๋ฐ์ดํฐ์ ๋ณต์ก๋(๊ธฐ๋ณธ:1)
2. train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
3. model ์์ฑ : ์ดํญ๋ถ๋ฅ๊ธฐ(centers=2)
model = XGBClassifier(Objective = "binary:logistic").fit(X = X, y = y, eval_metric = 'error')
ํ์ฑํจ์(activeation function) : model์์ธก๊ฐ -> ์ถ๋ ฅ y๋ก ํ์ฑํ
Objective = "binary:logistic" : ์ดํญ๋ถ๋ฅ๊ธฐ - sigmoidํจ์
Objective = "multi:softprob" : ๋คํฅ๋ถ๋ฅ๊ธฐ - softmaxํจ์
4. model ํ๊ฐ (๋ถ๋ฅ์ ํ๋)
y_pred = model.predict(X = X_test)
len(y_pred)
len(y_test)
acc = accuracy_score(y_test, y_pred)
print(acc) #1.0
5. ์ค์๋ณ์ ์๊ฐํ
fscore = model.get_booster().get_fscore()
print(fscore)
plot_importance(model)
XGBoost_boston
ํ๊ทํธ๋ฆฌ : y๋ณ์ ์์ ๋ณ์(๋น์จ์ฒ๋)
๋ถ๋ฅํธ๋ฆฌ : y๋ณ์ ์ง์ ๋ณ์(๋ช
๋ชฉ์ฒ๋)
from xgboost import XGBRegressor #ํ๊ทํธ๋ฆฌ ๋ชจ๋ธ
from xgboost import plot_importance #์ค์๋ณ์ ์๊ฐํ
from sklearn.datasets import load_boston #dataset
from sklearn.model_selection import train_test_split #split
from sklearn.metrics import mean_squared_error, r2_score #ํ๊ฐ
1. dataset load
boston = load_boston()
x_names = boston.feature_names #x๋ณ์ ์ด๋ฆ
print(x_names)
['CRIM' 'ZN' 'INDUS' 'CHAS' 'NOX' 'RM' 'AGE' 'DIS' 'RAD' 'TAX' 'PTRATIO'
'B' 'LSTAT']
X = boston.data
y = boston.target
X.shape #(506, 13)
type(X) #numpy.ndarray
y #์ฐ์ํ(๋น์จ์ฒ๋)
2. dataset split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3)
3. model ์์ฑ
model = XGBRegressor().fit(X=X_train, y=y_train)
4. ์ค์๋ณ์ ํ์ธ
fscore = model.get_booster().get_fscore()
print(fscore)
{'f0': 698.0, 'f1': 43.0, 'f2': 107.0, 'f3': 23.0, 'f4': 140.0, 'f5': 410.0, 'f6': 361.0, 'f7': 337.0, 'f8': 36.0, 'f9': 60.0, 'f10': 77.0, 'f11': 313.0, 'f12': 311.0}
plot_importance(model) #1 > 6 > 7
x_names[0] #'CRIM'
x_names[5] #'RM'
x_names[6] #'AGE'
5. model ํ๊ฐ
y_pred = model.predict(X = X_test)
1) MSE : y๋ณ์ ๋ก๊ทธ๋ณํ or ์ ๊ทํ(o)
mse = mean_squared_error(y_test, y_pred)
print(mse) #11.779651544177534
2) ๊ฒฐ์ ๊ณ์(r^2) : y๋ณ์ ๋ก๊ทธ๋ณํ or ์ ๊ทํ(x)
score = r2_score(y_test, y_pred)
print(score) #0.8240142993822802
์ค์๋ณ์ ์๊ฐํ์์ X๋ณ์๋ช ํ์
import pandas as pd
1. numpy -> DataFrame ๋ณํ
X = boston.data
X.shape #(506, 13)
y = boston.target
df = pd.DataFrame(X, columns=x_names)
df.info()
์นผ๋ผ ์ถ๊ฐ
df['target'] = y
df.info()
x,y๋ณ์ ์ ์
cols = list(df.columns)
X = df[cols[:-1]]
y = df['target']
2. dataset split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3)
3. model ์์ฑ
model = XGBRegressor().fit(X=X_train, y=y_train)
4. ์ค์๋ณ์ ํ์ธ
fscore = model.get_booster().get_fscore()
print(fscore)
plot_importance(model)
5. model ํ๊ฐ
y_pred = model.predict(X = X_test)
1) MSE : y๋ณ์ ๋ก๊ทธ๋ณํ or ์ ๊ทํ(o)
mse = mean_squared_error(y_test, y_pred)
print(mse) # 11.779651544177534
2) ๊ฒฐ์ ๊ณ์(r^2) : y๋ณ์ ๋ก๊ทธ๋ณํ or ์ ๊ทํ(x)
score = r2_score(y_test, y_pred)
print(score) # 0.8240142993822802
6. model save & load
model #object
import pickle #binary file
model save
pickle.dump(model, open('xgb_model.pkl', mode='wb'))
model load
load_model = pickle.load(open('xgb_model.pkl', mode='rb'))
y_pred = load_model.predict(X = X_test) #new_data
score = r2_score(y_test, y_pred)
print(score) #0.8919146007745038
XGBoost GridSearch
1. XGBoost Hyper parameter : ppt.19
2. model ํ์ต ์กฐ๊ธฐ ์ข
๋ฃ
3. Best Hyper parameter ์ฐพ๊ธฐ
from xgboost import XGBClassifier #model
from xgboost import plot_importance #์ค์๋ณ์ ์๊ฐํ
from sklearn.datasets import load_breast_cancer #์ดํญ๋ถ๋ฅ dataset
from sklearn.model_selection import train_test_split #split
from sklearn.metrics import accuracy_score, classification_report #ํ๊ฐ
1. XGBoost Hyper parameter
1) dataset load
cancer = load_breast_cancer()
x_names = cancer.feature_names #x๋ณ์๋ช
print(x_names, len(x_names)) #30
class_names = cancer.target_names #y๋ณ์ class๋ช
print(class_names) #['malignant' 'benign']
X = cancer.data
y = cancer.target
2) dataset split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3)
3) model ์์ฑ
model = XGBClassifier().fit(X_train, y_train) #default ํ๋ผ๋ฏธํฐ
print(model) #default ํ๋ผ๋ฏธํฐ
1. colsample_bytree=1 : ํธ๋ฆฌ ๋ชจ๋ธ ์์ฑ ์ ํ๋ จ์
์ํ๋ง ๋น์จ(๋ณดํต : 0.6 ~ 1)
2. learning_rate=0.3 : ํ์ต์จ(๋ณดํต : 0.01~0.1) = 0์ ์๋ ด์๋
3. max_depth=6 : ํธ๋ฆฌ์ ๊น์ด(ํด ์๋ก ์ฑ๋ฅ์ด ์ข์์ง, ๊ณผ์ ํฉ ์ํฅ)
4. min_child_weight=1 : ์์ ๋
ธ๋ ๋ถํ ์ ๊ฒฐ์ ํ๋ ๊ฐ์ค์น(Weight)์ ํฉ
* 0~n๊ฐ ์ง์ : ์์ ์๋ก ์ฑ๋ฅ์ด ์ข์์ง, ๊ณผ์ ํฉ ์ํฅ
5. n_estimators=100 ๊ฒฐ์ ํธ๋ฆฌ ๊ฐ์(default=100), ๋ง์ ์๋ก ๊ณ ์ฑ๋ฅ
6. objective='binary:logistic'(์ดํญ๋ถ๋ฅ๊ธฐ), 'multi:softprob'(๋คํญ๋ถ๋ฅ๊ธฐ)
๊ณผ์ ํฉ ์กฐ์ : max_depth ์๊ฒ, min_child_weight ๋๊ฒ ์ค์
2. model ํ์ต ์กฐ๊ธฐ ์ข
๋ฃ
xgb = XGBClassifier(n_estimators=500) #object
model = xgb.fit(X = X_train, y = y_train, #ํ๋ จ์
eval_set = [ (X_test, y_test) ], #ํ๊ฐ์
,
eval_metric = 'error', #ํ๊ฐ ๋ฐฉ๋ฒ(์ค์ฐจ)
early_stopping_rounds = 80, #๊ธฐ์ค tree ๊ฐ์
verbose = True) #ํ์ต๊ณผ์ ํ์ธ
ํ๋ จ์
: X = X_train, y = y_train
ํ๊ฐ์
: (X_test, y_test)
ํ๊ฐ๋ฐฉ๋ฒ : 'error'
์กฐ๊ธฐ์ข
๋ฃ ๋ผ์ด๋ ์ : early_stopping_rounds
ํ์ต๊ณผ์ ์ฝ์ ์ถ๋ ฅ : verbose = True
[91] validation_0-error:0.04094
4) model ํ๊ฐ
y_pred = model.predict(X = X_test)
acc = accuracy_score(y_test, y_pred)
print(acc) #0.9649122807017544
3. Best Hyper parameter ์ฐพ๊ธฐ
from sklearn.model_selection import GridSearchCV #class
1) ๊ธฐ๋ณธ model
model = XGBClassifier()
params = {'colsample_bytree' : [0.5, 0.7, 1],
'learning_rate' : [0.01, 0.3, 0.5],
'max_depth' : [5,6,7],
'min_child_weight' : [0.5,1,3],
'n_estimators' : [90, 100, 200]} #dict
2) grid search model
gs = GridSearchCV(estimator=model, param_grid=params, cv = 5)
grid_model = gs.fit(X_train, y_train)
3) best score & parameter
dir(grid_model)
grid_model.best_score_ #0.9649367088607596
grid_model.best_params_
{'colsample_bytree': 0.7,
'learning_rate': 0.3,
'max_depth': 5,
'min_child_weight': 1,
'n_estimators': 200}
4) best model ์์ฑ
model = XGBClassifier(colsample_bytree=0.7,
learning_rate=0.3,
max_depth=5,
min_child_weight=1,
n_estimators=200).fit(X_train, y_train)
y_pred = model.predict(X = X_test)
acc = accuracy_score(y_test, y_pred)
print(acc) #0.9532163742690059
report = classification_report(y_test, y_pred)
print(report)
์ค์๋ณ์ ์๊ฐํ
plot_importance(model)
'๋ฐ์ดํฐ๋ถ์๊ฐ ๊ณผ์ > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
DAY56. Python TextMining (1)WebCrawling (url, tag, html) (0) | 2021.12.09 |
---|---|
DAY55. Python Cluster&Recommand (๊ณ์ธต๊ตฐ์ง๋ถ์, KMeans, ์ถ์ฒ์์คํ ๋ชจํ) (0) | 2021.12.08 |
DAY53. Python TreeModel (1)DicisionTree (0) | 2021.12.06 |
DAY52. Python Classification (2)TF-IDF sparse Matrix (๋จ์ด ์ถ์ถ) (0) | 2021.12.03 |
DAY51. Python Classification (1)Classification (๋ฌธ์ ๋ถ๋ฅ) (0) | 2021.12.02 |