<commit_before>(ns analyze-data.naive-bayes.predict
  (:require [clojure.core.matrix :as m]))

(defn predict
  "model: map produced by analyze-data.naive-bayes.train/train
   x: vector of term weights representing a document

  Return a sequence of the form
  [[most-likely-label        probability]
   [second-most-likely-label probability]
   ...]"
  [model x]
  (let [{:keys [log-phi log-phi-y]} model
        log-probs (->> (m/mul log-phi x)
                       (map m/esum)
                       (m/add log-phi-y))
        indexed-log-probs (map-indexed #(vector %1 %2) log-probs)]
    (reverse (sort-by second indexed-log-probs))))
<commit_msg>Update docs to reflect predict is returning score not probability.<commit_after>(ns analyze-data.naive-bayes.predict
  (:require [clojure.core.matrix :as m]))

(defn predict
  "model: map produced by analyze-data.naive-bayes.train/train
   x: vector of term weights representing a document

  Return a sequence of the form
  [[most-likely-label        score]
   [second-most-likely-label score]
   ...]"
  [model x]
  (let [{:keys [log-phi log-phi-y]} model
        log-probs (->> (m/mul log-phi x)
                       (map m/esum)
                       (m/add log-phi-y))
        indexed-log-probs (map-indexed #(vector %1 %2) log-probs)]
    (reverse (sort-by second indexed-log-probs))))

{'fraction_non_alphanumeric': 0.13713862120088954, 'fraction_numerical': 0.0029651593773165306, 'mean_word_length': 2.435114503816794, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '2141392', 'n_tokens_mistral': 459, 'n_tokens_neox': 442, 'n_words': 121}