【Spark机器学习速成宝典】模型篇07梯度提升树【Gradient-Boosted Trees】(Python版)
目录
梯度提升树原理
梯度提升树代码(Spark Python)
|
梯度提升树原理 |
待续...
|
梯度提升树代码(Spark Python) |
代码里数据:https://pan.baidu.com/s/1jHWKG4I 密码:acq1
# -*-coding=utf-8 -*-
from pyspark import SparkConf, SparkContext
sc = SparkContext('local') from pyspark.mllib.tree import GradientBoostedTrees, GradientBoostedTreesModel
from pyspark.mllib.util import MLUtils # Load and parse the data file.
data = MLUtils.loadLibSVMFile(sc, "data/mllib/sample_libsvm_data.txt")
'''
每一行使用以下格式表示一个标记的稀疏特征向量
label index1:value1 index2:value2 ... tempFile.write(b"+1 1:1.0 3:2.0 5:3.0\\n-1\\n-1 2:4.0 4:5.0 6:6.0")
>>> tempFile.flush()
>>> examples = MLUtils.loadLibSVMFile(sc, tempFile.name).collect()
>>> tempFile.close()
>>> examples[0]
LabeledPoint(1.0, (6,[0,2,4],[1.0,2.0,3.0]))
>>> examples[1]
LabeledPoint(-1.0, (6,[],[]))
>>> examples[2]
LabeledPoint(-1.0, (6,[1,3,5],[4.0,5.0,6.0]))
'''
# Split the data into training and test sets (30% held out for testing) 分割数据集,留30%作为测试集
(trainingData, testData) = data.randomSplit([0.7, 0.3]) # Train a GradientBoostedTrees model. 训练决策树模型
# Notes: (a) Empty categoricalFeaturesInfo indicates all features are continuous. 空的categoricalFeaturesInfo意味着所有的特征都是连续的
# (b) Use more iterations in practice. 在实践中使用更多的迭代步数
model = GradientBoostedTrees.trainClassifier(trainingData,
categoricalFeaturesInfo={}, numIterations=30) # Evaluate model on test instances and compute test error 评估模型
predictions = model.predict(testData.map(lambda x: x.features))
labelsAndPredictions = testData.map(lambda lp: lp.label).zip(predictions)
testErr = labelsAndPredictions.filter(
lambda lp: lp[0] != lp[1]).count() / float(testData.count())
print('Test Error = ' + str(testErr)) #Test Error = 0.0
print('Learned classification GBT model:')
print(model.toDebugString())
'''
TreeEnsembleModel classifier with 30 trees Tree 0:
If (feature 434 <= 0.0)
If (feature 100 <= 165.0)
Predict: -1.0
Else (feature 100 > 165.0)
Predict: 1.0
Else (feature 434 > 0.0)
Predict: 1.0
Tree 1:
If (feature 490 <= 0.0)
If (feature 549 <= 253.0)
If (feature 184 <= 0.0)
Predict: -0.4768116880884702
Else (feature 184 > 0.0)
Predict: -0.47681168808847024
Else (feature 549 > 253.0)
Predict: 0.4768116880884694
Else (feature 490 > 0.0)
If (feature 215 <= 251.0)
Predict: 0.4768116880884701
Else (feature 215 > 251.0)
Predict: 0.4768116880884712
...
Tree 29:
If (feature 434 <= 0.0)
If (feature 209 <= 4.0)
Predict: 0.1335953290513215
Else (feature 209 > 4.0)
If (feature 372 <= 84.0)
Predict: -0.13359532905132146
Else (feature 372 > 84.0)
Predict: -0.1335953290513215
Else (feature 434 > 0.0)
Predict: 0.13359532905132146
'''
# Save and load model
model.save(sc, "myGradientBoostingClassificationModel")
sameModel = GradientBoostedTreesModel.load(sc,"myGradientBoostingClassificationModel")
print sameModel.predict(data.collect()[0].features) #0.0
【Spark机器学习速成宝典】模型篇07梯度提升树【Gradient-Boosted Trees】(Python版)的更多相关文章
- 梯度提升树 Gradient Boosting Decision Tree
Adaboost + CART 用 CART 决策树来作为 Adaboost 的基础学习器 但是问题在于,需要把决策树改成能接收带权样本输入的版本.(need: weighted DTree(D, u ...
- 机器学习(七)—Adaboost 和 梯度提升树GBDT
1.Adaboost算法原理,优缺点: 理论上任何学习器都可以用于Adaboost.但一般来说,使用最广泛的Adaboost弱学习器是决策树和神经网络.对于决策树,Adaboost分类用了CART分类 ...
- 【Spark机器学习速成宝典】模型篇08保序回归【Isotonic Regression】(Python版)
目录 保序回归原理 保序回归代码(Spark Python) 保序回归原理 待续... 返回目录 保序回归代码(Spark Python) 代码里数据:https://pan.baidu.com/s/ ...
- 【Spark机器学习速成宝典】模型篇06随机森林【Random Forests】(Python版)
目录 随机森林原理 随机森林代码(Spark Python) 随机森林原理 参考:http://www.cnblogs.com/itmorn/p/8269334.html 返回目录 随机森林代码(Sp ...
- 【Spark机器学习速成宝典】模型篇05决策树【Decision Tree】(Python版)
目录 决策树原理 决策树代码(Spark Python) 决策树原理 详见博文:http://www.cnblogs.com/itmorn/p/7918797.html 返回目录 决策树代码(Spar ...
- 【Spark机器学习速成宝典】模型篇04朴素贝叶斯【Naive Bayes】(Python版)
目录 朴素贝叶斯原理 朴素贝叶斯代码(Spark Python) 朴素贝叶斯原理 详见博文:http://www.cnblogs.com/itmorn/p/7905975.html 返回目录 朴素贝叶 ...
- 【Spark机器学习速成宝典】模型篇03线性回归【LR】(Python版)
目录 线性回归原理 线性回归代码(Spark Python) 线性回归原理 详见博文:http://www.cnblogs.com/itmorn/p/7873083.html 返回目录 线性回归代码( ...
- 【Spark机器学习速成宝典】模型篇02逻辑斯谛回归【Logistic回归】(Python版)
目录 Logistic回归原理 Logistic回归代码(Spark Python) Logistic回归原理 详见博文:http://www.cnblogs.com/itmorn/p/7890468 ...
- 【Spark机器学习速成宝典】模型篇01支持向量机【SVM】(Python版)
目录 支持向量机原理 支持向量机代码(Spark Python) 支持向量机原理 详见博文:http://www.cnblogs.com/itmorn/p/8011587.html 返回目录 支持向量 ...
随机推荐
- golang(6): 接口 & 反射
接口详解 // 举例:sort包中的 Sort 函数,如下: func Sort(data Interface) Sort sorts data. It makes one call to data. ...
- Kong/Konga - Docker容器化安装
1.0 安装kong + postgresDB docker network create kong-net docker pull postgres:latest docker run -d --n ...
- Visual Studio (VC) Win32 程序由于数据大,内存溢出怎么办?
Visual Studio (VC) 内编写的Win32 程序由于数据大,内存溢出,即使转移到64位系统也不行.在国外网站上找到了答案. 原来,只需在project->property中的Lin ...
- RAD Studio 10 up1欢迎页证书不可用
不只是欢迎页,每打开一个新的工程,都会出现上面那个证书不可用的提示. 解决方法: 1.通过Fiddler网络监控软件分析发现,出现这个问题的原因是bds启动的时候会用https协议访问谷歌的统计服务器 ...
- 腾讯地图JSAPI开发demo 定位,查询
1.IP定位切换 2.点击坐标获取地点 3.查询地点切换坐标 <!DOCTYPE html> <html> <head> <meta http-equiv=& ...
- postgres日常操作
1.启动pgsl数据库 [postgres@master ~]$ pg_ctl start [postgres@master data]$ pg_ctl -D /usr/local/pgsql/dat ...
- 关于windows下无法删除文件,需要TrueInstaller权限的问题
笔者办公室的笔记本今天突然弹出来一个ie浏览器,这不是为了下载其他浏览器而存在的浏览器吗?现在还臭不要脸的弹出来,然鹅我在删除文件夹的时候,提示我无法删除,必须要有TrueInstaller的权限,那 ...
- new一个有父类的对象时各代码块的执行顺序问题
public class QQ { public static void main(String[] args) { new B(); } } class A { static { System.ou ...
- PhpStorm中如何使用database工具,详细操作方法
1.简介: PhpStorm是一个轻量级且便捷的PHP IDE,其提供的智能代码补全,快速导航以及即时错误检查等功能大大提高了编码效率.它以其独特的开发便利性,短时间内赢得了大量PHPer的青睐. ...
- VMware Horizon Client剪贴板异常问题解决
接到用户反馈现象是:登录ERP系统操作是,无法复制粘贴本地电脑上的数据. 处理过程: 1.在域控服务器上建立独立的Horizon Computer OU,把所有RDS加入在改OU中 2.针对Horiz ...