<commit_before>package model.supervised.naivebayes;

import data.DataSet;
import model.Predictable;
import model.Trainable;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import utils.random.RandomUtils;
import utils.sort.SortIntDoubleUtils;

import java.util.HashMap;

/**
 * Created by hanxuan on 10/15/15 for machine_learning.
 */
public abstract class NaiveBayes implements Predictable, Trainable{

    private static final Logger log = LogManager.getLogger(NaiveBayes.class);

    protected DataSet data = null;

    protected int featureLength = Integer.MIN_VALUE;

    protected HashMap<Integer, Object> indexClassMap = null;

    protected int classCount = Integer.MIN_VALUE;

    private double[] priors = null;


    //******************************//

    protected abstract double[] predictClassProbability(double[] features);

    protected abstract void naiveBayesTrain();

    protected abstract void naiveBayesInit();

    //******************************//

    @Override
    public double predict(double[] feature) {

        double[] probabilities = predictClassProbability(feature);
        for (int i = 0; i < classCount; i++) {
            probabilities[i] += Math.log(priors[i]);
        }
        int[] indexes = RandomUtils.getIndexes(classCount);
        SortIntDoubleUtils.sort(indexes, probabilities);
        return indexes[indexes.length - 1];
    }

    @Override
    public double score(double[] feature) {
        double[] probabilities = predictClassProbability(feature);
        double score = probabilities[1] - probabilities[0];
        return score;
    }

    @Override<commit_msg>add probs method<commit_after>package model.supervised.naivebayes;

import data.DataSet;
import model.Predictable;
import model.Trainable;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import utils.array.ArraySumUtil;
import utils.random.RandomUtils;
import utils.sort.SortIntDoubleUtils;

import java.util.HashMap;

/**
 * Created by hanxuan on 10/15/15 for machine_learning.
 */
public abstract class NaiveBayes implements Predictable, Trainable{

    private static final Logger log = LogManager.getLogger(NaiveBayes.class);

    protected DataSet data = null;

    protected int featureLength = Integer.MIN_VALUE;

    protected HashMap<Integer, Object> indexClassMap = null;

    protected int classCount = Integer.MIN_VALUE;

    private double[] priors = null;


    //******************************//

    protected abstract double[] predictClassProbability(double[] features);

    protected abstract void naiveBayesTrain();

    protected abstract void naiveBayesInit();

    //******************************//

    @Override
    public double predict(double[] feature) {

        double[] probabilities = probs(feature);
        int[] indexes = RandomUtils.getIndexes(classCount);
        SortIntDoubleUtils.sort(indexes, probabilities);
        return indexes[indexes.length - 1];
    }

    @Override
    public double[] probs(double[] feature) {
        double[] probabilities = predictClassProbability(feature);
        for (int i = 0; i < classCount; i++) {
            probabilities[i] += Math.log(priors[i]);
        }
        return ArraySumUtil.normalize(probabilities);
    }

    @Override
    public double score(double[] feature) {
        double[] probabilities = predictClassProbability(feature);
        double score = probabilities[1] - probabilities[0];
        return score;
    }

    @Override
{'fraction_non_alphanumeric': 0.13011363636363638, 'fraction_numerical': 0.006818181818181818, 'mean_word_length': 3.620734908136483, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4608', 'n_tokens_mistral': 994, 'n_tokens_neox': 922, 'n_words': 237}