Skip to content

Instantly share code, notes, and snippets.

@yzhang1991
Created October 6, 2017 19:26
Show Gist options
  • Save yzhang1991/ef18172fb3a2ac04e2d71816c492027a to your computer and use it in GitHub Desktop.
Save yzhang1991/ef18172fb3a2ac04e2d71816c492027a to your computer and use it in GitHub Desktop.
IrisKNNClassifier example
public class IrisKNNClassifier extends VoltProcedure {
// The query to extract the data from a VoltDB table.
public final SQLStmt selectData =
new SQLStmt("SELECT sepal_length, sepal_width, petal_length, petal_width, class FROM iris;");
// Number of attributes.
private static final int attributeCount = 4;
private static Classifier knnc;
// The train method (Java stored procedure).
public long run() {
// Run the SELECT query to get the data.
voltQueueSQL(selectData);
VoltTable[] queryResults = voltExecuteSQL(true);
VoltTable dataTable = queryResults[0];
// Build the data set that can be used by the machine learning library.
Dataset dataset = new DefaultDataset();
while (dataTable.advanceRow()) {
DenseInstance dataRow = new DenseInstance(attributeCount);
for (int i = 0; i < attributeCount; i++) {
dataRow.put(i, dataTable.getDouble(i));
}
dataRow.setClassValue(dataTable.getString(attributeCount));
dataset.add(dataRow);
}
// Train classifier.
knnc = new KNearestNeighbors(5);
knnc.buildClassifier(dataset);
return ClientResponse.SUCCESS;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment