【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 返回目录 支持向量 ...
随机推荐
- O013、动手实践Linux VLAN
参考https://www.cnblogs.com/CloudMan6/p/5326737.html 本节我们来看如何在实验环境中实施和部署如下的VLAN 网络
- python 安装时,为何pip install不是内部或者外部命令错误解决办法
新安装的python 环境,第一次pip install 却报不是内部或者外部命令错误 首先检查一下环境变量,可能时你没有设置环境变量 再说一遍,安装python环境时,记得出了python.exe ...
- dedecms 列表 用分页标签 判断 当第一页则显示,第二页以上不显示 土办法!
arc.listview.class.php function GetPageListST($list_len,$listitem="index,end,pre,next,pageno,sp ...
- cmd拷贝文件夹时,处理提示
xcopy 若目标盘上不存在此子目录,而在目标盘的结束符又不以"\"为结束,则将提示: does destination specify a file name or direct ...
- AttributeError: module 'scipy.misc' has no attribute 'imread'
运行python程序报错:AttributeError: module 'scipy.misc' has no attribute 'imread' 报错原因1:scipy版本过高 解决方案:降低sc ...
- backtop返回页面顶部jquery代码
<div id="toTop" style="width:30px;height:110px;border:1px solid #74B9DC; border-ra ...
- anaconda安装失败
2019.10版本怎么安装都不行换了2018.10安装ok
- 我说CMMI之二:CMMI里有什么?--转载
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/dylanren/article/deta ...
- JavaWEB开发05_Bootstrap
上次课内容:什么JQ : write less do more 写更少的代码,做更多的事情 javascript函数库 基本选择器: ID选择器: #ID名称 类选择器: .类名 元素选择 ...
- Bash快捷操作
编辑命令 Ctrl + a :移到命令行首 Ctrl + e :移到命令行尾 Ctrl + f :按字符前移(右向) Ctrl + b :按字符后移(左向) Alt + f :按单词前移(右向) Al ...