【发布时间】:2017-10-24 00:51:19
【问题描述】:
如何将数值和分类特征传递给 Apache Spark 中的 RandomForestRegressor:Java 中的 MLlib?
我可以用数字或分类来做到这一点,但我不知道如何一起实现。
我的工作代码如下(仅用于预测的数字特征)
String[] featureNumericalCols = new String[]{
"squareM",
"timeTimeToPragueCityCenter",
};
String[] featureStringCols = new String[]{ //not used
"type",
"floor",
"disposition",
};
VectorAssembler assembler = new VectorAssembler().setInputCols(featureNumericalCols).setOutputCol("features");
Dataset<Row> numericalData = assembler.transform(data);
numericalData.show();
RandomForestRegressor rf = new RandomForestRegressor().setLabelCol("price")
.setFeaturesCol("features");
// Chain indexer and forest in a Pipeline
Pipeline pipeline = new Pipeline()
.setStages(new PipelineStage[]{assembler, rf});
// Train model. This also runs the indexer.
PipelineModel model = pipeline.fit(trainingData);
// Make predictions.
Dataset<Row> predictions = model.transform(testData);
【问题讨论】:
-
你看过VectorIndexer吗?这是documentation 中的一个示例
-
谢谢你的建议,我看过了,但似乎我只能传递给他一列(setInputCol)但不能传递多列(setInputCols)
标签: java apache-spark machine-learning regression random-forest