资源简介
从网上找了很多资料,发现都没有太合适的,而本程序则通过修改整理,包括完整数据集文件,代码文件,导入即可运行,有什么不合理的欢迎相互探讨。
代码片段和文件信息
package p;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Random;
public class KAverage {
private int sampleCount = 0;
private int dimensionCount = 0;
private int centerCount = 0;
private double[][] sampleValues;
private double[][] centers;
private double[][] tmpCenters;
private String dataFile = ““;
/**
* 通过构造器传入数据文件
*/
public KAverage(String dataFile) throws NumberInvalieException {
this.dataFile = dataFile;
}
/**
* 第一行为s;d;c含义分别为样例的数目,每个样例特征的维数,聚类中心个数 文件格式为d[d]...;d[d]... 如:12;23;15
* 每一维之间用隔开,每个样例间用;隔开。结尾没有‘;‘ 可以有多行
*/
private int initData(String fileName) {
String line;
String samplesValue[];
String dimensionsValue[] = new String[dimensionCount];
BufferedReader in;
try {
in = new BufferedReader(new FileReader(fileName));
} catch (FileNotFoundException e) {
e.printStackTrace();
return -1;
}
/*
* 预处理样本,允许后面几维为0时,不写入文件
*/
for (int i = 0; i < sampleCount; i++) {
for (int j = 0; j < dimensionCount; j++) {
sampleValues[i][j] = 0;
}
}
int i = 0;
double tmpValue = 0.0;
try {
line = in.readLine();
String params[] = line.split(“;“);
if (params.length != 3) {// 必须为3个参数,否则错误
return -1;
}
/**
* 获取参数
*/
this.sampleCount = Integer.parseInt(params[0]);
this.dimensionCount = Integer.parseInt(params[1]);
this.centerCount = Integer.parseInt(params[2]);
if (sampleCount <= 0 || dimensionCount <= 0 || centerCount <= 0) {
throw new NumberInvalieException(“input number <= 0.“);
}
if (sampleCount < centerCount) {
throw new NumberInvalieException(
“sample number < center number“);
}
sampleValues = new double[sampleCount][dimensionCount + 1];
centers = new double[centerCount][dimensionCount];
tmpCenters = new double[centerCount][dimensionCount];
while ((line = in.readLine()) != null) {
samplesValue = line.split(“;“);
for (int j = 0; j < samplesValue.length; j++) {
dimensionsValue = samplesValue[j].split(““);
for (int k = 0; k < dimensionsValue.length; k++) {
tmpValue = Double.parseDouble(dimensionsValue[k]);
sampleValues[i][k] = tmpValue;
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2015-04-15 16:31 Kmeans\
文件 299 2015-04-15 16:33 Kmeans\.classpath
文件 382 2015-04-15 16:31 Kmeans\.project
目录 0 2015-04-15 16:31 Kmeans\.settings\
文件 598 2015-04-15 16:33 Kmeans\.settings\org.eclipse.jdt.core.prefs
目录 0 2015-04-15 16:33 Kmeans\bin\
目录 0 2015-04-15 20:51 Kmeans\bin\p\
文件 5699 2015-04-16 10:06 Kmeans\bin\p\KAverage.class
文件 765 2015-04-15 16:33 Kmeans\bin\p\NumberInvalieException.class
文件 47 2015-04-15 20:20 Kmeans\bin\p\sample.txt
目录 0 2015-04-15 16:31 Kmeans\src\
目录 0 2015-04-15 17:01 Kmeans\src\p\
文件 9890 2015-04-16 10:06 Kmeans\src\p\KAverage.java
文件 517 2015-04-15 16:32 Kmeans\src\p\NumberInvalieException.java
文件 492 2015-04-16 09:40 Kmeans\src\p\sample.txt
评论
共有 条评论