Java program to cluster Iris species from Iris Dataset.
Output
It took 5 iterations to cluster. Cluster 1 Iris { sepal_length = 7.0, sepal_width = 3.2, petal_length = 4.7, petal_width = 1.4, species = 'versicolor' } Iris { sepal_length = 6.4, sepal_width = 3.2, petal_length = 4.5, petal_width = 1.5, species = 'versicolor' } Iris { sepal_length = 5.5, sepal_width = 2.3, petal_length = 4.0, petal_width = 1.3, species = 'versicolor' } Iris { sepal_length = 6.5, sepal_width = 2.8, petal_length = 4.6, petal_width = 1.5, species = 'versicolor' } Iris { sepal_length = 5.7, sepal_width = 2.8, petal_length = 4.5, petal_width = 1.3, species = 'versicolor' } Iris { sepal_length = 6.3, sepal_width = 3.3, petal_length = 4.7, petal_width = 1.6, species = 'versicolor' } Iris { sepal_length = 4.9, sepal_width = 2.4, petal_length = 3.3, petal_width = 1.0, species = 'versicolor' } Iris { sepal_length = 6.6, sepal_width = 2.9, petal_length = 4.6, petal_width = 1.3, species = 'versicolor' } Iris { sepal_length = 5.2, sepal_width = 2.7, petal_length = 3.9, petal_width = 1.4, species = 'versicolor' } Iris { sepal_length = 5.0, sepal_width = 2.0, petal_length = 3.5, petal_width = 1.0, species = 'versicolor' } Iris { sepal_length = 5.9, sepal_width = 3.0, petal_length = 4.2, petal_width = 1.5, species = 'versicolor' } Iris { sepal_length = 6.0, sepal_width = 2.2, petal_length = 4.0, petal_width = 1.0, species = 'versicolor' } Iris { sepal_length = 6.1, sepal_width = 2.9, petal_length = 4.7, petal_width = 1.4, species = 'versicolor' } Iris { sepal_length = 5.6, sepal_width = 2.9, petal_length = 3.6, petal_width = 1.3, species = 'versicolor' } Iris { sepal_length = 6.7, sepal_width = 3.1, petal_length = 4.4, petal_width = 1.4, species = 'versicolor' } Iris { sepal_length = 5.6, sepal_width = 3.0, petal_length = 4.5, petal_width = 1.5, species = 'versicolor' } Iris { sepal_length = 5.8, sepal_width = 2.7, petal_length = 4.1, petal_width = 1.0, species = 'versicolor' } Iris { sepal_length = 6.2, sepal_width = 2.2, petal_length = 4.5, petal_width = 1.5, species = 'versicolor' } Iris { sepal_length = 5.6, sepal_width = 2.5, petal_length = 3.9, petal_width = 1.1, species = 'versicolor' } Iris { sepal_length = 5.9, sepal_width = 3.2, petal_length = 4.8, petal_width = 1.8, species = 'versicolor' } Iris { sepal_length = 6.1, sepal_width = 2.8, petal_length = 4.0, petal_width = 1.3, species = 'versicolor' } Iris { sepal_length = 6.3, sepal_width = 2.5, petal_length = 4.9, petal_width = 1.5, species = 'versicolor' } Iris { sepal_length = 6.1, sepal_width = 2.8, petal_length = 4.7, petal_width = 1.2, species = 'versicolor' } Iris { sepal_length = 6.4, sepal_width = 2.9, petal_length = 4.3, petal_width = 1.3, species = 'versicolor' } Iris { sepal_length = 6.6, sepal_width = 3.0, petal_length = 4.4, petal_width = 1.4, species = 'versicolor' } Iris { sepal_length = 6.8, sepal_width = 2.8, petal_length = 4.8, petal_width = 1.4, species = 'versicolor' } Iris { sepal_length = 6.0, sepal_width = 2.9, petal_length = 4.5, petal_width = 1.5, species = 'versicolor' } Iris { sepal_length = 5.7, sepal_width = 2.6, petal_length = 3.5, petal_width = 1.0, species = 'versicolor' } Iris { sepal_length = 5.5, sepal_width = 2.4, petal_length = 3.8, petal_width = 1.1, species = 'versicolor' } Iris { sepal_length = 5.5, sepal_width = 2.4, petal_length = 3.7, petal_width = 1.0, species = 'versicolor' } Iris { sepal_length = 5.8, sepal_width = 2.7, petal_length = 3.9, petal_width = 1.2, species = 'versicolor' } Iris { sepal_length = 6.0, sepal_width = 2.7, petal_length = 5.1, petal_width = 1.6, species = 'versicolor' } Iris { sepal_length = 5.4, sepal_width = 3.0, petal_length = 4.5, petal_width = 1.5, species = 'versicolor' } Iris { sepal_length = 6.0, sepal_width = 3.4, petal_length = 4.5, petal_width = 1.6, species = 'versicolor' } Iris { sepal_length = 6.7, sepal_width = 3.1, petal_length = 4.7, petal_width = 1.5, species = 'versicolor' } Iris { sepal_length = 6.3, sepal_width = 2.3, petal_length = 4.4, petal_width = 1.3, species = 'versicolor' } Iris { sepal_length = 5.6, sepal_width = 3.0, petal_length = 4.1, petal_width = 1.3, species = 'versicolor' } Iris { sepal_length = 5.5, sepal_width = 2.5, petal_length = 4.0, petal_width = 1.3, species = 'versicolor' } Iris { sepal_length = 5.5, sepal_width = 2.6, petal_length = 4.4, petal_width = 1.2, species = 'versicolor' } Iris { sepal_length = 6.1, sepal_width = 3.0, petal_length = 4.6, petal_width = 1.4, species = 'versicolor' } Iris { sepal_length = 5.8, sepal_width = 2.6, petal_length = 4.0, petal_width = 1.2, species = 'versicolor' } Iris { sepal_length = 5.0, sepal_width = 2.3, petal_length = 3.3, petal_width = 1.0, species = 'versicolor' } Iris { sepal_length = 5.6, sepal_width = 2.7, petal_length = 4.2, petal_width = 1.3, species = 'versicolor' } Iris { sepal_length = 5.7, sepal_width = 3.0, petal_length = 4.2, petal_width = 1.2, species = 'versicolor' } Iris { sepal_length = 5.7, sepal_width = 2.9, petal_length = 4.2, petal_width = 1.3, species = 'versicolor' } Iris { sepal_length = 6.2, sepal_width = 2.9, petal_length = 4.3, petal_width = 1.3, species = 'versicolor' } Iris { sepal_length = 5.1, sepal_width = 2.5, petal_length = 3.0, petal_width = 1.1, species = 'versicolor' } Iris { sepal_length = 5.7, sepal_width = 2.8, petal_length = 4.1, petal_width = 1.3, species = 'versicolor' } Iris { sepal_length = 5.8, sepal_width = 2.7, petal_length = 5.1, petal_width = 1.9, species = 'virginica' } Iris { sepal_length = 4.9, sepal_width = 2.5, petal_length = 4.5, petal_width = 1.7, species = 'virginica' } Iris { sepal_length = 5.7, sepal_width = 2.5, petal_length = 5.0, petal_width = 2.0, species = 'virginica' } Iris { sepal_length = 5.8, sepal_width = 2.8, petal_length = 5.1, petal_width = 2.4, species = 'virginica' } Iris { sepal_length = 6.0, sepal_width = 2.2, petal_length = 5.0, petal_width = 1.5, species = 'virginica' } Iris { sepal_length = 5.6, sepal_width = 2.8, petal_length = 4.9, petal_width = 2.0, species = 'virginica' } Iris { sepal_length = 6.3, sepal_width = 2.7, petal_length = 4.9, petal_width = 1.8, species = 'virginica' } Iris { sepal_length = 6.2, sepal_width = 2.8, petal_length = 4.8, petal_width = 1.8, species = 'virginica' } Iris { sepal_length = 6.1, sepal_width = 3.0, petal_length = 4.9, petal_width = 1.8, species = 'virginica' } Iris { sepal_length = 6.3, sepal_width = 2.8, petal_length = 5.1, petal_width = 1.5, species = 'virginica' } Iris { sepal_length = 6.1, sepal_width = 2.6, petal_length = 5.6, petal_width = 1.4, species = 'virginica' } Iris { sepal_length = 6.0, sepal_width = 3.0, petal_length = 4.8, petal_width = 1.8, species = 'virginica' } Iris { sepal_length = 5.8, sepal_width = 2.7, petal_length = 5.1, petal_width = 1.9, species = 'virginica' } Iris { sepal_length = 6.3, sepal_width = 2.5, petal_length = 5.0, petal_width = 1.9, species = 'virginica' } Iris { sepal_length = 5.9, sepal_width = 3.0, petal_length = 5.1, petal_width = 1.8, species = 'virginica' } --------------------------------------------------------------------------------------------------------------- Cluster 2 Iris { sepal_length = 5.1, sepal_width = 3.5, petal_length = 1.4, petal_width = 0.2, species = 'setosa' } Iris { sepal_length = 4.9, sepal_width = 3.0, petal_length = 1.4, petal_width = 0.2, species = 'setosa' } Iris { sepal_length = 4.7, sepal_width = 3.2, petal_length = 1.3, petal_width = 0.2, species = 'setosa' } Iris { sepal_length = 4.6, sepal_width = 3.1, petal_length = 1.5, petal_width = 0.2, species = 'setosa' } Iris { sepal_length = 5.0, sepal_width = 3.6, petal_length = 1.4, petal_width = 0.2, species = 'setosa' } Iris { sepal_length = 5.4, sepal_width = 3.9, petal_length = 1.7, petal_width = 0.4, species = 'setosa' } Iris { sepal_length = 4.6, sepal_width = 3.4, petal_length = 1.4, petal_width = 0.3, species = 'setosa' } Iris { sepal_length = 5.0, sepal_width = 3.4, petal_length = 1.5, petal_width = 0.2, species = 'setosa' } Iris { sepal_length = 4.4, sepal_width = 2.9, petal_length = 1.4, petal_width = 0.2, species = 'setosa' } Iris { sepal_length = 4.9, sepal_width = 3.1, petal_length = 1.5, petal_width = 0.1, species = 'setosa' } Iris { sepal_length = 5.4, sepal_width = 3.7, petal_length = 1.5, petal_width = 0.2, species = 'setosa' } Iris { sepal_length = 4.8, sepal_width = 3.4, petal_length = 1.6, petal_width = 0.2, species = 'setosa' } Iris { sepal_length = 4.8, sepal_width = 3.0, petal_length = 1.4, petal_width = 0.1, species = 'setosa' } Iris { sepal_length = 4.3, sepal_width = 3.0, petal_length = 1.1, petal_width = 0.1, species = 'setosa' } Iris { sepal_length = 5.8, sepal_width = 4.0, petal_length = 1.2, petal_width = 0.2, species = 'setosa' } Iris { sepal_length = 5.7, sepal_width = 4.4, petal_length = 1.5, petal_width = 0.4, species = 'setosa' } Iris { sepal_length = 5.4, sepal_width = 3.9, petal_length = 1.3, petal_width = 0.4, species = 'setosa' } Iris { sepal_length = 5.1, sepal_width = 3.5, petal_length = 1.4, petal_width = 0.3, species = 'setosa' } Iris { sepal_length = 5.7, sepal_width = 3.8, petal_length = 1.7, petal_width = 0.3, species = 'setosa' } Iris { sepal_length = 5.1, sepal_width = 3.8, petal_length = 1.5, petal_width = 0.3, species = 'setosa' } Iris { sepal_length = 5.4, sepal_width = 3.4, petal_length = 1.7, petal_width = 0.2, species = 'setosa' } Iris { sepal_length = 5.1, sepal_width = 3.7, petal_length = 1.5, petal_width = 0.4, species = 'setosa' } Iris { sepal_length = 4.6, sepal_width = 3.6, petal_length = 1.0, petal_width = 0.2, species = 'setosa' } Iris { sepal_length = 5.1, sepal_width = 3.3, petal_length = 1.7, petal_width = 0.5, species = 'setosa' } Iris { sepal_length = 4.8, sepal_width = 3.4, petal_length = 1.9, petal_width = 0.2, species = 'setosa' } Iris { sepal_length = 5.0, sepal_width = 3.0, petal_length = 1.6, petal_width = 0.2, species = 'setosa' } Iris { sepal_length = 5.0, sepal_width = 3.4, petal_length = 1.6, petal_width = 0.4, species = 'setosa' } Iris { sepal_length = 5.2, sepal_width = 3.5, petal_length = 1.5, petal_width = 0.2, species = 'setosa' } Iris { sepal_length = 5.2, sepal_width = 3.4, petal_length = 1.4, petal_width = 0.2, species = 'setosa' } Iris { sepal_length = 4.7, sepal_width = 3.2, petal_length = 1.6, petal_width = 0.2, species = 'setosa' } Iris { sepal_length = 4.8, sepal_width = 3.1, petal_length = 1.6, petal_width = 0.2, species = 'setosa' } Iris { sepal_length = 5.4, sepal_width = 3.4, petal_length = 1.5, petal_width = 0.4, species = 'setosa' } Iris { sepal_length = 5.2, sepal_width = 4.1, petal_length = 1.5, petal_width = 0.1, species = 'setosa' } Iris { sepal_length = 5.5, sepal_width = 4.2, petal_length = 1.4, petal_width = 0.2, species = 'setosa' } Iris { sepal_length = 4.9, sepal_width = 3.1, petal_length = 1.5, petal_width = 0.1, species = 'setosa' } Iris { sepal_length = 5.0, sepal_width = 3.2, petal_length = 1.2, petal_width = 0.2, species = 'setosa' } Iris { sepal_length = 5.5, sepal_width = 3.5, petal_length = 1.3, petal_width = 0.2, species = 'setosa' } Iris { sepal_length = 4.9, sepal_width = 3.1, petal_length = 1.5, petal_width = 0.1, species = 'setosa' } Iris { sepal_length = 4.4, sepal_width = 3.0, petal_length = 1.3, petal_width = 0.2, species = 'setosa' } Iris { sepal_length = 5.1, sepal_width = 3.4, petal_length = 1.5, petal_width = 0.2, species = 'setosa' } Iris { sepal_length = 5.0, sepal_width = 3.5, petal_length = 1.3, petal_width = 0.3, species = 'setosa' } Iris { sepal_length = 4.5, sepal_width = 2.3, petal_length = 1.3, petal_width = 0.3, species = 'setosa' } Iris { sepal_length = 4.4, sepal_width = 3.2, petal_length = 1.3, petal_width = 0.2, species = 'setosa' } Iris { sepal_length = 5.0, sepal_width = 3.5, petal_length = 1.6, petal_width = 0.6, species = 'setosa' } Iris { sepal_length = 5.1, sepal_width = 3.8, petal_length = 1.9, petal_width = 0.4, species = 'setosa' } Iris { sepal_length = 4.8, sepal_width = 3.0, petal_length = 1.4, petal_width = 0.3, species = 'setosa' } Iris { sepal_length = 5.1, sepal_width = 3.8, petal_length = 1.6, petal_width = 0.2, species = 'setosa' } Iris { sepal_length = 4.6, sepal_width = 3.2, petal_length = 1.4, petal_width = 0.2, species = 'setosa' } Iris { sepal_length = 5.3, sepal_width = 3.7, petal_length = 1.5, petal_width = 0.2, species = 'setosa' } Iris { sepal_length = 5.0, sepal_width = 3.3, petal_length = 1.4, petal_width = 0.2, species = 'setosa' } --------------------------------------------------------------------------------------------------------------- Cluster 3 Iris { sepal_length = 6.9, sepal_width = 3.1, petal_length = 4.9, petal_width = 1.5, species = 'versicolor' } Iris { sepal_length = 6.7, sepal_width = 3.0, petal_length = 5.0, petal_width = 1.7, species = 'versicolor' } Iris { sepal_length = 6.3, sepal_width = 3.3, petal_length = 6.0, petal_width = 2.5, species = 'virginica' } Iris { sepal_length = 7.1, sepal_width = 3.0, petal_length = 5.9, petal_width = 2.1, species = 'virginica' } Iris { sepal_length = 6.3, sepal_width = 2.9, petal_length = 5.6, petal_width = 1.8, species = 'virginica' } Iris { sepal_length = 6.5, sepal_width = 3.0, petal_length = 5.8, petal_width = 2.2, species = 'virginica' } Iris { sepal_length = 7.6, sepal_width = 3.0, petal_length = 6.6, petal_width = 2.1, species = 'virginica' } Iris { sepal_length = 7.3, sepal_width = 2.9, petal_length = 6.3, petal_width = 1.8, species = 'virginica' } Iris { sepal_length = 6.7, sepal_width = 2.5, petal_length = 5.8, petal_width = 1.8, species = 'virginica' } Iris { sepal_length = 7.2, sepal_width = 3.6, petal_length = 6.1, petal_width = 2.5, species = 'virginica' } Iris { sepal_length = 6.5, sepal_width = 3.2, petal_length = 5.1, petal_width = 2.0, species = 'virginica' } Iris { sepal_length = 6.4, sepal_width = 2.7, petal_length = 5.3, petal_width = 1.9, species = 'virginica' } Iris { sepal_length = 6.8, sepal_width = 3.0, petal_length = 5.5, petal_width = 2.1, species = 'virginica' } Iris { sepal_length = 6.4, sepal_width = 3.2, petal_length = 5.3, petal_width = 2.3, species = 'virginica' } Iris { sepal_length = 6.5, sepal_width = 3.0, petal_length = 5.5, petal_width = 1.8, species = 'virginica' } Iris { sepal_length = 7.7, sepal_width = 3.8, petal_length = 6.7, petal_width = 2.2, species = 'virginica' } Iris { sepal_length = 7.7, sepal_width = 2.6, petal_length = 6.9, petal_width = 2.3, species = 'virginica' } Iris { sepal_length = 6.9, sepal_width = 3.2, petal_length = 5.7, petal_width = 2.3, species = 'virginica' } Iris { sepal_length = 7.7, sepal_width = 2.8, petal_length = 6.7, petal_width = 2.0, species = 'virginica' } Iris { sepal_length = 6.7, sepal_width = 3.3, petal_length = 5.7, petal_width = 2.1, species = 'virginica' } Iris { sepal_length = 7.2, sepal_width = 3.2, petal_length = 6.0, petal_width = 1.8, species = 'virginica' } Iris { sepal_length = 6.4, sepal_width = 2.8, petal_length = 5.6, petal_width = 2.1, species = 'virginica' } Iris { sepal_length = 7.2, sepal_width = 3.0, petal_length = 5.8, petal_width = 1.6, species = 'virginica' } Iris { sepal_length = 7.4, sepal_width = 2.8, petal_length = 6.1, petal_width = 1.9, species = 'virginica' } Iris { sepal_length = 7.9, sepal_width = 3.8, petal_length = 6.4, petal_width = 2.0, species = 'virginica' } Iris { sepal_length = 6.4, sepal_width = 2.8, petal_length = 5.6, petal_width = 2.2, species = 'virginica' } Iris { sepal_length = 7.7, sepal_width = 3.0, petal_length = 6.1, petal_width = 2.3, species = 'virginica' } Iris { sepal_length = 6.3, sepal_width = 3.4, petal_length = 5.6, petal_width = 2.4, species = 'virginica' } Iris { sepal_length = 6.4, sepal_width = 3.1, petal_length = 5.5, petal_width = 1.8, species = 'virginica' } Iris { sepal_length = 6.9, sepal_width = 3.1, petal_length = 5.4, petal_width = 2.1, species = 'virginica' } Iris { sepal_length = 6.7, sepal_width = 3.1, petal_length = 5.6, petal_width = 2.4, species = 'virginica' } Iris { sepal_length = 6.9, sepal_width = 3.1, petal_length = 5.1, petal_width = 2.3, species = 'virginica' } Iris { sepal_length = 6.8, sepal_width = 3.2, petal_length = 5.9, petal_width = 2.3, species = 'virginica' } Iris { sepal_length = 6.7, sepal_width = 3.3, petal_length = 5.7, petal_width = 2.5, species = 'virginica' } Iris { sepal_length = 6.7, sepal_width = 3.0, petal_length = 5.2, petal_width = 2.3, species = 'virginica' } Iris { sepal_length = 6.5, sepal_width = 3.0, petal_length = 5.2, petal_width = 2.0, species = 'virginica' } Iris { sepal_length = 6.2, sepal_width = 3.4, petal_length = 5.4, petal_width = 2.3, species = 'virginica' } ---------------------------------------------------------------------------------------------------------------
As can be seen not all Irises have been clustered correctly. Also note that, this is a sample output, due to Forgy initialization method.
Main.java
// ? 2018 TheFlyingKeyboard and released under MIT License // theflyingkeyboard.net public class Main { public static void main(String[] args) { CSVLoader csvLoader = new CSVLoader(); KMeansIris kMeansIris = new KMeansIris(csvLoader.load("iris.csv"), 3); kMeansIris.cluster(); kMeansIris.printClusters(); } }
KMeansIris.java
import java.util.ArrayList; import java.util.List; import java.util.concurrent.ThreadLocalRandom; // ? 2018 TheFlyingKeyboard and released under MIT License // theflyingkeyboard.net public class KMeansIris { private List<Iris> irises; private List<Iris> centroids = new ArrayList<>(); private List<List<Iris>> clusters = new ArrayList<>(); private List<Iris> previousCentroids = new ArrayList<>(); public KMeansIris(List<Iris> irises, int k) { this.irises = irises; for (int i = 0; i < k; ++i) { centroids.add(new Iris(irises.get(ThreadLocalRandom.current().nextInt(irises.size() / k) * (i + 1)))); //Forgy Initialization Method. previousCentroids.add(new Iris(centroids.get(i))); clusters.add(new ArrayList<>()); } } public void cluster() { boolean isOver = false; int counter = 0; while (!isOver) { clearClusters(); addPointsToClusters(); moveCentroids(); ++counter; isOver = !haveCentroidsMoved(); } System.out.println("It took " + counter + " iterations to cluster."); } public void printClusters() { for (int i = 0; i < clusters.size(); ++i) { System.out.println("Cluster " + (i + 1)); for (int j = 0; j < clusters.get(i).size(); ++j) { System.out.println(clusters.get(i).get(j)); } System.out.println("---------------------------------------------------------------------------------------------------------------"); } } private boolean haveCentroidsMoved() { for (int i = 0; i < centroids.size(); ++i) { if (!centroids.get(i).equals(previousCentroids.get(i))) { return true; } } return false; } private void clearClusters() { for (int i = 0; i < clusters.size(); ++i) { clusters.get(i).clear(); } } private void addPointsToClusters() { for (int i = 0; i < irises.size(); ++i) { clusters.get(findClosestCentroid(irises.get(i))).add(irises.get(i)); } } private void moveCentroids() { double x = 0.0d; double y = 0.0d; double z = 0.0d; double w = 0.0d; for (int i = 0; i < clusters.size(); ++i) { for (int j = 0; j < clusters.get(i).size(); ++j) { x += clusters.get(i).get(j).getX(); y += clusters.get(i).get(j).getY(); z += clusters.get(i).get(j).getZ(); w += clusters.get(i).get(j).getW(); } x /= clusters.get(i).size(); y /= clusters.get(i).size(); z /= clusters.get(i).size(); w /= clusters.get(i).size(); previousCentroids.set(i, new Iris(centroids.get(i))); centroids.set(i, new Iris(x, y, z, w, "Centroid")); } } private int findClosestCentroid(Iris a) { double minDistance = distance(a, centroids.get(0)); double distance; int closestCentroid = 0; for (int i = 1; i < centroids.size(); ++i) { distance = distance(a, centroids.get(i)); if (distance < minDistance) { minDistance = distance; closestCentroid = i; } } return closestCentroid; } private double distance(Iris a, Iris b) { return Math.sqrt((a.getX() - b.getX()) * (a.getX() - b.getX()) + (a.getY() - b.getY()) * (a.getY() - b.getY()) + (a.getZ() - b.getZ()) * (a.getZ() - b.getZ()) + (a.getW() - b.getW()) * (a.getW() - b.getW())); } }
CSVLoader.java
import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; import java.util.List; // ? 2018 TheFlyingKeyboard and released under MIT License // theflyingkeyboard.net public class CSVLoader { public List<Iris> load(String fileName) { String line; List<Iris> iris = new ArrayList<>(); try { BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName)); while ((line = bufferedReader.readLine()) != null) { String[] data = line.split(","); iris.add(new Iris(Double.parseDouble(data[0]), Double.parseDouble(data[1]), Double.parseDouble(data[2]), Double.parseDouble(data[3]), data[4])); } bufferedReader.close(); } catch (Exception e) { e.printStackTrace(); } return iris; } }
Iris.java
// ? 2018 TheFlyingKeyboard and released under MIT License // theflyingkeyboard.net public class Iris { private final double x; //Sepal Length private final double y; //Sepal Width private final double z; //Petal Length private final double w; //Petal Width private final String name; public Iris(double x, double y, double z, double w, String name) { this.x = x; this.y = y; this.z = z; this.w = w; this.name = name; } public Iris(Iris iris) { this.x = iris.x; this.y = iris.y; this.z = iris.z; this.w = iris.w; this.name = iris.name; } public double getX() { return x; } public double getY() { return y; } public double getZ() { return z; } public double getW() { return w; } @Override public String toString() { return "Iris { " + "sepal_length = " + x + ", sepal_width = " + y + ", petal_length = " + z + ", petal_width = " + w + ", species = '" + name + '\'' + " }"; } @Override public boolean equals(Object object) { if (this == object) return true; if (object == null || getClass() != object.getClass()) return false; Iris iris = (Iris) object; return Double.compare(iris.x, x) == 0 && Double.compare(iris.y, y) == 0 && Double.compare(iris.z, z) == 0 && Double.compare(iris.w, w) == 0 && name.equals(((Iris) object).name); } }
Java KMeans Iris Dataset