调用weka包进行kmeans聚类

声明:本博客由 Lilian原创,如需使用请在开头引用或者添加转载字样,谢谢配合。同时也仅代表个人观点。

目录

所用数据文件:data1.txt

@RELATION data1


@ATTRIBUTE one REAL
@ATTRIBUTE two REAL




@DATA
0.184000 0.482000
0.152000 0.540000
0.152000 0.596000
0.178000 0.626000
0.206000 0.598000
0.230000 0.562000
0.224000 0.524000
0.204000 0.540000
0.190000 0.572000
0.216000 0.608000
0.240000 0.626000
0.256000 0.584000
0.272000 0.546000
0.234000 0.468000
0.222000 0.490000
0.214000 0.414000
0.252000 0.336000
0.298000 0.336000
0.316000 0.376000
0.318000 0.434000
0.308000 0.480000
0.272000 0.408000
0.272000 0.462000
0.280000 0.524000
0.296000 0.544000
0.340000 0.534000
0.346000 0.422000
0.354000 0.356000
0.160000 0.282000
0.160000 0.282000
0.156000 0.398000
0.138000 0.466000
0.154000 0.442000
0.180000 0.334000
0.184000 0.300000
0.684000 0.420000
0.678000 0.494000
0.710000 0.592000
0.716000 0.508000
0.744000 0.528000
0.716000 0.540000
0.692000 0.540000
0.696000 0.494000
0.722000 0.466000
0.738000 0.474000
0.746000 0.484000
0.750000 0.500000
0.746000 0.440000
0.718000 0.446000
0.692000 0.466000
0.746000 0.418000
0.768000 0.460000
0.272000 0.290000
0.240000 0.376000
0.212000 0.410000
0.154000 0.564000
0.252000 0.704000
0.298000 0.714000
0.314000 0.668000
0.326000 0.566000
0.344000 0.468000
0.324000 0.632000
0.164000 0.688000
0.216000 0.684000
0.392000 0.682000
0.392000 0.628000
0.392000 0.518000
0.398000 0.502000
0.392000 0.364000
0.360000 0.308000
0.326000 0.308000
0.402000 0.342000
0.404000 0.418000
0.634000 0.458000
0.650000 0.378000
0.698000 0.348000
0.732000 0.350000
0.766000 0.364000
0.800000 0.388000
0.808000 0.428000
0.826000 0.466000
0.842000 0.510000
0.842000 0.556000
0.830000 0.594000
0.772000 0.646000
0.708000 0.654000
0.632000 0.640000
0.628000 0.564000
0.624000 0.352000
0.650000 0.286000
0.694000 0.242000
0.732000 0.214000
0.832000 0.214000
0.832000 0.264000
0.796000 0.280000
0.778000 0.288000
0.770000 0.294000
0.892000 0.342000
0.910000 0.366000
0.910000 0.394000
0.872000 0.382000
0.774000 0.314000
0.718000 0.252000
0.688000 0.284000
0.648000 0.322000
0.602000 0.460000
0.596000 0.496000
0.570000 0.550000
0.564000 0.592000
0.574000 0.624000
0.582000 0.644000
0.596000 0.664000
0.662000 0.704000
0.692000 0.722000
0.710000 0.736000
0.848000 0.732000
0.888000 0.686000
0.924000 0.514000
0.914000 0.470000
0.880000 0.492000
0.848000 0.706000
0.730000 0.736000
0.676000 0.734000
0.628000 0.732000
0.782000 0.708000
0.806000 0.674000
0.830000 0.630000
0.564000 0.730000
0.554000 0.538000
0.570000 0.502000
0.572000 0.432000
0.590000 0.356000
0.652000 0.232000
0.676000 0.178000
0.684000 0.152000
0.728000 0.172000
0.758000 0.148000
0.864000 0.176000
0.646000 0.242000
0.638000 0.254000
0.766000 0.276000
0.882000 0.278000
0.900000 0.278000
0.906000 0.302000
0.892000 0.316000
0.570000 0.324000
0.798000 0.150000
0.832000 0.114000
0.714000 0.156000
0.648000 0.154000
0.644000 0.212000
0.642000 0.250000
0.658000 0.284000
0.710000 0.296000
0.794000 0.288000
0.846000 0.260000
0.856000 0.304000
0.858000 0.392000
0.858000 0.476000
0.778000 0.640000
0.736000 0.662000
0.718000 0.690000
0.634000 0.692000
0.596000 0.710000
0.570000 0.720000
0.554000 0.732000
0.548000 0.686000
0.524000 0.740000
0.598000 0.768000
0.660000 0.796000

前言:Kmeans是一种非常经典的聚类算法。它利用簇的中心到对象的距离来分配每个对象的簇所属关系。同时迭代的进行簇的中心的更新以及簇分配的更新,直到收敛。

下面是调用weka包中实现的kmeans的代码

package others;

import java.io.File;

import weka.clusterers.SimpleKMeans;
import weka.core.DistanceFunction;
import weka.core.Instances;
import weka.core.converters.ArffLoader;

public class ArrayListTest {

	public static void main(String[] args){
		Instances ins = null;
		
		SimpleKMeans KM = null;
		DistanceFunction disFun = null;
		
		try {
			// 读入样本数据
			File file = new File("data/data1.txt");
			ArffLoader loader = new ArffLoader();
			loader.setFile(file);
			ins = loader.getDataSet();
			
			// 初始化聚类器 (加载算法)
			KM = new SimpleKMeans();
			KM.setNumClusters(4); 		//设置聚类要得到的类别数量
			KM.buildClusterer(ins);		//开始进行聚类
			System.out.println(KM.preserveInstancesOrderTipText());
			// 打印聚类结果
			System.out.println(KM.toString());
			
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
}