sklearn与GBDT入门案例

来源:互联网 发布:下载布鲁帝同志软件 编辑:程序博客网 时间:2024/05/16 03:14
GBDT概念自行网上搜索下,下面入门调用sklearn包中的GBDT

安装

SCIKIT-LEARN是一个基于Python/numpy/scipy的机器学习库

GBDT使用

这段代码展示了一个简单的GBDT调用过程

import numpy as npfrom sklearn.ensemble import GradientBoostingRegressorgbdt=GradientBoostingRegressor(  loss='ls', learning_rate=0.1, n_estimators=100, subsample=1, min_samples_split=2, min_samples_leaf=1, max_depth=3, init=None, random_state=None, max_features=None, alpha=0.9, verbose=0, max_leaf_nodes=None, warm_start=False)train_feat=np.genfromtxt("train_feat.txt",dtype=np.float32)train_id=np.genfromtxt("train_id.txt",dtype=np.float32)test_feat=np.genfromtxt("test_feat.txt",dtype=np.float32)test_id=np.genfromtxt("test_id.txt",dtype=np.float32)print train_feat.shape,rain_id.shape,est_feat.shape,est_id.shapegbdt.fit(train_feat,train_id)pred=gbdt.predict(test_feat)total_err=0for i in range(pred.shape[0]):    print pred[i],test_id[i]    err=(pred[i]-test_id[i])/test_id[i]    total_err+=err*errprint total_err/pred.shape[0]

train_id.txt示例
320
361
364
336
358

train_feat.txt示例
0.00598802 0.569231 0.647059 0.95122 -0.225434 0.837989 0.357258 -0.0030581 -0.383475
0.161677 0.743195 0.682353 0.960976 -0.0867052 0.780527 0.282945 0.149847 -0.0529661
0.113772 0.744379 0.541176 0.990244 -0.00578035 0.721468 0.43411 -0.318043 0.288136
0.0538922 0.608284 0.764706 0.95122 -0.248555 0.821229 0.848604 -0.0030581 0.239407
0.173653 0.866272 0.682353 0.95122 0.017341 0.704709 -0.0210016 -0.195719 0.150424

测试集我使用一样的数据
test_id.txt示例
320
361
364
336
358

test_feat.txt示例
0.00598802 0.569231 0.647059 0.95122 -0.225434 0.837989 0.357258 -0.0030581 -0.383475
0.161677 0.743195 0.682353 0.960976 -0.0867052 0.780527 0.282945 0.149847 -0.0529661
0.113772 0.744379 0.541176 0.990244 -0.00578035 0.721468 0.43411 -0.318043 0.288136
0.0538922 0.608284 0.764706 0.95122 -0.248555 0.821229 0.848604 -0.0030581 0.239407
0.173653 0.866272 0.682353 0.95122 0.017341 0.704709 -0.0210016 -0.195719 0.150424

测试结果与真值:
320.000817398 320.0
360.999648447 361.0
363.999282781 364.0
336.000234432 336.0
358.000016939 358.0