Skip to content

Instantly share code, notes, and snippets.

@actsasgeek
Created July 8, 2011 17:16
Show Gist options
  • Select an option

  • Save actsasgeek/1072298 to your computer and use it in GitHub Desktop.

Select an option

Save actsasgeek/1072298 to your computer and use it in GitHub Desktop.
Instance object that handles missing values.
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