Created
April 25, 2021 01:30
-
-
Save shubham0204/25221055def8ce81e0020c90e60d1974 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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