Data Mining (II) -- java Implementation of Knn algorithm, knnjava
1. K-Nearest Neighbor Algorithm (Knn)
The principle is that there are some samples with known classifications in a sample space. When a sample with an unknown classification appears, it is determined by k samples closest to this unknown sample.
For example, a love movie or action movie contains a kiss or action, and an unknown movie is displayed, it is determined based on the nearest k points in the coordinate system established by the number of kiss scenes and the number of motion.
2. algorithm implementation steps
(1) Calculate the Euclidean distance between some unknown points.
(2) Sort all vertices
(3) locate the nearest k points to the unknown point
(4) Calculate the occurrence frequency of the classification of these k points
(5) Select the classification with the highest frequency as the classification of unknown points.
3. java implementation
Point class
Public class Point {private long id; private double x; private double y; private String type; public Point (long id, double x, double y) {this. x = x; this. y = y; this. id = id;} public Point (long id, double x, double y, String type) {this. x = x; this. y = y; this. type = type; this. id = id;} // The get and set methods are omitted}
Distance class
Public class Distance {// known idprivate long id; // unknown idprivate long nid; // The Distance between the two private double disatance; public Distance (long id, long nid, double disatance) {this. id = id; this. nid = nid; this. disatance = disatance;} // get, set Method omitted}
Comparator CompareClass class
Import java. util. Comparator; // The Comparator class public class CompareClass implements Comparator <Distance> {public int compare (Distance d1, Distance d2) {return d1.getDisatance ()> d2.getDisatance ()? 20:-1 ;}}
KNN main class
/*** 1. Enter all known points. 2. Enter unknown points. 3. Calculate the Euclidean distance between all known points and unknown points. 4. Sort all known points by distance. 5. Select unknown distance. calculate the occurrence frequency of the classification of k points. 7. Select the category with the highest frequency as the category of the unknown point. ** @ author fzj **/public class KNN {public static void main (String [] args) {// 1. Enter a List of all known Points <Point> dataList = creatDataSet (); // 2. Enter an unknown Point x = new Point (5, 1.2, 1.2 ); // 3. Calculate the Euclidean Distance between all known points and unknown points, and sort CompareClass compare = new CompareClass (); Set <Distance> distanceSet = new Tr EeSet <Distance> (compare); for (Point point: dataList) {distanceSet. add (new Distance (point. getId (), x. getId (), oudistance (point, x);} // 4. Select the nearest k vertices, double k = 5; /*** 5. Calculate the frequency of occurrence of k points in the Classification * // 1. Calculate the number of points contained in each classification List <Distance> distanceList = new ArrayList <Distance> (distanceSet ); map <String, Integer> map = getNumberOfType (distanceList, dataList, k); // 2. Computing frequency Map <String, Double> p = computeP (map, k); X. setType (maxP (p); System. out. println ("unknown point type:" + x. getType ();} // calculate the public static double oudistance (Point point1, Point point2) {double temp = Math. pow (point1.getX ()-point2.getX (), 2) + Math. pow (point1.getY ()-point2.getY (), 2); return Math. sqrt (temp);} // find out the maximum frequency of public static String maxP (Map <String, Double> map) {String key = null; double value = 0.0; for (Map. entry <String, Double> Entry: map. entrySet () {if (entry. getValue ()> value) {key = entry. getKey (); value = entry. getValue () ;}} return key ;}// calculation frequency: public static Map <String, Double> computeP (Map <String, Integer> map, double k) {Map <String, double> p = new HashMap <String, Double> (); for (Map. entry <String, Integer> entry: map. entrySet () {p. put (entry. getKey (), entry. getValue ()/k);} return p;} // calculate the points contained in each category Public static Map <String, Integer> getNumberOfType (List <Distance> listDistance, List <Point> listPoint, double k) {Map <String, integer> map = new HashMap <String, Integer> (); int I = 0; System. out. println ("the selected k points are:"); for (Distance distance: listDistance) {System. out. println ("id is" + distance. getId () + ", distance:" + distance. getDisatance (); long id = distance. getId (); // locate the type by id and store it to Ha In shMap, for (Point point: listPoint) {if (point. getId () = id) {if (map. get (point. getType ())! = Null) map. put (point. getType (), map. get (point. getType () + 1); else {map. put (point. getType (), 1) ;}} I ++; if (I >= k) break;} return map;} public static ArrayList <Point> creatDataSet () {Point point1 = new Point (1, 1.0, 1.1, "A"); Point point2 = new Point (2, 1.0, 1.0, ""); point point3 = new Point (3, 1.0, 1.2, "A"); Point point4 = new Point (4, 0, 0, "B "); point point5 = new Point (5, 0, 0.1, "B"); Point point6 = new Point (6, 0, 0.2, "B "); arrayList <Point> dataList = new ArrayList <Point> (); dataList. add (point1); dataList. add (point2); dataList. add (point3); dataList. add (point4); dataList. add (point5); dataList. add (point6); return dataList ;}}
4. Running result
Reference
[1] machine learning practices