Last active
July 28, 2024 18:41
-
-
Save AllanCapistrano/ae2f580a9252ce7a4388681bb064ce07 to your computer and use it in GitHub Desktop.
Implementação do algoritmo K-means em Java que pode ser executado em um array unidimensional.
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; | |
import java.util.Random; | |
public class Main { | |
public static void main(String[] args) { | |
double[] data = {0.98264424, 0.25215435, 0.74409057, 0.75786744, 0.07386903, | |
0.117157 , 0.97317236, 0.95556455, 0.5144924 , 0.42371915, | |
0.92337679, 0.8653283 , 0.36893858, 0.34717299, 0.30595674, | |
0.66200543, 0.06913437, 0.7688657 , 0.26539155, 0.96080647, | |
0.55704921, 0.56980981, 0.72020838, 0.75304748, 0.24754639, | |
0.32642188, 0.98389965, 0.4578372 , 0.70768994, 0.52584569, | |
0.49147528, 0.57535727, 0.53293532, 0.91167438, 0.30085932, | |
0.16126732, 0.66802735, 0.94289875, 0.348455 , 0.76173667, | |
0.97230067, 0.89632707, 0.81638 , 0.62457346, 0.74827341, | |
0.15837867, 0.71967043, 0.06886217, 0.34626093, 0.04983491, | |
0.28311969, 0.31418299, 0.90311239, 0.34862766, 0.69386019, | |
0.24504107, 0.47277046, 0.99693053, 0.30635468, 0.69889772, | |
0.19073208, 0.20364353, 0.47800083, 0.94715012, 0.0650246 , | |
0.48318999, 0.59401152, 0.87454866, 0.89569624, 0.15977646, | |
0.85190751, 0.81422958, 0.62973862, 0.87553822, 0.2697018 , | |
0.04570862, 0.80200116, 0.2524995 , 0.33107931, 0.15291125, | |
0.83280916, 0.97061817, 0.85977651, 0.99244892, 0.36586921, | |
0.63840262, 0.80707333, 0.61164927, 0.12571482, 0.00256861, | |
0.63440281, 0.62328818, 0.16194688, 0.84304936, 0.62026441, | |
0.48547423, 0.64747173, 0.6135524 , 0.78726922, 0.52646756, | |
0.28996378, 0.52022795, 0.08416657, 0.95225221, 0.32186962, | |
0.81003451, 0.75172307, 0.87812398, 0.47510704, 0.26118821, | |
0.3827031 , 0.39261068, 0.02932706, 0.83645779, 0.54790772, | |
0.82383049, 0.20571072, 0.97459193, 0.38361944, 0.68620629, | |
0.06333592, 0.45624489, 0.52952168, 0.32309734, 0.55797279, | |
0.69165539, 0.25132298, 0.27506052, 0.87224968, 0.29771511, | |
0.31803004, 0.57166827, 0.67945477, 0.87641007, 0.39279738, | |
0.5712728 , 0.85742303, 0.13866213, 0.10459185, 0.24655289, | |
0.23077288, 0.01850509, 0.46761722, 0.48929042, 0.14551705, | |
0.50106598, 0.38295613, 0.59973969, 0.1211079 , 0.82220104}; | |
KMeans1D kmeans = new KMeans1D(4, 10, data); | |
kmeans.init(); | |
kmeans.calculate(); | |
for (int i = 0; i < data.length; i++) { | |
System.out.println( | |
"Data point " + data[i] + " is in cluster " + kmeans.labels[i] | |
); | |
} | |
} | |
} | |
class KMeans1D { | |
private int numClusters; | |
private int numIterations; | |
private double[] data; | |
private double[] centroids; | |
public int[] labels; | |
public KMeans1D(int numClusters, int numIterations, double[] data) { | |
this.numClusters = numClusters; | |
this.numIterations = numIterations; | |
this.data = data; | |
this.centroids = new double[numClusters]; | |
this.labels = new int[data.length]; | |
} | |
public void init() { | |
Random random = new Random(); | |
for (int i = 0; i < numClusters; i++) { | |
centroids[i] = data[random.nextInt(data.length)]; | |
} | |
} | |
public void calculate() { | |
boolean finish = false; | |
int iteration = 0; | |
while (!finish && iteration < numIterations) { | |
assignClusters(); | |
double[] newCentroids = calculateNewCentroids(); | |
double distance = 0; | |
for (int i = 0; i < numClusters; i++) { | |
distance += Math.abs(centroids[i] - newCentroids[i]); | |
} | |
centroids = newCentroids; | |
iteration++; | |
if (distance == 0) { | |
finish = true; | |
} | |
} | |
} | |
private void assignClusters() { | |
for (int i = 0; i < data.length; i++) { | |
double minDistance = Double.MAX_VALUE; | |
int clusterIndex = 0; | |
for (int j = 0; j < numClusters; j++) { | |
double distance = Math.abs(data[i] - centroids[j]); | |
if (distance < minDistance) { | |
minDistance = distance; | |
clusterIndex = j; | |
} | |
} | |
labels[i] = clusterIndex; | |
} | |
} | |
private double[] calculateNewCentroids() { | |
double[] newCentroids = new double[numClusters]; | |
int[] counts = new int[numClusters]; | |
for (int i = 0; i < data.length; i++) { | |
newCentroids[labels[i]] += data[i]; | |
counts[labels[i]]++; | |
} | |
for (int i = 0; i < numClusters; i++) { | |
if (counts[i] != 0) { | |
newCentroids[i] /= counts[i]; | |
} | |
} | |
return newCentroids; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment