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

weka.associations.CheckAssociator 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 .
 */

/*
 * CheckAssociator.java
 * Copyright (C) 2006-2012 University of Waikato, Hamilton, New Zealand
 *
 */

package weka.associations;

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

import weka.core.Attribute;
import weka.core.CheckScheme;
import weka.core.Instances;
import weka.core.MultiInstanceCapabilitiesHandler;
import weka.core.Option;
import weka.core.OptionHandler;
import weka.core.RevisionHandler;
import weka.core.RevisionUtils;
import weka.core.SerializationHelper;
import weka.core.TestInstances;
import weka.core.Utils;
import weka.core.WeightedInstancesHandler;

/**
 * Class for examining the capabilities and finding problems with associators.
 * If you implement an associators using the WEKA.libraries, you should run the
 * checks on it to ensure robustness and correct operation. Passing all the
 * tests of this object does not mean bugs in the associators don't exist, but
 * this will help find some common ones.
 * 

* * Typical usage: *

* java weka.associations.CheckAssociator -W associator_name * -- associator_options *

* * CheckAssociator reports on the following: *

    *
  • Associator abilities *
      *
    • Possible command line options to the associators
    • *
    • Whether the associators can predict nominal, numeric, string, date or * relational class attributes.
    • *
    • Whether the associators can handle numeric predictor attributes
    • *
    • Whether the associators can handle nominal predictor attributes
    • *
    • Whether the associators can handle string predictor attributes
    • *
    • Whether the associators can handle date predictor attributes
    • *
    • Whether the associators can handle relational predictor attributes
    • *
    • Whether the associators can handle multi-instance data
    • *
    • Whether the associators can handle missing predictor values
    • *
    • Whether the associators can handle missing class values
    • *
    • Whether a nominal associators only handles 2 class problems
    • *
    • Whether the associators can handle instance weights
    • *
    *
  • *
  • Correct functioning *
      *
    • Correct initialisation during buildAssociations (i.e. no result changes * when buildAssociations called repeatedly)
    • *
    • Whether the associators alters the data pased to it (number of instances, * instance order, instance weights, etc)
    • *
    *
  • *
  • Degenerate cases *
      *
    • building associators with zero training instances
    • *
    • all but one predictor attribute values missing
    • *
    • all predictor attribute values missing
    • *
    • all but one class values missing
    • *
    • all class values missing
    • *
    *
  • *
* Running CheckAssociator with the debug option set will output the training * dataset for any failed tests. *

* * The weka.associations.AbstractAssociatorTest uses this class to * test all the associators. Any changes here, have to be checked in that * abstract test class, too. *

* * Valid options are: *

* *

 * -D
 *  Turn on debugging output.
 * 
* *
 * -S
 *  Silent mode - prints nothing to stdout.
 * 
* *
 * -N <num>
 *  The number of instances in the datasets (default 20).
 * 
* *
 * -nominal <num>
 *  The number of nominal attributes (default 2).
 * 
* *
 * -nominal-values <num>
 *  The number of values for nominal attributes (default 1).
 * 
* *
 * -numeric <num>
 *  The number of numeric attributes (default 1).
 * 
* *
 * -string <num>
 *  The number of string attributes (default 1).
 * 
* *
 * -date <num>
 *  The number of date attributes (default 1).
 * 
* *
 * -relational <num>
 *  The number of relational attributes (default 1).
 * 
* *
 * -num-instances-relational <num>
 *  The number of instances in relational/bag attributes (default 10).
 * 
* *
 * -words <comma-separated-list>
 *  The words to use in string attributes.
 * 
* *
 * -word-separators <chars>
 *  The word separators to use in string attributes.
 * 
* *
 * -W
 *  Full name of the associator analysed.
 *  eg: weka.associations.Apriori
 *  (default weka.associations.Apriori)
 * 
* *
 * Options specific to associator weka.associations.Apriori:
 * 
* *
 * -N <required number of rules output>
 *  The required number of rules. (default = 10)
 * 
* *
 * -T <0=confidence | 1=lift | 2=leverage | 3=Conviction>
 *  The metric type by which to rank rules. (default = confidence)
 * 
* *
 * -C <minimum metric score of a rule>
 *  The minimum confidence of a rule. (default = 0.9)
 * 
* *
 * -D <delta for minimum support>
 *  The delta by which the minimum support is decreased in
 *  each iteration. (default = 0.05)
 * 
* *
 * -U <upper bound for minimum support>
 *  Upper bound for minimum support. (default = 1.0)
 * 
* *
 * -M <lower bound for minimum support>
 *  The lower bound for the minimum support. (default = 0.1)
 * 
* *
 * -S <significance level>
 *  If used, rules are tested for significance at
 *  the given level. Slower. (default = no significance testing)
 * 
* *
 * -I
 *  If set the itemsets found are also output. (default = no)
 * 
* *
 * -R
 *  Remove columns that contain all missing values (default = no)
 * 
* *
 * -V
 *  Report progress iteratively. (default = no)
 * 
* *
 * -A
 *  If set class association rules are mined. (default = no)
 * 
* *
 * -c <the class index>
 *  The class index. (default = last)
 * 
* * * * Options after -- are passed to the designated associator. *

* * @author Len Trigg ([email protected]) * @author FracPete (fracpete at waikato dot ac dot nz) * @version $Revision: 11247 $ * @see TestInstances */ public class CheckAssociator extends CheckScheme implements RevisionHandler { /* * Note about test methods: - methods return array of booleans - first index: * success or not - second index: acceptable or not (e.g., Exception is OK) * * FracPete (fracpete at waikato dot ac dot nz) */ /** a "dummy" class type */ public final static int NO_CLASS = -1; /*** The associator to be examined */ protected Associator m_Associator = new weka.associations.Apriori(); /** * Returns an enumeration describing the available options. * * @return an enumeration of all the available options. */ @Override public Enumeration

* *

   * -D
   *  Turn on debugging output.
   * 
* *
   * -S
   *  Silent mode - prints nothing to stdout.
   * 
* *
   * -N <num>
   *  The number of instances in the datasets (default 20).
   * 
* *
   * -nominal <num>
   *  The number of nominal attributes (default 2).
   * 
* *
   * -nominal-values <num>
   *  The number of values for nominal attributes (default 1).
   * 
* *
   * -numeric <num>
   *  The number of numeric attributes (default 1).
   * 
* *
   * -string <num>
   *  The number of string attributes (default 1).
   * 
* *
   * -date <num>
   *  The number of date attributes (default 1).
   * 
* *
   * -relational <num>
   *  The number of relational attributes (default 1).
   * 
* *
   * -num-instances-relational <num>
   *  The number of instances in relational/bag attributes (default 10).
   * 
* *
   * -words <comma-separated-list>
   *  The words to use in string attributes.
   * 
* *
   * -word-separators <chars>
   *  The word separators to use in string attributes.
   * 
* *
   * -W
   *  Full name of the associator analysed.
   *  eg: weka.associations.Apriori
   *  (default weka.associations.Apriori)
   * 
* *
   * Options specific to associator weka.associations.Apriori:
   * 
* *
   * -N <required number of rules output>
   *  The required number of rules. (default = 10)
   * 
* *
   * -T <0=confidence | 1=lift | 2=leverage | 3=Conviction>
   *  The metric type by which to rank rules. (default = confidence)
   * 
* *
   * -C <minimum metric score of a rule>
   *  The minimum confidence of a rule. (default = 0.9)
   * 
* *
   * -D <delta for minimum support>
   *  The delta by which the minimum support is decreased in
   *  each iteration. (default = 0.05)
   * 
* *
   * -U <upper bound for minimum support>
   *  Upper bound for minimum support. (default = 1.0)
   * 
* *
   * -M <lower bound for minimum support>
   *  The lower bound for the minimum support. (default = 0.1)
   * 
* *
   * -S <significance level>
   *  If used, rules are tested for significance at
   *  the given level. Slower. (default = no significance testing)
   * 
* *
   * -I
   *  If set the itemsets found are also output. (default = no)
   * 
* *
   * -R
   *  Remove columns that contain all missing values (default = no)
   * 
* *
   * -V
   *  Report progress iteratively. (default = no)
   * 
* *
   * -A
   *  If set class association rules are mined. (default = no)
   * 
* *
   * -c <the class index>
   *  The class index. (default = last)
   * 
* * * * @param options the list of options as an array of strings * @throws Exception if an option is not supported */ @Override public void setOptions(String[] options) throws Exception { String tmpStr; super.setOptions(options); tmpStr = Utils.getOption('W', options); if (tmpStr.length() == 0) { tmpStr = weka.associations.Apriori.class.getName(); } setAssociator((Associator) forName("weka.associations", Associator.class, tmpStr, Utils.partitionOptions(options))); } /** * Gets the current settings of the CheckAssociator. * * @return an array of strings suitable for passing to setOptions */ @Override public String[] getOptions() { Vector result = new Vector(); Collections.addAll(result, super.getOptions()); if (getAssociator() != null) { result.add("-W"); result.add(getAssociator().getClass().getName()); } if ((m_Associator != null) && (m_Associator instanceof OptionHandler)) { String[] options = ((OptionHandler) m_Associator).getOptions(); if (options.length > 0) { result.add("--"); Collections.addAll(result, options); } } return result.toArray(new String[result.size()]); } /** * Begin the tests, reporting results to System.out */ @Override public void doTests() { if (getAssociator() == null) { println("\n=== No associator set ==="); return; } println("\n=== Check on Associator: " + getAssociator().getClass().getName() + " ===\n"); // Start tests m_ClasspathProblems = false; println("--> Checking for interfaces"); canTakeOptions(); boolean weightedInstancesHandler = weightedInstancesHandler()[0]; boolean multiInstanceHandler = multiInstanceHandler()[0]; println("--> Associator tests"); declaresSerialVersionUID(); println("--> no class attribute"); testsWithoutClass(weightedInstancesHandler, multiInstanceHandler); println("--> with class attribute"); testsPerClassType(Attribute.NOMINAL, weightedInstancesHandler, multiInstanceHandler); testsPerClassType(Attribute.NUMERIC, weightedInstancesHandler, multiInstanceHandler); testsPerClassType(Attribute.DATE, weightedInstancesHandler, multiInstanceHandler); testsPerClassType(Attribute.STRING, weightedInstancesHandler, multiInstanceHandler); testsPerClassType(Attribute.RELATIONAL, weightedInstancesHandler, multiInstanceHandler); } /** * Set the associator to test. * * @param newAssociator the Associator to use. */ public void setAssociator(Associator newAssociator) { m_Associator = newAssociator; } /** * Get the associator being tested * * @return the associator being tested */ public Associator getAssociator() { return m_Associator; } /** * Run a battery of tests for a given class attribute type * * @param classType true if the class attribute should be numeric * @param weighted true if the associator says it handles weights * @param multiInstance true if the associator is a multi-instance associator */ protected void testsPerClassType(int classType, boolean weighted, boolean multiInstance) { boolean PNom = canPredict(true, false, false, false, false, multiInstance, classType)[0]; boolean PNum = canPredict(false, true, false, false, false, multiInstance, classType)[0]; boolean PStr = canPredict(false, false, true, false, false, multiInstance, classType)[0]; boolean PDat = canPredict(false, false, false, true, false, multiInstance, classType)[0]; boolean PRel; if (!multiInstance) { PRel = canPredict(false, false, false, false, true, multiInstance, classType)[0]; } else { PRel = false; } if (PNom || PNum || PStr || PDat || PRel) { if (weighted) { instanceWeights(PNom, PNum, PStr, PDat, PRel, multiInstance, classType); } if (classType == Attribute.NOMINAL) { canHandleNClasses(PNom, PNum, PStr, PDat, PRel, multiInstance, 4); } if (!multiInstance) { canHandleClassAsNthAttribute(PNom, PNum, PStr, PDat, PRel, multiInstance, classType, 0); canHandleClassAsNthAttribute(PNom, PNum, PStr, PDat, PRel, multiInstance, classType, 1); } canHandleZeroTraining(PNom, PNum, PStr, PDat, PRel, multiInstance, classType); boolean handleMissingPredictors = canHandleMissing(PNom, PNum, PStr, PDat, PRel, multiInstance, classType, true, false, 20)[0]; if (handleMissingPredictors) { canHandleMissing(PNom, PNum, PStr, PDat, PRel, multiInstance, classType, true, false, 100); } boolean handleMissingClass = canHandleMissing(PNom, PNum, PStr, PDat, PRel, multiInstance, classType, false, true, 20)[0]; if (handleMissingClass) { canHandleMissing(PNom, PNum, PStr, PDat, PRel, multiInstance, classType, false, true, 100); } correctBuildInitialisation(PNom, PNum, PStr, PDat, PRel, multiInstance, classType); datasetIntegrity(PNom, PNum, PStr, PDat, PRel, multiInstance, classType, handleMissingPredictors, handleMissingClass); } } /** * Run a battery of tests without a class * * @param weighted true if the associator says it handles weights * @param multiInstance true if the associator is a multi-instance associator */ protected void testsWithoutClass(boolean weighted, boolean multiInstance) { boolean PNom = canPredict(true, false, false, false, false, multiInstance, NO_CLASS)[0]; boolean PNum = canPredict(false, true, false, false, false, multiInstance, NO_CLASS)[0]; boolean PStr = canPredict(false, false, true, false, false, multiInstance, NO_CLASS)[0]; boolean PDat = canPredict(false, false, false, true, false, multiInstance, NO_CLASS)[0]; boolean PRel; if (!multiInstance) { PRel = canPredict(false, false, false, false, true, multiInstance, NO_CLASS)[0]; } else { PRel = false; } if (PNom || PNum || PStr || PDat || PRel) { if (weighted) { instanceWeights(PNom, PNum, PStr, PDat, PRel, multiInstance, NO_CLASS); } canHandleZeroTraining(PNom, PNum, PStr, PDat, PRel, multiInstance, NO_CLASS); boolean handleMissingPredictors = canHandleMissing(PNom, PNum, PStr, PDat, PRel, multiInstance, NO_CLASS, true, false, 20)[0]; if (handleMissingPredictors) { canHandleMissing(PNom, PNum, PStr, PDat, PRel, multiInstance, NO_CLASS, true, false, 100); } correctBuildInitialisation(PNom, PNum, PStr, PDat, PRel, multiInstance, NO_CLASS); datasetIntegrity(PNom, PNum, PStr, PDat, PRel, multiInstance, NO_CLASS, handleMissingPredictors, false); } } /** * Checks whether the scheme can take command line options. * * @return index 0 is true if the associator can take options */ protected boolean[] canTakeOptions() { boolean[] result = new boolean[2]; print("options..."); if (m_Associator instanceof OptionHandler) { println("yes"); if (m_Debug) { println("\n=== Full report ==="); Enumeration




© 2015 - 2024 Weber Informatics LLC | Privacy Policy