2016-06-07 4 views
1

MLlib의 임의 포리스트에 대한 결합 전략을 선택할 수 있습니까? 공식 API 문서에 대한 단서를 찾을 수 없습니다. 나는 방법 (treeEnsembleModels 클래스에서 구현)을 예측하는 것을 알고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) 
} 

결합 전략 (합계, 평균 또는 투표)에 걸릴 계정 : 내가 '

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

을 여기

내 코드입니다 가능한 유일한 방법은 모델을 만든 후에 리플렉션을 사용하는 것입니다. 필드 사용이 지연되기 때문에 가능해야합니다 (이 코드를 실행하려고하지 않았지만이 코드는 작동합니다).

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