python XGBoost 分类 代码

python XGBoost 分类 代码

安装xgboost

pip install xgboost

分类代码

#XGBoost分类

import pandas as pd
from xgboost.sklearn import XGBClassifier  # 导入XGBClassifier函数
from sklearn.model_selection import train_test_split
from sklearn import metrics  # 分类结果评价函数

data = pd.read_csv('./wm.csv', index_col=False, encoding='gb18030')
print(data)

'''   
    y    x1    x2    x3
0   1  0.697  0.460   0
1   1  0.774  0.376   0
2   1  0.634  0.264   0
3   1  0.608  0.318   0
4   1  0.556  0.215   0
5   1  0.403  0.237   1
6   1  0.481  0.149   1
7   1  0.437  0.211   1
8   0  0.666  0.091   1
9   0  0.243  0.267   2
10  0  0.245  0.057   2
11  0  0.343  0.099   2
12  0  0.639  0.161   0
13  0  0.657  0.198   0
14  0  0.360  0.370   1
15  0  0.593  0.042   2
16  0  0.719  0.103   1
'''

x = data[['x1', 'x2', 'x3']]  # 特征
y = data['y']  # 标签

x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=0, train_size=0.8)  # 划分数据集
print('训练集和测试集 shape', x_train.shape, y_train.shape, x_test.shape, y_test.shape)

model = XGBClassifier() # 实例化模型XGBClassifier()
model.fit(x_train, y_train)  # 在训练集上训练模型
print(model)  # 输出模型XGBClassifier()

# 在测试集上测试模型
expected = y_test  # 测试样本的期望输出
predicted = model.predict(x_test)  # 测试样本预测

# 输出结果
print(metrics.classification_report(expected, predicted))  # 输出结果,精确度、召回率、f-1分数
print(metrics.confusion_matrix(expected, predicted))  # 混淆矩阵

auc = metrics.roc_auc_score(y_test, predicted)
accuracy = metrics.accuracy_score(y_test, predicted)  # 求精度
print("Accuracy: %.2f%%" % (accuracy * 100.0))

  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值