Skip to content

Instantly share code, notes, and snippets.

@shubham0204
Created April 25, 2021 01:30
Show Gist options
  • Save shubham0204/25221055def8ce81e0020c90e60d1974 to your computer and use it in GitHub Desktop.
Save shubham0204/25221055def8ce81e0020c90e60d1974 to your computer and use it in GitHub Desktop.
// Predict the label for the given sample.
private fun predictLabel( sample : FloatArray ) {
val probArray = FloatArray( dataFrame.numClasses )
for ( ( i , priorProb ) in priorProbabilities.values.withIndex()) {
// We take the log probabilities so as to avoid underflow.
var p = log10( priorProb )
// While we take log, the product is transformed into a sum
// log( a . b ) = log(a) + log(b)
for ( j in 0 until dataFrame.numFeatures ) {
p += featureDistributions[ j ].getLogProb( sample[ i ] )
}
probArray[ i ] = p
}
// Get the label with highest probability.
val label = priorProbabilities.keys.toTypedArray()[ probArray.indexOf( probArray.max()!!) ]
resultCallback?.onPredictionResult( label , probArray )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment