| Did you know ... | Search Documentation: |
| Pack logtalk -- logtalk-3.100.1/docs/handbook/_sources/libraries/c45_classifier.rst.txt |
.. _library_c45_classifier:
c45_classifierThis library implements the C4.5 decision tree learning algorithm. C4.5 is an extension of the ID3 algorithm that uses information gain ratio instead of information gain for attribute selection, which avoids bias towards attributes with many values (see below for implementation details).
The library implements the classifier_protocol defined in the
classification_protocols library. It provides predicates for
learning a decision tree from a dataset, optionally prune it, using it
to make predictions, and exporting it as a list of predicate clauses or
to a file.
Datasets are represented as objects implementing the
dataset_protocol protocol from the classification_protocols
library. See test_files directory for examples.
Open the `../../apis/library_index.html#c45_classifier <../../apis/library_index.html#c45_classifier>`__ link in a web browser.
To load all entities in this library, load the loader.lgt file:
::
| ?- logtalk_load(c45_classifier(loader)).
To test this library predicates, load the tester.lgt file:
::
| ?- logtalk_load(c45_classifier(tester)).
For discrete attributes, the learned decision tree is represented as
leaf(Class) for leaf nodes and tree(Attribute, Subtrees) for
internal nodes with discrete attributes, where Subtrees is a list of
Value-Subtree pairs.
For continuous (numeric) attributes, the tree uses binary threshold
splits represented as
tree(Attribute, threshold(Threshold), LeftSubtree, RightSubtree)
where LeftSubtree corresponds to values =< Threshold and
RightSubtree to values > Threshold.
To learn a decision tree from a dataset:
::
| ?- c45_classifier::learn(play_tennis, Tree).
To prune a learned tree with the default parameters (confidence factor 0.25, minimum instances per leaf 2):
::
| ?- c45_classifier::learn(breast_cancer, Tree),
c45_classifier::prune(breast_cancer, Tree, PrunedTree).
To prune a learned tree with both custom confidence factor and minimum instances per leaf:
::
| ?- c45_classifier::learn(breast_cancer, Tree),
c45_classifier::prune(breast_cancer, Tree, 0.1, 3, PrunedTree).
To export the tree as a list of predicate clauses:
::
| ?- c45_classifier::learn(play_tennis, Tree),
c45_classifier::tree_to_clauses(play_tennis, Tree, classify, Clauses).
To export the tree to a file:
::
| ?- c45_classifier::learn(play_tennis, Tree),
c45_classifier::tree_to_file(play_tennis, Tree, classify, 'tree.pl').
To print the tree to the current output:
::
| ?- c45_classifier::learn(play_tennis, Tree),
c45_classifier::print_tree(Tree).
To predict the class for a new instance (as a list of attribute-value pairs):
::
| ?- c45_classifier::learn(play_tennis, Tree),
c45_classifier::predict(Tree, [outlook-sunny, temperature-hot, humidity-high, wind-weak], Class).