<jupyter_start><jupyter_text>调整树的参数：subsample 和 colsample_bytree(粗调，参数的步长为0.1；下一步是在粗调最佳参数周围，将步长降为0.05，进行精细调整)<jupyter_code>from xgboost import XGBClassifier
import xgboost as xgb

import pandas as pd 
import numpy as np

from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import StratifiedKFold

from sklearn.metrics import log_loss

from matplotlib import pyplot
import seaborn as sns
%matplotlib inline<jupyter_output><empty_output><jupyter_text>读取数据<jupyter_code># path to where the data lies
dpath = './data/'
train = pd.read_csv(dpath +"RentListingInquries_FE_train.csv")
# train.head()<jupyter_output><empty_output><jupyter_text>准备数据<jupyter_code>y_train = train['interest_level']

train = train.drop(['interest_level'], axis=1, inplace = False)
X_train = train
# 训练样本6w+，交叉验证太慢，用train_test_split估计模型性能
from sklearn.model_selection import train_test_split
X_train_part, X_val, y_train_part, y_val = train_test_split(X_train, y_train, train_size = 0.2,random_state = 0)
X_train_part.shape<jupyter_output><empty_output><jupyter_text>各类样本不均衡，交叉验证是采用StratifiedKFold，在每折采样时各类样本按比例采样<jupyter_code># prepare cross validation
kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=3)<jupyter_output><empty_output><jupyter_text>第二轮参数调整得到的n_estimators最优值（104），其余参数继续默认值<jupyter_code>#max_depth 建议3-10， min_child_weight=1／sqrt(ratio_rare_event) =5.5
subsample = [i/10.0 for i in range(3,9)]
colsample_bytree = [i/10.0 for i in range(6,10)]
param_test3_1 = dict(subsample=subsample, colsample_bytree=colsample_bytree)
param_test3_1
xgb3_1 = XGBClassifier(
        learning_rate =0.1,
        n_estimators=103,  #第二轮参数调整得到的n_estimators最优值
        max_depth=6,
        min_child_weight=3,
        gamma=0,
        subsample=0.5,
        colsample_bytree=0.8,
        colsample_bylevel = 0.7,
        objective= 'multi:softprob',
        seed=3)


gsearch3_1 = GridSearchCV(xgb3_1, param_grid = param_test3_1, scoring='neg_log_loss',n_jobs=-1, cv=kfold)
gsearch3_1.fit(X_train_part , y_train_part)

gsearch3_1.grid_scores_, gsearch3_1.best_params_,     gsearch3_1.best_score_
gsearch3_1.cv_results_
# summarize results
print("Best: %f using %s" % (-gsearch3_1.best_score_, gsearch3_1.best_params_))
test_means = gsearch3_1.cv_results_[ 'mean_test_score' ]
test_stds = gsearch3_1.cv_results_[ 'std_test_score' ]
train_means = gsearch3_1.cv_results_[ 'mean_train_score' ]
train_stds = gsearch3_1.cv_results_[ 'std_train_score' ]

pd.DataFrame(gsearch3_1.cv_results_).to_csv('my_preds_subsampleh_colsample_bytree_1.csv')

# plot results
test_scores = np.array(test_means).reshape(len(colsample_bytree), len(subsample))
train_scores = np.array(train_means).reshape(len(colsample_bytree), len(subsample))

for i, value in enumerate(colsample_bytree):
    pyplot.plot(subsample, -test_scores[i], label= 'test_colsample_bytree:'   + str(value))
# for i, value in enumerate(colsample_bytree):
#     pyplot.plot(subsample, -train_scores[i], label= 'train_colsample_bytree:'   + str(value))
    
pyplot.legend()
pyplot.xlabel( 'subsample' )                                                                                                      
pyplot.ylabel( 'Log Loss' )
pyplot.savefig( 'subsample_vs_colsample_bytree1.png' )<jupyter_output>Best: -0.621183 using {'colsample_bytree': 0.8, 'subsample': 0.7}
{'fraction_non_alphanumeric': 0.10656231186032511, 'fraction_numerical': 0.029199277543648403, 'mean_word_length': 5.554240631163708, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '32879', 'license': 'MIT', 'n_tokens_mistral': 1390, 'n_tokens_neox': 1404, 'n_words': 193, 'path': '5_xgboost_Rental_subsample_colsample.ipynb', 'repo_name': 'ice-melt/rent_listing_inquries', 'size': '3322'}