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 """
0019 An example demonstrating generalized linear regression.
0020 Run with:
0021   bin/spark-submit examples/src/main/python/ml/generalized_linear_regression_example.py
0022 """
0023 from __future__ import print_function
0024 
0025 from pyspark.sql import SparkSession
0026 # $example on$
0027 from pyspark.ml.regression import GeneralizedLinearRegression
0028 # $example off$
0029 
0030 if __name__ == "__main__":
0031     spark = SparkSession\
0032         .builder\
0033         .appName("GeneralizedLinearRegressionExample")\
0034         .getOrCreate()
0035 
0036     # $example on$
0037     # Load training data
0038     dataset = spark.read.format("libsvm")\
0039         .load("data/mllib/sample_linear_regression_data.txt")
0040 
0041     glr = GeneralizedLinearRegression(family="gaussian", link="identity", maxIter=10, regParam=0.3)
0042 
0043     # Fit the model
0044     model = glr.fit(dataset)
0045 
0046     # Print the coefficients and intercept for generalized linear regression model
0047     print("Coefficients: " + str(model.coefficients))
0048     print("Intercept: " + str(model.intercept))
0049 
0050     # Summarize the model over the training set and print out some metrics
0051     summary = model.summary
0052     print("Coefficient Standard Errors: " + str(summary.coefficientStandardErrors))
0053     print("T Values: " + str(summary.tValues))
0054     print("P Values: " + str(summary.pValues))
0055     print("Dispersion: " + str(summary.dispersion))
0056     print("Null Deviance: " + str(summary.nullDeviance))
0057     print("Residual Degree Of Freedom Null: " + str(summary.residualDegreeOfFreedomNull))
0058     print("Deviance: " + str(summary.deviance))
0059     print("Residual Degree Of Freedom: " + str(summary.residualDegreeOfFreedom))
0060     print("AIC: " + str(summary.aic))
0061     print("Deviance Residuals: ")
0062     summary.residuals().show()
0063     # $example off$
0064 
0065     spark.stop()