Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  *
0009  *    http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  */
0017 
0018 package org.apache.spark.examples.ml;
0019 
0020 // $example on$
0021 import java.util.Arrays;
0022 
0023 import org.apache.spark.ml.regression.GeneralizedLinearRegression;
0024 import org.apache.spark.ml.regression.GeneralizedLinearRegressionModel;
0025 import org.apache.spark.ml.regression.GeneralizedLinearRegressionTrainingSummary;
0026 import org.apache.spark.sql.Dataset;
0027 import org.apache.spark.sql.Row;
0028 // $example off$
0029 import org.apache.spark.sql.SparkSession;
0030 
0031 /**
0032  * An example demonstrating generalized linear regression.
0033  * Run with
0034  * <pre>
0035  * bin/run-example ml.JavaGeneralizedLinearRegressionExample
0036  * </pre>
0037  */
0038 
0039 public class JavaGeneralizedLinearRegressionExample {
0040 
0041   public static void main(String[] args) {
0042     SparkSession spark = SparkSession
0043       .builder()
0044       .appName("JavaGeneralizedLinearRegressionExample")
0045       .getOrCreate();
0046 
0047     // $example on$
0048     // Load training data
0049     Dataset<Row> dataset = spark.read().format("libsvm")
0050       .load("data/mllib/sample_linear_regression_data.txt");
0051 
0052     GeneralizedLinearRegression glr = new GeneralizedLinearRegression()
0053       .setFamily("gaussian")
0054       .setLink("identity")
0055       .setMaxIter(10)
0056       .setRegParam(0.3);
0057 
0058     // Fit the model
0059     GeneralizedLinearRegressionModel model = glr.fit(dataset);
0060 
0061     // Print the coefficients and intercept for generalized linear regression model
0062     System.out.println("Coefficients: " + model.coefficients());
0063     System.out.println("Intercept: " + model.intercept());
0064 
0065     // Summarize the model over the training set and print out some metrics
0066     GeneralizedLinearRegressionTrainingSummary summary = model.summary();
0067     System.out.println("Coefficient Standard Errors: "
0068       + Arrays.toString(summary.coefficientStandardErrors()));
0069     System.out.println("T Values: " + Arrays.toString(summary.tValues()));
0070     System.out.println("P Values: " + Arrays.toString(summary.pValues()));
0071     System.out.println("Dispersion: " + summary.dispersion());
0072     System.out.println("Null Deviance: " + summary.nullDeviance());
0073     System.out.println("Residual Degree Of Freedom Null: " + summary.residualDegreeOfFreedomNull());
0074     System.out.println("Deviance: " + summary.deviance());
0075     System.out.println("Residual Degree Of Freedom: " + summary.residualDegreeOfFreedom());
0076     System.out.println("AIC: " + summary.aic());
0077     System.out.println("Deviance Residuals: ");
0078     summary.residuals().show();
0079     // $example off$
0080 
0081     spark.stop();
0082   }
0083 }