【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 返回目录 支持向量 ...
随机推荐
- Centos7:dubbo监控中心安装,配置和使用
制作dubbo-admin.war文件 下载dubbo-admin https://github.com/alibaba/dubbo 注:2.6版本后源码中不包含dubbo-admin工程 在dubb ...
- Windows常用快捷键与常用命令
应用窗口: Alt+F4 关闭当前窗口Win+上 最大化当前窗口Win+下 最小化当前窗口WIN+D 最小化所有窗口/还原Win+Tab 切换窗口 常用工具: Win+R 打开运行对话框Win+E 打 ...
- symfony3 使用命令行工具生成Entity实体所踩的坑
1.把配置文件汇总连接邮箱的配置信息注释掉了,在创建Entity时php bin/console doctrine:generate:entity报错 2. 错误原因是实体文件映射到数据库中的字段时候 ...
- 全局捕获异常(适用于SpringMvc,SpringBoot项目)
@ControllerAdvice 是controller的一个辅助类,最常用的就是作为全局异常处理的切面类.约定了几种可行的返回值,可以返回String字符串,也可以返回ModelAndView,也 ...
- centos 7 安装 Oracle 12c
#!/bin/bash #!/usr/bin/expect -f #/etc/sysctl.conf --bash-srcipts-- echo 'net.ipv6.conf.all.disable_ ...
- 2019.9.25使用BP和Hydra爆破相关的服务
使用BP和Hydra爆破相关的服务. Hydra:九头蛇,开源的功能强大的爆破工具,支持的服务有很多,使用hydra爆破c/s架构的服务.使用bp爆破web登录端口. dvwa:web应用程序漏洞演练 ...
- mongo启动报错问题处理
关键错误信息child process failed, exited with error number 100 这是服务器断电导致数据库意外关闭导致的问题,处理方法也比较简单 rm -rf /var ...
- 常用sql:按照表中的某一列对数据进行分组,统计数据条数
select FROM_UNIXTIME(start_time,'%Y-%m-%d')as date,COUNT(*) FROM random_num GROUP BY FROM_UNIXTIME(s ...
- RWD(Responsive Web Design)(转)
The key point is adapting to the user’s needs and device capabilities. Suppose a mobile user will be ...
- 零拷贝的原理及Java实现
在谈论Kafka高性能时不得不提到零拷贝.Kafka通过采用零拷贝大大提供了应用性能,减少了内核和用户模式之间的上下文切换次数.那么什么是零拷贝,如何实现零拷贝呢? 什么是零拷贝 WIKI中对其有如下 ...