<jupyter_start><jupyter_text>作業請使用不同的資料集，並使用 hyper-parameter search 的方式，看能不能找出最佳的超參數組合<jupyter_code>from sklearn import datasets, metrics
from sklearn.model_selection import train_test_split, KFold, GridSearchCV
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.ensemble import GradientBoostingClassifier
import numpy as np
#讀入手寫辨識資料
digits = datasets.load_digits()

# 切分訓練集/測試集
x_train, x_test, y_train, y_test = train_test_split(digits.data, digits.target, test_size=0.25, random_state=4)

# 建立模型
GBC = GradientBoostingClassifier(random_state=7)

# 訓練模型
GBC.fit(x_train, y_train)

# 預測測試集
GBC_y_pred = GBC.predict(x_test)
GBC_acc = metrics.accuracy_score(y_test, GBC_y_pred)
print("GBC Acuuracy: ", GBC_acc)
# 設定要訓練的超參數組合
n_estimators = [100, 500, 1000]
max_depth = [5, 10, 15, 20]
param_grid = dict(n_estimators=n_estimators, max_depth=max_depth)

## 建立搜尋物件，放入模型及參數組合字典 (n_jobs=-1 會使用全部 cpu 平行運算)
grid_search = GridSearchCV(GBC, param_grid, scoring="accuracy", n_jobs=-1, verbose=1)

# 開始搜尋最佳參數
grid_result = grid_search.fit(x_train, y_train)

# 預設會跑 3-fold cross-validadtion，總共 12 種參數組合，總共要 train 36 次模型
# 印出最佳結果與最佳參數
print("Best Accuracy: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
grid_result.best_params_
# 使用最佳參數重新建立模型
GBC_bestparam = GradientBoostingClassifier(max_depth=grid_result.best_params_['max_depth'],
                                           n_estimators=grid_result.best_params_['n_estimators'])
# 訓練模型
GBC_bestparam.fit(x_train, y_train)

# 預測測試集
y_pred = GBC_bestparam.predict(x_test)
# 調整參數後沒有比原來設定的低
print(metrics.accuracy_score(y_test, y_pred))
#利用隨機搜尋法，尋找最佳參數
from sklearn.model_selection import RandomizedSearchCV
# Number of trees in random forest
n_estimators = [int(x) for x in np.linspace(start = 50, stop = 2000, num = 5)]
# Number of features to consider at every split
max_features = ['auto', 'sqrt']
# Maximum number of levels in tree
max_depth = [int(x) for x in np.linspace(10, 110, num = 5)]
max_depth.append(None)
# Minimum number of samples required to split a node
min_samples_split = [2, 5, 10, 20, 50]
# Minimum number of samples required at each leaf node
min_samples_leaf = [1, 2, 4, 10, 20, 50]
# Create the random grid
random_grid = {'n_estimators': n_estimators,
               'max_features': max_features,
               'max_depth': max_depth,
               'min_samples_split': min_samples_split,
               'min_samples_leaf': min_samples_leaf,}
print(random_grid)
GBC_random = RandomizedSearchCV(estimator = GBC, param_distributions = random_grid, n_iter = 100, cv = 3, verbose=2, random_state=7, n_jobs = -1)
GBC_random.fit(x_train, y_train)
GBC_random.best_params_
#最佳模型
GBC_best_random = GBC_random.best_estimator_

# 使用最佳參數重新建立模型
y_pred = GBC_best_random.predict(x_test)

# 調整參數後提升了約2%準確率
print(metrics.accuracy_score(y_test, y_pred))<jupyter_output>0.9888888888888889
{'fraction_non_alphanumeric': 0.09913043478260869, 'fraction_numerical': 0.029565217391304348, 'mean_word_length': 5.626728110599078, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '639357', 'license': 'MIT', 'n_tokens_mistral': 1315, 'n_tokens_neox': 1311, 'n_words': 232, 'path': 'Day_047_hyperParameterTunning.ipynb', 'repo_name': 'build0220/homework', 'size': '2875'}