Python+GBDT算法实战——预测实现100%准确率

在本文中,你将学习到以下内容:


  • GBDT算法实现
  • 模型保存
  • 模型加载及预测

前言

GBDT属于Boosting算法,它是利用损失函数的负梯度方向在当前模型的值作为残差的近似值,进而拟合一棵CART回归树。GBDT的会累加所有树的结果,而这种累加是无法通过分类完成的,因此GBDT的树都是CART回归树,而不是分类树(尽管GBDT调整后也可以用于分类但不代表GBDT的树为分类树)。本文就是利用GBDT算法实现一个例子。

数据说明

新能源汽车充电桩的故障检测问题,提供85500条训练数据(标签:0代表充电桩正常,1代表充电桩有故障),参赛者需对36644条测试数据进行预测。

训练数据

数据文件:data_train.csv
字段说明:

《Python+GBDT算法实战——预测实现100%准确率》

测试数据

数据文件:data_test.csv
字段说明:

《Python+GBDT算法实战——预测实现100%准确率》

GBDT算法

导入所需库:

 

 

1

2

3

4

import pandas as pd

from sklearn.model_selection import train_test_split

from sklearn.ensemble import GradientBoostingClassifier

from sklearn.externals import joblib

 

通过pandas读取csv文件,GDBT算法直接从sklearn.ensemble调用,导入joblib函数保存模型。
接着读取文件,去除id和label标签项,并把数据分成训练集和验证集:

 

 

1

2

3

4

5

6

7

8

data = pd.read_csv(r"./data_train.csv")

x_columns = []

for x in data.columns:

    if x not in ['id', 'label']:

        x_columns.append(x)

X = data[x_columns]

y = data['label']

x_train, x_test, y_train, y_test = train_test_split(X, y)

 

这里采用默认划分比例,即75%数据作为训练集,25%作为预测集。
接下来调用GBDT算法:

 

 

1

2

3

4

# 模型训练,使用GBDT算法

gbr = GradientBoostingClassifier(n_estimators=3000, max_depth=2, min_samples_split=2, learning_rate=0.1)

gbr.fit(x_train, y_train.ravel())

joblib.dump(gbr, 'train_model_result4.m')   # 保存模型

 

GBDT算法参数设置如上,也可以通过网格搜索寻找最优参数设置,这里不赘述。模型train_model_result4.m保存在当前目录下。
最后我们打印训练和验证的准确率:

 

 

1

2

3

4

5

6

y_gbr = gbr.predict(x_train)

y_gbr1 = gbr.predict(x_test)

acc_train = gbr.score(x_train, y_train)

acc_test = gbr.score(x_test, y_test)

print(acc_train)

print(acc_test)

 

我们的模型对于训练和预测都达到了100%的准确率:

《Python+GBDT算法实战——预测实现100%准确率》

训练和验证准确率

完整代码如下:

 

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

import pandas as pd

from sklearn.model_selection import train_test_split

from sklearn.ensemble import GradientBoostingClassifier

from sklearn.externals import joblib

 

 

data = pd.read_csv(r"./data_train.csv")

x_columns = []

for x in data.columns:

    if x not in ['id', 'label']:

        x_columns.append(x)

X = data[x_columns]

y = data['label']

x_train, x_test, y_train, y_test = train_test_split(X, y)

 

# 模型训练,使用GBDT算法

gbr = GradientBoostingClassifier(n_estimators=3000, max_depth=2, min_samples_split=2, learning_rate=0.1)

gbr.fit(x_train, y_train.ravel())

joblib.dump(gbr, 'train_model_result4.m')   # 保存模型

 

y_gbr = gbr.predict(x_train)

y_gbr1 = gbr.predict(x_test)

acc_train = gbr.score(x_train, y_train)

acc_test = gbr.score(x_test, y_test)

print(acc_train)

print(acc_test)

 

模型预测

在训练和验证集上,我们的模型都达到了100%的准确率,接下来用模型预测测试集的结果。
代码如下:

 

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

import numpy as np

import pandas as pd

from sklearn.externals import joblib

 

 

# 加载模型并预测

gbr = joblib.load('train_model_result4.m')    # 加载模型

test_data = pd.read_csv(r"./data_test.csv")

testx_columns = []

for xx in test_data.columns:

    if xx not in ['id', 'label']:

        testx_columns.append(xx)

test_x = test_data[testx_columns]

test_y = gbr.predict(test_x)

test_y = np.reshape(test_y, (36644, 1))

 

# 保存预测结果

df = pd.DataFrame()

df['id'] = test_data['id']

df['label'] = test_y

df.to_csv("./data_predict.csv", header=None, index=None)

 

最终我们将结果保存在data_predict.csv提交比赛作业。该结果准确率100%。
我们通过一个具体的项目实现GBDT算法的训练及预测过程,本文所需资料我都放在这里, 密码:47v5,希望对你们有帮助。

来源:https://finthon.com/python-gbdt-prediction/

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值