Invoke the Weka packet for Kmeans Clustering (Java) __ Data mining

Source: Internet
Author: User

Data file used: 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.608 000 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.2800 00 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.49400 0 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.4  66000 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.3920 00 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.38800 0 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.6  54000 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.91
0000 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.70600 0 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.7  30000 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.88
2000 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.288 000 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.7
 96000

Kmeans is a kind of very classical clustering algorithm. It uses the center of the cluster to the distance of the object to allocate the cluster affiliation of each object. At the same time, the update of the center of the cluster and the update of the cluster distribution, until convergence.


Here is the code that calls the Kmeans implemented in the Weka package


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 {
			//Read sample data
			file = new file ("Data/data1.txt");
			Arffloader loader = new Arffloader ();
			Loader.setfile (file);
			INS = Loader.getdataset ();
			
			Initialize the cluster (loading algorithm)
			KM = new Simplekmeans ();
			Km.setnumclusters (4); 		Set the number of categories to get the cluster
			km.buildclusterer (INS);		Start the cluster
			System.out.println (Km.preserveinstancesordertiptext ());
			Print clustering Results
			System.out.println (km.tostring ());
			
		} catch (Exception e) {
			e.printstacktrace ();
		}
	}
}




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.