Last active
September 18, 2020 02:27
-
-
Save wnina/3e76a6afd12c8efc3cfe89a93f4eb4e3 to your computer and use it in GitHub Desktop.
This file contains 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
import java.util.ArrayList; | |
import java.util.List; | |
public class Kmeans { | |
int numCenter = 3; | |
int numPoint = 9; | |
int maxIter = 10; | |
List<List<Double>> listPoint; | |
List<List<Double>> listCenter; | |
List<List<Integer>> listCluster; | |
public static void main(String[] args) { | |
Kmeans kmeans = new Kmeans(); | |
kmeans.initPoint(); | |
kmeans.initCenterAndCluster(); | |
int i = 0; | |
System.out.println("----- Begin ----"); | |
while(i < kmeans.maxIter) { | |
System.out.println("Iter " + i); | |
for(int j=0; j<kmeans.numPoint; j++) { | |
double xPoint = kmeans.listPoint.get(j).get(0); | |
double yPoint = kmeans.listPoint.get(j).get(1); | |
double distance = 999999999; | |
int iCenter = 0; | |
for(int k=0; k<kmeans.numCenter; k++) { | |
double xCenter = kmeans.listCenter.get(k).get(0); | |
double yCenter = kmeans.listCenter.get(k).get(1); | |
double newDistance = Math.sqrt(Math.pow((xPoint-xCenter), 2) + Math.pow((yPoint-yCenter), 2)); | |
if(distance > newDistance) { | |
distance = newDistance; | |
iCenter = k; | |
} | |
} | |
kmeans.listCluster.get(iCenter).add(j); | |
} | |
for(int m=0; m<kmeans.numCenter; m++) { | |
int s = kmeans.listCluster.get(m).size(); | |
double xPointSum = 0.0; | |
double yPointSum = 0.0; | |
for(int n=0; n<s; n++) { | |
int iPoint = kmeans.listCluster.get(m).get(n); | |
double xPoint = kmeans.listPoint.get(iPoint).get(0); | |
double yPoint = kmeans.listPoint.get(iPoint).get(1); | |
xPointSum = xPointSum + xPoint; | |
yPointSum = yPointSum + yPoint; | |
} | |
double xPointCenterNew = xPointSum/s; | |
double yPointCenterNew = yPointSum/s; | |
kmeans.listCenter.get(m).set(0, xPointCenterNew); | |
kmeans.listCenter.get(m).set(1, yPointCenterNew); | |
} | |
for(int m=0; m<kmeans.numCenter; m++) { | |
int s = kmeans.listCluster.get(m).size(); | |
System.out.print(" Cluster " + m + ": "); | |
for(int z=0; z<s; z++) { | |
int index = kmeans.listCluster.get(m).get(z); | |
System.out.print(index + " "); | |
} | |
double xPointCenter = kmeans.listCenter.get(m).get(0); | |
double yPointCenter = kmeans.listCenter.get(m).get(1); | |
System.out.print("PointCenter " + m + ": (" + xPointCenter + "," +yPointCenter + ")" ); | |
System.out.print("\n"); | |
} | |
for(int p=0; p<kmeans.numCenter; p++) { | |
kmeans.listCluster.get(p).clear(); | |
} | |
i++; | |
} | |
System.out.println("----- End ----"); | |
} | |
public void initPoint() { | |
listPoint = new ArrayList<List<Double>>(); | |
listCenter = new ArrayList<List<Double>>(); | |
listCluster = new ArrayList<List<Integer>>(); | |
List<Double> listP0 = new ArrayList<Double>(); | |
listP0.add(2.0); | |
listP0.add(5.0); | |
List<Double> listP1 = new ArrayList<Double>(); | |
listP1.add(5.0); | |
listP1.add(10.0); | |
List<Double> listP2 = new ArrayList<Double>(); | |
listP2.add(1.0); | |
listP2.add(7.0); | |
List<Double> listP3 = new ArrayList<Double>(); | |
listP3.add(5.0); | |
listP3.add(8.0); | |
List<Double> listP4 = new ArrayList<Double>(); | |
listP4.add(1.0); | |
listP4.add(1.0); | |
List<Double> listP5 = new ArrayList<Double>(); | |
listP5.add(1.0); | |
listP5.add(1.0); | |
List<Double> listP6 = new ArrayList<Double>(); | |
listP6.add(1.0); | |
listP6.add(9.0); | |
List<Double> listP7 = new ArrayList<Double>(); | |
listP7.add(3.0); | |
listP7.add(3.0); | |
List<Double> listP8 = new ArrayList<Double>(); | |
listP8.add(5.0); | |
listP8.add(5.0); | |
listPoint.add(listP0); | |
listPoint.add(listP1); | |
listPoint.add(listP2); | |
listPoint.add(listP3); | |
listPoint.add(listP4); | |
listPoint.add(listP5); | |
listPoint.add(listP6); | |
listPoint.add(listP7); | |
listPoint.add(listP8); | |
} | |
public void initCenterAndCluster() { | |
listCenter.add(listPoint.get(0)); | |
listCenter.add(listPoint.get(1)); | |
listCenter.add(listPoint.get(2)); | |
listCluster.add(new ArrayList<>()); | |
listCluster.add(new ArrayList<>()); | |
listCluster.add(new ArrayList<>()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment