• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

Java LinearRegressionWithSGD类的典型用法和代码示例

java 2次浏览

本文整理汇总了Java中org.apache.spark.mllib.regression.LinearRegressionWithSGD的典型用法代码示例。如果您正苦于以下问题:Java LinearRegressionWithSGD类的具体用法?Java LinearRegressionWithSGD怎么用?Java LinearRegressionWithSGD使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。

LinearRegressionWithSGD类属于org.apache.spark.mllib.regression包,在下文中一共展示了LinearRegressionWithSGD类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: generateKMeansModel

点赞 4

import org.apache.spark.mllib.regression.LinearRegressionWithSGD; //导入依赖的package包/类
public LinearRegressionModel generateKMeansModel(JavaRDD<LabeledPoint> parsedData,
                                                 LinearRegressionDetectionAlgorithm linearRegressionDetectionAlgorithm,
                                                 LinearRegressionModelSummary linearRegressionModelSummary) {
    LinearRegressionModel model;

    if (linearRegressionDetectionAlgorithm.getMiniBatchFraction() != -1) {
        model = LinearRegressionWithSGD.train(parsedData.rdd(),
                linearRegressionDetectionAlgorithm.getNumIterations(),
                linearRegressionDetectionAlgorithm.getStepSize(),
                linearRegressionDetectionAlgorithm.getMiniBatchFraction());
    } else {
        model = LinearRegressionWithSGD.train(parsedData.rdd(),
                linearRegressionDetectionAlgorithm.getNumIterations(),
                linearRegressionDetectionAlgorithm.getStepSize());
    }


    linearRegressionModelSummary.setLinearRegressionDetectionAlgorithm(linearRegressionDetectionAlgorithm);
    return model;
}
 

开发者ID:shlee89,
项目名称:athena,
代码行数:21,
代码来源:LinearRegressionDistJob.java

示例2: main

点赞 2

import org.apache.spark.mllib.regression.LinearRegressionWithSGD; //导入依赖的package包/类
public static void main(String[] args) {
	SparkConf configuration = new SparkConf().setMaster("local[4]").setAppName("Linear Regression Example");
	JavaSparkContext sparkContext = new JavaSparkContext(configuration);

	// Load and parse the data
	String inputData = "data/lr-data.txt";
	JavaRDD<String> data = sparkContext.textFile(inputData);
	JavaRDD<LabeledPoint> parsedData = data.map(
			new Function<String, LabeledPoint>() {
				public LabeledPoint call(String line) {
					String[] parts = line.split(",");
					String[] features = parts[1].split(" ");
					double[] featureVector = new double[features.length];
					for (int i = 0; i < features.length - 1; i++){
						featureVector[i] = Double.parseDouble(features[i]);
					}
					return new LabeledPoint(Double.parseDouble(parts[0]), Vectors.dense(featureVector));
				}
			}
	);
	parsedData.cache();

	// Building the model
	int numIterations = 100;
	final LinearRegressionModel model = 
			LinearRegressionWithSGD.train(JavaRDD.toRDD(parsedData), numIterations);

	// Evaluate model on training examples and compute training error
	JavaRDD<Tuple2<Double, Double>> valuesAndPreds = parsedData.map(
			new Function<LabeledPoint, Tuple2<Double, Double>>() {
				public Tuple2<Double, Double> call(LabeledPoint point) {
					double prediction = model.predict(point.features());
					return new Tuple2<Double, Double>(prediction, point.label());
				}
			}
	);
	double MSE = new JavaDoubleRDD(valuesAndPreds.map(
			new Function<Tuple2<Double, Double>, Object>() {
				public Object call(Tuple2<Double, Double> pair) {
					return Math.pow(pair._1() - pair._2(), 2.0);
				}
			}
	).rdd()).mean();
	System.out.println("training Mean Squared Error = " + MSE);
}
 

开发者ID:PacktPublishing,
项目名称:Java-Data-Science-Cookbook,
代码行数:46,
代码来源:LinearRegressionMlib.java

示例3: main

点赞 2

import org.apache.spark.mllib.regression.LinearRegressionWithSGD; //导入依赖的package包/类
public static void main(String[] args) {
  JavaSparkContext sc = new JavaSparkContext("local", "University of Wisconson Cancer Data");

  // Load and parse the data
  String path = "data/university_of_wisconson_data_.txt";
  JavaRDD<String> data = sc.textFile(path);
  JavaRDD<LabeledPoint> parsedData = data.map(
      new Function<String, LabeledPoint>() {
        public LabeledPoint call(String line) {
          String[] features = line.split(",");
          double label = 0;
          double[] v = new double[features.length - 2];
          for (int i = 0; i < features.length - 2; i++)
            v[i] = Double.parseDouble(features[i + 1]) * 0.09;
          if (features[10].equals("2"))
            label = 0; // benign
          else
            label = 1; // malignant
          return new LabeledPoint(label, Vectors.dense(v));
        }
      }
  );
  // Split initial RDD into two with 70% training data and 30% testing data (13L is a random seed):
  JavaRDD<LabeledPoint>[] splits = parsedData.randomSplit(new double[]{0.7, 0.3}, 13L);
  JavaRDD<LabeledPoint> training = splits[0].cache();
  JavaRDD<LabeledPoint> testing = splits[1];
  training.cache();

  // Building the model
  int numIterations = 100;
  final LinearRegressionModel model =
      LinearRegressionWithSGD.train(JavaRDD.toRDD(training), numIterations);

  // Evaluate model on training examples and compute training error
  JavaRDD<Tuple2<Double, Double>> valuesAndPreds = testing.map(
      new Function<LabeledPoint, Tuple2<Double, Double>>() {
        public Tuple2<Double, Double> call(LabeledPoint point) {
          double prediction = model.predict(point.features());
          return new Tuple2<Double, Double>(prediction, point.label());
        }
      }
  );
  double MSE = new JavaDoubleRDD(valuesAndPreds.map(
      new Function<Tuple2<Double, Double>, Object>() {
        public Object call(Tuple2<Double, Double> pair) {
          return Math.pow(pair._1() - pair._2(), 2.0);
        }
      }
  ).rdd()).mean();
  System.out.println("Test Data Mean Squared Error = " + MSE);

  // Save and load model and test:
  model.save(sc.sc(), "generated_models");
  LinearRegressionModel loaded_model = LinearRegressionModel.load(sc.sc(), "generated_models");
  double[] malignant_test_data_1 = {0.81, 0.6, 0.92, 0.8, 0.55, 0.83, 0.88, 0.71, 0.81};
  System.err.println("Should be malignant (close to 1.0): " +
      testModel(loaded_model, malignant_test_data_1));
  double[] benign_test_data_1 = {0.55, 0.25, 0.34, 0.31, 0.29, 0.016, 0.51, 0.01, 0.05};
  System.err.println("Should be benign (close to 0.0): " +
      testModel(loaded_model, benign_test_data_1));
}
 

开发者ID:mark-watson,
项目名称:power-java
代码行数:62,
代码来源:LogisticRegression.java

示例4: train

点赞 1

import org.apache.spark.mllib.regression.LinearRegressionWithSGD; //导入依赖的package包/类
/**
 * This method uses stochastic gradient descent (SGD) algorithm to train a linear regression model
 *
 * @param trainingDataset       Training dataset as a JavaRDD of LabeledPoints
 * @param noOfIterations        Number of iterarations
 * @param initialLearningRate   Initial learning rate (SGD step size)
 * @param miniBatchFraction     SGD minibatch fraction
 * @return                      Linear regression model
 */
public LinearRegressionModel train(JavaRDD<LabeledPoint> trainingDataset, int noOfIterations,
        double initialLearningRate, double miniBatchFraction) {
    return LinearRegressionWithSGD.train(trainingDataset.rdd(), noOfIterations, initialLearningRate,
            miniBatchFraction);
}
 

开发者ID:wso2-attic,
项目名称:carbon-ml,
代码行数:15,
代码来源:LinearRegression.java


版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)