({{, (RefLink[SequencePredictorFunction, paclet:ref/SequencePredictorFunction])[…] 
represents a function generated by (RefLink[SequencePredict, paclet:ref/SequencePredict]) that predicts the next elements from a sequence.}})

OpenersNotesSection(Spacer1)Details and Options

(RefLink[SequencePredictorFunction, paclet:ref/SequencePredictorFunction]) works like (RefLink[Function, paclet:ref/Function]).

(RefLink[SequencePredictorFunction, paclet:ref/SequencePredictorFunction])[…][seq] attempts to predict the next element in the sequence seq.

(RefLink[SequencePredictorFunction, paclet:ref/SequencePredictorFunction])[…][{(Subscript[seq, 1]),(Subscript[seq, 2]),…}] attempts to predict all the (Subscript[seq, i]).

(RefLink[SequencePredictorFunction, paclet:ref/SequencePredictorFunction])[…][seq,prop] gives the specified property associated with seq.

In (RefLink[SequencePredictorFunction, paclet:ref/SequencePredictorFunction])[…][{},prop], {} is interpreted as an empty list of sequences rather than an empty sequence.

Sequence seq is assumed to be a subsequence of an unknown infinite sequence.

Possible properties include:

In (RefLink[SequencePredictorFunction, paclet:ref/SequencePredictorFunction])[…][…,"SequenceProbability"], some probability mass is kept for unknown elements.

(RefLink[SequencePredictorFunction, paclet:ref/SequencePredictorFunction])[…][data,…,opts] specifies that the sequence predictor should use the options opts when applied to data.

Possible options are:

Possible settings for (RefLink[PerformanceGoal, paclet:ref/PerformanceGoal]) include:

Possible settings for (RefLink[RandomSeeding, paclet:ref/RandomSeeding]) include:

OpenersPrimaryExamplesSection(Spacer1)Examples  (8)

OpenersExampleSection0(Spacer1)Basic Examples  (1)

Create a sequence predictor function with (RefLink[SequencePredict, paclet:ref/SequencePredict]) and a training set of subsequences:

sp=SequencePredict[{{,},{,,},{,,},{,}}]

Use the sequence predictor function to predict the next element:

sp[{,}]

Obtain the probabilities of the next element given the sequence:

sp[{,},"Probabilities"]

Obtain a random next element according to the preceding distribution:

sp[{,},"RandomNextElement"]

Obtain multiple predictions at a time:

sp[{{,}, {,}}]

Predict the most likely next element and reuse this intermediate guess to predict the following element:

sp[{,},"NextElement"-> 2]

Predict the most likely following sequence:

sp[{,},"NextSequence"-> 2]

Compare the probabilities for the preceding sequences:

sp[{{,,,},{,,,}},"SequenceProbability"]

OpenersExampleSection1(Spacer1)Scope  (4)

Train a sequence predictor on a list of strings:

sp=SequencePredict[{"this dog is scary","the cat is grey","my cat is fast","the big dog", "what a lovely cat","this is not a dog"}]

Predict the next character following a given string:

sp["my do"]

Predict the next four characters:

sp["my do","NextElement"-> 4]

Obtain the probabilities for each character to follow the given string:

sp["my do","Probabilities"]

Train a sequence predictor on the list of common English words, each word treated as a sequence of characters:

sp=SequencePredict[WordList[]]

Predict the most likely next character from a given sequence:

sp["mo"]

For the sequence predictor trained in the preceding example, each word is considered as a subsequence of an infinite sequence. Use the character | to mark boundaries between words:

markedWords="|"<>#<>"|"& /@WordList[];

Build a new sequence predictor aware of word boundaries:

sp=SequencePredict[markedWords]

Create an English-like word generator trimming once the boundary is found:

First[StringSplit[sp["|","RandomNextElement"->20],"|"]]

Load a book from (RefLink[ExampleData, paclet:ref/ExampleData]):

dq=ExampleData[{"Text","DonQuixoteISpanish"}];

Train a sequence predictor on this book at character level:

spchar=SequencePredict[{dq}]

Sample a random string in the book style:

spchar[{"thou "},"RandomNextElement"->20]

Train another sequence predictor, interpreting strings as word sequences rather than character sequences:

spword=SequencePredict[{dq},FeatureExtractor-> "SegmentedWords"]

Complete the preceding string with 10 consecutive words (spaces and punctuation marks are considered as words):

spword["I","RandomNextElement"->10]

OpenersExampleSection2(Spacer1)Options  (2)

OpenersExampleSubsection0(Spacer1)PerformanceGoal  (1)

Tune the computation time and precision when exploring the full sequence probability space:

sp=SequencePredict[{ExampleData[{"Text","LesFleursDuMal"}]}]

Favor fast and approximated exploration:

{shortertime,quickprediction}=AbsoluteTiming[sp["j'aime","NextSequence"->20,PerformanceGoal->"Speed"]]

Favor more in-depth exploration taking longer computation time:

{longertime,betterprediction}=AbsoluteTiming[sp["j'aime","NextSequence"->20,PerformanceGoal->"Quality"]]

Compare the results:

sp[#,"SequenceProbability"]& /@ {quickprediction, betterprediction}

OpenersExampleSubsection1(Spacer1)RandomSeeding  (1)

Create a sequence predictor function with (RefLink[SequencePredict, paclet:ref/SequencePredict]) and a training set of subsequences:

sp=SequencePredict[{{,},{,,},{,,},{,}}]

Obtain the probabilities of the next element given the sequence:

sp[{,},"Probabilities"]

When obtaining random next elements, they will be sampled from the preceding distribution:

res = Table[sp[{,},"RandomNextElement"], 100];

N@Counts[res]/Total[Counts[res]]

The (RefLink[RandomSeeding, paclet:ref/RandomSeeding]) option prevents randomness from happening:

res2 = Table[sp[{,},"RandomNextElement", RandomSeeding->1], 100];

N@Counts[res2]/Total[Counts[res2]]

OpenersExampleSection3(Spacer1)Possible Issues  (1)

An empty list is parsed as the list with no sequences inside and will return an empty list:

SequencePredict[{{1,2,3},{2,3,1},{3,1,2},{1,2,3}},{}]

To obtain the most likely next element completing an empty sequence, nest it in a second list for disambiguation:

First@SequencePredict[{{1,2,3},{2,3,1},{3,1,2},{1,2,3}},{{}}]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[PredictorFunction, paclet:ref/PredictorFunction])  ▪  (RefLink[SequencePredict, paclet:ref/SequencePredict])  ▪  (RefLink[TimeSeriesModel, paclet:ref/TimeSeriesModel])  ▪  (RefLink[TimeSeriesForecast, paclet:ref/TimeSeriesForecast])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Machine Learning, paclet:guide/MachineLearning])}})}})