Kmeans Clustering Algorithm Java Lite design for programming

Source: Internet
Author: User

There are many Java algorithms written on the Internet, of course, according to the different styles of personal coding, resulting in the code written out, each has a different kmeans. Therefore, on the basis of understanding the principle, it is best to follow their own design ideas to write their own code.

Degree Niang Search Kmeans the basic principle, directly on the code, the code has comments:

Package Net.codeal.suanfa.kmeans;import java.util.set;/** *  * @ClassName: distancable  * @Description: TODO ( Can calculate the distance between two points of the center of the parent Class)  * @author Fuhuaguo * @date September 1, 2015 morning 11:41:23  * */public class Kmeansable<e> {/** * get two The distance between points * @param other * @return */public double getdistance (E other) {return 0;} /** * Get new center point * @param eSet * @return */public E getnewcenter (set<e> eSet) {return null;}}

Package Net.codeal.suanfa.kmeans;import java.util.set;/** * * @ClassName: Point * @Description: TODO (a clustered dimension information bean, can be divided into K- Dimension, similarity calculation is its own behavior, put inside the bean is appropriate, cancel the annotation use) * @author Fuhuaguo * @email [email protected] * @date September 1, 2015 Morning 10:43:25 * */PU Blic class Point extends Kmeansable<point>{private String id;//dimension 1private double k1;//dimension 2private double k2;// Dimension 3private double K3;public point () {}public point (String id,double k1,double k2,double k3) {this.id = Id;this.k1 = K1;thi S.K2 = K2;this.k3 = K3;} /** * Calculates the distance from another point, using Euclid's algorithm, calculates the SQRT value of the sum of squares of the dimension's arithmetic, namely: dissimilarity * @param other * @return */@Overridepublic double getdistance ) {return math.sqrt ((THIS.K1-OTHER.GETK1 ()) * (THIS.K1-OTHER.GETK1 ()) + (THIS.K2-OTHER.GETK2 ()) * (THIS.K2-OTHER.GETK2 ()) + (THIS.K3-OTHER.GETK3 ()) * (THIS.K3-OTHER.GETK3 ()));} @Overridepublic Point Getnewcenter (set<point> eset) {if (eset = null | | eset.size () = = 0) {return this;} Point temp = new Point (), int count = 0;for (point p:eset) {Temp.setk1 (TEMP.GETK1 () + P.GETK1 ());TEMP.SETK2 (TEMP.GETK2 () + P.GETK2 ()); Temp.setk3 (TEMP.GETK3 () + P.GETK3 ()); count++;} Temp.setk1 (TEMP.GETK1 ()/count); Temp.setk2 (TEMP.GETK2 ()/count); Temp.setk3 (TEMP.GETK3 ()/count); return temp;} @Overridepublic boolean equals (Object obj) {if (obj = = NULL | |! ( obj instanceof point)) return false; Point other = (point) obj;return (This.k1 = = Other.getk1 ()) && (This.k2 = = Other.getk2 ()) && (This.k3 = = O THER.GETK3 ());} @Overridepublic int hashcode () {return new Double (K1+K2+K3). Hashcode ();} @Overridepublic string toString () {return ' ("+k1+", "+k2+", "+k3+"), "public string getId () {return ID;} public void SetId (String id) {this.id = ID;} Public double getK1 () {return k1;} public void setK1 (double k1) {this.k1 = K1;} Public double getK2 () {return k2;} public void setK2 (double k2) {this.k2 = K2;} Public double getK3 () {return K3;} public void setK3 (double K3) {this.k3 = K3;}}

Package Net.codeal.suanfa.kmeans;import Java.util.hashmap;import Java.util.hashset;import java.util.Map;import Java.util.set;public class Kmeansalgorithm<e extends kmeansable<e>> {/** * to Set K-value clustering, the maximum calculation depth is depth */ public void Kmeans (set<e> dataSet, int k, int depth) {///class number set not appropriate if (k <= 1 | | dataset.size () <= k) {return;} set<e> kset = new hashset<e> (); int count = 0;//randomly determines K center point for (E e:dataset) {if (count++ >= k) break;kset.add (e);} Calculates the distance of each value from each center point, assigning it to the center of the smallest distance, Boolean flag = True;while (flag && Depth > 0) {map<e, set<e>> Kmap = New Hashmap<e, set<e>> (); for (e E:kset) {kmap.put (E, New hashset<e> ());} Complete cluster for (E data:dataset) {double d = double.max_value; E e = null;for (e center:kset) {Double D1 = data.getdistance (center), if (d > D1) {e = Center;d = D1;}} Kmap.get (E). Add (data);} The first set of calculations is completed with the new center point System.out.println ("This is the +depth+" sub-cluster "); for (Map.entry<e, set<e>> m:kmap.entryset ()) {System.out.priNtln (M.getkey () + ":" +m.getvalue ()); Get the new cluster center set<e> oldset = Kset;kset = Getnewcenters (kmap); flag =!issamecenters (kset,oldset);d epth--;}} /** * Get a new center point list */public set<e> getnewcenters (map<e, set<e>> kmap) {set<e> eSet = new HashSet<E > (); for (Map.entry<e, set<e>> m:kmap.entryset ()) {Eset.add (M.getkey (). Getnewcenter (M.getvalue ()));} return eSet;} /** * Determine if the same center list */public boolean issamecenters (set<e> oldset,set<e> newset) {//two sets as long as the intersection is 0 is the same return Oldset.containsall (Newset);} public static void Main (string[] args) {set<point> dataSet = new hashset<point> ();d Ataset.add (The new Point ("1", 1,1,1));d Ataset.add (New Point ("1", 2,2,2));d the Ataset.add (New Point ("1", 5,6,1));d Ataset.add ("1", 10,10,10)); Dataset.add (New Point ("1", 11,11,11)), New Kmeansalgorithm<point> (). Kmeans (DataSet, 2,10);}}
Results:

This is the 10th time cluster
(1.0,1.0,1.0): [(1.0,1.0,1.0), (2.0,2.0,2.0), (5.0,6.0,1.0)]
(10.0,10.0,10.0): [(10.0,10.0,10.0), (11.0,11.0,11.0)]
This is the 9th time cluster
(10.5,10.5,10.5): [(10.0,10.0,10.0), (11.0,11.0,11.0)]
(2.6666666666666665,3.0,1.3333333333333333): [(1.0,1.0,1.0), (2.0,2.0,2.0), (5.0,6.0,1.0)]




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Kmeans Clustering Algorithm Java Lite design for programming

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.