xgboost机器学习python库的使用:
- 读取csv为pandas数组结构:
pd.read_csv(data_path, names=columns, dtype={"uid": "string", "date": "string"})
X = pd.DataFrame(data.values[:, 0:index], columns=x_columns) # index为标签所在的列 特征
Y = pd.DataFrame(data.values[:, index], columns=y_columns) # index为标签所在的列 标签
- 对数据进行填充默认值,并标记数据类型:
X["uid"] = X["uid"].astype("category")
X["yy"] = X["yy"].fillna(value=0).astype("int")
Y["xx"] = Y["xx"].fillna(value=0).astype("int")
X = X.drop(columns=['date', 'uid'])
- 训练模型:
二分类问题
other_params = {'eta': 0.3, 'n_estimators': 500, 'gamma': 0, 'max_depth': 6, 'min_child_weight': 1,
'colsample_bytree': 1, 'colsample_bylevel': 1, 'subsample': 1, 'reg_lambda': 1, 'reg_alpha': 0,
'seed': 33,'scale_pos_weight': 7.88} # scale_pos_weight = count(negative examples) / count(positive examples)
model = XGBClassifier(**other_params)
self.model.fit(X_train, y_train)
多分类问题
other_params = {'eta': 0.3, 'n_estimators': 500, 'gamma': 0, 'max_depth': 6, 'min_child_weight': 1,
'colsample_bytree': 1, 'colsample_bylevel': 1, 'subsample': 1, 'reg_lambda': 1, 'reg_alpha': 0,
'seed': 33
'booster': 'gbtree', 'objective': 'multi:softprob', 'num_class': 3}
model = XGBClassifier(**other_params)
sample_weights = class_weight.compute_sample_weight(
class_weight='balanced',
y=y_train
)
# sample_weights = None
self.model.fit(X_train, y_train, sample_weight=sample_weights)
- 预测:
y_pred = model.predict(X_test)
- 模型准确率召回率评分
多分类问题
test_predictions = [round(value) for value in y_pred]
print(classification_report(y_test, test_predictions))
print('Model Score:', model.score(X_train.drop(columns=drop_columns), y_train))
二分类问题
test_accuracy = accuracy_score(y_test, test_predictions)
test_recall = recall_score(y_test, test_predictions)
print('Test auc: ', roc_auc_score(y_test, test_predictions))
print("Test Accuracy【准确率】: %.2f%%" % (test_accuracy * 100.0))
print("Test Recall【召回率】: %.2f%%" % (test_recall * 100.0))
fpr, tpr, thresholds = roc_curve(np.array(y_test), test_predictions)
print('Test KS:%.2f%%' % (max(tpr - fpr) * 100))
print('Test precision:', precision_score(y_test, test_predictions) * 100)
print('Test F1:', f1_score(y_test, test_predictions))
print('Model Score:', model.score(X_test.drop(columns=['column_a', 'column_b']), y_test))
print(classification_report(y_test, test_predictions))
- 网格搜索模型参数调优:
cv_params = {'n_estimators': np.linspace(100, 1000, 10, dtype=int)}
gs = GridSearchCV(model, cv_params, verbose=2, refit=True, cv=5, n_jobs=-1)
gs.fit(X, Y)
print("参数的最佳取值::", gs.best_params_)
print("最佳模型得分:", gs.best_score_)
顺序:n_estimators=》max_depth=》min_child_weight=》gamma=》subsample=》colsample_bytree=》reg_lambda=》reg_alpha=》eta
- 预测结果合并原始测试数据:
y_pred_pb = pd.DataFrame(y_pred, columns=y_columns)
x_columns_test = b_X_test.columns
x_test_v = b_X_test.values
x_test_v = pd.DataFrame(x_test_v, columns=x_columns_test)
x_test_v = pd.concat([x_test_v, y_pred_pb], axis=1)
x_test_v = x_test_v[columns]