资源简介
多维k-means聚类算法java简单实现,导入运行KmeansTest.java可看到结果
代码片段和文件信息
package com.kmeans;
import java.util.Arrays;
import java.util.Collections;
import java.util.linkedList;
import java.util.List;
import java.util.Random;
/**
*
* @author Yuanbo She
*
*/
public class Kmeans {
/**
* double[][] 元素全置0
*
* @param matrix
* double[][]
* @param highDim
* int
* @param lowDim
* int
* double[highDim][lowDim]
*/
private static void setDouble2Zero(double[][] matrix int highDim int lowDim) {
for (int i = 0; i < highDim; i++) {
for (int j = 0; j < lowDim; j++) {
matrix[i][j] = 0;
}
}
}
/**
* 拷贝源二维矩阵元素到目标二维矩阵。 foreach (dests[highDim][lowDim] = sources[highDim][lowDim]);
*
* @param dests
* double[][]
* @param sources
* double[][]
* @param highDim
* int
* @param lowDim
* int
*/
private static void copyCenters(double[][] dests double[][] sources int highDim int lowDim) {
for (int i = 0; i < highDim; i++) {
for (int j = 0; j < lowDim; j++) {
dests[i][j] = sources[i][j];
}
}
}
/**
* 更新聚类中心坐标
*
* @param k
* int 分类个数
* @param data
* kmeans_data
*/
private static void updateCenters(int k Kmeans_data data) {
double[][] centers = data.centers;
setDouble2Zero(centers k data.dim);
int[] labels = data.labels;
int[] centerCounts = data.centerCounts;
for (int i = 0; i < data.dim; i++) {
for (int j = 0; j < data.length; j++) {
centers[labels[j]][i] += data.data[j][i];
}
}
for (int i = 0; i < k; i++) {
for (int j = 0; j < data.dim; j++) {
centers[i][j] = centers[i][j] / centerCounts[i];
}
}
}
/**
* 计算两点欧氏距离
*
* @param pa
* double[]
* @param pb
* double[]
* @param dim
* int 维数
* @return double 距离
*/
public static double dist(double[] pa double[] pb int dim) {
double rv = 0;
for (int i = 0; i < dim; i++) {
double temp = pa[i] - pb[i];
temp = temp * temp;
rv += temp;
}
return Math.sqrt(rv);
}
/**
* 做Kmeans运算
*
* @param k
* int 聚类个数
* @param data
* kmeans_data kmeans数据类
* @param param
* kmeans_param kmeans参数类
* @return kmeans_result kmeans运行信息类
*/
public static Kmeans_result doKmeans(int k Kmeans_data data Kmeans_param param) {
// 预处理
double[][] centers = new double[k][data.dim]; // 聚类中心点集
data.cente
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 301 2014-05-06 16:56 kmeans\.classpath
文件 382 2014-05-06 16:56 kmeans\.project
文件 78 2014-05-06 16:56 kmeans\.settings\org.eclipse.core.resources.prefs
文件 598 2014-05-06 16:56 kmeans\.settings\org.eclipse.jdt.core.prefs
文件 5167 2014-05-06 17:01 kmeans\bin\com\kmeans\Kmeans.class
文件 1483 2014-05-06 17:01 kmeans\bin\com\kmeans\KmeansTest.class
文件 501 2014-05-06 17:01 kmeans\bin\com\kmeans\Kmeans_data.class
文件 627 2014-05-06 17:01 kmeans\bin\com\kmeans\Kmeans_param.class
文件 358 2014-05-06 17:01 kmeans\bin\com\kmeans\Kmeans_result.class
文件 24 2014-05-06 16:44 kmeans\bin\com\kmeans\readme.txt
文件 8176 2014-05-06 16:56 kmeans\src\com\kmeans\Kmeans.java
文件 709 2014-05-06 16:56 kmeans\src\com\kmeans\KmeansTest.java
文件 879 2014-05-06 16:56 kmeans\src\com\kmeans\Kmeans_data.java
文件 529 2014-05-06 17:01 kmeans\src\com\kmeans\Kmeans_param.java
文件 286 2014-05-06 16:56 kmeans\src\com\kmeans\Kmeans_result.java
文件 24 2014-05-06 16:44 kmeans\src\com\kmeans\readme.txt
评论
共有 条评论