1. Package
import logger
import joblib
import lightgbm as lgb
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import mean_squared_error, mean_absolute_erro
2. Regression 指标
1. MAPE
def mean_absolute_percentage_error(y_true, y_pred):
return np.mean(np.abs((y_true - y_pred) / (y_true))) * 100
2. MSE、MAE
3. 训练
1. 直接fit
X_train, X_test, y_train, y_test = train_test_split(df_train_org_other_x,
df_train_org_other_y,
test_size=0.2,
random_state=999)
# train
gbm = lgb.LGBMRegressor(objective='regression',
metric='rmse',
max_depth=-1,
num_leaves=40,
learning_rate=0.08,
feature_fraction=0.7,
min_child_samples=21,
min_child_weight=0.001,
bagging_fraction=1,
bagging_freq=2,
reg_alpha=0.001,
reg_lambda=8,
cat_smooth=0,
alias=200,
n_jobs=-1,
silent=True,
verbose=-1,
tree_method='gpu_hist'
)
logger.info('训练开始model {0} X_train shape{1}, X_test shape{2}'.format(model_name, X_train.shape, X_test.shape))
#fit
model = gbm.fit(X_train,y_train)
# save model
d1 = datetime.date.today()
dates = d1.isoformat()
joblib.dump(other_model, '../online_model1221/{0}_{1}_gbm.pkl'.format(model_name, dates))
2. GridSearch
# 参数
parameters = {
'max_depth': [6, 8, 10, 12],
'num_leaves': [40, 60, 80, 100],
'learning_rate': [0.04, 0.06, 0.08, 0.1],
'reg_alpha': [0.03, 0.08, 0.3, 0.5],
'reg_lambda': [0.03, 0.08, 0.3, 0.5]
}
#GridSearch
gsearch = GridSearchCV(gbm, param_grid=parameters, cv=10)
gsearch.fit(X_train, y_train)
logger.info('参数的最佳取值:{0}'.format(gsearch.best_params_))
logger.info('最佳模型得分:{0}'.format(gsearch.best_score_))
lgbcv_model = gsearch.best_estimator_
# save model
d1 = datetime.date.today()
dates = d1.isoformat()
joblib.dump(lgbcv_model, '../online_model1221/{0}_{1}_gbm.pkl'.format(model_name, dates))
4. Model importance
lgv_model = joblib.load('../online_model/{0}_2021-08-23_gbm.pkl'.format(model_name))
logger.info('加载模型')
logger.info(model_name)
# feature importance
feature_ls = train_data_total_x.columns.tolist()
model_imp = lgv_model.feature_importances_.tolist()
feature_weight = pd.DataFrame( [feature_ls, model_imp]).T
feature_weight.columns = ['feature_name', 'weight']
feature_weight['model_name'] = model_name
feature_weight.sort_values(by='weight',ascending=0, inplace=True )