SMILearn Tutorial 4: Learning Naive Bayes
From DSL
This tutorial shows how to use a learning procedure on the example of learning naive Bayes model. We assume that the input of a learning algorithm is only a text file "naive.txt" that has the following content:
c x y State1 State1 State0 State1 State1 State0 State0 State1 State1 State0 State0 State0 State0 State0 State1 State0 State0 State1 State0 State1 State0 State0 State0 State1 State0 State1 State1 State0 State1 State1
The first step is to create a text parser and obtain a DSL_dataset object:
DSL_textParser parser;
parser.SetUseHeader(true);
if (parser.Parse("naive.txt")!=DSL_OKAY)
cout << "Parsing failed!" << endl;
DSL_dataset d = parser.GetDataset();
Having a data set ready, we initialize an object of DSL_naiveBayes class and perform learning. In case of the naive Bayes, it is crucial to indicate id of variable that is the class variable. In this example, the variable with id "c" will be the class variable.
DSL_network result;
DSL_naiveBayes naive;
naive.classVariableId = "c";
if (naive.Learn(d,result)!=DSL_OKAY)
{
cout << "Learning failed." << endl;
return;
}
The result of learning procedure is stored in a DSL_network object. To see the learned network, we can save the network:
result.WriteFile("naivebayes.xdsl");
Note that all nodes in such network will be placed at the same position (the top right corner), which can make an impression that there is only one node in the network.
