java 中一维数据的轻量级增量分类

Lightweight incremental classification of 1 dimensional data in java

我有一组观察值对(值,class)。值是自然数。只有两个 classes。我希望在单个决策点分离 classes 非常容易,例如,class A if value < 10,class B if value >= 10。唯一的困难classes 和决策边界附近的值之间存在一些重叠。

是否有一种快速、轻量级的方法来更新观察结果并class在Java中为这个问题确定一个新的数据点?理想情况下是这样的:

classifier.addObservation(observation);
classifier.classify(value);   

一个解决方案将是一个 Java 包的演示,以及您选择算法的理由。

经过一番搜索,我最终使用了 Weka。特别是我使用了朴素贝叶斯分类器。他们的数据结构有点深奥,但它确实有效而且速度很快。

package agent.agenttype.ijcai;
import weka.classifiers.Classifier;
import weka.classifiers.bayes.NaiveBayes;
import weka.core.Attribute;
import weka.core.FastVector;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.SparseInstance;

public class Example {

     public static enum ClassLabel {A, B};

     Instances trainingSet;
     FastVector att = new FastVector(2);
     FastVector cl = new FastVector(2);

     public Example(){
         //Add class labels
         cl.addElement(ClassLabel.values()[0].name());
         cl.addElement(ClassLabel.values()[1].name());
         //set the name of our value attribute
         Attribute Attribute1 = new Attribute("Value"); 
         //set the name of our class label atrribute
         Attribute ClassAttribute = new Attribute("Label", cl);      
         att.addElement(Attribute1);
         att.addElement(ClassAttribute);    
         //create training set that uses our attributes to interpret instances
         trainingSet = new Instances("TrainingSet", att, 2);
         trainingSet.setClassIndex(1);//tell our training set that index 2 of instances is the class label
     }

    public void addObservationToEdge(int value, ClassLabel classLabel){
            Instance instance = new SparseInstance(2);
            instance.setValue((Attribute)att.elementAt(0), value); //set value
            instance.setValue((Attribute)att.elementAt(1), classLabel.name());//set our
            trainingSet.add(instance);
    }

    public ClassLabel classifyValue( int value) throws Exception{

         Instance instanceForClassification = new SparseInstance(1);
         instanceForClassification.setValue((Attribute)att.elementAt(0), value);
         instanceForClassification.setDataset(trainingSet);//make instance inherit attribute labels from training set

         Classifier cModel = (Classifier)new NaiveBayes();//create naive bayes classifier
         cModel.buildClassifier(trainingSet);

         int labelNumber = (int) cModel.classifyInstance(instanceForClassification);
         return ClassLabel.values()[labelNumber];
    }

    public static void main(String[] args){
        Example example = new Example();
        example.addObservationToEdge(1, ClassLabel.A);
        example.addObservationToEdge(2, ClassLabel.A);
        example.addObservationToEdge(5, ClassLabel.A);
        example.addObservationToEdge(11, ClassLabel.A);
        example.addObservationToEdge(9, ClassLabel.B);
        example.addObservationToEdge(12, ClassLabel.B);
        example.addObservationToEdge(15, ClassLabel.B);
        example.addObservationToEdge(20, ClassLabel.B);

        try {
        //print classification results
        for(int i = 0; i<20; i++){
            System.out.println("Value: " + i + " Class Label:" + example.classifyValue(i));
        }
        } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }       
    }

}

输出:

Value: 0 Class Label:A
Value: 1 Class Label:A
Value: 2 Class Label:A
Value: 3 Class Label:A
Value: 4 Class Label:A
Value: 5 Class Label:A
Value: 6 Class Label:A
Value: 7 Class Label:A
Value: 8 Class Label:A
Value: 9 Class Label:A
Value: 10 Class Label:B
Value: 11 Class Label:B
Value: 12 Class Label:B
Value: 13 Class Label:B
Value: 14 Class Label:B
Value: 15 Class Label:B
Value: 16 Class Label:B
Value: 17 Class Label:B
Value: 18 Class Label:B
Value: 19 Class Label:B