Java K-均值算法
朋友们,我希望得到你们的帮助,我正在用Java开发K-Means算法,所以我已经执行了个周期,对数据进行了相应的质心操作,并确定了距离,但是每个质心的距离在个周期中是不同的。现在我需要的是把所有这些结果放在周期之外。我附上部分代码Java K-均值算法,java,arrays,Java,Arrays,朋友们,我希望得到你们的帮助,我正在用Java开发K-Means算法,所以我已经执行了个周期,对数据进行了相应的质心操作,并确定了距离,但是每个质心的距离在个周期中是不同的。现在我需要的是把所有这些结果放在周期之外。我附上部分代码 System.out.println("\n" + "----------" + "\n" + "Cluster K" + "\n" + "----------"); System.out.println(); for (int i
System.out.println("\n" + "----------" + "\n" + "Cluster K" + "\n" + "----------");
System.out.println();
for (int i = 0; i < Datos.length; i++) {
distanciaK = (float) Math.sqrt(Math.pow(Datos[i][0] - Datos[10][0], 2) + Math.pow(Datos[i][1] - Datos[10][1], 2) + Math.pow(Datos[i][2] - Datos[10][2], 2) + Math.pow(Datos[i][3] - Datos[10][3], 2) + Math.pow(Datos[i][4] - Datos[10][4], 2));
System.out.println(distanciaK);
}
System.out.println("\n" + "----------" + "\n" + "Cluster M" + "\n" + "----------");
System.out.println();
for (int i = 0; i < Datos.length; i++) {
distanciaM = (float) Math.sqrt(Math.pow(Datos[i][0] - Datos[12][0], 2) + Math.pow(Datos[i][1] - Datos[12][1], 2) + Math.pow(Datos[i][2] - Datos[12][2], 2) + Math.pow(Datos[i][3] - Datos[12][3], 2) + Math.pow(Datos[i][4] - Datos[12][4], 2));
System.out.println(distanciaM);
}
System.out.println();
System.out.println(“\n”+“------------”+“\n”+“集群K”+“\n”+“------------”);
System.out.println();
对于(int i=0;i
代码输出
簇K
9.0
8.485281
9.380832
6.3245554
4.2426405
3.6055512
7.615773
8.83176
2.828427
5.8309517
0
4.7958317
5.477226
5.196152
群M 5.196152 4.8989797 9.486833 6.928203 4.8989797 3.8729835 7.071068 7.071068 3.1622777 4.2426405 5.477226 6.708204 0 1.0 导入以上软件包 已创建映射,其名称为群集名称,值为表示群集的列表。 将方法添加到相应的地图条目中
System.out.println("\n" + "----------" + "\n" + "Cluster K" + "\n" + "----------");
System.out.println();
Map<String,List<Float>> clusterMeanInfo = new HashMap<>();
clusterMeanInfo.put("Cluster K",new ArrayList<>());
clusterMeanInfo.put("Cluster M",new ArrayList<>());
for (int i = 0; i < Datos.length; i++) {
distanciaK = (float) Math.sqrt(Math.pow(Datos[i][0] - Datos[10][0], 2) + Math.pow(Datos[i][1] - Datos[10][1], 2) + Math.pow(Datos[i][2] - Datos[10][2], 2) + Math.pow(Datos[i][3] - Datos[10][3], 2) + Math.pow(Datos[i][4] - Datos[10][4], 2));
System.out.println(distanciaK);
clusterMeanInfo.get("Cluster K").add(distanciaK);
}
System.out.println("\n" + "----------" + "\n" + "Cluster M" + "\n" + "----------");
System.out.println();
for (int i = 0; i < Datos.length; i++) {
distanciaM = (float) Math.sqrt(Math.pow(Datos[i][0] - Datos[12][0], 2) + Math.pow(Datos[i][1] - Datos[12][1], 2) + Math.pow(Datos[i][2] - Datos[12][2], 2) + Math.pow(Datos[i][3] - Datos[12][3], 2) + Math.pow(Datos[i][4] - Datos[12][4], 2));
System.out.println(distanciaM);
kMeans.add(distanciaM);
clusterMeanInfo.get("Cluster K").add(distanciaM);
}
for (String key :clusterMeanInfo.keySet()) {
System.out.print(key + ' ');
clusterMeanInfo.get(key).forEach( value -> {
System.out.print(value + ' ');
});
System.out.println();
}
System.out.println(“\n”+“------------”+“\n”+“集群K”+“\n”+“------------”);
System.out.println();
Map clusterMeanInfo=新HashMap();
clusterMeanInfo.put(“clusterk”,newarraylist());
clusterMeanInfo.put(“Cluster M”,new ArrayList());
对于(int i=0;i{
系统输出打印(值+“”);
});
System.out.println();
}
Hello friend,修改代码,如果它出现了。有人问代码KMeans.add(DistanceM)的一部分,KMeans是指类名吗?结果出来了,但一切都是水平的,因为我尝试了换行,但它不起作用,我想以表格的形式显示这些数据。**{Cluster K=[9.0,8.485281,9.380832,6.3245554,4.2426405,3.6055512,7.615773,8.83176,2.828427,5.8309517,0.0,4.7958317,5.477226,5.196152],Cluster M=[5.196152、4.898989797、9.486833、6.928203、4.898989797、3.8729835、7.071068、7.071068、3.1622777、4.2426405、5.477226、6.708204、0.0、1.0],聚类D=[7.1414285、7.071068、3.1622777、0.0.0、9.273619、6.8556547、2.828427、4.690416、6.78233、10.198039、3246.5555584、924]**将值打印为格式化值,不完全是表格格式,但更具可读性
System.out.println("\n" + "----------" + "\n" + "Cluster K" + "\n" + "----------");
System.out.println();
Map<String,List<Float>> clusterMeanInfo = new HashMap<>();
clusterMeanInfo.put("Cluster K",new ArrayList<>());
clusterMeanInfo.put("Cluster M",new ArrayList<>());
for (int i = 0; i < Datos.length; i++) {
distanciaK = (float) Math.sqrt(Math.pow(Datos[i][0] - Datos[10][0], 2) + Math.pow(Datos[i][1] - Datos[10][1], 2) + Math.pow(Datos[i][2] - Datos[10][2], 2) + Math.pow(Datos[i][3] - Datos[10][3], 2) + Math.pow(Datos[i][4] - Datos[10][4], 2));
System.out.println(distanciaK);
clusterMeanInfo.get("Cluster K").add(distanciaK);
}
System.out.println("\n" + "----------" + "\n" + "Cluster M" + "\n" + "----------");
System.out.println();
for (int i = 0; i < Datos.length; i++) {
distanciaM = (float) Math.sqrt(Math.pow(Datos[i][0] - Datos[12][0], 2) + Math.pow(Datos[i][1] - Datos[12][1], 2) + Math.pow(Datos[i][2] - Datos[12][2], 2) + Math.pow(Datos[i][3] - Datos[12][3], 2) + Math.pow(Datos[i][4] - Datos[12][4], 2));
System.out.println(distanciaM);
kMeans.add(distanciaM);
clusterMeanInfo.get("Cluster K").add(distanciaM);
}
for (String key :clusterMeanInfo.keySet()) {
System.out.print(key + ' ');
clusterMeanInfo.get(key).forEach( value -> {
System.out.print(value + ' ');
});
System.out.println();
}