Skip to content

Instantly share code, notes, and snippets.

@tharinduwijewardane
Created December 29, 2018 08:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tharinduwijewardane/e4eb5e59622aa01cebb56bcd3241dc05 to your computer and use it in GitHub Desktop.
Save tharinduwijewardane/e4eb5e59622aa01cebb56bcd3241dc05 to your computer and use it in GitHub Desktop.
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