LightGBM是一種使用基于樹的學(xué)習(xí)算法的梯度提升框架声诸。相比XGBoost速度更快酱讶,結(jié)果也相近。
使用交叉驗證彼乌,以f1為評價方法的baseline:
#!/usr/bin/env python
# _*_ coding:utf-8 _*_
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from sklearn.externals import joblib
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import f1_score
import lightgbm as lgb
import warnings
warnings.filterwarnings('ignore')
pd.set_option('display.max_columns', 500, 'display.width', 1000) # 設(shè)置顯示寬度
# data_train = pd.read_csv("train/train.csv")
data_train = pd.read_csv("train_3.csv") # 訓(xùn)練集
data_test = pd.read_csv("test_3.csv") # 測試集
# 對current_service映射編碼泻肯,對要分類的label不是從0開始的一般操作
label2current_service = dict(zip(range(0, len(set(data_train['current_service']))),
sorted(list(set(data_train['current_service'])))))
current_service2label = dict(zip(sorted(list(set(data_train['current_service']))),
range(0, len(set(data_train['current_service'])))))
data_train['current_service'] = data_train['current_service'].map(current_service2label)
# print(len(set(data_train['current_service']))) # 15種類型
y = data_train.pop('current_service')
user_id = data_train.pop('user_id')
x_train = data_train
test_user_id = data_test.pop('user_id')
x_test = data_test
print(x_train.info())
X, y, X_test = x_train.values, y.values, x_test.values # 轉(zhuǎn)為np.array類型
n_splits = 5 # 分為5折
seed = 2333 # 隨機(jī)種子
# lgb 參數(shù)
lgb_params = {
"learning_rate": 0.05,
"lambda_l1": 0.1,
"lambda_l2": 0.2,
"max_depth": 7,
"num_leaves": 120,
"objective": "multiclass",
"num_class": 15,
"verbose": -1,
'feature_fraction': 0.8,
"min_split_gain": 0.1,
"boosting_type": "gbdt",
"subsample": 0.8,
"min_data_in_leaf": 50,
"colsample_bytree": 0.7,
"colsample_bylevel": 0.7,
"tree_method": 'exact'
}
# 采取k折模型方案
# 自定義F1評價函數(shù)
def f1_score_vail(pred, data_vail):
labels = data_vail.get_label()
pred = np.argmax(pred.reshape(15, -1), axis=0) # lgb的predict輸出為各類型概率值
score_vail = f1_score(y_true=labels, y_pred=pred, average='macro')
return 'f1_score', score_vail, True
x_score = [] # 交叉驗證各折的f1值
cv_pred = [] # 各折的預(yù)測值
skf = StratifiedKFold(n_splits=n_splits, random_state=seed, shuffle=True)
# 交叉驗證
for index, (train_index, test_index) in enumerate(skf.split(X, y)):
print(index)
X_train, X_valid, y_train, y_valid = X[train_index], X[test_index], y[train_index], y[test_index]
train_data = lgb.Dataset(X_train, label=y_train) # 訓(xùn)練數(shù)據(jù)
validation_data = lgb.Dataset(X_valid, label=y_valid) # 驗證數(shù)據(jù)
clf = lgb.train(lgb_params, train_data, num_boost_round=150000, valid_sets=[validation_data],
early_stopping_rounds=100, feval=f1_score_vail, verbose_eval=1) # 訓(xùn)練
# clf = joblib.load("model/lgb_{}.m".format(index)) # 保存模型
# joblib.dump(clf, "model/lgb_{}.m".format(index)) # 加載模型
x_pred = clf.predict(X_valid, num_iteration=clf.best_iteration)
x_pred = [np.argmax(x) for x in x_pred]
x_score.append(f1_score(y_valid, x_pred, average='macro')) # 計算f1值
y_test = clf.predict(X_test, num_iteration=clf.best_iteration) # 預(yù)測
y_test = [np.argmax(x) for x in y_test]
if index == 0:
cv_pred = np.array(y_test).reshape(-1, 1)
else:
cv_pred = np.hstack((cv_pred, np.array(y_test).reshape(-1, 1)))
if index == 4:
lgb.plot_importance(clf, figsize=(50, 50)) # 畫出重要特征
plt.title("Feature_importance")
plt.show()
# 投票
submit = []
for line in cv_pred:
submit.append(np.argmax(np.bincount(line)))
# 保存結(jié)果
df_test = pd.DataFrame()
df_test['id'] = list(test_user_id.unique())
df_test['predict'] = submit
df_test['predict'] = df_test['predict'].map(label2current_service)
df_test.to_csv('output/lgb4.csv', index=False)
print(x_score, np.mean(x_score))