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

cc.mallet.cluster.Clustering Maven / Gradle / Ivy

Go to download

MALLET is a Java-based package for statistical natural language processing, document classification, clustering, topic modeling, information extraction, and other machine learning applications to text.

The newest version!
/* Copyright (C) 2003 Univ. of Massachusetts Amherst, Computer Science Dept.
   This file is part of "MALLET" (MAchine Learning for LanguagE Toolkit).
   http://www.cs.umass.edu/~mccallum/mallet
   This software is provided under the terms of the Common Public License,
   version 1.0, as published by http://www.opensource.org.  For further
   information, see the file `LICENSE' included with this distribution. */

/** A clustering of a set of points (instances).
    @author Jerod Weinman [email protected]
*/

package cc.mallet.cluster;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Arrays;

import cc.mallet.types.InstanceList;


public class Clustering implements Serializable {
	
	protected int numLabels;
	protected int labels[];
	protected InstanceList instances;
	
	/** Clustering constructor.
	 *
	 * @param instances Instances that are clustered
	 * @param numLabels Number of clusters
	 * @param labels Assignment of instances to clusters; many-to-one with 
	 *               range [0,numLabels).     
	 */
	public Clustering (InstanceList instances, int numLabels, int[] labels) {
		if (instances.size() != labels.length)
	    throw new IllegalArgumentException("Instance list length does not match cluster labeling");
		
		if (numLabels < 1)
	    throw new IllegalArgumentException("Number of labels must be strictly positive.");
		
		for (int i = 0 ; i < labels.length ; i++)
	    if (labels[i] < 0 || labels[i] >= numLabels)
				throw new IllegalArgumentException("Label mapping must have range [0,numLabels).");
		
		this.instances = instances;
		this.numLabels = numLabels;
		this.labels = labels;	
	}

	// GETTERS
	
	public InstanceList getInstances () { return this.instances; }

	/** Return an list of instances with a particular label. */
	public InstanceList getCluster(int label) {		
		InstanceList cluster = new InstanceList(instances.getPipe());		
		for (int n=0 ; n




© 2015 - 2025 Weber Informatics LLC | Privacy Policy