Skip to content

Instantly share code, notes, and snippets.

@rayjcwu
Created March 24, 2014 19:22
Show Gist options
  • Select an option

  • Save rayjcwu/9747197 to your computer and use it in GitHub Desktop.

Select an option

Save rayjcwu/9747197 to your computer and use it in GitHub Desktop.
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