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

0.4字数 609阅读 8031

-- 全文阅读3分钟 --

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

#### 前言

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

#### GBDT算法

``````import pandas as pd
from sklearn.model_selection import train_test_split
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')   # 保存模型
``````

GBDT算法参数设置如上，也可以通过网格搜索寻找最优参数设置，这里不赘述。模型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)
``````

``````import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.externals import joblib

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)
``````

#### 模型预测

``````import numpy as np
import pandas as pd
from sklearn.externals import joblib

# 加载模型并预测
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