org.knime.base.node.mine.bayes.naivebayes.datamodel
Class AttributeModel

java.lang.Object
  extended by org.knime.base.node.mine.bayes.naivebayes.datamodel.AttributeModel
All Implemented Interfaces:
Comparable<AttributeModel>
Direct Known Subclasses:
ClassAttributeModel, NominalAttributeModel, NumericalAttributeModel

public abstract class AttributeModel
extends Object
implements Comparable<AttributeModel>

This abstract class needs to be implemented by all attribute models and provides missing value handling and some common methods.

Author:
Tobias Koetter, University of Konstanz

Field Summary
(package private) static String MODEL_CONTAINS_NO_CLASS_VALUES
          Invalid cause if the model contains no class values.
(package private) static String MODEL_CONTAINS_NO_RECORDS
          Invalid cause if the model contains no records at all.
 
Constructor Summary
AttributeModel(String attributeName, int noOfMissingVals, boolean skipMissingVals)
          Constructor for class ClassValue.
 
Method Summary
(package private)  void addValue(String classValue, DataCell attrValue)
          Adds the given value to this attribute model.
(package private) abstract  void addValueInternal(String classValue, DataCell attrValue)
          Adds the given value to the concrete implementation.
 int compareTo(AttributeModel o)
          
(package private) static String createHTMLTable(String tableHeading, String keyHeading, String valueHeading, int noOfRows, Map<String,? extends Object> map, boolean addLineBreak)
           
protected static String createTableHeader(String firstHeading, List<String> headings, String lastHeading)
           
 String getAttributeName()
           
(package private) abstract  Collection<String> getClassValues()
           
(package private) abstract  Class<? extends DataValue> getCompatibleType()
           
(package private) abstract  String getHTMLView(int totalNoOfRecs)
           
(package private) abstract  String getHTMLViewHeadLine()
           
 String getInvalidCause()
           
(package private)  String getMissingValueHeader(Collection<String> colNames)
           
(package private)  int getNoOfMissingVals()
           
(package private) abstract  Integer getNoOfRecs4ClassValue(String classValue)
           
(package private)  Double getProbability(String classValue, DataCell attributeValue, double laplaceCorrector)
           
(package private) abstract  double getProbabilityInternal(String classValue, DataCell attributeValue, double laplaceCorrector)
          This should also handle missing values.
(package private) abstract  String getType()
           
(package private) static AttributeModel loadModel(Config config)
           
(package private)  void saveModel(Config config)
           
(package private) abstract  void saveModelInternal(Config config)
           
(package private)  void setInvalidCause(String cause)
           
(package private) static List<String> sortCollection(Collection<String> vals)
           
(package private) abstract  void validate()
          Called after all training rows where added to validate the model.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MODEL_CONTAINS_NO_RECORDS

static final String MODEL_CONTAINS_NO_RECORDS
Invalid cause if the model contains no records at all.

See Also:
Constant Field Values

MODEL_CONTAINS_NO_CLASS_VALUES

static final String MODEL_CONTAINS_NO_CLASS_VALUES
Invalid cause if the model contains no class values.

See Also:
Constant Field Values
Constructor Detail

AttributeModel

AttributeModel(String attributeName,
               int noOfMissingVals,
               boolean skipMissingVals)
Constructor for class ClassValue.

Parameters:
attributeName - the name of the attribute
noOfMissingVals - the number of missing values
skipMissingVals - set to true if the missing values should be skipped during learning and prediction
Method Detail

loadModel

static AttributeModel loadModel(Config config)
                         throws InvalidSettingsException
Parameters:
config - the Config object to read from
Returns:
the attribute model for the given Config object
Throws:
InvalidSettingsException - if the settings are invalid

saveModel

void saveModel(Config config)
Parameters:
config - the Config object to write to

saveModelInternal

abstract void saveModelInternal(Config config)
Parameters:
config - the config object to save to

getType

abstract String getType()
Returns:
the unique type of the model

getAttributeName

public String getAttributeName()
Returns:
the attribute name

getCompatibleType

abstract Class<? extends DataValue> getCompatibleType()
Returns:
the DataValue class to check if the rows are compatible

addValue

void addValue(String classValue,
              DataCell attrValue)
        throws TooManyValuesException
