RandomForestRegressor
RandomForestRegressor
is a Predictor for Random Forest machine learning algorithm that trains a RandomForestRegressionModel
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import org.apache.spark.mllib.linalg.Vectors val features = Vectors.sparse(10, Seq((2, 0.2), (4, 0.4))) val data = (0.0 to 4.0 by 1).map(d => (d, features)).toDF("label", "features") // data.as[LabeledPoint] scala> data.show(false) +-----+--------------------------+ |label|features | +-----+--------------------------+ |0.0 |(10,[2,4,6],[0.2,0.4,0.6])| |1.0 |(10,[2,4,6],[0.2,0.4,0.6])| |2.0 |(10,[2,4,6],[0.2,0.4,0.6])| |3.0 |(10,[2,4,6],[0.2,0.4,0.6])| |4.0 |(10,[2,4,6],[0.2,0.4,0.6])| +-----+--------------------------+ import org.apache.spark.ml.regression.{ RandomForestRegressor, RandomForestRegressionModel } val rfr = new RandomForestRegressor val model: RandomForestRegressionModel = rfr.fit(data) scala> model.trees.foreach(println) DecisionTreeRegressionModel (uid=dtr_247e77e2f8e0) of depth 1 with 3 nodes DecisionTreeRegressionModel (uid=dtr_61f8eacb2b61) of depth 2 with 7 nodes DecisionTreeRegressionModel (uid=dtr_63fc5bde051c) of depth 2 with 5 nodes DecisionTreeRegressionModel (uid=dtr_64d4e42de85f) of depth 2 with 5 nodes DecisionTreeRegressionModel (uid=dtr_693626422894) of depth 3 with 9 nodes DecisionTreeRegressionModel (uid=dtr_927f8a0bc35e) of depth 2 with 5 nodes DecisionTreeRegressionModel (uid=dtr_82da39f6e4e1) of depth 3 with 7 nodes DecisionTreeRegressionModel (uid=dtr_cb94c2e75bd1) of depth 0 with 1 nodes DecisionTreeRegressionModel (uid=dtr_29e3362adfb2) of depth 1 with 3 nodes DecisionTreeRegressionModel (uid=dtr_d6d896abcc75) of depth 3 with 7 nodes DecisionTreeRegressionModel (uid=dtr_aacb22a9143d) of depth 2 with 5 nodes DecisionTreeRegressionModel (uid=dtr_18d07dadb5b9) of depth 2 with 7 nodes DecisionTreeRegressionModel (uid=dtr_f0615c28637c) of depth 2 with 5 nodes DecisionTreeRegressionModel (uid=dtr_4619362d02fc) of depth 2 with 5 nodes DecisionTreeRegressionModel (uid=dtr_d39502f828f4) of depth 2 with 5 nodes DecisionTreeRegressionModel (uid=dtr_896f3a4272ad) of depth 3 with 9 nodes DecisionTreeRegressionModel (uid=dtr_891323c29838) of depth 3 with 7 nodes DecisionTreeRegressionModel (uid=dtr_d658fe871e99) of depth 2 with 5 nodes DecisionTreeRegressionModel (uid=dtr_d91227b13d41) of depth 2 with 5 nodes DecisionTreeRegressionModel (uid=dtr_4a7976921f4b) of depth 2 with 5 nodes scala> model.treeWeights res12: Array[Double] = Array(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0) scala> model.featureImportances res13: org.apache.spark.mllib.linalg.Vector = (1,[0],[1.0]) |