【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

 

 返回目錄

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM