Inteligentne aplikacje internetowe

Laboratorium 3

Użycie klasyfikatorów z pakietu WEKI w kodzie Javy

Kod każdego ćwiczenia zapisz w oddzielnym pliku w projekcie (Eclipse lub NetBeans)

Ćwiczenie 1. Budowa klasyfikatora (WEKI in Java)

  1. Wczytaj dane

  2. Utwórz klasyfikatora (J48)

  3. Wyświetl na konsoli dostępne właściwości klasyfikatora




Ćwiczenie 2.

  1. Wczytaj dane

  2. Utwórz klasyfikator (k-nn) (weka.classifiers.lazy.IBk)

  3. Wyświetl na konsoli dostępne właściwości klasyfikatora



Ćwiczenie 3. Ewaluacja klasyfikatora (CV)

  1. Dokonaj ewaluacji zbudowanych klasyfikatorów

  2. Wypisz statystyki z ewaluacji, wyświetl macierz pomyłek




PS.

Cross-validation

If you only have a training set and no test you might want to evaluate the classifier by using 10 times 10-fold cross-validation. This can be very easily done via the Evaluation class. Here we seed the random selection of our folds for the CV with 1. Check out the Evaluation class for more information about the statistics it produces.

Note: The classifier (in our example tree) is not supposed to be trained when handed over to the crossValidateModel method. Why? If the classifier does not abide to the Weka conventions, that a classifier has to be re-initialized every time the buildClassifier method is called (in other words: subsequent calls to the buildClassifier method always return the same results), you will get inconsistent and worthless results. The crossValidateModel takes care of training and evaluating the classifier (it creates a copy of the original classifier that you hand over to the crossValidateModel for each run of the cross-validation).

Statistics

Some methods for retrieving the results from the evaluation:

  • nominal class

    • correct() - number of correctly classified instances (see also incorrect())

    • pctCorrect() - percentage of correctly classified instances (see also pctIncorrect())

    • kappa() - Kappa statistics

  • numeric class

    • correlationCoefficient() - correlation coefficient

  • general

    • meanAbsoluteError() - the mean absolute error

    • rootMeanSquaredError() - the root mean squared error

    • unclassified() - number of unclassified instances

    • pctUnclassified() - percentage of unclassified instances



Ćwiczenie 4.Ustawianie opcji klasyfikatora

  1. Utwórz klasyfikator J48 z opcją bez przycinania drzewa

  2. Utwórz klasyfikator k-NN z opcją 3 najbliższych sąsiadów

  3. Porównaj wyniki ewaluacji

import weka.classifiers.trees.J48;
 ...
 String[] options = new String[1];
 options[0] = "-U";            // unpruned tree
 J48 tree = new J48();         // new instance of tree
 tree.setOptions(options);     // set the options
 tree.buildClassifier(data);   // build classifier

NAME

weka.classifiers.lazy.IBk

SYNOPSIS

K-nearest neighbours classifier. Can select appropriate value of K based on cross-validation. Can also do distance weighting.

For more information, see

D. Aha, D. Kibler (1991). Instance-based learning algorithms. Machine Learning. 6:37-66.

OPTIONS

KNN -- The number of neighbours to use.

NAME

weka.classifiers.trees.J48

SYNOPSIS

Class for generating a pruned or unpruned C4.5 decision tree. For more information, see

Ross Quinlan (1993). C4.5: Programs for Machine Learning. Morgan Kaufmann Publishers, San Mateo, CA.

Ćwiczenie 5. Klasyfikowanie nowych przykładów podanych w pliku arff

  1. Utwórz plik arff z przykładami bez podanej klasy decyzyjnej (unlabeled.arff)

  2. Dokonaj klasyfikacji obydwoma klasyfikatorami przykładów wczytanych z pliku (unlabeled.arff)

  3. Zapisz wyniki klasyfikacji w pliku labeled.arff






PS.

Classifying instances

In case you have an unlabeled dataset that you want to classify with your newly trained classifier, you can use the following code snippet. It loads the file /some/where/unlabeled.arff, uses the previously built classifier tree to label the instances, and saves the labeled data as /some/where/labeled.arff.

Note on nominal classes:

  • If you're interested in the distribution over all the classes, use the method distributionForInstance(Instance). This method returns a double array with the probability for each class.

  • The returned double value from classifyInstance (or the index in the array returned by distributionForInstance) is just the index for the string values in the attribute. I.e., if you want the string representation for the above returned class label clsLabel, then you can, e.g., print it like this:






Wydział Informatyki, Politechnika Białostocka