Mastering Spark Machine Learning Library -07.6-linear regression to realize house price forecast

Source: Internet
Author: User

Data set

House.csv

Data overview

Code

 PackageORG.APACHE.SPARK.EXAMPLES.EXAMPLESFORMLImportOrg.apache.spark.ml.feature.VectorAssemblerImportorg.apache.spark.ml.regression.LinearRegressionImportorg.apache.spark.sql.SparkSessionImportOrg.apache.spark. {sparkconf, sparkcontext}ImportScala.util.Random/*Date: 2018.10.15 description: 7-6 linear regression algorithm forecast price data set: House.csv*/Object Linear {def main (args:array[string]): Unit={val conf=NewSparkconf (). Setmaster ("local[*]"). Setappname ("Linearregression") Val SC=Newsparkcontext (conf) Val Spark=sparksession.builder (). config (conf). Getorcreate () Val file=spark.read.format ("CSV"). Option ("Header", "true")//y. Option ("Sep", ";")//Separators. Load ("D:\\ machine learning algorithm prepares \\7-6 linear regression-Forecast rate \\house.csv")    Importspark.implicits._ val Random=NewRandom () Val Data=file.select ("Square", "Price"). Map (Row= (Row.getas[string] (0). todouble,row.getas[string] (1). Todouble,random.nextdouble ()). TODF ("Square", "Price", "Rand"). Sort ("Rand") Data.show () Val Assembler=NewVectorassembler (). Setinputcols (Array ("Square"). Setoutputcol ("Features") Val DataSet=assembler.transform (data) var Array (train,test)=dataset.randomsplit (Array (0.8,0.2), 1234L) Train.show () println (Test.count ()) var regression=NewLinearregression (). Setmaxiter () Setregparam (0.3). Setelasticnetparam (0.8) Val Model=regression.setlabelcol ("Price"). Setfeaturescol ("Features"). Fit (train) model.transform (test). Show () Val s=model.summary.totalIterations println (s"ITER: ${s}")  }}

Output:

Mastering Spark Machine Learning Library -07.6-linear regression to realize house price forecast

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.