"Spark mllib crash book" model 02 Logistic regression "Logistic regression" (Python version)

Source: Internet
Author: User
Tags pyspark spark mllib

Catalog Logistic regression principle Logistic regression code (Spark Python)

Logistic regression principle

See blog: http://www.cnblogs.com/itmorn/p/7890468.html

Back to Catalog

Logistic regression code (Spark Python)

code data:https://pan.baidu.com/s/1jHWKG4I Password: acq1

#-*-coding=utf-8-*- fromPysparkImportsparkconf, SPARKCONTEXTSC= Sparkcontext ('Local') fromPyspark.mllib.classificationImportLogisticregressionwithlbfgs, Logisticregressionmodel fromPyspark.mllib.regressionImportLabeledpoint#Load and parse the data to load and parse it, converting each number to a floating point. The first number of each line as a marker, followed by a featuredefParsepoint (line): Values= [Float (x) forXinchLine.split (' ')]    returnLabeledpoint (Values[0], values[1:]) Data= Sc.textfile ("Data/mllib/sample_svm_data.txt")PrintData.collect () [0]#1 0 2.52078447201548 0 0 0 2.004684436494304 2.00034729926846 .....Parseddata =Data.map (parsepoint)PrintParseddata.collect () [0]#(1.0,[0.0,2.52078447202,0.0,0.0,0.0,2.00468 ....#Build ModelModel =Logisticregressionwithlbfgs.train (parseddata)#evaluating the model on training data evaluates the error on the training setLabelsandpreds = Parseddata.map (LambdaP: (P.label, Model.predict (p.features))) Trainerr= Labelsandpreds.filter (LambdaLP:LP[0]! = lp[1]). COUNT ()/Float (parseddata.count ())Print("Training Error ="+ str (TRAINERR))#Training Error = 0.366459627329#Save and load model saving models and loading modelsModel.save (SC,"Pythonlogisticregressionwithlbfgsmodel") Samemodel= Logisticregressionmodel.load (SC,"Pythonlogisticregressionwithlbfgsmodel")PrintSamemodel.predict (Parseddata.collect () [0].features)#1

Back to Catalog

"Spark mllib crash book" model 02 Logistic regression "Logistic regression" (Python version)

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.