Hej, w ramach mini projektu na uczelnię muszę napisać program grupujący kwiaty przy użyciu k-means.
Sczytuję z pliku kwiatki, wrzucam do listy. Z klawiatury pobieram od użytkownika k, następnie tworzę k centroidów i wrzucam je do listy. Teraz muszę zacząć liczyć odległości między poszczególnymi centroidami a kwiatkami, wybierać najmniejsze wartości i przyporządkowywać do odpowiedniego centroidu.
Oto moje klasy:
public class Iris {
private Double v1;
private Double v2;
private Double v3;
private Double v4;
private String species;
// constructor, getter, toString, equals and hashcode
}
public class Centroid {
private Double x;
private Double y;
private Double z;
private Double a;
// constructor, getter, toString, equals and hashcode
public class EuclideanDistance implements Distance {
@Override
public Double calculate(Centroid point1, Iris point2) {
Double sum = null;
Double xPow = null;
Double yPow = null;
Double zPow = null;
Double aPow = null;
xPow = Math.pow(point1.getX() - point2.getV1(), 2);
yPow = Math.pow(point1.getY() - point2.getV2(), 2);
zPow = Math.pow(point1.getY() - point2.getV3(), 2);
aPow = Math.pow(point1.getA() - point2.getV4(), 2);
sum = xPow + yPow + zPow + aPow;
return Math.sqrt(sum);
}
}
W mainie na razie mam taki bałagan:
FileReader fr = new FileReader();
List<Iris> irisList = fr.fileReader("iris_training.txt");
Scanner scanner = new Scanner(System.in);
System.out.println("Podaj liczbę grup k");
int k = scanner.nextInt();
List<Centroid> centroidList = new ArrayList<>();
for (int i = 0; i < k; i++) {
Centroid centroid = new Centroid();
centroidList.add(centroid);
}
Map<Centroid, List<Iris>> centroidIrisMap = new HashMap<>();
for (int i = 0; i < k; i++) {
centroidIrisMap.put(centroidList.get(i), irisList);
}
Ktoś podpowie jak najlepiej to ugryźć? Oczywiście nie mogę używać bibliotek. Wszystko muszę zrobić w pętlach, ifach.