spark 決策樹分類算法demo


分類(Classification)

下面的例子說明了怎樣導入LIBSVM 數據文件,解析成RDD[LabeledPoint],然后使用決策樹進行分類。GINI不純度作為不純度衡量標准並且樹的最大深度設置為5。最后計算了測試錯誤率從而評估算法的准確性。

from pyspark.mllib.regression import LabeledPoint from pyspark.mllib.tree import DecisionTree, DecisionTreeModel 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') # Split the data into training and test sets (30% held out for testing) (trainingData, testData) = data.randomSplit([0.7, 0.3]) # Train a DecisionTree model. # Empty categoricalFeaturesInfo indicates all features are continuous. model = DecisionTree.trainClassifier(trainingData, numClasses=2, categoricalFeaturesInfo={}, impurity='gini', maxDepth=5, 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 (v, p): v != p).count() / float(testData.count()) print('Test Error = ' + str(testErr)) print('Learned classification tree model:') print(model.toDebugString()) # Save and load model model.save(sc, "myModelPath") sameModel = DecisionTreeModel.load(sc, "myModelPath")

 

以下代碼展示了如何載入一個LIBSVM數據文件,解析成一個LabeledPointRDD,然后使用決策樹,使用Gini不純度作為不純度衡量指標,最大樹深度是5.測試誤差用來計算算法准確率。

  1. # -*- coding:utf-8 -*-
  2. """
  3. 測試決策樹
  4. """
  5. import os
  6. import sys
  7. import logging
  8. from pyspark.mllib.tree import DecisionTree,DecisionTreeModel
  9. from pyspark.mllib.util import MLUtils
  10. # Path for spark source folder
  11. os.environ['SPARK_HOME']="D:\javaPackages\spark-1.6.0-bin-hadoop2.6"
  12. # Append pyspark to Python Path
  13. sys.path.append("D:\javaPackages\spark-1.6.0-bin-hadoop2.6\python")
  14. sys.path.append("D:\javaPackages\spark-1.6.0-bin-hadoop2.6\python\lib\py4j-0.9-src.zip")
  15. from pyspark import SparkContext
  16. from pyspark import SparkConf
  17. conf = SparkConf()
  18. conf.set("YARN_CONF_DIR ", "D:\javaPackages\hadoop_conf_dir\yarn-conf")
  19. conf.set("spark.driver.memory", "2g")
  20. #conf.set("spark.executor.memory", "1g")
  21. #conf.set("spark.python.worker.memory", "1g")
  22. conf.setMaster("yarn-client")
  23. conf.setAppName("TestDecisionTree")
  24. logger = logging.getLogger('pyspark')
  25. sc = SparkContext(conf=conf)
  26. mylog = []
  27. #載入和解析數據文件為 LabeledPoint RDDdata = MLUtils.loadLibSVMFile(sc,"/home/xiatao/machine_learing/")
  28. #將數據拆分成訓練集合測試集
  29. (trainingData,testData) = data.randomSplit([0.7,0.3])
  30. ##訓練決策樹模型
  31. #空的 categoricalFeauresInfo 代表了所有的特征都是連續的
  32. model = DecisionTree.trainClassifier(trainingData, numClasses=2,categoricalFeaturesInfo={},impurity='gini',maxDepth=5,maxBins=32)
  33. # 在測試實例上評估模型並計算測試誤差
  34. predictions = model.predict(testData.map(lambda x:x.features))
  35. labelsAndPoint = testData.map(lambda lp:lp.label).zip(predictions)
  36. testMSE = labelsAndPoint.map(lambda (v,p):(v-p)**2).sum()/float(testData.count())
  37. mylog.append("測試誤差是")
  38. mylog.append(testMSE)
  39. #存儲模型
  40. model.save(sc,"/home/xiatao/machine_learing/")
  41. sc.parallelize(mylog).saveAsTextFile("/home/xiatao/machine_learing/log")
  42. sameModel = DecisionTreeModel.load(sc,"/home/xiatao/machine_learing/")
 


免責聲明!

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



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