1 | package org.greenstone.mars;
|
---|
2 |
|
---|
3 | import weka.core.Instances;
|
---|
4 |
|
---|
5 | import weka.classifiers.Classifier;
|
---|
6 | import weka.classifiers.Evaluation;
|
---|
7 |
|
---|
8 | // Based on:
|
---|
9 | // https://waikato.github.io/weka-wiki/use_weka_in_your_java_code/
|
---|
10 |
|
---|
11 |
|
---|
12 | // Also, for a more direct command-line approach see
|
---|
13 | // https://waikato.github.io/weka-wiki/saving_and_loading_models/
|
---|
14 | //
|
---|
15 | // You save a trained classifier with the -d option (dumping), e.g.:
|
---|
16 | //
|
---|
17 | // java weka.classifiers.trees.J48 -C 0.25 -M 2 -t /some/where/train.arff -d /other/place/j48.model
|
---|
18 | //
|
---|
19 | // And you can load it with -l and use it on a test set, e.g.:
|
---|
20 | //
|
---|
21 | // java weka.classifiers.trees.J48 -l /other/place/j48.model -T /some/where/test.arff
|
---|
22 |
|
---|
23 |
|
---|
24 | class WekaTrainArousalModel
|
---|
25 | {
|
---|
26 | // Scheme: weka.classifiers.trees.REPTree -M 2 -V 0.001 -N 3 -S 1 -L -1 -I 0.0 -- (i.e., default vals)
|
---|
27 | // Relation: deam-essentia-features-arousal-valence-weka.filters.unsupervised.attribute.Remove-R472-weka.filters.unsupervised.attribute.Remove-R458-466
|
---|
28 | // Instances: 1743
|
---|
29 |
|
---|
30 | // Note, above missed -R 425 and 427 (ordinal vals)
|
---|
31 |
|
---|
32 | public static void main(String[] args)
|
---|
33 | {
|
---|
34 | WekaUtil.checkUsageTraining(args);
|
---|
35 |
|
---|
36 | String input_data_filename = args[0];
|
---|
37 | String output_model_filename = args[1];
|
---|
38 |
|
---|
39 | // knock out 472 (valence) from dataset
|
---|
40 | Instances filtered_data_instances = WekaUtil.loadAndFilterDataInstances(input_data_filename, "472");
|
---|
41 | Classifier classifier = WekaUtil.trainREPTree(filtered_data_instances);
|
---|
42 | System.out.println(classifier);
|
---|
43 |
|
---|
44 | Evaluation eval = WekaUtil.evaluateClassifier(classifier,filtered_data_instances);
|
---|
45 | System.out.println(eval.toSummaryString());
|
---|
46 |
|
---|
47 | try {
|
---|
48 | System.out.println("Saving REPTree classifier model as: " + output_model_filename);
|
---|
49 | weka.core.SerializationHelper.write(output_model_filename, classifier);
|
---|
50 | }
|
---|
51 | catch (Exception e) {
|
---|
52 | e.printStackTrace();
|
---|
53 | }
|
---|
54 | }
|
---|
55 | }
|
---|