Created
July 8, 2011 17:16
-
-
Save actsasgeek/1072298 to your computer and use it in GitHub Desktop.
Instance object that handles missing values.
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
| object Instance { | |
| def parseCSV( instanceAsCSVString: String): Instance = { | |
| def extractFeatureValues( parsedInstance: Array[ String]): List[ Option[Double]] = { | |
| val featuresStartIndex = 0 | |
| val featuresEndIndex = parsedInstance.length - 2 | |
| val featureBuffer = new ListBuffer[Option[Double]]() | |
| for ( index <- featuresStartIndex to featuresEndIndex) { | |
| val token = parsedInstance( index) | |
| val value = try { | |
| Some( token.toDouble) | |
| } catch { | |
| case nfe: NumberFormatException => { | |
| if ( token == "?") { | |
| None | |
| } else { | |
| throw new MalformedInstanceException | |
| } | |
| } | |
| } | |
| featureBuffer += value | |
| } | |
| featureBuffer.toList | |
| } | |
| val parsedInstance = instanceAsCSVString.split( ",") | |
| val featureValues = extractFeatureValues( parsedInstance) | |
| val classLabelIndex = parsedInstance.length - 1 | |
| val classLabel = parsedInstance( classLabelIndex) | |
| new Instance( featureValues, Some( classLabel)) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment