Skip to content

1.4 WEKA Integration

Nepomuk Seiler edited this page Feb 2, 2012 · 1 revision

A lot of features of knowing are inspired by WEKA or built on top of WEKA like the general exchange class weka.core.Instances. Thus it is not surprising that we want to provide an easy integration of WEKA Algorithms and algorithms built on top of the WEKA API.

Classifier

The minimal way

If you have a WEKA classifier which doesn't need any configuration your are done with two lines of scala code:

class OneR extends WekaClassifier(new weka.classifiers.rules.OneR)

class OneRFactory extends WekaClassifierFactory[OneR, weka.classifiers.rules.OneR](classOf[OneR], classOf[weka.classifiers.rules.OneR]) 

The first line defines our new WekaClassifier wrapper. It takes just the original classifier itself as a parameter. Line two creates the factory. You have to generices: wrapper class and original classifier . The same goes for the class parameters: wrapper class and original classifier.

Configurable Classifier

Normally your classifier got some parameters you want to set. No big deal. Just override some methods:

class NaiveBayes extends WekaClassifier(new weka.classifiers.bayes.NaiveBayes) {

  override def configure(properties:Properties) = {
    //Cast the classifier so you can call your methods
    val bayes = classifier.asInstanceOf[weka.classifiers.bayes.NaiveBayes]
    
    //Extract parameters and configure you're classifier
    val kernel = properties.getProperty(KERNEL_ESTIMATOR, "false")
    val boolKernel = kernel.toBoolean
    bayes.setUseKernelEstimator(boolKernel)
    
    val supervised = properties.getProperty(SUPERVISED_DISCRETIZATION, "false")
    bayes.setUseSupervisedDiscretization(supervised.toBoolean)
    
    val debug = properties.getProperty(DEBUG, "false")
    bayes.setDebug(debug.toBoolean)
  }
}

The Factory is a bit more code:

object NaiveBayesFactory {
        //parameter names for the DPU
	val KERNEL_ESTIMATOR = "kernel-estimator"
	val SUPERVISED_DISCRETIZATION = "supervised-discretization"
}

class NaiveBayesFactory extends WekaClassifierFactory[NaiveBayes, weka.classifiers.bayes.NaiveBayes](classOf[NaiveBayes], classOf[weka.classifiers.bayes.NaiveBayes]) {

  // Standard configuration
  override def createDefaultProperties: Properties = {
    val returns = new Properties
    returns.setProperty(KERNEL_ESTIMATOR, "false")
    returns.setProperty(SUPERVISED_DISCRETIZATION, "false")
    returns.setProperty(DEBUG, "false")
    returns
  }
  
  // Map: parameter-name -> Valid parameter values
  // For an editor to validate input
  override def createPropertyValues: Map[String, Array[_<:Any]] = {
    Map(KERNEL_ESTIMATOR -> boolean_property,
        SUPERVISED_DISCRETIZATION -> boolean_property,
        DEBUG -> boolean_property)
  }

  // Map: parameter-name -> parameter description
  // Information displayed for users
  override def createPropertyDescription: Map[String, String] = {
        Map(KERNEL_ESTIMATOR -> "?",
        SUPERVISED_DISCRETIZATION -> "?",
        DEBUG -> "Debug true/false")
  }
}

The last two methods are just for easier configuration for others if your algorithm sjould be used by other people as well.