Menu

[572a65]: / src / junit / clustering / TestKMeans.java  Maximize  Restore  History

Download this file

108 lines (94 with data), 2.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
/**
* %SVN.HEADER%
*/
package junit.clustering;
import java.io.File;
import java.io.IOException;
import net.sf.javaml.clustering.Clusterer;
import net.sf.javaml.clustering.KMeans;
import net.sf.javaml.core.Dataset;
import net.sf.javaml.core.DefaultDataset;
import net.sf.javaml.core.SparseInstance;
import net.sf.javaml.distance.EuclideanDistance;
import net.sf.javaml.tools.data.FileHandler;
import org.junit.Assert;
import org.junit.Test;
public class TestKMeans {
/**
* Test endless loop
*/
@Test
public void testEndless() {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
SparseInstance i1 = new SparseInstance(2);
SparseInstance i2 = new SparseInstance(2);
i1.put(0, 1d);
i2.put(1, 1d);
Dataset dataset = new DefaultDataset();
dataset.add(i1);
dataset.add(i2);
KMeans cluster = new KMeans(2, 1);
cluster.cluster(dataset);
}
});
t.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/* If it is still alive, it is endlessly looping */
Assert.assertFalse(t.isAlive());
}
/**
* Tests the k-means algorithm with default parameter settings.
*/
@Test
public void testKMean() {
try {
/* Load a dataset */
Dataset data = FileHandler.loadDataset(new File("devtools/data/iris.data"), 4, ",");
/*
* Create a new instance of the KMeans algorithm, with no options
* specified. By default this will generate 4 clusters.
*/
Clusterer km = new KMeans();
/*
* Cluster the data, it will be returned as an array of data sets,
* with each dataset representing a cluster
*/
Dataset[] clusters = km.cluster(data);
System.out.println("Cluster count: " + clusters.length);
} catch (IOException e) {
Assert.assertTrue(false);
}
}
/**
* Tests the k-means algorithm with user-specified parameter settings.
*/
@Test
public void testKMeanWithParameters() {
try {
/* Load a dataset */
Dataset data = FileHandler.loadDataset(new File("devtools/data/iris.data"), 4, ",");
/*
* Create a new instance of the KMeans algorithm, with all options
* specified. This instance of the k-means algorithm will generate 3
* clusters, will run for 100 iteration and will use the euclidean
* distance.
*/
Clusterer km = new KMeans(3, 100, new EuclideanDistance());
/*
* Cluster the data, it will be returned as an array of data sets,
* with each data set representing a cluster
*/
Dataset[] clusters = km.cluster(data);
System.out.println("Cluster count: " + clusters.length);
} catch (IOException e) {
Assert.assertTrue(false);
}
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.