All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.ucla.sspace.clustering.OnlineKMeans Maven / Gradle / Ivy

Go to download

The S-Space Package is a collection of algorithms for building Semantic Spaces as well as a highly-scalable library for designing new distributional semantics algorithms. Distributional algorithms process text corpora and represent the semantic for words as high dimensional feature vectors. This package also includes matrices, vectors, and numerous clustering algorithms. These approaches are known by many names, such as word spaces, semantic spaces, or distributed semantics and rest upon the Distributional Hypothesis: words that appear in similar contexts have similar meanings.

The newest version!
/*
 * Copyright 2009 Keith Stevens 
 *
 * This file is part of the S-Space package and is covered under the terms and
 * conditions therein.
 *
 * The S-Space package is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation and distributed hereunder to you.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND NO REPRESENTATIONS OR WARRANTIES,
 * EXPRESS OR IMPLIED ARE MADE.  BY WAY OF EXAMPLE, BUT NOT LIMITATION, WE MAKE
 * NO REPRESENTATIONS OR WARRANTIES OF MERCHANT- ABILITY OR FITNESS FOR ANY
 * PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE OR DOCUMENTATION
 * WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER
 * RIGHTS.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see .
 */

package edu.ucla.sspace.clustering;

import edu.ucla.sspace.vector.DoubleVector;
import edu.ucla.sspace.vector.Vectors;
import edu.ucla.sspace.vector.VectorMath;

import edu.ucla.sspace.util.Generator;
import edu.ucla.sspace.util.Properties;

import java.util.Iterator;
import java.util.List;

import java.util.concurrent.CopyOnWriteArrayList;

import java.util.concurrent.atomic.AtomicInteger;


/**
 * A {@link Generator} class for generating a new {@code OnlineKMeansClustering}
 * instance. This class supports the following properties:
 *
 * 
* *
Property: {@value #WEIGHTING_PROPERTY} *
* Default: {@value #DEFAULT_WEIGHT} * *
This variable sets the weight given to the mean * vector in a rolling average of vectors.

* *
Property: {@value #MERGE_THRESHOLD_PROPERTY } *
* Default: {@value #DEFAULT_MERGE_THRESHOLD} * *
This variable sets the threshold for merging * two clusters.

* *
Property: {@value #NUM_CLUSTERS} *
* Default: {@value #DEFAULT_MAX_CLUSTERS} * *
This variable sets the maximum number of * clusters used.

* *
* * @author Keith Stevens */ public class OnlineKMeans implements Generator> { /** * A property prefix. */ private static final String PROPERTY_PREFIX = "edu.ucla.sspace.cluster.OnlineKMeans"; /** * The property for setting the threshold for merging two clusters. */ public static final String MERGE_THRESHOLD_PROPERTY = PROPERTY_PREFIX + ".merge"; /** * The default merge threshold. */ public static final double DEFAULT_MERGE_THRESHOLD = .35; /** * The default number of clusters. */ public static final int DEFAULT_MAX_CLUSTERS = 15; /** * The threshold for clustering */ private final double clusterThreshold; /** * The maximum number of clusters permitted. */ private final int maxNumClusters; /** * Creates a new generator using the system properties. */ public OnlineKMeans() { this(new Properties()); } /** * Creates a new generator using the given properties. */ public OnlineKMeans(Properties props) { clusterThreshold = props.getProperty( MERGE_THRESHOLD_PROPERTY, DEFAULT_MERGE_THRESHOLD); maxNumClusters = props.getProperty( OnlineClustering.NUM_CLUSTERS_PROPERTY, DEFAULT_MAX_CLUSTERS); } /** * Generates a new instance of a {@code OnlineClustering} based on the * values used to construct this generator. */ public OnlineClustering generate() { return new OnlineKMeansClustering(clusterThreshold, maxNumClusters); } public String toString() { return "OnLineKMeans_" + maxNumClusters + "c_"; } /** * A simple online implementation of K-Means clustering for {@code Vector}s, * with the option to perform agglomerative clustering once all elements * have been clustered. * * @author Keith Stevens */ public class OnlineKMeansClustering implements OnlineClustering { /** * The threshold for clustering */ private final double clusterThreshold; /** * The maximum number of clusters permitted. */ private final int maxNumClusters; /** * The set of clusters. */ private final List> elements; /** * A counter for generating item identifiers. */ private final AtomicInteger idCounter; /** * Creates a new instance of online KMeans clustering. */ public OnlineKMeansClustering(double mergeThreshold, int maxNumClusters) { elements = new CopyOnWriteArrayList>(); idCounter = new AtomicInteger(0); this.clusterThreshold = mergeThreshold; this.maxNumClusters = maxNumClusters; } /** * {@inheritDoc} */ public int addVector(T value) { int id = idCounter.getAndAdd(1); Iterator> elementIter = elements.iterator(); // Find the centriod with the best similarity. Cluster bestMatch = null; int bestIndex = elements.size(); double bestScore = -1; double similarity = -1; int i = 0; while (elementIter.hasNext()) { Cluster cluster = elementIter.next(); similarity = cluster.compareWithVector(value); if (similarity >= bestScore) { bestScore = similarity; bestMatch = cluster; bestIndex = i; } ++i; } // Add the current term vector if the similarity is high enough, or // set it as a new centroid. if (bestScore >= clusterThreshold || elements.size() >= maxNumClusters) { bestMatch.addVector(value, id); } else { // lock to ensure that the number of clusters doesn't change // while we add this one synchronized(elements) { // Perform an additional check to see whether the number of // elements changed while we waiting on the lock if (elements.size() < maxNumClusters) { bestMatch = new SynchronizedCluster( new CentroidCluster(Vectors.instanceOf(value))); elements.add(bestMatch); } if (bestMatch != null) bestMatch.addVector(value, id); } } return id; } /** * {@inheritDoc} */ public Cluster getCluster(int clusterIndex) { if (elements.size() <= clusterIndex) return null; return elements.get(clusterIndex); } /** * {@inheritDoc} */ public List> getClusters() { return elements; } /** * {@inheritDoc} */ public synchronized int size() { return elements.size(); } /** * Returns a string describing this {@code ClusterMap}. */ public String toString() { return "OnlineKMeansClustering-maxNumClusters" + maxNumClusters + "-threshold" + clusterThreshold; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy