【Spark机器学习速成宝典】模型篇06随机森林【Random Forests】(Python版)
目录
随机森林原理
随机森林代码(Spark Python)
随机森林原理 |
参考:http://www.cnblogs.com/itmorn/p/8269334.html
随机森林代码(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 RandomForest, RandomForestModel
from pyspark.mllib.util import MLUtils # Load and parse the data file into an RDD of LabeledPoint.
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 RandomForest model. 训练决策树模型
# Empty categoricalFeaturesInfo indicates all features are continuous. 空的categoricalFeaturesInfo意味着所有的特征都是连续的
# Note: Use larger numTrees in practice. 注意:在实践中可以使用更多棵树
# Setting featureSubsetStrategy="auto" lets the algorithm choose. featureSubsetStrategy="auto"的意思是让算法自己选择
model = RandomForest.trainClassifier(trainingData, numClasses=2, categoricalFeaturesInfo={},
numTrees=3, featureSubsetStrategy="auto",
impurity='gini', maxDepth=4, maxBins=32) # 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 forest model:')
print(model.toDebugString())
'''
TreeEnsembleModel classifier with 3 trees Tree 0:
If (feature 517 <= 116.0)
If (feature 489 <= 11.0)
If (feature 437 <= 218.0)
Predict: 0.0
Else (feature 437 > 218.0)
Predict: 1.0
Else (feature 489 > 11.0)
Predict: 1.0
Else (feature 517 > 116.0)
Predict: 1.0
Tree 1:
If (feature 456 <= 0.0)
If (feature 471 <= 0.0)
Predict: 1.0
Else (feature 471 > 0.0)
Predict: 0.0
Else (feature 456 > 0.0)
Predict: 0.0
Tree 2:
If (feature 377 <= 3.0)
If (feature 212 <= 253.0)
Predict: 0.0
Else (feature 212 > 253.0)
Predict: 1.0
Else (feature 377 > 3.0)
If (feature 299 <= 204.0)
Predict: 1.0
Else (feature 299 > 204.0)
Predict: 0.0
'''
# Save and load model
model.save(sc, "myRandomForestClassificationModel")
sameModel = RandomForestModel.load(sc, "myRandomForestClassificationModel")
print sameModel.predict(data.collect()[0].features) #0.0
【Spark机器学习速成宝典】模型篇06随机森林【Random Forests】(Python版)的更多相关文章
- 第九篇:随机森林(Random Forest)
前言 随机森林非常像<机器学习实践>里面提到过的那个AdaBoost算法,但区别在于它没有迭代,还有就是森林里的树长度不限制. 因为它是没有迭代过程的,不像AdaBoost那样需要迭代,不 ...
- 随机森林——Random Forests
[基础算法] Random Forests 2011 年 8 月 9 日 Random Forest(s),随机森林,又叫Random Trees[2][3],是一种由多棵决策树组合而成的联合预测模型 ...
- 【Spark机器学习速成宝典】模型篇08保序回归【Isotonic Regression】(Python版)
目录 保序回归原理 保序回归代码(Spark Python) 保序回归原理 待续... 返回目录 保序回归代码(Spark Python) 代码里数据:https://pan.baidu.com/s/ ...
- 【Spark机器学习速成宝典】模型篇07梯度提升树【Gradient-Boosted Trees】(Python版)
目录 梯度提升树原理 梯度提升树代码(Spark Python) 梯度提升树原理 待续... 返回目录 梯度提升树代码(Spark Python) 代码里数据:https://pan.baidu.co ...
- 【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 返回目录 支持向量 ...
随机推荐
- O005、远程管理 KVM 虚机
参考https://www.cnblogs.com/CloudMan6/p/5256018.html 上一节我们通过 virt-manager 在本地主机上创建并管理 KVM 虚机,其实 virt ...
- PHP--API
PHP所有能力都是函数,内置1000多个函数,不是每一个函数都默认直接可以使用,有一些需要安装或者启用额外的“插件”扩展. 1,获取字符串长度 <?php $str='hello'; echo ...
- React 长列表修改时避免全体渲染
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <script ...
- java apache-commons-collections中Map辅助类的使用
前言 apache-commons-collections中Map辅助类,很是有用.尽管我们通过原生Map经过业务逻辑处理也能达到相同的作用与效果,但毕竟作为一个开源的工具类辅助类,对它有个了解还是有 ...
- hive元数据库理解
在hive2.1.1 里面一共有59张表 表1 VERSION ; version表存hive的版本信息,该表中数据只有一条,如果存在多条,会造成hive启动不起来. 表2 DBS select * ...
- 点击切换JS
$(function(){ var tabnav = $("#tab-nav ul li"); tabnav.click(function(){ $(this).addClass( ...
- @Validated @RequestBody @RequestParam配合使用校验参数
1. @Validated @RequestBody 配合使用 两者搭配进行参数的校验,要想自己捕获该异常,需要自定义全局异常处理器 2. @Validated @RequestParam 配合使 ...
- java8学习之收集器用法详解与多级分组和分区
收集器用法详解: 在上次已经系统的阅读了Collector收集器的Javadoc对它已经有一个比较详细的认知了,但是!!!它毕境是只是一个接口,要使用的话还得用它的实现类,所以在Java8中有它进行了 ...
- python生成器并行实例
生成器并行实例: send发送值被yield接受到赋值给baozi变量 #yield作用只是在这里保存这个值的当前状态然后返回之后在调用next,又回到yield #单纯调用next不会给yield传 ...
- 二值图像连通分量的提取(python+opencv)
算法: 第一步,将图片转换为二值图像A 第二步,创建和A相同大小但是元素都为0的图像B,并复制A到A_copy中 第三步,A中任选一点值为255的像素,设为p1,并使用计算连通分量算法,当算法收敛时, ...