2016-06-07 57 views
1

是否可以選擇MLlib隨機森林的組合策略?我無法找到官方API文檔的任何線索。如何選擇MLlib隨機森林的組合策略

這裏是我的代碼:

val numClasses = 10 
val categoricalFeaturesInfo = Map[Int, Int]() 
val numTrees = 10 
val featureSubsetStrategy = "auto" 
val impurity = "entropy" 
val maxDepth = 2 
val maxBins = 320 

val model = RandomForest.trainClassifier(trainData, numClasses, categoricalFeaturesInfo, 
    numTrees, featureSubsetStrategy, impurity, maxDepth, maxBins) 

val predictionAndLabels = testData.map { case LabeledPoint(label, features) => 
    val prediction = model.predict(features) 
    (prediction, label) 
} 

我知道,預測方法(在treeEnsembleModels類實現)您考慮合併策略(總和,平均或投票):

def predict(features: Vector): Double = { 
    (algo, combiningStrategy) match { 
     case (Regression, Sum) => 
     predictBySumming(features) 
     case (Regression, Average) => 
     predictBySumming(features)/sumWeights 
     case (Classification, Sum) => // binary classification 
     val prediction = predictBySumming(features) 
     // TODO: predicted labels are +1 or -1 for GBT. Need a better way to store this info. 
     if (prediction > 0.0) 1.0 else 0.0 
     case (Classification, Vote) => 
     predictByVoting(features) 
     case _ => 
     throw new IllegalArgumentException(
      "TreeEnsembleModel given unsupported (algo, combiningStrategy) combination: " + 
     s"($algo, $combiningStrategy).") 
    } 
} 

回答

0

我d說它可能做的唯一方法就是在模型建立之後使用反射。這必須是可能的,因爲字段的使用是延遲的(我沒有試過運行這段代碼,但是像這樣的行爲會起作用)。

RandomForestModel model = ...; 
Class<?> c = model.getClass(); 
Field strategy = c.getDeclaredField("combiningStrategy"); 
strategy.set(model, whatever);