Adds the given value to this attribute model.

Parameters:
classValue - the class value
attrValue - the attribute value. Could be a missing value.
Throws:
TooManyValuesException - if the column contains more unique values than supported by this attribute model

addValueInternal

abstract void addValueInternal(String classValue,
                               DataCell attrValue)
                        throws TooManyValuesException
Adds the given value to the concrete implementation. Should handle missing values as well.

Parameters:
classValue - the class value
attrValue - the attribute value. Could be a missing value.
Throws:
TooManyValuesException - if the column contains more unique values than supported by this attribute model

getNoOfMissingVals

int getNoOfMissingVals()
Returns:
the noOfMissingVals

getMissingValueHeader

String getMissingValueHeader(Collection<String> colNames)
Parameters:
colNames - all column names of the table to check for uniquness
Returns:
the missing value header or null if this model contains no missing attribute values

getClassValues

abstract Collection<String> getClassValues()
Returns:
all class values

getNoOfRecs4ClassValue

abstract Integer getNoOfRecs4ClassValue(String classValue)
Parameters:
classValue - the class value we want the number of records for
Returns:
the number of records with the given class value or null if only missing values where in this row for the given class value or the class value wasn't found at all

getProbability

Double getProbability(String classValue,
                      DataCell attributeValue,
                      double laplaceCorrector)
Parameters:
classValue - the class value to calculate the probability for
attributeValue - the attribute value to calculate the probability for. Could be a missing value.
laplaceCorrector - the Laplace corrector to use. A value greater 0 overcomes zero counts.
Returns:
the calculated probability or null if the cell was a missing one and missing values should be skipped

getProbabilityInternal

abstract double getProbabilityInternal(String classValue,
                                       DataCell attributeValue,
                                       double laplaceCorrector)
This should also handle missing values.

Parameters:
classValue - the class value to calculate the probability for
attributeValue - the attribute value to calculate the probability for. Could be a missing value.
laplaceCorrector - the Laplace corrector to use. A value greater 0 overcomes zero counts.
Returns:
the calculated probability

getHTMLView

abstract String getHTMLView(int totalNoOfRecs)
Parameters:
totalNoOfRecs - the total number of records in the training data
Returns:
the HTML view of this attribute model

sortCollection

static List<String> sortCollection(Collection<String> vals)
Parameters:
vals - the Collection to sort
Returns:
the given Collection in her natural order

validate

abstract void validate()
                throws InvalidSettingsException
Called after all training rows where added to validate the model.

Throws:
InvalidSettingsException - if the model isn't valid

setInvalidCause

void setInvalidCause(String cause)
Parameters:
cause - the cause why this model is invalid

getInvalidCause

public String getInvalidCause()
Returns:
if the model is invalid this method returns the reason why otherwise it returns null.

compareTo

public int compareTo(AttributeModel o)

Specified by:
compareTo in interface Comparable<AttributeModel>

getHTMLViewHeadLine

abstract String getHTMLViewHeadLine()
Returns:
the headline of this model to use in the HTML view

createHTMLTable

static String createHTMLTable(String tableHeading,
                              String keyHeading,
                              String valueHeading,
                              int noOfRows,
                              Map<String,? extends Object> map,
                              boolean addLineBreak)
Parameters:
tableHeading - the optional table headline
keyHeading - the optional headline for the key row
valueHeading - the optional headline for the value row
noOfRows - the number of rows displayed per row
map - the map to create the html table for
addLineBreak - if each sub table should be displayed on a new line
Returns:
a html table with the keys and values of the given map

createTableHeader

protected static String createTableHeader(String firstHeading,
                                          List<String> headings,
                                          String lastHeading)
Parameters:
firstHeading - the optional first heading
headings - the head lines
lastHeading - the optional last heading
Returns:
the string with a html table head line row


Copyright, 2003 - 2010. All rights reserved.
University of Konstanz, Germany.
Chair for Bioinformatics and Information Mining, Prof. Dr. Michael R. Berthold.
You may not modify, publish, transmit, transfer or sell, reproduce, create derivative works from, distribute, perform, display, or in any way exploit any of the content, in whole or in part, except as otherwise expressly permitted in writing by the copyright owner or as specified in the license file distributed with this product.