本文整理匯總了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());
}