本文整理匯總了Java中smile.clustering.KMeans類的典型用法代碼示例。如果您正苦於以下問題:Java KMeans類的具體用法?Java KMeans怎麽用?Java KMeans使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
KMeans類屬於smile.clustering包,在下文中一共展示了KMeans類的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: learnGaussianRadialBasis
import smile.clustering.KMeans; //導入依賴的package包/類
/**
* Learns Gaussian RBF function and centers from data. The centers are
* chosen as the centroids of K-Means. Let d<sub>max</sub> be the maximum
* distance between the chosen centers, the standard deviation (i.e. width)
* of Gaussian radial basis function is d<sub>max</sub> / sqrt(2*k), where
* k is number of centers. This choice would be close to the optimal
* solution if the data were uniformly distributed in the input space,
* leading to a uniform distribution of centroids.
* @param x the training dataset.
* @param centers an array to store centers on output. Its length is used as k of k-means.
* @return a Gaussian RBF function with parameter learned from data.
*/
public static GaussianRadialBasis learnGaussianRadialBasis(double[][] x, double[][] centers) {
int k = centers.length;
KMeans kmeans = new KMeans(x, k, 10);
System.arraycopy(kmeans.centroids(), 0, centers, 0, k);
double r0 = 0.0;
for (int i = 0; i < k; i++) {
for (int j = 0; j < i; j++) {
double d = Math.distance(centers[i], centers[j]);
if (r0 < d) {
r0 = d;
}
}
}
r0 /= Math.sqrt(2*k);
return new GaussianRadialBasis(r0);
}
示例2: learn
import smile.clustering.KMeans; //導入依賴的package包/類
@Override
public JComponent learn() {
long clock = System.currentTimeMillis();
KMeans kmeans = new KMeans(dataset[datasetIndex], clusterNumber, 100, 4);
System.out.format("K-Means clusterings %d samples in %dms\n", dataset[datasetIndex].length, System.currentTimeMillis()-clock);
PlotCanvas plot = ScatterPlot.plot(dataset[datasetIndex], kmeans.getClusterLabel(), pointLegend, Palette.COLORS);
plot.points(kmeans.centroids(), '@');
return plot;
}
示例3: impute
import smile.clustering.KMeans; //導入依賴的package包/類
@Override
public void impute(double[][] data) throws MissingValueImputationException {
int[] count = new int[data[0].length];
for (int i = 0; i < data.length; i++) {
int n = 0;
for (int j = 0; j < data[i].length; j++) {
if (Double.isNaN(data[i][j])) {
n++;
count[j]++;
}
}
if (n == data[i].length) {
throw new MissingValueImputationException("The whole row " + i + " is missing");
}
}
for (int i = 0; i < data[0].length; i++) {
if (count[i] == data.length) {
throw new MissingValueImputationException("The whole column " + i + " is missing");
}
}
KMeans kmeans = KMeans.lloyd(data, k, Integer.MAX_VALUE, runs);
for (int i = 0; i < k; i++) {
if (kmeans.getClusterSize()[i] > 0) {
double[][] d = new double[kmeans.getClusterSize()[i]][];
for (int j = 0, m = 0; j < data.length; j++) {
if (kmeans.getClusterLabel()[j] == i) {
d[m++] = data[j];
}
}
columnAverageImpute(d);
}
}
// In case of some clusters miss all values in some columns.
columnAverageImpute(data);
}
示例4: testCPU
import smile.clustering.KMeans; //導入依賴的package包/類
/**
* Test of learn method, of class GaussianProcessRegression.
*/
@Test
public void testCPU() {
System.out.println("CPU");
ArffParser parser = new ArffParser();
parser.setResponseIndex(6);
try {
AttributeDataset data = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/cpu.arff"));
double[] datay = data.toArray(new double[data.size()]);
double[][] datax = data.toArray(new double[data.size()][]);
Math.standardize(datax);
int n = datax.length;
int k = 10;
CrossValidation cv = new CrossValidation(n, k);
double rss = 0.0;
double sparseRSS30 = 0.0;
double nystromRSS30 = 0.0;
for (int i = 0; i < k; i++) {
double[][] trainx = Math.slice(datax, cv.train[i]);
double[] trainy = Math.slice(datay, cv.train[i]);
double[][] testx = Math.slice(datax, cv.test[i]);
double[] testy = Math.slice(datay, cv.test[i]);
GaussianProcessRegression<double[]> rkhs = new GaussianProcessRegression<>(trainx, trainy, new GaussianKernel(47.02), 0.1);
KMeans kmeans = new KMeans(trainx, 30, 10);
double[][] centers = kmeans.centroids();
double r0 = 0.0;
for (int l = 0; l < centers.length; l++) {
for (int j = 0; j < l; j++) {
r0 += Math.distance(centers[l], centers[j]);
}
}
r0 /= (2 * centers.length);
System.out.println("Kernel width = " + r0);
GaussianProcessRegression<double[]> sparse30 = new GaussianProcessRegression<>(trainx, trainy, centers, new GaussianKernel(r0), 0.1);
GaussianProcessRegression<double[]> nystrom30 = new GaussianProcessRegression<>(trainx, trainy, centers, new GaussianKernel(r0), 0.1, true);
for (int j = 0; j < testx.length; j++) {
double r = testy[j] - rkhs.predict(testx[j]);
rss += r * r;
r = testy[j] - sparse30.predict(testx[j]);
sparseRSS30 += r * r;
r = testy[j] - nystrom30.predict(testx[j]);
nystromRSS30 += r * r;
}
}
System.out.println("Regular 10-CV MSE = " + rss / n);
System.out.println("Sparse (30) 10-CV MSE = " + sparseRSS30 / n);
System.out.println("Nystrom (30) 10-CV MSE = " + nystromRSS30 / n);
} catch (Exception ex) {
ex.printStackTrace();
}
}
示例5: test2DPlanes
import smile.clustering.KMeans; //導入依賴的package包/類
/**
* Test of learn method, of class GaussianProcessRegression.
*/
@Test
public void test2DPlanes() {
System.out.println("2dplanes");
ArffParser parser = new ArffParser();
parser.setResponseIndex(10);
try {
AttributeDataset data = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/regression/2dplanes.arff"));
double[][] x = data.toArray(new double[data.size()][]);
double[] y = data.toArray(new double[data.size()]);
int[] perm = Math.permutate(x.length);
double[][] datax = new double[4000][];
double[] datay = new double[datax.length];
for (int i = 0; i < datax.length; i++) {
datax[i] = x[perm[i]];
datay[i] = y[perm[i]];
}
int n = datax.length;
int k = 10;
CrossValidation cv = new CrossValidation(n, k);
double rss = 0.0;
double sparseRSS30 = 0.0;
for (int i = 0; i < k; i++) {
double[][] trainx = Math.slice(datax, cv.train[i]);
double[] trainy = Math.slice(datay, cv.train[i]);
double[][] testx = Math.slice(datax, cv.test[i]);
double[] testy = Math.slice(datay, cv.test[i]);
GaussianProcessRegression<double[]> rkhs = new GaussianProcessRegression<>(trainx, trainy, new GaussianKernel(34.866), 0.1);
KMeans kmeans = new KMeans(trainx, 30, 10);
double[][] centers = kmeans.centroids();
double r0 = 0.0;
for (int l = 0; l < centers.length; l++) {
for (int j = 0; j < l; j++) {
r0 += Math.distance(centers[l], centers[j]);
}
}
r0 /= (2 * centers.length);
System.out.println("Kernel width = " + r0);
GaussianProcessRegression<double[]> sparse30 = new GaussianProcessRegression<>(trainx, trainy, centers, new GaussianKernel(r0), 0.1);
for (int j = 0; j < testx.length; j++) {
double r = testy[j] - rkhs.predict(testx[j]);
rss += r * r;
r = testy[j] - sparse30.predict(testx[j]);
sparseRSS30 += r * r;
}
}
System.out.println("Regular 10-CV MSE = " + rss / n);
System.out.println("Sparse (30) 10-CV MSE = " + sparseRSS30 / n);
} catch (Exception ex) {
System.err.println(ex);
}
}
示例6: testAilerons
import smile.clustering.KMeans; //導入依賴的package包/類
/**
* Test of learn method, of class GaussianProcessRegression.
*/
@Test
public void testAilerons() {
System.out.println("ailerons");
ArffParser parser = new ArffParser();
parser.setResponseIndex(40);
try {
AttributeDataset data = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/regression/ailerons.arff"));
double[][] x = data.toArray(new double[data.size()][]);
Math.standardize(x);
double[] y = data.toArray(new double[data.size()]);
for (int i = 0; i < y.length; i++) {
y[i] *= 10000;
}
int[] perm = Math.permutate(x.length);
double[][] datax = new double[4000][];
double[] datay = new double[datax.length];
for (int i = 0; i < datax.length; i++) {
datax[i] = x[perm[i]];
datay[i] = y[perm[i]];
}
int n = datax.length;
int k = 10;
CrossValidation cv = new CrossValidation(n, k);
double rss = 0.0;
double sparseRSS30 = 0.0;
for (int i = 0; i < k; i++) {
double[][] trainx = Math.slice(datax, cv.train[i]);
double[] trainy = Math.slice(datay, cv.train[i]);
double[][] testx = Math.slice(datax, cv.test[i]);
double[] testy = Math.slice(datay, cv.test[i]);
GaussianProcessRegression<double[]> rkhs = new GaussianProcessRegression<>(trainx, trainy, new GaussianKernel(183.96), 0.1);
KMeans kmeans = new KMeans(trainx, 30, 10);
double[][] centers = kmeans.centroids();
double r0 = 0.0;
for (int l = 0; l < centers.length; l++) {
for (int j = 0; j < l; j++) {
r0 += Math.distance(centers[l], centers[j]);
}
}
r0 /= (2 * centers.length);
System.out.println("Kernel width = " + r0);
GaussianProcessRegression<double[]> sparse30 = new GaussianProcessRegression<>(trainx, trainy, centers, new GaussianKernel(r0), 0.1);
for (int j = 0; j < testx.length; j++) {
double r = testy[j] - rkhs.predict(testx[j]);
rss += r * r;
r = testy[j] - sparse30.predict(testx[j]);
sparseRSS30 += r * r;
}
}
System.out.println("Regular 10-CV MSE = " + rss / n);
System.out.println("Sparse (30) 10-CV MSE = " + sparseRSS30 / n);
} catch (Exception ex) {
System.err.println(ex);
}
}
示例7: testBank32nh
import smile.clustering.KMeans; //導入依賴的package包/類
/**
* Test of learn method, of class GaussianProcessRegression.
*/
@Test
public void testBank32nh() {
System.out.println("bank32nh");
ArffParser parser = new ArffParser();
parser.setResponseIndex(32);
try {
AttributeDataset data = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/regression/bank32nh.arff"));
double[] y = data.toArray(new double[data.size()]);
double[][] x = data.toArray(new double[data.size()][]);
Math.standardize(x);
int[] perm = Math.permutate(x.length);
double[][] datax = new double[4000][];
double[] datay = new double[datax.length];
for (int i = 0; i < datax.length; i++) {
datax[i] = x[perm[i]];
datay[i] = y[perm[i]];
}
int n = datax.length;
int k = 10;
CrossValidation cv = new CrossValidation(n, k);
double rss = 0.0;
double sparseRSS30 = 0.0;
for (int i = 0; i < k; i++) {
double[][] trainx = Math.slice(datax, cv.train[i]);
double[] trainy = Math.slice(datay, cv.train[i]);
double[][] testx = Math.slice(datax, cv.test[i]);
double[] testy = Math.slice(datay, cv.test[i]);
GaussianProcessRegression<double[]> rkhs = new GaussianProcessRegression<>(trainx, trainy, new GaussianKernel(55.3), 0.1);
KMeans kmeans = new KMeans(trainx, 30, 10);
double[][] centers = kmeans.centroids();
double r0 = 0.0;
for (int l = 0; l < centers.length; l++) {
for (int j = 0; j < l; j++) {
r0 += Math.distance(centers[l], centers[j]);
}
}
r0 /= (2 * centers.length);
System.out.println("Kernel width = " + r0);
GaussianProcessRegression<double[]> sparse30 = new GaussianProcessRegression<>(trainx, trainy, centers, new GaussianKernel(r0), 0.1);
for (int j = 0; j < testx.length; j++) {
double r = testy[j] - rkhs.predict(testx[j]);
rss += r * r;
r = testy[j] - sparse30.predict(testx[j]);
sparseRSS30 += r * r;
}
}
System.out.println("Regular 10-CV MSE = " + rss / n);
System.out.println("Sparse (30) 10-CV MSE = " + sparseRSS30 / n);
} catch (Exception ex) {
System.err.println(ex);
}
}
示例8: testPuma8nh
import smile.clustering.KMeans; //導入依賴的package包/類
/**
* Test of learn method, of class GaussianProcessRegression.
*/
@Test
public void testPuma8nh() {
System.out.println("puma8nh");
ArffParser parser = new ArffParser();
parser.setResponseIndex(8);
try {
AttributeDataset data = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/regression/puma8nh.arff"));
double[] y = data.toArray(new double[data.size()]);
double[][] x = data.toArray(new double[data.size()][]);
int[] perm = Math.permutate(x.length);
double[][] datax = new double[4000][];
double[] datay = new double[datax.length];
for (int i = 0; i < datax.length; i++) {
datax[i] = x[perm[i]];
datay[i] = y[perm[i]];
}
int n = datax.length;
int k = 10;
CrossValidation cv = new CrossValidation(n, k);
double rss = 0.0;
double sparseRSS30 = 0.0;
for (int i = 0; i < k; i++) {
double[][] trainx = Math.slice(datax, cv.train[i]);
double[] trainy = Math.slice(datay, cv.train[i]);
double[][] testx = Math.slice(datax, cv.test[i]);
double[] testy = Math.slice(datay, cv.test[i]);
GaussianProcessRegression<double[]> rkhs = new GaussianProcessRegression<>(trainx, trainy, new GaussianKernel(38.63), 0.1);
KMeans kmeans = new KMeans(trainx, 30, 10);
double[][] centers = kmeans.centroids();
double r0 = 0.0;
for (int l = 0; l < centers.length; l++) {
for (int j = 0; j < l; j++) {
r0 += Math.distance(centers[l], centers[j]);
}
}
r0 /= (2 * centers.length);
System.out.println("Kernel width = " + r0);
GaussianProcessRegression<double[]> sparse30 = new GaussianProcessRegression<>(trainx, trainy, centers, new GaussianKernel(r0), 0.1);
for (int j = 0; j < testx.length; j++) {
double r = testy[j] - rkhs.predict(testx[j]);
rss += r * r;
r = testy[j] - sparse30.predict(testx[j]);
sparseRSS30 += r * r;
}
}
System.out.println("Regular 10-CV MSE = " + rss / n);
System.out.println("Sparse (30) 10-CV MSE = " + sparseRSS30 / n);
} catch (Exception ex) {
System.err.println(ex);
}
}
示例9: testKin8nm
import smile.clustering.KMeans; //導入依賴的package包/類
/**
* Test of learn method, of class GaussianProcessRegression.
*/
@Test
public void testKin8nm() {
System.out.println("kin8nm");
ArffParser parser = new ArffParser();
parser.setResponseIndex(8);
try {
AttributeDataset data = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/regression/kin8nm.arff"));
double[] y = data.toArray(new double[data.size()]);
double[][] x = data.toArray(new double[data.size()][]);
int[] perm = Math.permutate(x.length);
double[][] datax = new double[4000][];
double[] datay = new double[datax.length];
for (int i = 0; i < datax.length; i++) {
datax[i] = x[perm[i]];
datay[i] = y[perm[i]];
}
int n = datax.length;
int k = 10;
CrossValidation cv = new CrossValidation(n, k);
double rss = 0.0;
double sparseRSS30 = 0.0;
for (int i = 0; i < k; i++) {
double[][] trainx = Math.slice(datax, cv.train[i]);
double[] trainy = Math.slice(datay, cv.train[i]);
double[][] testx = Math.slice(datax, cv.test[i]);
double[] testy = Math.slice(datay, cv.test[i]);
GaussianProcessRegression<double[]> rkhs = new GaussianProcessRegression<>(trainx, trainy, new GaussianKernel(34.97), 0.1);
KMeans kmeans = new KMeans(trainx, 30, 10);
double[][] centers = kmeans.centroids();
double r0 = 0.0;
for (int l = 0; l < centers.length; l++) {
for (int j = 0; j < l; j++) {
r0 += Math.distance(centers[l], centers[j]);
}
}
r0 /= (2 * centers.length);
System.out.println("Kernel width = " + r0);
GaussianProcessRegression<double[]> sparse30 = new GaussianProcessRegression<>(trainx, trainy, centers, new GaussianKernel(r0), 0.1);
for (int j = 0; j < testx.length; j++) {
double r = testy[j] - rkhs.predict(testx[j]);
rss += r * r;
r = testy[j] - sparse30.predict(testx[j]);
sparseRSS30 += r * r;
}
}
System.out.println("Regular 10-CV MSE = " + rss / n);
System.out.println("Sparse (30) 10-CV MSE = " + sparseRSS30 / n);
} catch (Exception ex) {
System.err.println(ex);
}
}
示例10: Kmeans
import smile.clustering.KMeans; //導入依賴的package包/類
public Kmeans(int k, NumericColumn... columns) {
double[][] input = DoubleArrays.to2dArray(columns);
this.kMeans = new KMeans(input, k);
this.inputColumns = columns;
}