【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 返回目录 支持向量 ...
随机推荐
- 07 MySQL之索引原理
一.介绍 为什么有索引:使用索引可快速访问数据库表中的特定信息.索引是对数据库表中一列或多列的值进行排序的一种结构. 作用: 1. 快速查询数据 2. 保证数据的唯一性 3 ...
- Javascript问题集锦
1.Date.parse()函数兼容性问题: IE Chrome Firefox Date.parse("07-17-2019") 1563292800000 15632928 ...
- js之运算符(算术运算符)
Javascript中的运算符大多是由标点符号少数由关键字表示.可以根据其操作数的个数进行分类.大多数运算符是一个二元运算符,将两个表达式合成一个比较复杂的表达式.还有需要注意的一点是运算符的优先级, ...
- Axure(二)
回顾1.Axure 动态面板 图片转换 画面滚动2.使用元件 --> page box 盒子 width height ...
- Session的生命周期和工作原理
一.什么是Session,如何使用?Session是用于存放用户与web服务器之间的会话,即服务器为客户端开辟的存储空间. 由于客户端与服务器之间的会话是无状态的机制,Session则可用于关联访问, ...
- MySQL配置文件my.cnf中文详解
#BEGIN CONFIG INFO #DESCR: 4GB RAM, 只使用InnoDB, ACID, 少量的连接, 队列负载大 #TYPE: SYSTEM #END CONFIG INFO # # ...
- 如何删掉git版本库master分支的第一个commit
这个操作会将库清空,一般来说在建库开始的时候操作. 适用场景: git init初始化版本库之后,提交第一个点之后发现这个点出问题了,但是此时内心如果有洁癖的话, 你会觉得不完美,很想把这个点干掉重来 ...
- zencart模板列表下载地址
下载index.html文件后用浏览器打开,里面有一百多个zencart模板示例 下载地址:zencart模板示例下载地址 或者复制下面网址,用浏览器打开即可下载: http://bcs.duapp. ...
- 【CERC 2014 E】2048
题意 2048曾经是一款风靡全球的小游戏. 今天,我们换一种方式来玩这个小游戏. 你有一个双端队列,你只能把元素从左端或从右端放入双端队列中.一旦放入就不得取出.放入后,若队列中有连续两个相同的元素, ...
- Spring-data-jpa操作数据库环境配置
application.xml文件 <?xml version="1.0" encoding="UTF-8"?><beans xmlns=&q ...