Last active
March 13, 2019 21:00
-
-
Save dkohlsdorf/ca7579d05798435e6409 to your computer and use it in GitHub Desktop.
K Means Clustering
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.Random; | |
public class KMeans { | |
public static double distance(double x[], double y[]) { | |
int D = x.length; | |
double distance = 0; | |
for(int i = 0; i < D; i++) { | |
distance += Math.pow(x[i] - y[i], 2); | |
} | |
return Math.sqrt(distance); | |
} | |
public static double[][] means(double[][] data, int k, int iterations) { | |
int N = data.length; | |
int D = data[0].length; | |
double means[][] = new double[k][D]; | |
Random random = new Random(); | |
for(int i = 0; i < k; i++) { | |
int center = random.nextInt(N); | |
means[i] = data[center]; | |
} | |
System.out.println(means[0][0] + " " + means[1][0]); | |
int responsability[] = new int[N]; | |
for(int iteration = 0; iteration < iterations; iteration++) { | |
for(int i = 0; i < N; i++) { | |
double min = Integer.MAX_VALUE; | |
int minVal = 0; | |
for(int j = 0; j < k; j++) { | |
double dist = distance(data[i], means[j]); | |
if(dist < min) { | |
min = dist; | |
minVal = j; | |
} | |
} | |
responsability[i] = minVal; | |
} | |
for(int i = 0; i < k; i++) { | |
means[i] = new double[D]; | |
int count = 0; | |
for(int j = 0; j < N; j++) { | |
if(responsability[j] == i) { | |
for(int d = 0; d < D; d++) { | |
means[i][d] += data[j][d]; | |
} | |
count++; | |
} | |
} | |
for(int d = 0; d < D; d++) { | |
means[i][d] /= count; | |
} | |
} | |
} | |
return means; | |
} | |
public static void main(String[] args) { | |
double data[][] = new double[][] { | |
{1 + Math.random()}, | |
{2 + Math.random()}, | |
{1 + Math.random()}, | |
{2 + Math.random()}, | |
{1 + Math.random()}, | |
{2 + Math.random()}, | |
{10 + Math.random()}, | |
{20 + Math.random()}, | |
{10 + Math.random()}, | |
{20 + Math.random()}, | |
{10 + Math.random()}, | |
{20 + Math.random()}, | |
{10 + Math.random()}, | |
{20 + Math.random()}, | |
}; | |
double means[][] = means(data, 2, 10); | |
System.out.println(means[0][0] + " " + means[1][0]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment