Skip to content

Instantly share code, notes, and snippets.

@shubham0204
Created March 14, 2021 08:54
Show Gist options
  • Save shubham0204/331159f55f28cf65cfa6e7d156a3a2b0 to your computer and use it in GitHub Desktop.
Save shubham0204/331159f55f28cf65cfa6e7d156a3a2b0 to your computer and use it in GitHub Desktop.
// Predict a class for the given sample using the Random Forest.
fun predict( x : HashMap<String,String> ) : String {
// Create an empty array to store class labels.
val treeOutputs = Array( NUM_TREES ) { "" }
for ( i in 0 until NUM_TREES ) {
// Store the output of each DecisionTree in our forest.
treeOutputs[ i ] = forest[i].predict( x )
println( "Prediction ${i+1} DecisionTree is ${treeOutputs[i]}")
}
// Get the majority label, which is our final prediction for the given sample.
val mostVotedLabel = treeOutputs.groupingBy{ it }.eachCount().maxBy{ entry -> entry.value }!!.key
println( "Most voted label : $mostVotedLabel" )
return mostVotedLabel
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment