Spark Machine Learning (7): Kmenas algorithm

Source: Internet
Author: User

Kmenas algorithm is relatively simple, not detailed introduction, directly on the code.

Importorg.apache.log4j. {level, Logger}ImportOrg.apache.spark. {sparkconf, sparkcontext}Importorg.apache.spark.mllib.linalg.VectorsImportorg.apache.spark.mllib.clustering._/*** Created by Administrator on 2017/7/11. */Object Kmenas {def main (args:array[string]): Unit={    //setting up the operating environmentVal conf =NewSparkconf (). Setappname ("Kmeans Test"). Setmaster ("spark://master:7077"). Setjars (Seq ("E:\\intellij\\projects\\machinelearning\\machinelearning.jar"))) Val SC=Newsparkcontext (conf) Logger.getRootLogger.setLevel (Level.warn)//read sample data and parseVal data = Sc.textfile ("Hdfs://master:9000/ml/data/kmeans_data.txt") Val parseddata= Data.map (s = = Vectors.dense (S.split ("). map (_.todouble)). Cache ()//New Kmeans Clustering model and trainingVal initmode = "k-means| |"Val numclusters= 2Val numiterations= 500Val Model=NewKmeans ().      Setinitializationmode (Initmode).      SETK (numclusters).      Setmaxiterations (numiterations). Run (parseddata) Val Centers=model.clustercenters println ("Centers:")     for(I <-0 to Centers.length-1) {println (Centers (i) (0) + "\ T" + Centers (i) (1))    }    //Error CalculationVal Error =model.computecost (parseddata) println ("Errors =" +Error)}}

Operation Result:

Spark Machine Learning (7): Kmenas algorithm

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.