3

I am facing the following problem: I am trying to implement a MNB classifier in a sliding window. I implemented a LinkedList of the size of the window and store all instances of the stream which have to be considered in it. When a new instance arrives which doesn't fit in the window anymore the first instance is removed. To remove the corresponding word counts I implemented the following method which basically is the same as trainOnInstanceImpl() by moa just backwards:

private void removeInstance(Instance instToRemove) {

    int classIndex = instToRemove.classIndex();
    int classValue = (int) instToRemove.value(classIndex);

    double w = instToRemove.weight();
    m_probOfClass[classValue] -= w;

    m_classTotals[classValue] -= w * totalSize(instToRemove);
    double total = m_classTotals[classValue];

    for (int i = 0; i < instToRemove.numValues(); i++) {
        int index = instToRemove.index(i); 

        if (index != classIndex && !instToRemove.isMissing(i)) {

            double laplaceCorrection = 0.0;
            if (m_wordTotalForClass[classValue].getValue(index) == w*instToRemove.valueSparse(i) + this.laplaceCorrectionOption.getValue()) {
                laplaceCorrection = this.laplaceCorrectionOption.getValue(); //1.0

            }

            m_wordTotalForClass[classValue].addToValue(index,
                    (-1)*(w * instToRemove.valueSparse(i) + laplaceCorrection));
        }
    }

Now if I output the m_wordTotalForClass[classValue] I get different results for a classical MNB over a stream with 3000 instances from instance 2000-3000 as from the sliding window MNB (see above) with a window Size of 1000. And the only differences are that it outputs a 1 instead of a 0 at some points but not always. I guess this has something to do with the laplace correction. Maybe there is a problem with rounding in the if-statement:

if (m_wordTotalForClass[classValue].getValue(index) == w*instToRemove.valueSparse(i) + this.laplaceCorrectionOption.getValue())

so that we not always enter the part where the laplace value is set.

Does anybody have an idea? I am kind of going crazy as I have been thinking about where the problem may be for the last three days. Any help would be greatly appreciated!

Geshode
  • 3,600
  • 6
  • 18
  • 32

0 Answers0