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

weka.filters.supervised.attribute.PartitionMembership Maven / Gradle / Ivy

Go to download

The Waikato Environment for Knowledge Analysis (WEKA), a machine learning workbench. This version represents the developer version, the "bleeding edge" of development, you could say. New functionality gets added to this version.

There is a newer version: 3.9.6
Show newest version
/*
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see .
 */

/*
 *    PartitionMembership.java
 *    Copyright (C) 2012 Eibe Frank
 *
 */

package weka.filters.supervised.attribute;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Vector;

import weka.classifiers.trees.J48;
import weka.core.*;
import weka.filters.Filter;
import weka.filters.SupervisedFilter;

/**
 * 
 * * A filter that uses a PartitionGenerator to generate partition membership values; filtered instances are composed of these values plus the class attribute (if set in the input data) and rendered as sparse instances. See Section 3 of
* * Eibe Frank, Bernhard Pfahringer: Propositionalisation of Multi-instance Data Using Random Forests. In: AI 2013: Advances in Artificial Intelligence, 362-373, 2013. * *

* * * * * Valid options are:

* * * *

 -W <name of partition generator>
 * *  Full name of partition generator to use, e.g.:
 * *   weka.classifiers.trees.J48
 * *  Additional options after the '--'.
 * *  (default: weka.classifiers.trees.J48)
* * * * * Options after the -- are passed on to the clusterer. * * @author Eibe Frank ([email protected]) * @author Mark Hall ([email protected]) * @version $Revision: 14508 $ */ public class PartitionMembership extends Filter implements SupervisedFilter, OptionHandler, RevisionHandler, TechnicalInformationHandler, WeightedInstancesHandler { /** for serialization */ static final long serialVersionUID = 333532554667754026L; /** The partition generator */ protected PartitionGenerator m_partitionGenerator = new J48(); /** * Returns the Capabilities of this filter. * * @return the capabilities of this object * @see Capabilities */ @Override public Capabilities getCapabilities() { Capabilities result = m_partitionGenerator.getCapabilities(); result.setMinimumNumberInstances(0); return result; } /** * Tests the data whether the filter can actually handle it * * @param instanceInfo the data to test * @throws Exception if the test fails */ @Override protected void testInputFormat(Instances instanceInfo) throws Exception { getCapabilities().testWithFail(instanceInfo); } /** * Sets the format of the input instances. * * @param instanceInfo an Instances object containing the input instance * structure (any instances contained in the object are ignored - * only the structure is required). * @return true if the outputFormat may be collected immediately * @throws Exception if the inputFormat can't be set successfully */ @Override public boolean setInputFormat(Instances instanceInfo) throws Exception { super.setInputFormat(instanceInfo); return false; } /** * Signify that this batch of input to the filter is finished. * * @return true if there are instances pending output * @throws IllegalStateException if no input structure has been defined */ @Override public boolean batchFinished() throws Exception { if (getInputFormat() == null) { throw new IllegalStateException("No input instance format defined"); } if (outputFormatPeek() == null) { Instances toFilter = getInputFormat(); // Build the partition generator m_partitionGenerator.generatePartition(toFilter); // Create output dataset ArrayList attInfo = new ArrayList(); for (int i = 0; i < m_partitionGenerator.numElements(); i++) { attInfo.add(new Attribute("partition_" + i)); } if (toFilter.classIndex() >= 0) { attInfo.add((Attribute) toFilter.classAttribute().copy()); } attInfo.trimToSize(); Instances filtered = new Instances(toFilter.relationName() + "_partitionMembership", attInfo, 0); if (toFilter.classIndex() >= 0) { filtered.setClassIndex(filtered.numAttributes() - 1); } setOutputFormat(filtered); // build new dataset for (int i = 0; i < toFilter.numInstances(); i++) { convertInstance(toFilter.instance(i)); } } flushInput(); m_NewBatch = true; return (numPendingOutput() != 0); } /** * Input an instance for filtering. Ordinarily the instance is processed and * made available for output immediately. Some filters require all instances * be read before producing output. * * @param instance the input instance * @return true if the filtered instance may now be collected with output(). * @throws IllegalStateException if no input format has been defined. */ @Override public boolean input(Instance instance) throws Exception { if (getInputFormat() == null) { throw new IllegalStateException("No input instance format defined"); } if (m_NewBatch) { resetQueue(); m_NewBatch = false; } if (outputFormatPeek() != null) { convertInstance(instance); return true; } bufferInput(instance); return false; } /** * Convert a single instance over. The converted instance is added to the end * of the output queue. * * @param instance the instance to convert * @throws Exception if something goes wrong */ protected void convertInstance(Instance instance) throws Exception { // Make copy and set weight to one Instance cp = (Instance) instance.copy(); cp.setWeight(1.0); // Set up values double[] instanceVals = new double[outputFormatPeek().numAttributes()]; double[] vals = m_partitionGenerator.getMembershipValues(cp); System.arraycopy(vals, 0, instanceVals, 0, vals.length); if (instance.classIndex() >= 0) { instanceVals[instanceVals.length - 1] = instance.classValue(); } push(new SparseInstance(instance.weight(), instanceVals)); } /** * Returns an enumeration describing the available options. * * @return an enumeration of all the available options. */ @Override public Enumeration




© 2015 - 2024 Weber Informatics LLC | Privacy Policy