本文整理汇总了Java中org.apache.spark.api.java.JavaRDD.randomSplit方法的典型用法代码示例。如果您正苦于以下问题:Java JavaRDD.randomSplit方法的具体用法?Java JavaRDD.randomSplit怎么用?Java JavaRDD.randomSplit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.spark.api.java.JavaRDD
的用法示例。
在下文中一共展示了JavaRDD.randomSplit方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: findBestModel
import org.apache.spark.api.java.JavaRDD; //导入方法依赖的package包/类
public TrainedModel findBestModel(JavaRDD<Rating> ratings) {
double weights[] = {6, 2, 2};
JavaRDD<Rating>[] randomRatings = ratings.randomSplit(weights, 0L);
JavaRDD<Rating> trainingRdd = randomRatings[0];
JavaRDD<Rating> validationRdd = randomRatings[1];
JavaRDD<Rating> testRdd = randomRatings[2];
TrainConfig trainConfig = findBestTrainingParameters(trainingRdd, validationRdd);
TrainedModel model = ModelFactory.create(trainingRdd, testRdd, trainConfig.getRankNr(),
trainConfig.getIterationsNr());
logger.info("best model have RMSE = " + model.getError());
return model;
}
示例2: createAlsModel
import org.apache.spark.api.java.JavaRDD; //导入方法依赖的package包/类
private TrainedModel createAlsModel(JavaRDD<Rating> ratings, TrainConfig trainConfig) {
double[] weights = {8, 2};
JavaRDD<Rating>[] randomRatings = ratings.randomSplit(weights, 0L);
return ModelFactory.create(randomRatings[0],
randomRatings[1],
trainConfig.getRankNr(),
trainConfig.getIterationsNr()
);
}
示例3: main
import org.apache.spark.api.java.JavaRDD; //导入方法依赖的package包/类
public static void main(String args[]){
SparkConf configuration = new SparkConf().setMaster("local[4]").setAppName("Any");
JavaSparkContext sc = new JavaSparkContext(configuration);
// Load and parse the data file.
String input = "data/rf-data.txt";
JavaRDD<LabeledPoint> data = MLUtils.loadLibSVMFile(sc.sc(), input).toJavaRDD();
// Split the data into training and test sets (30% held out for testing)
JavaRDD<LabeledPoint>[] dataSplits = data.randomSplit(new double[]{0.7, 0.3});
JavaRDD<LabeledPoint> trainingData = dataSplits[0];
JavaRDD<LabeledPoint> testData = dataSplits[1];
// Train a RandomForest model.
Integer numClasses = 2;
HashMap<Integer, Integer> categoricalFeaturesInfo = new HashMap<Integer, Integer>();// Empty categoricalFeaturesInfo indicates all features are continuous.
Integer numTrees = 3; // Use more in practice.
String featureSubsetStrategy = "auto"; // Let the algorithm choose.
String impurity = "gini";
Integer maxDepth = 5;
Integer maxBins = 32;
Integer seed = 12345;
final RandomForestModel rfModel = RandomForest.trainClassifier(trainingData, numClasses,
categoricalFeaturesInfo, numTrees, featureSubsetStrategy, impurity, maxDepth, maxBins,
seed);
// Evaluate model on test instances and compute test error
JavaPairRDD<Double, Double> label =
testData.mapToPair(new PairFunction<LabeledPoint, Double, Double>() {
public Tuple2<Double, Double> call(LabeledPoint p) {
return new Tuple2<Double, Double>(rfModel.predict(p.features()), p.label());
}
});
Double testError =
1.0 * label.filter(new Function<Tuple2<Double, Double>, Boolean>() {
public Boolean call(Tuple2<Double, Double> pl) {
return !pl._1().equals(pl._2());
}
}).count() / testData.count();
System.out.println("Test Error: " + testError);
System.out.println("Learned classification forest model:\n" + rfModel.toDebugString());
}