<jupyter_start><jupyter_text>8장. XGBoost 기본 학습기 *아래 링크를 통해 이 노트북을 주피터 노트북 뷰어(nbviewer.org)로 보거나 구글 코랩(colab.research.google.com)에서 실행할 수 있습니다.*      주피터 노트북 뷰어로 보기        구글 코랩(Colab)에서 실행하기<jupyter_code># 노트북이 코랩에서 실행 중인지 체크합니다.
import sys
if 'google.colab' in sys.modules:
    !pip install -q --upgrade xgboost
    !wget -q https://raw.githubusercontent.com/rickiepark/handson-gb/main/Chapter08/census_cleaned.csv
# 경고 끄기
import warnings
warnings.filterwarnings('ignore')

import xgboost as xgb
xgb.set_config(verbosity=0)<jupyter_output><empty_output><jupyter_text>gblinear 적용하기 gblinear를 당뇨병 데이터셋에 적용하기<jupyter_code>import pandas as pd
import numpy as np
from sklearn.datasets import load_diabetes
from sklearn.model_selection import cross_val_score
from xgboost import XGBRegressor, XGBClassifier, XGBRFRegressor, XGBRFClassifier
from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier
from sklearn.linear_model import LinearRegression, LogisticRegression
from sklearn.linear_model import Lasso, Ridge
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import KFold, StratifiedKFold
from sklearn.metrics import mean_squared_error as MSE
X, y = load_diabetes(return_X_y=True)
kfold = KFold(n_splits=5, shuffle=True, random_state=2)
def regression_model(model):
    # 5폴드 교차 검증으로 음의 평균 제곱 오차를 구합니다.
    scores = cross_val_score(model, X, y, scoring='neg_mean_squared_error', cv=kfold)

    # 제곱근을 계산합니다.
    rmse = (-scores)**0.5

    # 평균을 반환합니다.
    return rmse.mean()
regression_model(XGBRegressor(booster='gblinear'))
regression_model(LinearRegression())
regression_model(Lasso())
regression_model(Ridge())
regression_model(XGBRegressor(booster='gbtree'))<jupyter_output><empty_output><jupyter_text>gblinear 그리드 서치<jupyter_code>def grid_search(params, reg=XGBRegressor(booster='gblinear')):

    # GridSearchCV를 초기화합니다.
    grid_reg = GridSearchCV(reg, params, scoring='neg_mean_squared_error', cv=kfold)
    
    # X_train와 y_train에서 훈련합니다.
    grid_reg.fit(X, y)

    # 최상의 매개변수를 추출합니다.
    best_params = grid_reg.best_params_

    # 최상의 매개변수를 출력합니다.
    print("최상의 매개변수:", best_params)
    
    # 최상의 점수를 추출합니다.
    best_score = np.sqrt(-grid_reg.best_score_)

    # 최상의 점수를 출력합니다.
    print("최상의 점수:", best_score)
grid_search(params={'reg_alpha':[0.001, 0.01, 0.1, 0.5, 1, 5]})
grid_search(params={'reg_lambda':[0.001, 0.01, 0.1, 0.5, 1, 5]})
grid_search(params={'feature_selector':['shuffle']})
grid_search(params={'feature_selector':['random', 'greedy', 'thrifty'], 
                    'updater':['coord_descent'] })
grid_search(params={'feature_selector':['greedy', 'thrifty'], 
                    'updater':['coord_descent'], 'top_k':[3, 5, 7, 9]})
param_grid = [{'updater':['shotgun'],
               'feature_selector':['cyclic', 'shuffle']},
              {'updater':['coord_descent'],
               'feature_selector':['random', 'greedy', 'thrifty']}]
grid_search(params=param_grid)<jupyter_output>최상의 매개변수: {'feature_selector': 'thrifty', 'updater': 'coord_descent'}
최상의 점수: 55.488143951136536<jupyter_text>선형 데이터셋<jupyter_code>X = np.arange(1,100)
np.random.seed(2) 
y = []
for i in X:
    y.append(i*np.random.uniform(-0.2, 0.2))
y = np.array(y)
X = X.reshape(X.shape[0], 1)
y = y.reshape(y.shape[0], 1)
import matplotlib.pyplot as plt

xgbr = XGBRegressor(booster='gblinear')
xgbr.fit(X, y)

plt.scatter(X, y)
plt.plot((0, 99), (xgbr.intercept_, xgbr.coef_*99+xgbr.intercept_))
plt.show()
regression_model(XGBRegressor(booster='gblinear'))
regression_model(XGBRegressor(booster='gbtree'))
regression_model(LinearRegression())<jupyter_output><empty_output><jupyter_text>dart 비교하기 dart를 사용한 XGBRegressor<jupyter_code>X, y = load_diabetes(return_X_y=True)
regression_model(XGBRegressor(booster='dart', rate_drop=0.5))<jupyter_output><empty_output><jupyter_text>dart를 사용한 XGBClassifier<jupyter_code>df_census = pd.read_csv('census_cleaned.csv')
X_census = df_census.iloc[:, :-1]
y_census = df_census.iloc[:, -1]
skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=2)

def classification_model(model):
    # 교차 검증 정확도를 계산합니다.
    scores = cross_val_score(model, X_census, y_census, 
                             scoring='accuracy', cv=skf)

    # 평균 값을 반환합니다.
    return scores.mean()
classification_model(XGBClassifier(booster='gbtree'))
classification_model(XGBClassifier(booster='dart', rate_drop=0.1))
classification_model(XGBClassifier(booster='gblinear'))
classification_model(LogisticRegression(max_iter=1000))<jupyter_output><empty_output><jupyter_text>dart 매개변수 적용<jupyter_code>classification_model(XGBClassifier(booster='dart', one_drop=1))
regression_model(XGBRegressor(booster='dart', rate_drop=0.5, 
                              sample_type='weighted'))
regression_model(XGBRegressor(booster='dart', rate_drop=0.5, 
                              normalize_type='forest'))
regression_model(XGBRegressor(booster='dart', one_drop=1))
grid_search(params={'rate_drop':[0.01, 0.1, 0.2, 0.4]}, 
            reg=XGBRegressor(booster='dart', one_drop=1))
grid_search(params={'skip_drop': [0.01, 0.1, 0.2, 0.4]}, 
            reg=XGBRegressor(booster='dart', rate_drop=0.2, 
                             one_drop=1))<jupyter_output>최상의 매개변수: {'skip_drop': 0.01}
최상의 점수: 62.24723633700593<jupyter_text>XGBoost 랜덤 포레스트 랜덤 포레스트 기본 학습기<jupyter_code>regression_model(XGBRegressor(booster='gbtree', num_parallel_tree=25))<jupyter_output><empty_output><jupyter_text>스탠드얼론 랜덤 포레스트<jupyter_code>regression_model(XGBRFRegressor())
regression_model(RandomForestRegressor())
classification_model(XGBRFClassifier())
classification_model(RandomForestClassifier())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13412263650821699, 'fraction_numerical': 0.023855804912528714, 'mean_word_length': 5.61214953271028, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '522414', 'license': 'MIT', 'n_tokens_mistral': 2552, 'n_tokens_neox': 2640, 'n_words': 348, 'path': 'Chapter08/Alternative_Base_Learners.ipynb', 'repo_name': 'rickiepark/handson-gb', 'size': '5659'}