Created
March 24, 2014 19:22
-
-
Save rayjcwu/9747197 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Point { | |
| double x; | |
| double y; | |
| String type; | |
| public Point() { | |
| } | |
| public Point(Point other) { | |
| this(other.type, other.x, other.y); | |
| } | |
| public Point(String type, double x, double y) { | |
| this.type = type; | |
| this.x = x; | |
| this.y = y; | |
| } | |
| } | |
| public Set<Point> training(Set<Point> points) { | |
| Set<String, Integer> count = new HashSet<String, Integer>(); | |
| Set<Point> result = new HashSet<Point>(); | |
| for (Point point: points) { | |
| if (!count.contains(point.type)) { | |
| Point newPoint = new Point(point); | |
| result.put(point.type, newPoint); | |
| count.put(point.type, 1); | |
| } else { | |
| Point sumPoint = result.get(point.type); | |
| sumPoint.x += point.x; | |
| sumPoint.y += point.y; | |
| count.put(point.type, count.get(type) + 1); | |
| } | |
| } | |
| for (Point point: result) { | |
| point.x /= count.get(point.type); | |
| point.y /= count.get(point.type); | |
| } | |
| return result; | |
| } | |
| public String classify(Point point, Set<Point> meanPoints) { | |
| double distMin = Double.MAX_VALUE; | |
| final double THRESHOLD = 2000.0 * 2000.0; | |
| String result = "Outlier"; | |
| for (Point mean: meanPoints) { | |
| final double distTmp = distSq(mean, point); | |
| if (distTmp < distMin && distTmp < THRESHOLD) { | |
| distMin = distTmp; | |
| result = mean.type; | |
| } | |
| } | |
| return result; | |
| } | |
| public double distSq(Point a, Point b) { | |
| return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment