Skip to content

Commit

Permalink
moved test to test package, fixed bugs, added learning curves compari…
Browse files Browse the repository at this point in the history
…son pdf
  • Loading branch information
ptnplanet committed Mar 11, 2012
1 parent aa92e66 commit 22f3889
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 72 deletions.
25 changes: 13 additions & 12 deletions BayesClassifier.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package de.daslaboratorium.machinelearning.classifier;

import java.util.AbstractMap.SimpleEntry;
import java.util.Collection;
import java.util.Comparator;
import java.util.Map.Entry;
import java.util.SortedSet;
import java.util.TreeSet;

Expand Down Expand Up @@ -97,22 +95,25 @@ public int compare(Classification<T, K> o1,
* @return The category the set of features is classified as.
*/
@Override
public K classify(Collection<T> features) {
public Classification<T, K> classify(Collection<T> features) {
SortedSet<Classification<T, K>> probabilites =
this.categoryProbabilities(features);

System.out.println("Results:\t");
for (Classification<T, K> prob : probabilites)
System.out.println(prob);

if (probabilites.size() > 0) {
System.out.println("Classified as " +
probabilites.last().getCategory());
return probabilites.last().getCategory();
} else {
System.out.println("No results");
return probabilites.last();
}
return null;
}

/**
* Classifies the given set of features. and return the full details of the
* classification.
*
* @return The set of categories the set of features is classified as.
*/
public Collection<Classification<T, K>> classifyDetailed(
Collection<T> features) {
return this.categoryProbabilities(features);
}

}
53 changes: 36 additions & 17 deletions Classifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public abstract class Classifier<T, K> implements IFeatureProbability<T, K> {
/**
* The initial memory capacity or how many classifications are memorized.
*/
private static final int MEMORY_CAPACITY = 5;
private int memoryCapacity = 200;

/**
* A dictionary mapping features to their number of occurrences in each
Expand Down Expand Up @@ -114,6 +114,28 @@ public int getCategoriesTotal() {
return toReturn;
}

/**
* Retrieves the memory's capacity.
*
* @return The memory's capacity.
*/
public int getMemoryCapacity() {
return memoryCapacity;
}

/**
* Sets the memory's capacity. If the new value is less than the old
* value, the memory will be truncated accordingly.
*
* @param memoryCapacity The new memory capacity.
*/
public void setMemoryCapacity(int memoryCapacity) {
for (int i = this.memoryCapacity; i > memoryCapacity; i--) {
this.memoryQueue.poll();
}
this.memoryCapacity = memoryCapacity;
}

/**
* Increments the count of a given feature in the given category. This is
* equal to telling the classifier, that this feature has occurred in this
Expand All @@ -140,7 +162,7 @@ public void incrementFeature(T feature, K category) {

Integer totalCount = this.totalFeatureCount.get(feature);
if (totalCount == null) {
this.totalFeatureCount.put(feature, 1);
this.totalFeatureCount.put(feature, 0);
totalCount = this.totalFeatureCount.get(feature);
}
this.totalFeatureCount.put(feature, ++totalCount);
Expand All @@ -155,7 +177,7 @@ public void incrementFeature(T feature, K category) {
public void incrementCategory(K category) {
Integer count = this.totalCategoryCount.get(category);
if (count == null) {
this.totalCategoryCount.put(category, 1);
this.totalCategoryCount.put(category, 0);
count = this.totalCategoryCount.get(category);
}
this.totalCategoryCount.put(category, ++count);
Expand All @@ -179,20 +201,23 @@ public void decrementFeature(T feature, K category) {
if (count == null) {
return;
}
if (count == 1) {
if (count.intValue() == 1) {
features.remove(feature);
if (features.size() == 0) {
this.featureCountPerCategory.remove(category);
}
} else {
count--;
features.put(feature, --count);
}

Integer totalCount = this.totalFeatureCount.get(feature);
if (totalCount == null) {
return;
}
if (totalCount == 1) {
if (totalCount.intValue() == 1) {
this.totalFeatureCount.remove(feature);
} else {
totalCount--;
this.totalFeatureCount.put(feature, --totalCount);
}
}

Expand All @@ -207,10 +232,10 @@ public void decrementCategory(K category) {
if (count == null) {
return;
}
if (count == 1) {
if (count.intValue() == 1) {
this.totalCategoryCount.remove(category);
} else {
count--;
this.totalCategoryCount.put(category, --count);
}
}

Expand Down Expand Up @@ -360,20 +385,14 @@ public void learn(K category, Collection<T> features) {
*/
public void learn(Classification<T, K> classification) {

System.out.println("Learning new classification:\t"
+ classification);

for (T feature : classification.getFeatureset())
this.incrementFeature(feature, classification.getCategory());
this.incrementCategory(classification.getCategory());

this.memoryQueue.offer(classification);
if (this.memoryQueue.size() > Classifier.MEMORY_CAPACITY) {
if (this.memoryQueue.size() > this.memoryCapacity) {
Classification<T, K> toForget = this.memoryQueue.remove();

System.out.println("Memory over capacity. Forgetting about\t"
+ toForget);

for (T feature : toForget.getFeatureset())
this.decrementFeature(feature, toForget.getCategory());
this.decrementCategory(toForget.getCategory());
Expand All @@ -387,6 +406,6 @@ public void learn(Classification<T, K> classification) {
* @param features The features to classify.
* @return The category most likely.
*/
public abstract K classify(Collection<T> features);
public abstract Classification<T, K> classify(Collection<T> features);

}
43 changes: 0 additions & 43 deletions ClassifierTester.java

This file was deleted.

Binary file added forgetful-learning.pdf
Binary file not shown.

0 comments on commit 22f3889

Please sign in to comment.