Skip to content

Instantly share code, notes, and snippets.

@shubham0204
Last active April 24, 2021 14:31
Show Gist options
  • Save shubham0204/f70e035bbcdc9c812529fd4840dcd834 to your computer and use it in GitHub Desktop.
Save shubham0204/f70e035bbcdc9c812529fd4840dcd834 to your computer and use it in GitHub Desktop.
// Class to implement Gaussian Naive Bayes
class GaussianNB( private var dataFrame : DataFrame ) {
...
// Prior probabilities stored in a HashMap of form ( column_name , prior_prob )
private var priorProbabilities : HashMap<String,Float>
...
// Compute the prior probabilities.
// These probabilities are p( class=some_class ) which are calculated as
// p( class=apple ) = num_samples_label_as_apple / num_samples_in_ds
private fun computePriorProbabilities( labels : Array<String> ) : HashMap<String,Float> {
// Get the count ( freq ) of each unique class in labels.
val labelCountMap = labels.groupingBy { it }.eachCount()
// The prior probabilties are stored in a HashMap of form ( column_name , prob )
val out = HashMap<String,Float>()
for ( ( label , count ) in labelCountMap ) {
// Append the prob with key=column_name
out[ label ] = count.toFloat() / dataFrame.numSamples.toFloat()
}
return out
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment