Method for (RefLink[Classify, paclet:ref/Classify]).

Model class probabilities using the n-gram frequencies of the given sequence.

In a Markov model, at training time, an n-gram language model is computed for each class. At test time, the probability for each class is computed according to Bayes's theorem, P(class|sequence)∝P(class) P(sequence|class), where  P(sequence|class) is given by the language model of the given class and P(class) is class prior.

The following options can be given:

When "Order"->n, the method partitions sequences in (n+1)-grams.

When "Order"->0, the method uses unigrams (single tokens). The model can then be called a unigram model or naive Bayes model.

The value of "AdditiveSmoothing" is added to all n-gram counts. It is used to regularize the language model.

OpenersPrimaryExamplesSection(Spacer1)Examples  (5)

OpenersExampleSection0(Spacer1)Basic Examples  (1)

Train a classifier function on labeled examples:

c=Classify[{{"flour","butter"},{"pasta", "tomato"}, {"apple", "ice cream"},{"salt", "meat"},{"honey","sugar", "butter"}}->{"dessert","main course","dessert","main course","dessert"},Method->"Markov"]

Obtain information about the classifier:

Information[c]

Classify a new example:

c[{"tomato", "meat", "salt"}]

OpenersExampleSection1(Spacer1)Options  (4)

OpenersExampleSubsection0(Spacer1)"AdditiveSmoothing"  (2)

Train a classifier using the "AdditiveSmoothing" suboption:

Classify[{{, "this is dark red"}->Red, {, "this is blue"}->Blue,{, "this is dark blue"}->Blue,{, "this red is almost orange"}->Red}, Method->{"Markov", "AdditiveSmoothing"->2}]

Train two classifiers on an imbalanced dataset by varying the value of "AdditiveSmoothing":

data={"aaabb"->True,"cdfff"->True,"cdffvvv"->True,"aaa"->True,"vvvtul"->False,"dqedewf"->True};

c1=Classify[data, Method->{"Markov","AdditiveSmoothing"->.1}];
c2 = Classify[data, Method->{"Markov","AdditiveSmoothing"->7}];

Look at the corresponding probabilities for the imbalanced element:

c1["vvvtul","Probabilities"]

c2["vvvtul","Probabilities"]

OpenersExampleSubsection1(Spacer1)"Order"  (2)

Train a classifier by specifying the "Order":

Classify[{{, "this is dark red"}->Red, {, "this is blue"}->Blue,{, "this is dark blue"}->Blue,{, "this red is almost orange"}->Red}, Method->{"Markov", "Order"->2}]

Generate a dataset of real words and random strings:

alphabet=Alphabet[];
randomstrings=RandomChoice[alphabet, RandomInteger[{2, 5}]];
realwords=DictionaryLookup["a"~~___];
trainingset=<|"RealWord"->RandomSample[realwords, 200], 
"RandomString"-> Table[StringJoin@@randomstrings, 200]|>;

Generate classifiers using different values for the "Order":

c0=Classify[trainingset, Method->{"Markov" ,"Order"->0}]

c2=Classify[trainingset, Method->{"Markov","Order"->2}]

Compare the probabilities of these classifiers on a new real word:

SeedRandom[4]
notRandom=RandomSample[DictionaryLookup["a"~~___], 1];
Dataset@<|"Classifier1"->c0[notRandom, "Probabilities"],"Classifier2"-> c2[notRandom, "Probabilities"]|>

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[Classify, paclet:ref/Classify])  ▪  (RefLink[ClassifierFunction, paclet:ref/ClassifierFunction])  ▪  (RefLink[ClassifierMeasurements, paclet:ref/ClassifierMeasurements])  ▪  (RefLink[Predict, paclet:ref/Predict])  ▪  (RefLink[SequencePredict, paclet:ref/SequencePredict])  ▪  (RefLink[ClusterClassify, paclet:ref/ClusterClassify])  ▪    ▪    ▪    ▪    ▪    ▪  }, {Related Machine Learning Methods:  (RefLink[DecisionTree, paclet:ref/method/DecisionTree])  ▪  (RefLink[LogisticRegression, paclet:ref/method/LogisticRegression])  ▪  (RefLink[NearestNeighbors, paclet:ref/method/NearestNeighbors])  ▪  (RefLink[NeuralNetwork, paclet:ref/method/NeuralNetwork])  ▪  (RefLink[RandomForest, paclet:ref/method/RandomForest])  ▪  (RefLink[SupportVectorMachine, paclet:ref/method/SupportVectorMachine])}})}})

({{(Spacer1), ({{(Spacer1)Related Links}, {(WebLink[DocumentationFooterIconsRelatedLinks-EIWL.png(Spacer1)An Elementary Introduction to the Wolfram Language, https://www.wolfram.com/language/elementary-introduction/22-machine-learning.html])(WebLink[: Machine Learning, https://www.wolfram.com/language/elementary-introduction/22-machine-learning.html])}})}})