({{, (RefLink[ClassifierFunction, paclet:ref/ClassifierFunction])[…] 
represents a function generated by (RefLink[Classify, paclet:ref/Classify]) that classifies data into classes.}})

OpenersNotesSection(Spacer1)Details and Options

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

(RefLink[ClassifierFunction, paclet:ref/ClassifierFunction])[…][data] attempts to classify data, returning the class in which data is considered most likely to be.

(RefLink[ClassifierFunction, paclet:ref/ClassifierFunction])[…][{(Subscript[data, 1]),(Subscript[data, 2]),…}] attempts to classify all the (Subscript[data, i]).

(RefLink[ClassifierFunction, paclet:ref/ClassifierFunction])[…][data,prop] gives the specified property of the classification associated with data.

Possible properties applicable to all methods include:

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

Possible options are:

A (RefLink[ClassifierFunction, paclet:ref/ClassifierFunction])[…] trained in an older version of the Wolfram Language will still work in the current version.

(RefLink[Classify, paclet:ref/Classify])[net] can be used to convert a (RefLink[NetChain, paclet:ref/NetChain]) or (RefLink[NetGraph, paclet:ref/NetGraph]) representing a classifier into a (RefLink[ClassifierFunction, paclet:ref/ClassifierFunction])[…].

(RefLink[Classify, paclet:ref/Classify])[ClassifierFunction[…],opts], can be used to update the values of (RefLink[PerformanceGoal, paclet:ref/PerformanceGoal]), (RefLink[ClassPriors, paclet:ref/ClassPriors]), (RefLink[IndeterminateThreshold, paclet:ref/IndeterminateThreshold]), (RefLink[UtilityFunction, paclet:ref/UtilityFunction]) or (RefLink[FeatureExtractor, paclet:ref/FeatureExtractor]) of the classifier.

In (RefLink[Classify, paclet:ref/Classify])[(RefLink[ClassifierFunction, paclet:ref/ClassifierFunction])[…],(RefLink[FeatureExtractor, paclet:ref/FeatureExtractor])->fe], the (RefLink[FeatureExtractorFunction, paclet:ref/FeatureExtractorFunction])[…] fe will be prepended to the existing feature extractor.

(RefLink[Information, paclet:ref/Information])[ClassifierFunction[…]] generates an information panel about the classifier and its estimated performances.

(RefLink[Information, paclet:ref/Information])[ClassifierFunction[…],prop] can be used to obtain specific properties.

(RefLink[Information, paclet:ref/Information]) of a (RefLink[ClassifierFunction, paclet:ref/ClassifierFunction]) may include the following properties:

(RefLink[Information, paclet:ref/Information]) properties also include all method suboptions.

OpenersPrimaryExamplesSection(Spacer1)Examples  (10)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

Create a (RefLink[ClassifierFunction, paclet:ref/ClassifierFunction]) with (RefLink[Classify, paclet:ref/Classify]) and a list of labeled examples:

c=Classify[{1->"A",2->"A",3->"B",4->"B",5->"C",6->"C"}]

Classify an unlabeled example with the (RefLink[ClassifierFunction, paclet:ref/ClassifierFunction]):

c[1.5]

Classify multiple examples:

c[{-3, 1.5,4.1,7, 10}]

Return the probabilities of the classes given the feature of an example:

c[2.55, "Probabilities"]

Return the sorted probabilities of the most likely classes:

c[2.55,"TopProbabilities"]

Return the probability of the most probable class:

c[2.55,{"TopProbabilities",1}]

Return the probability of a given class:

c[2.55,{"Probability","C"}]

Plot the probability of class "B" as a function of the feature:

Plot[c[x,"Probability"->"B"],{x,0,7},Exclusions->None]

Generate a (RefLink[ClassifierFunction, paclet:ref/ClassifierFunction]) using multiple features:

c=Classify[{
{1.5,"blue"}->"a",{3.2,"blue"}->"a",{4.1,"red"}->"b",{5.3,"red"}->"b",{10.,"green"}->"c",{12.4,"red"}->"c"}]

Use the function on a new example:

c[{10.1,"blue"}]

Classify an example that has missing features:

c[{10.1,Missing[]}]

Get the probabilities for the most probable classes:

c[{10.1,"blue"},"TopProbabilities"]

OpenersExampleSection1(Spacer1)Scope  (4)

Create a function classifying textual data:

c=Classify[{"the cat is grey"->,"my cat is cute"->,"this dog is scary"-> ,"the big dog"->}]

Classify new examples:

c[{"nice cat","what a dog"}]

Obtain information on the function:

Information[c]

Obtain the properties that can be used by this function:

Information[c, "FunctionProperties"]

Train a classifier function:

c=Classify[{1,2,3.5,4,5}->{"A","A","A","B","B"}]

Generate a classifier measurements object of the function applied to a test set:

cm=ClassifierMeasurements[c,{1.4->"A",5.3->"B",1.2->"B",4.6->"B",-3->"A"}]

Get the accuracy from the function on the test set:

cm["Accuracy"]

Visualize the confusion matrix:

cm["ConfusionMatrixPlot"]

Generate a classifier function whose input is an association:

c=Classify[{
<|"Age"->32,"Height"->160|>->"Female",
<|"Height"->183,"Age"->41|>->"Male",
<|"Height"->123|>->"Female",
<|"Height"->175,"Age"->21|>-> "Male",
<|"Age"->11|>->"Male",
<|"Age"->52,"Height"->164|>->"Female"}]

Use the function on an example:

c[<|"Age"->12,"Height"->120|>]

Classify examples containing missing features:

c[{<|"Height"->190|>, <|"Age"->90|>,<|"Height"->160|> , <||>}]

Train a classifier:

c=Classify[{1->"A",2->"A",3->"B",4->"B",5->"C",6->"C"}]

Store the (RefLink[ClassifierFunction, paclet:ref/ClassifierFunction])[…] into a file using the (StringTypeLink["WMLF", paclet:ref/format/WMLF]) format:

file=File[FileNameJoin[{$TemporaryDirectory,"classifier.wmlf"}]]

Export[file,c]

Load the classifier from the file using (RefLink[Import, paclet:ref/Import]):

c2=Import[file]

Use the loaded classifier on new data:

c2[1.6]

c2[1.6,"Probabilities"]

OpenersExampleSection2(Spacer1)Options  (4)

OpenersExampleSubsection0(Spacer1)ClassPriors  (1)

Train a classifier on an imbalanced dataset:

data={1->True,2->True,3->True,4->True,5->False,6->True};

c=Classify[data,Method->"LogisticRegression"]

The example 5->(RefLink[False, paclet:ref/False]) is classified as (RefLink[True, paclet:ref/True]):

c[5]

c[5,"Probabilities"]

Classify this example with a uniform prior over classes:

c[5,ClassPriors->\[LeftAssociation]False->0.5,True->0.5\[RightAssociation]]

c[5,"Probabilities",ClassPriors->\[LeftAssociation]False->0.5,True->0.5\[RightAssociation]]

The class priors of a classifier can also be updated after training:

c2=Classify[c, ClassPriors->\[LeftAssociation]False->0.5,True->0.5\[RightAssociation]]

c2[5, "Probabilities"]

OpenersExampleSubsection1(Spacer1)IndeterminateThreshold  (1)

Train a classifier:

data={1-> "B", 2-> "B", 3-> "A", 4-> "B", 5-> "A", 6-> "A"};

c=Classify[data]

Obtain class probabilities for an example:

c[3,"Probabilities"]

The most probable class is chosen as the prediction:

c[3]

No prediction is made if no class probabilities exceed a specified probability threshold:

c[5,IndeterminateThreshold-> 0.9]

Update the value of the threshold permanently:

c2=Classify[c,IndeterminateThreshold-> 0.9]

c2[5]

OpenersExampleSubsection2(Spacer1)TargetDevice  (1)

Train a classifier using a neural network:

trainingData=RandomReal[1,{2000,4}]->RandomChoice[{"a", "b", "c", "d"},2000];
classifier = Classify[trainingData, Method->"NeuralNetwork", TargetDevice->"GPU"]

Evaluate the resulting classifier on system's default GPU and look at its (RefLink[AbsoluteTiming, paclet:ref/AbsoluteTiming]):

n = 10000
AbsoluteTiming[classifier[RandomReal[1,{n,4}],TargetDevice->"GPU"];]

Compare the previous timing with the one achieved by using the default CPU computation:

AbsoluteTiming[classifier[RandomReal[1,{n,4}]];]

OpenersExampleSubsection3(Spacer1)UtilityFunction  (1)

Train a classifier:

trainingset={1,2,3,4}->{"yes","yes","no","no"};

c=Classify[trainingset]

By default, the most probable class is predicted:

c[2.55,"Probabilities"]

c[2.55]

Specify a utility function that penalizes examples of class "yes" being misclassified as "no":

c[2.55,UtilityFunction-><|"no"-> <|"no"->1,"yes"-> 0|>,"yes"-><|"no"-> -100, "yes"-> 1|> |>]

Update the value of the utility function permanently:

c2=Classify[c,UtilityFunction-><|"no"-> <|"no"->1,"yes"-> 0|>,"yes"-><|"no"-> -100, "yes"-> 1|> |>]

c[2.55]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[Classify, paclet:ref/Classify])  ▪  (RefLink[ClassifierMeasurements, paclet:ref/ClassifierMeasurements])  ▪  (RefLink[Information, paclet:ref/Information])  ▪  (RefLink[PredictorFunction, paclet:ref/PredictorFunction])  ▪  (RefLink[NearestFunction, paclet:ref/NearestFunction])  ▪  (RefLink[DimensionReducerFunction, paclet:ref/DimensionReducerFunction])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Machine Learning, paclet:guide/MachineLearning])}, {▪(RefLinkPlain[Scientific Models, paclet:guide/ScientificModels])}, {▪(RefLinkPlain[Nonparametric Statistical Distributions, paclet:guide/NonparametricStatisticalDistributions])}})}})

({{(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])}})}})