examples.ml.example5.example.md Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jstat Show documentation
Show all versions of jstat Show documentation
Java Library for Statistical Analysis.
The newest version!
# Example 5: Clustering with KMeans
## Contents
* [Overview](#overview)
* [KMeans algorithm](#kmeans_algorithm)
* [Import files](#include_files)
* [The main function](#m_func)
* [Results](#results)
* [Source Code](#source_code)
## Overview
### KMeans algorithm
## Import files
```
package examples.ml.example5;
import utils.DefaultIterativeAlgorithmController;
import base.CommonConstants;
import datastructs.IVector;
import datasets.DenseMatrixSet;
import datastructs.RowBuilder;
import datastructs.RowType;
import maths.functions.distances.DistanceCalculator;
import maths.functions.distances.EuclideanVectorCalculator;
import maths.functions.generators.IRandomGenerator;
import maths.functions.generators.UniformRandomGenerator;
import ml.clustering.KMeans;
import ml.clustering.KMeansInput;
```
## The main function
```
public class Example5 {
public static void main(String[] args){
//some synthetic data
DenseMatrixSet matrix = new DenseMatrixSet(RowType.Type.DOUBLE_VECTOR, new RowBuilder(), 6, 2, 0.0);
matrix.set(0, 1.0, 2.0);
matrix.set(1, 1.0, 4.0);
matrix.set(2, 1.0, 0.0);
matrix.set(3, 10.0, 2.0);
matrix.set(4, 10.0, 2.0);
matrix.set(5, 10.0, 0.0);
KMeansInput input = new KMeansInput();
input.k = 2;
input.iterationContorller = new DefaultIterativeAlgorithmController(10, CommonConstants.getTol());
KMeans kmeans = new KMeans<>(input);
DistanceCalculator, Double> similarity = new EuclideanVectorCalculator();
IRandomGenerator randomGenerator = new UniformRandomGenerator();
kmeans.cluster(matrix, similarity, randomGenerator);
}
}
```
## Results
```
```
## Source Code
Example5.java