Menu

Get raw percentages using API

Help
2014-12-15
2014-12-16
  • Peter Figliozzi

    Peter Figliozzi - 2014-12-15

    I am currently using GNaiveBayes along with GAutoFilter. When I use my model for a prediction:

    model.predict(query_vector, predicted_label);
    

    Then the predicted_label holds the predicted class (0 or 1). Instead, I would like to get the numerical values, such p(0)=0.45 and p(1)=0.55, because I would like to set the classification threshold to another value. (I assume it uses 0.5 as default).

    Is this possible with the existing Waffles classes, or do I need to make a new subclass of GNaiveBayes?

    Thanks

    Pete

     
  • Mike Gashler

    Mike Gashler - 2014-12-15

    The GNaiveBayes::predictDistribution method returns GPrediction objects. The GPrediction::asCategorical method returns categorical distributions. The, GCategoricalDistribution::likelihood method will tell you the likelihood of each category.

     
  • Peter Figliozzi

    Peter Figliozzi - 2014-12-16

    Thanks Mike.
    Here's a code snippet for anyone else who is trying to figure this out:

    void printRaw(double *query_vector, GAutoFilter & model){
      GPrediction prediction;
      model.predictDistribution(query_vector,  & prediction);
      GCategoricalDistribution * catDist = prediction.asCategorical();
      double p_bad = catDist->likelihood(0);
      double p_good = catDist->likelihood(1);
      cout << "p_good = " << p_good << "  p_bad = " << p_bad << endl;
    }
    

    This assumes you've created your model like so:

     // load the training data
      GMatrix training_matrix;
      training_matrix.loadArff("training.arff");
      // Split into separate data and class matrices, as required by Waffles algos
      // The "1" in the constructor means keep the last column as the label
      GDataColSplitter splitter(training_matrix, 1);
      GMatrix & features = splitter.features();
      GMatrix & labels = splitter.labels();
      // Create and train a model
      GAutoFilter model(new GNaiveBayes());
      model.train(features, labels);
    
     

Anonymous
Anonymous

Add attachments
Cancel





Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.