weka.classifiers.trees.ht.NominalConditionalSufficientStats Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of weka-dev Show documentation
Show all versions of weka-dev Show documentation
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.
/*
* 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 .
*/
/*
* NominalConditionalSufficientStats.java
* Copyright (C) 2013 University of Waikato, Hamilton, New Zealand
*
*/
package weka.classifiers.trees.ht;
import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import weka.core.Utils;
/**
* Maintains sufficient stats for the distribution of a nominal attribute
*
* @author Richard Kirkby ([email protected])
* @author Mark Hall (mhall{[at]}pentaho{[dot]}com)
* @version $Revision: 10432 $
*/
public class NominalConditionalSufficientStats extends
ConditionalSufficientStats implements Serializable {
/**
* For serialization
*/
private static final long serialVersionUID = -669902060601313488L;
/**
* Inner class that implements a discrete distribution
*
* @author Mark Hall (mhall{[at]}pentaho{[dot]}com)
*
*/
protected class ValueDistribution implements Serializable {
/**
* For serialization
*/
private static final long serialVersionUID = -61711544350888154L;
protected final Map m_dist = new LinkedHashMap();
private double m_sum;
public void add(int val, double weight) {
WeightMass count = m_dist.get(val);
if (count == null) {
count = new WeightMass();
count.m_weight = 1.0;
m_sum += 1.0;
m_dist.put(val, count);
}
count.m_weight += weight;
m_sum += weight;
}
public void delete(int val, double weight) {
WeightMass count = m_dist.get(val);
if (count != null) {
count.m_weight -= weight;
m_sum -= weight;
}
}
public double getWeight(int val) {
WeightMass count = m_dist.get(val);
if (count != null) {
return count.m_weight;
}
return 0.0;
}
public double sum() {
return m_sum;
}
}
protected double m_totalWeight;
protected double m_missingWeight;
@Override
public void update(double attVal, String classVal, double weight) {
if (Utils.isMissingValue(attVal)) {
m_missingWeight += weight;
} else {
new Integer((int) attVal);
ValueDistribution valDist = (ValueDistribution) m_classLookup
.get(classVal);
if (valDist == null) {
valDist = new ValueDistribution();
valDist.add((int) attVal, weight);
m_classLookup.put(classVal, valDist);
} else {
valDist.add((int) attVal, weight);
}
}
m_totalWeight += weight;
}
@Override
public double probabilityOfAttValConditionedOnClass(double attVal,
String classVal) {
ValueDistribution valDist = (ValueDistribution) m_classLookup.get(classVal);
if (valDist != null) {
double prob = valDist.getWeight((int) attVal) / valDist.sum();
return prob;
}
return 0;
}
protected List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy