Plugging the classifier into a mobile app

There are two ways to incorporate a classifier into a mobile application. The first one involves exporting a model in the Weka format, using the Weka library as a dependency in our mobile application, loading the model, and so on. The procedure is identical to the example we saw in Chapter 3, Basic Algorithms–Classification, Regression, and Clustering. The second approach is more lightweight: we export the model as source code, for example, we create a class implementing the decision tree classifier. Then, we can simply copy and paste the source code into our mobile app, without even importing any Weka dependencies.

Fortunately, some Weka models can be easily exported to source code by the toSource(String) function:

// Output source code implementing the decision tree 
System.out.println("Source code:
" +  
  model.toSource("ActivityRecognitionEngine")); 

This outputs an ActivityRecognitionEngine class that corresponds to our model. Now, let's take a closer look at the output code:

class ActivityRecognitionEngine { 
 
  public static double classify(Object[] i) 
    throws Exception { 
 
    double p = Double.NaN; 
    p = ActivityRecognitionEngine.N17a7cec20(i); 
    return p; 
  } 
  static double N17a7cec20(Object []i) { 
    double p = Double.NaN; 
    if (i[64] == null) { 
      p = 1; 
    } else if (((Double) i[64]).doubleValue() <= 10.353474) { 
    p = ActivityRecognitionEngine.N65b3120a1(i); 
    } else if (((Double) i[64]).doubleValue() > 10.353474) { 
      p = 2; 
    }  
    return p; 
  } 
... 

The outputted ActivityRecognitionEngine class implements the decision tree that we discussed earlier. The machine-generated function names, such as N17a7cec20(Object []), correspond to decision tree nodes. The classifier can be called by the classify(Object[]) method, where we should pass a feature vector obtained by the same procedure as we discussed in the previous sections. As usual, it returns a double, indicating a class label index.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.137.37