我在大数据分析应用程序之一中将Spark Mllib与Hadoop一起使用。我有41个功能和一个标签的功能集。现在,在培训期间,我想混合使用我的功能以与功能工程师匹配,并找到最适合我的场景的最小功能集。
为此,我想在训练时选择在训练和测试模型准确性时要使用的功能。
我正在做这个
JavaRDD<LabeledPoint>[] splits = data.randomSplit(new double[] { 0.5, 0.5 });
JavaRDD<LabeledPoint> trainingData = splits[0];
JavaRDD<LabeledPoint> testData = splits[1];
然后使用这些数据训练不同的模型。
modelLR = new LogisticRegressionWithLBFGS().setNumClasses(numClasses).run(trainingData.rdd());
modelRF = RandomForest.trainClassifier(trainingData, numClasses, categoricalFeaturesInfo, numTrees, featureSubsetStrategy, impurity, maxDepth, maxBins, seed);
modelNB = NaiveBayes.train(trainingData.rdd(), 1.0);
modelGBT = GradientBoostedTrees.train(trainingData, boostingStrategy);
modelDT = DecisionTree.trainClassifier(trainingData, numClasses, categoricalFeaturesInfo, impurity, maxDepth, maxBins);
现在,在使用数据集训练模型之前,我想过滤数据以选择要使用的选择性特征。有人可以建议我这样做JavaRDD<LabeledPoint>
吗?
如果需要更多详细信息,请随时询问。
没关系。我自己想出了答案。
对于任何对此感兴趣的人,我都做了这样的事情。
public static JavaRDD<LabeledPoint> filterData(JavaRDD<LabeledPoint> data, String filterString) {
return data.map(new Function<LabeledPoint, LabeledPoint>() {
@Override
public LabeledPoint call(LabeledPoint point) throws Exception {
double label = point.label();
double[] features = point.features().toArray();
String[] featuresInUse = filterString.split(",");
double[] filteredFeatures = new double[featuresInUse.length];
for (int i = 0; i < featuresInUse.length; i++) {
filteredFeatures[i] = features[Integer.parseInt(VectorizationProperties.getProperty(featuresInUse[i]))];
}
LabeledPoint newPoint = new LabeledPoint(label, Vectors.dense(filteredFeatures));
System.out.println(newPoint);
return newPoint;
}
});
}
它将过滤每个记录并返回过滤后的JavaRDD。
请随时询问需要进一步了解的任何详细信息。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句