本文整理了Java中de.lmu.ifi.dbs.elki.algorithm.clustering.kmeans.KMeans
类的一些代码示例,展示了KMeans
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。KMeans
类的具体详情如下:
包路径:de.lmu.ifi.dbs.elki.algorithm.clustering.kmeans.KMeans
类名称:KMeans
[英]Some constants and options shared among kmeans family algorithms.
[中]kmeans系列算法之间共享的一些常量和选项。
代码示例来源:origin: elki-project/elki
@Override
public DistanceFunction<? super V> getDistanceFunction() {
return innerkMeans.getDistanceFunction();
}
代码示例来源:origin: elki-project/elki
@Override
public TypeInformation[] getInputTypeRestriction() {
return innerKMeans.getInputTypeRestriction();
}
代码示例来源:origin: de.lmu.ifi.dbs.elki/elki
@Override
public void setDistanceFunction(NumberVectorDistanceFunction<? super V> distanceFunction) {
innerkMeans.setDistanceFunction(distanceFunction);
}
代码示例来源:origin: elki-project/elki
@Override
public Clustering<M> run(Database database, Relation<V> relation) {
if(!(innerkMeans.getDistanceFunction() instanceof PrimitiveDistanceFunction)) {
throw new AbortException("K-Means results can only be evaluated for primitive distance functions, got: " + innerkMeans.getDistanceFunction().getClass());
}
@SuppressWarnings("unchecked")
final NumberVectorDistanceFunction<? super NumberVector> df = (NumberVectorDistanceFunction<? super NumberVector>) innerkMeans.getDistanceFunction();
Clustering<M> bestResult = null;
double bestCost = Double.NaN;
FiniteProgress prog = LOG.isVerbose() ? new FiniteProgress("K-means iterations", trials, LOG) : null;
for(int i = 0; i < trials; i++) {
Clustering<M> currentCandidate = innerkMeans.run(database, relation);
double currentCost = qualityMeasure.quality(currentCandidate, df, relation);
LOG.verbose("Cost of candidate " + i + ": " + currentCost);
if(qualityMeasure.isBetter(currentCost, bestCost)) {
bestResult = currentCandidate;
bestCost = currentCost;
}
LOG.incrementProcessed(prog);
}
LOG.ensureCompleted(prog);
return bestResult;
}
代码示例来源:origin: de.lmu.ifi.dbs.elki/elki
@Override
public <T extends V, O extends NumberVector> List<O> chooseInitialMeans(Database database, Relation<T> relation, int k, NumberVectorDistanceFunction<? super T> distanceFunction, NumberVector.Factory<O> factory) {
final DBIDs sample = DBIDUtil.randomSample(relation.getDBIDs(), rate, rnd);
// Ugly cast, sorry
@SuppressWarnings("unchecked")
Relation<V> rel = (Relation<V>) relation;
// FIXME: This does not necessarily hold. Check and fail!
if(!distanceFunction.getInputTypeRestriction().isAssignableFromType(TypeUtil.NUMBER_VECTOR_FIELD)) {
LoggingUtil.warning("Initializing k-means with k-means using specialized distance functions MAY fail, if the initialization method does require a distance defined on arbitrary number vectors.");
}
@SuppressWarnings("unchecked")
NumberVectorDistanceFunction<? super V> pdf = (NumberVectorDistanceFunction<? super V>) distanceFunction;
ProxyView<V> proxyv = new ProxyView<>(sample, rel);
ProxyDatabase proxydb = new ProxyDatabase(sample, proxyv);
innerkMeans.setK(k);
innerkMeans.setDistanceFunction(pdf);
Clustering<?> clusters = innerkMeans.run(proxydb, proxyv);
List<O> means = new ArrayList<>();
for(Cluster<?> cluster : clusters.getAllClusters()) {
means.add(factory.newNumberVector(ModelUtil.getPrototype(cluster.getModel(), relation)));
}
return means;
}
代码示例来源:origin: elki-project/elki
innerKMeans.setK(k_min);
LOG.statistics(new StringStatistic(KEY + ".initialization", initializer.toString()));
splitInitializer.setInitialMeans(initializer.chooseInitialMeans(database, relation, k_min, getDistanceFunction()));
Clustering<M> clustering = innerKMeans.run(database, relation);
innerKMeans.setK(nextClusters.size());
clustering = innerKMeans.run(database, relation);
clusters.clear();
clusters.addAll(clustering.getAllClusters());
代码示例来源:origin: elki-project/elki
/**
* Constructor.
*
* @param distanceFunction Distance function
* @param k_min k_min parameter - minimum number of result clusters
* @param k_max k_max parameter - maximum number of result clusters
* @param maxiter Maximum number of iterations each.
* @param innerKMeans K-Means variant to use inside.
* @param informationCriterion The information criterion used for the
* splitting step
* @param random Random factory
*/
public XMeans(NumberVectorDistanceFunction<? super V> distanceFunction, int k_min, int k_max, int maxiter, KMeans<V, M> innerKMeans, KMeansInitialization initializer, KMeansQualityMeasure<V> informationCriterion, RandomFactory random) {
super(distanceFunction, k_min, maxiter, initializer);
this.k_min = k_min;
this.k_max = k_max;
this.k = k_min;
this.innerKMeans = innerKMeans;
this.splitInitializer = new PredefinedInitialMeans((double[][]) null);
this.innerKMeans.setInitializer(this.splitInitializer);
this.innerKMeans.setDistanceFunction(distanceFunction);
this.informationCriterion = informationCriterion;
this.rnd = random;
}
代码示例来源:origin: elki-project/elki
Clustering<M> innerResult = innerkMeans.run(proxyDB);
代码示例来源:origin: de.lmu.ifi.dbs.elki/elki-clustering
@Override
public void setK(int k) {
innerkMeans.setK(k);
}
代码示例来源:origin: elki-project/elki
@Override
public void setInitializer(KMeansInitialization init) {
innerkMeans.setInitializer(init);
}
代码示例来源:origin: de.lmu.ifi.dbs.elki/elki-clustering
@Override
public Clustering<M> run(Database database, Relation<V> relation) {
if(!(innerkMeans.getDistanceFunction() instanceof PrimitiveDistanceFunction)) {
throw new AbortException("K-Means results can only be evaluated for primitive distance functions, got: " + innerkMeans.getDistanceFunction().getClass());
}
@SuppressWarnings("unchecked")
final NumberVectorDistanceFunction<? super NumberVector> df = (NumberVectorDistanceFunction<? super NumberVector>) innerkMeans.getDistanceFunction();
Clustering<M> bestResult = null;
double bestCost = Double.NaN;
FiniteProgress prog = LOG.isVerbose() ? new FiniteProgress("K-means iterations", trials, LOG) : null;
for(int i = 0; i < trials; i++) {
Clustering<M> currentCandidate = innerkMeans.run(database, relation);
double currentCost = qualityMeasure.quality(currentCandidate, df, relation);
LOG.verbose("Cost of candidate " + i + ": " + currentCost);
if(qualityMeasure.isBetter(currentCost, bestCost)) {
bestResult = currentCandidate;
bestCost = currentCost;
}
LOG.incrementProcessed(prog);
}
LOG.ensureCompleted(prog);
return bestResult;
}
代码示例来源:origin: elki-project/elki
@Override
public double[][] chooseInitialMeans(Database database, Relation<? extends NumberVector> relation, int k, NumberVectorDistanceFunction<?> distanceFunction) {
if(relation.size() < k) {
throw new IllegalArgumentException("Cannot choose k=" + k + " means from N=" + relation.size() + " < k objects.");
}
final DBIDs sample = DBIDUtil.randomSample(relation.getDBIDs(), rate, rnd);
if(sample.size() < k) {
throw new IllegalArgumentException("Sampling rate=" + rate + " from N=" + relation.size() + " yields only " + sample.size() + " < k objects.");
}
// Ugly cast, sorry
@SuppressWarnings("unchecked")
Relation<V> rel = (Relation<V>) relation;
// FIXME: This does not necessarily hold. Check and fail!
if(!distanceFunction.getInputTypeRestriction().isAssignableFromType(TypeUtil.NUMBER_VECTOR_FIELD)) {
LoggingUtil.warning("Initializing k-means with k-means using specialized distance functions MAY fail, if the initialization method does require a distance defined on arbitrary number vectors.");
}
@SuppressWarnings("unchecked")
NumberVectorDistanceFunction<? super V> pdf = (NumberVectorDistanceFunction<? super V>) distanceFunction;
ProxyView<V> proxyv = new ProxyView<>(sample, rel);
ProxyDatabase proxydb = new ProxyDatabase(sample, proxyv);
innerkMeans.setK(k);
innerkMeans.setDistanceFunction(pdf);
Clustering<?> clusters = innerkMeans.run(proxydb, proxyv);
double[][] means = new double[clusters.getAllClusters().size()][];
int i = 0;
for(Cluster<?> cluster : clusters.getAllClusters()) {
means[i++] = ModelUtil.getPrototype(cluster.getModel(), relation).toArray();
}
return means;
}
代码示例来源:origin: de.lmu.ifi.dbs.elki/elki-clustering
innerKMeans.setK(k_min);
LOG.statistics(new StringStatistic(KEY + ".initialization", initializer.toString()));
splitInitializer.setInitialMeans(initializer.chooseInitialMeans(database, relation, k_min, getDistanceFunction()));
Clustering<M> clustering = innerKMeans.run(database, relation);
innerKMeans.setK(nextClusters.size());
clustering = innerKMeans.run(database, relation);
clusters.clear();
clusters.addAll(clustering.getAllClusters());
代码示例来源:origin: de.lmu.ifi.dbs.elki/elki-clustering
/**
* Constructor.
*
* @param distanceFunction Distance function
* @param k_min k_min parameter - minimum number of result clusters
* @param k_max k_max parameter - maximum number of result clusters
* @param maxiter Maximum number of iterations each.
* @param innerKMeans K-Means variant to use inside.
* @param informationCriterion The information criterion used for the
* splitting step
* @param random Random factory
*/
public XMeans(NumberVectorDistanceFunction<? super V> distanceFunction, int k_min, int k_max, int maxiter, KMeans<V, M> innerKMeans, KMeansInitialization initializer, KMeansQualityMeasure<V> informationCriterion, RandomFactory random) {
super(distanceFunction, k_min, maxiter, initializer);
this.k_min = k_min;
this.k_max = k_max;
this.k = k_min;
this.innerKMeans = innerKMeans;
this.splitInitializer = new PredefinedInitialMeans((double[][]) null);
this.innerKMeans.setInitializer(this.splitInitializer);
this.innerKMeans.setDistanceFunction(distanceFunction);
this.informationCriterion = informationCriterion;
this.rnd = random;
}
代码示例来源:origin: de.lmu.ifi.dbs.elki/elki-clustering
Clustering<M> innerResult = innerkMeans.run(proxyDB);
代码示例来源:origin: elki-project/elki
@Override
public void setK(int k) {
innerkMeans.setK(k);
}
代码示例来源:origin: elki-project/elki
@Override
public void setInitializer(KMeansInitialization init) {
innerkMeans.setInitializer(init);
}
代码示例来源:origin: de.lmu.ifi.dbs.elki/elki
@Override
public Clustering<M> run(Database database, Relation<V> relation) {
if(!(innerkMeans.getDistanceFunction() instanceof PrimitiveDistanceFunction)) {
throw new AbortException("K-Means results can only be evaluated for primitive distance functions, got: " + innerkMeans.getDistanceFunction().getClass());
}
@SuppressWarnings("unchecked")
final NumberVectorDistanceFunction<? super NumberVector> df = (NumberVectorDistanceFunction<? super NumberVector>) innerkMeans.getDistanceFunction();
Clustering<M> bestResult = null;
double bestCost = Double.NaN;
FiniteProgress prog = LOG.isVerbose() ? new FiniteProgress("K-means iterations", trials, LOG) : null;
for(int i = 0; i < trials; i++) {
Clustering<M> currentCandidate = innerkMeans.run(database, relation);
double currentCost = qualityMeasure.quality(currentCandidate, df, relation);
if(LOG.isVerbose()) {
LOG.verbose("Cost of candidate " + i + ": " + currentCost);
}
if(qualityMeasure.isBetter(currentCost, bestCost)) {
bestResult = currentCandidate;
bestCost = currentCost;
}
LOG.incrementProcessed(prog);
}
LOG.ensureCompleted(prog);
return bestResult;
}
代码示例来源:origin: de.lmu.ifi.dbs.elki/elki-clustering
@Override
public double[][] chooseInitialMeans(Database database, Relation<? extends NumberVector> relation, int k, NumberVectorDistanceFunction<?> distanceFunction) {
if(relation.size() < k) {
throw new IllegalArgumentException("Cannot choose k=" + k + " means from N=" + relation.size() + " < k objects.");
}
final DBIDs sample = DBIDUtil.randomSample(relation.getDBIDs(), rate, rnd);
if(sample.size() < k) {
throw new IllegalArgumentException("Sampling rate=" + rate + " from N=" + relation.size() + " yields only " + sample.size() + " < k objects.");
}
// Ugly cast, sorry
@SuppressWarnings("unchecked")
Relation<V> rel = (Relation<V>) relation;
// FIXME: This does not necessarily hold. Check and fail!
if(!distanceFunction.getInputTypeRestriction().isAssignableFromType(TypeUtil.NUMBER_VECTOR_FIELD)) {
LoggingUtil.warning("Initializing k-means with k-means using specialized distance functions MAY fail, if the initialization method does require a distance defined on arbitrary number vectors.");
}
@SuppressWarnings("unchecked")
NumberVectorDistanceFunction<? super V> pdf = (NumberVectorDistanceFunction<? super V>) distanceFunction;
ProxyView<V> proxyv = new ProxyView<>(sample, rel);
ProxyDatabase proxydb = new ProxyDatabase(sample, proxyv);
innerkMeans.setK(k);
innerkMeans.setDistanceFunction(pdf);
Clustering<?> clusters = innerkMeans.run(proxydb, proxyv);
double[][] means = new double[clusters.getAllClusters().size()][];
int i = 0;
for(Cluster<?> cluster : clusters.getAllClusters()) {
means[i++] = ModelUtil.getPrototype(cluster.getModel(), relation).toArray();
}
return means;
}
代码示例来源:origin: de.lmu.ifi.dbs.elki/elki
innerKMeans.setK(k_min);
if(LOG.isStatistics()) {
LOG.statistics(new StringStatistic(KEY + ".initialization", initializer.toString()));
Clustering<M> clustering = innerKMeans.run(database, relation);
innerKMeans.setK(nextClusters.size());
clustering = innerKMeans.run(database, relation);
clusters.clear();
clusters.addAll(clustering.getAllClusters());
内容来源于网络,如有侵权,请联系作者删除!