📄 kmeans.java
字号:
// Gausskurve verwendet. double r = 5*gaus.sigma*Math.pow(rand.nextDouble(),2); double alpha = 2*Math.PI*rand.nextDouble(); int x = gaus.mux + (int) Math.round(r*Math.cos(alpha)); int y = gaus.muy + (int) Math.round(r*Math.sin(alpha)); // 躡erpr黤ung, ob Position erlaubt ist... if (allowedMousePosition(x,y)==true) { // F黦e den Merkmalvektor der CrossList hinzu. Cross s = new Cross(); s.color = Color.black; s.x = x; s.y = y; CrossList.addElement(s); } } } repaint(); return true; } if ((event.target==RestartButton) && (step !=-1)) { step = -1; abort = false; Centroids.removeAllElements(); int numShapes = CrossList.size(); Cross s; for (int i = 0; i < numShapes; i++) { s = (Cross) CrossList.elementAt(i); s.color = Color.black; } StartButton.setLabel("Start"); StartButton.setEnabled(true); ResetButton.setEnabled(true); RunButton.setEnabled(true); this.repaint(); return true; } if ((event.target==ResetButton)) { Reset(); return true; } return true; } /** Zur點ksetzen des Applet durch L鰏chen von allen Merkmalsvektoren und * Clusterschwerpunkten. */ public void Reset() { step = -1; abort = false; Centroids.removeAllElements(); int numShapes = CrossList.size(); Cross s; for (int i = 0; i < numShapes; i++) { s = (Cross) CrossList.elementAt(i); s.color = Color.white; } StartButton.setLabel("Start"); StartButton.setEnabled(false); RestartButton.setEnabled(false); ResetButton.setEnabled(false); RunButton.setEnabled(false); CrossList.removeAllElements(); this.repaint(); } /** Verteilt zuf鋖lig die Clusterschwerpunkte im 2d Merkmalsraum. */ public void step1() { abort = false; String SubsetString = SubsetChoice.getSelectedItem(); if (SubsetString.equals("2")) subset = 2; if (SubsetString.equals("3")) subset = 3; if (SubsetString.equals("4")) subset = 4; if (SubsetString.equals("5")) subset = 5; if (SubsetString.equals("6")) subset = 6; if (SubsetString.equals("7")) subset = 7; if (SubsetString.equals("8")) subset = 8; int numShapes = CrossList.size(); boolean ch[] = new boolean[numShapes]; for (int i = 0; i<numShapes;i++) ch[i]=false; for (int i = 0; i<subset;) { Cross s; Quad p = new Quad(); int r = Math.abs(rand.nextInt() % numShapes); if (ch[r]==false) { s = (Cross) CrossList.elementAt(r); p.x = s.x; p.y = s.y; if (i == 0) p.color = Color.green; else if (i == 1) p.color = Color.red; else if (i == 2) p.color = Color.blue; else if (i == 3) p.color = Color.yellow; else if (i == 4) p.color = Color.orange; else if (i == 5) p.color = Color.magenta; else if (i == 6) p.color = Color.cyan; else if (i == 7) p.color = Color.lightGray; else if (i == 8) p.color = Color.darkGray; p.History = new Vector(); Centroids.addElement(p); ch[r] = true; i++; } } step = 1; } /** Zuordnung von jedem Merkmalsvektore zum jeweils n鋍hsten Clusterschwerpunkt */ public void step2() { Cross s; Quad p; int numShapes = CrossList.size(); for (int i = 0; i < numShapes; i++) { s = (Cross) CrossList.elementAt(i); int numCent = Centroids.size(); int min = 0; double dist_min = 99999999.9; for (int j = 0; j < numCent; j++) { p = (Quad) Centroids.elementAt(j); double dist = Point.distance(s.x, s.y, p.x, p.y); if (dist < dist_min) { dist_min = dist; min = j; } } p = (Quad) Centroids.elementAt(min); s.color = p.color; } step = 2; } /** Neuberechnung der Clusterschwerpunkte. */ public void step3() { Quad p; Cross s; Point m = new Point(); double changes = 0.0; int numCent = Centroids.size(); for (int j = 0; j < numCent; j++) { p = (Quad) Centroids.elementAt(j); m.x = 0; m.y = 0; int Count = 0; int numShapes = CrossList.size(); for (int i = 0; i < numShapes; i++) { s = (Cross) CrossList.elementAt(i); if (s.color == p.color) { m.x += s.x; m.y += s.y; Count++; } } if (Count>0) { changes += Point.distance(p.x,p.y,m.x/Count, m.y/Count); Point pt = new Point(); pt.x = p.x; pt.y = p.y; p.History.addElement(pt); p.x = m.x / Count; p.y = m.y / Count; } } if (changes<0.1) abort = true; step = 3; } }/** Enth鋖t Informationen der Clusterschwerpunkte, wie Position und Color, aber * auch eine Draw-Methode zum Zeichnen der Schwerpunkte. * Au遝rdem werden die Schwerpunkte aus jedem Schleifendurchlauf gespeichert, um * den Weg, den die Clusterschwerpunkte von Iteration zu Iteration zur點klegen, * darstellen zu k鰊nen. */class Quad { static public final int shapeRadius = 12; Color color; Vector History; int x; int y; boolean hist; void draw(Graphics g) { if ((hist==true) && (History.size()>0)) { Point p1,p2; g.setColor(Color.black); for (int i = 0;i<History.size();i++) { p1 = (Point) History.elementAt(i); if (i+1!=History.size()) p2 = (Point) History.elementAt(i+1); else { p2 = new Point(); p2.x = this.x; p2.y = this.y; } g.drawLine(p1.x+6,p1.y+6,p2.x+6,p2.y+6); } } g.setColor(this.color); g.fillOval(this.x, this.y, shapeRadius, shapeRadius); g.setColor(Color.black); g.drawOval(this.x, this.y, shapeRadius, shapeRadius); }}/** Enth鋖t Informationen der Merkmalsvektoren, sowie eine Methode zum Zeichnen * der Vektoren. */class Cross { static public final int shapeRadius = 2; Color color; int x; int y; void draw(Graphics g) { g.setColor(this.color); g.drawLine(this.x - shapeRadius, this.y, this.x + shapeRadius, this.y); g.drawLine(this.x, this.y - shapeRadius, this.x, this.y + shapeRadius); }}/** Repr鋝entiert eine Gausskurve. */class Gaussian { int mux; int muy; double sigma; double function(int x, int y) { double ret = Math.exp(-0.5*((this.mux-x)*(this.mux-x)+(this.muy-y)*(this.muy-y))/(this.sigma*this.sigma)); return ret/(Math.sqrt(2*Math.PI)*this.sigma); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -