Created
April 20, 2021 14:08
-
-
Save shubham0204/eee57053e97a8c6510a463100eb87b1e 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
// Holds data for a particular feature. | |
class FeatureColumn( var name : String , var data : FloatArray ) { | |
// Mean of given `data` | |
var featureMean : Float | |
// Variance of given `data` | |
var featureVariance : Float | |
// Standard deviation of given `data` | |
var featureStdDev : Float | |
init { | |
featureMean = computeMean() | |
featureVariance = computeVariance() | |
// Compute the standard deviation of `data` = sqrt( variance ) | |
featureStdDev = featureVariance.pow( 0.5f ) | |
} | |
override fun toString(): String { | |
return "{ name = $name , mean = $featureMean , stddev = $featureStdDev }" | |
} | |
// Compute the mean of `data`. | |
private fun computeMean() : Float { | |
return data.average().toFloat() | |
} | |
// Compute the variance of `data`. | |
private fun computeVariance() : Float { | |
val mean = data.average().toFloat() | |
val meanSquaredSum = data.map{ xi -> ( xi - mean ).pow( 2 ) }.sum() | |
return meanSquaredSum / data.size | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment