目录

  随机森林原理

  随机森林代码(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版)的更多相关文章

  1. 第九篇:随机森林(Random Forest)

    前言 随机森林非常像<机器学习实践>里面提到过的那个AdaBoost算法,但区别在于它没有迭代,还有就是森林里的树长度不限制. 因为它是没有迭代过程的,不像AdaBoost那样需要迭代,不 ...

  2. 随机森林——Random Forests

    [基础算法] Random Forests 2011 年 8 月 9 日 Random Forest(s),随机森林,又叫Random Trees[2][3],是一种由多棵决策树组合而成的联合预测模型 ...

  3. 【Spark机器学习速成宝典】模型篇08保序回归【Isotonic Regression】(Python版)

    目录 保序回归原理 保序回归代码(Spark Python) 保序回归原理 待续... 返回目录 保序回归代码(Spark Python) 代码里数据:https://pan.baidu.com/s/ ...

  4. 【Spark机器学习速成宝典】模型篇07梯度提升树【Gradient-Boosted Trees】(Python版)

    目录 梯度提升树原理 梯度提升树代码(Spark Python) 梯度提升树原理 待续... 返回目录 梯度提升树代码(Spark Python) 代码里数据:https://pan.baidu.co ...

  5. 【Spark机器学习速成宝典】模型篇05决策树【Decision Tree】(Python版)

    目录 决策树原理 决策树代码(Spark Python) 决策树原理 详见博文:http://www.cnblogs.com/itmorn/p/7918797.html 返回目录 决策树代码(Spar ...

  6. 【Spark机器学习速成宝典】模型篇04朴素贝叶斯【Naive Bayes】(Python版)

    目录 朴素贝叶斯原理 朴素贝叶斯代码(Spark Python) 朴素贝叶斯原理 详见博文:http://www.cnblogs.com/itmorn/p/7905975.html 返回目录 朴素贝叶 ...

  7. 【Spark机器学习速成宝典】模型篇03线性回归【LR】(Python版)

    目录 线性回归原理 线性回归代码(Spark Python) 线性回归原理 详见博文:http://www.cnblogs.com/itmorn/p/7873083.html 返回目录 线性回归代码( ...

  8. 【Spark机器学习速成宝典】模型篇02逻辑斯谛回归【Logistic回归】(Python版)

    目录 Logistic回归原理 Logistic回归代码(Spark Python) Logistic回归原理 详见博文:http://www.cnblogs.com/itmorn/p/7890468 ...

  9. 【Spark机器学习速成宝典】模型篇01支持向量机【SVM】(Python版)

    目录 支持向量机原理 支持向量机代码(Spark Python) 支持向量机原理 详见博文:http://www.cnblogs.com/itmorn/p/8011587.html 返回目录 支持向量 ...

随机推荐

  1. luogu P5366 [SNOI2017]遗失的答案

    luogu 首先gcd为\(G\),lcm为\(L\),有可能出现的数(指同时是\(G\)的因数以及是\(L\)的倍数)可以发现只有几百个.如果选出的数要能取到gcd,那么对于每种质因子,都要有一个数 ...

  2. 分布式的几件小事(四)dubbo负载均衡策略和集群容错策略

    1.dubbo负载均衡策略 ①random loadbalance 策略 默认情况下,dubbo是random loadbalance 随机调用实现负载均衡,可以对provider不同实例设置不同的权 ...

  3. OpenSSL源码简介

    1.X.509标准 x509是由国际电信联盟(ITU-T)制定的数字证书标准:包含公钥和用户标志符.CA等: x509是数字证书的规范,P7和P12是两种封装形式:X.509是常见通用的证书格式.所有 ...

  4. hdfs 配置文件详解

    – dfs.name.dir – NameNode 元数据存放位置 – 默认值:使用core-site.xml中的hadoop.tmp.dir/dfs/name – dfs.block.size –  ...

  5. linux下如何查询jdk的安装路径

    首先这个问题困扰了我很久,由于是新装的系统还不熟悉,配置java环境变量时很头疼,找不到JDK位置,还好google一波,发现了这个方法. 首先确保安装了JDK java -version java ...

  6. 14、Nginx四层负载均衡

    1.Nginx四层负载均衡基本概述 1.1.什么是四层负载均衡 四层负载均衡基于传输层协议包来封装的(如:TCP/IP),那我们前面使用到的七层是指的应用层,它的组装在四层基础之上,无论四层还是七层都 ...

  7. 如何使用python生成gif

    如何使用python生成gif? 在我的文件夹里面有很多图片,我们如何将其合成一个gif呢?可以使用PIL模块,这个模块在我的"python图像处理"板块中有详细介绍. # -*- ...

  8. linux pip使用国内源

    最近在Linux里面使用pip安装应用的速度十分的慢,于是便上网找了一些国内的源. 清华大学:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:https:// ...

  9. AIX的shell脚本异常笔记

    一点点心得:1.set -x 运行时显示明细,前面加#则不显示2.空格要打好,如if [ -n "str" ]; then 可以,if[ -n "str" ]; ...

  10. 牛客练习赛47 D DongDong坐飞机 (分层最短路)

    链接:https://ac.nowcoder.com/acm/contest/904/D 来源:牛客网 DongDong坐飞机 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 5242 ...