Created
October 6, 2017 19:26
-
-
Save yzhang1991/ef18172fb3a2ac04e2d71816c492027a to your computer and use it in GitHub Desktop.
IrisKNNClassifier example
This file contains 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
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