我制作了随机森林模型,并可视化了结果。
#training code
from sklearn.datasets import load_digits
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV
digits = load_digits()
forest_param = {'max_depth': np.arange(1,15),
'n_estimators': [50, 100, 150, 200, 250, 300, 350, 400]}
forest_classifier = RandomForestClassifier()
forest_grid = GridSearchCV(forest_classifier, forest_param, n_jobs=-1, return_train_score=True, cv=10)
digit_data = digits.data
digit_target = digits.target
forest_grid.fit(digit_data, digit_target)
print("best forest validation score")
forest_grid.best_score_
#visualize code
def plot_search_results(grid, lsi_log_index):
"""
Params:
grid: A trained GridSearchCV object.
"""
## Results from grid search
results = grid.cv_results_
means_test = results['mean_test_score']
stds_test = results['std_test_score']
means_train = results['mean_train_score']
stds_train = results['std_train_score']
## Getting indexes of values per hyper-parameter
masks=[]
masks_names= list(grid.best_params_.keys())
for p_k, p_v in grid.best_params_.items():
masks.append(list(results['param_'+p_k].data==p_v))
params=grid.param_grid
## Ploting results
fig, ax = plt.subplots(1,len(params),sharex='none', sharey='all',figsize=(20,5))
fig.suptitle('Score per parameter')
fig.text(0.04, 0.5, 'MEAN SCORE', va='center', rotation='vertical')
pram_preformace_in_best = {}
for i, p in enumerate(masks_names):
m = np.stack(masks[:i] + masks[i+1:])
pram_preformace_in_best
best_parms_mask = m.all(axis=0)
best_index = np.where(best_parms_mask)[0]
x = np.array(params[p])
y_1 = np.array(means_test[best_index])
e_1 = np.array(stds_test[best_index])
y_2 = np.array(means_train[best_index])
e_2 = np.array(stds_train[best_index])
ax[i].errorbar(x, y_1, e_1, linestyle='--', marker='o', label='test')
ax[i].errorbar(x, y_2, e_2, linestyle='-', marker='^',label='train' )
ax[i].set_xlabel(p.upper())
for log_scaler in lsi_log_index:
ax[log_scaler].set_xscale("log")
plt.legend()
plt.show()
plot_search_results(forest_grid,[])
当发生过度拟合时,我想让验证分数缩小。
像这个 SVR_C 参数。当发生过度拟合时, Image1
验证分数会缩小。
但是,当发生过拟合时,最大深度参数的验证分数不会缩小。
图片2
我了解到验证分数会缩小,会出现过度拟合的情况。
你能告诉我为什么会出现这种情况吗?:)