Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Weka and LibSVM usage
DatabaseLoader databaseLoader = new DatabaseLoader();
String q = "SELECT news, type FROM table_name
databaseLoader.setUser("root"); // username
databaseLoader.setPassword(""); // password
databaseLoader.setQuery(q);
// load the data
Instances dataUnfiltered0 = databaseLoader.getDataSet();
// coverting the 'type' column into nominal values
StringToNominal filter0 = new StringToNominal();
String[] options = weka.core.Utils.splitOptions("-R last"); //apply only to last column (type)
filter0.setOptions(options);
filter0.setInputFormat(dataUnfiltered0);
Instances dataFiltered0 = Filter.useFilter(dataUnfiltered0, filter0);
dataFiltered0.setClassIndex(dataFiltered0.numAttributes() - 1);
// converting textual data to numerical data
//create new filter for vector transformation
StringToWordVector filter = new StringToWordVector();
filter.setLowerCaseTokens(true);
filter.setOutputWordCounts(true);
// you can set some more options to filter here
filter.setInputFormat(dataFiltered0);
Instances dataFiltered = Filter.useFilter(dataFiltered0, filter);
System.out.println("\n\nFiltered data:\n\n" + dataFiltered);
//initialize the model
LibSVM svm = new LibSVM();
// you can set more options to svm here
// evaluating the model
Evaluation evaluation = new Evaluation(dataFiltered);
evaluation.crossValidateModel(svm, dataFiltered, 2, new Random(1));
System.out.println(evaluation.toSummaryString());
System.out.println(evaluation.weightedAreaUnderROC());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.