({{, (RefLink[Classify, paclet:ref/Classify])[{(Subscript[example, 1])->(Subscript[class, 1]),(Subscript[example, 2])->(Subscript[class, 2]),…}] 
generates a (RefLink[ClassifierFunction, paclet:ref/ClassifierFunction])[…] based on the examples and classes given.}, {, (RefLink[Classify, paclet:ref/Classify])[{(Subscript[example, 1]),(Subscript[example, 2]),…}->{(Subscript[class, 1]),(Subscript[class, 2]),…}]
also generates a (RefLink[ClassifierFunction, paclet:ref/ClassifierFunction])[…] based on the examples and classes given.}, {, (RefLink[Classify, paclet:ref/Classify])[\[LeftAssociation](Subscript[class, 1])->{(Subscript[example, 11]),(Subscript[example, 12]),…},(Subscript[class, 2])->{(Subscript[example, 21]),…},…\[RightAssociation]]
generates a (RefLink[ClassifierFunction, paclet:ref/ClassifierFunction])[…] based on an association of classes with their examples.}, {, (RefLink[Classify, paclet:ref/Classify])[training,data]
attempts to classify data using a classifier function deduced from the training set given.}, {, (RefLink[Classify, paclet:ref/Classify])["name",data]
attempts to classify data using the built-in classifier function represented by "name".}, {, (RefLink[Classify, paclet:ref/Classify])[…,data,prop]
gives the specified property of the classification associated with data.}, {, (RefLink[Classify, paclet:ref/Classify])[classifier,opts]
takes an existing classifier function and modifies it with the new options given.}})

OpenersNotesSection(Spacer1)Details and Options

(RefLink[Classify, paclet:ref/Classify]) can be used on many types of data, including numerical, textual, sounds, and images, as well as combinations of these.

Each (Subscript[example, i]) can be a single data element, a list of data elements, an association of data elements, or a (RefLink[Dataset, paclet:ref/Dataset]) object. In (RefLink[Classify, paclet:ref/Classify])[training,…], training can be a (RefLink[Dataset, paclet:ref/Dataset]) object.

(RefLink[Classify, paclet:ref/Classify])[training] returns a (RefLink[ClassifierFunction, paclet:ref/ClassifierFunction])[…] that can then be applied to specific data.

In (RefLink[Classify, paclet:ref/Classify])[…,data], data can be a single item or a list of items.

In (RefLink[Classify, paclet:ref/Classify])[…,data,prop], properties are as given in (RefLink[ClassifierFunction, paclet:ref/ClassifierFunction])[…]; they include:

Examples of built-in classifier functions include:

The following options can be given:

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

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

The following settings for (RefLink[TrainingProgressReporting, paclet:ref/TrainingProgressReporting]) can be used:

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

(RefLink[Classify, paclet:ref/Classify])[{(Subscript[assoc, 1]),(Subscript[assoc, 2]),…}->"key",…] can be used to specify that the class is given by the value of "key" in each association (Subscript[assoc, i]).

(RefLink[Classify, paclet:ref/Classify])[{(Subscript[list, 1]),(Subscript[list, 2]),…}->n,…] can be used to specify that the class is given by the value of part n in each list (Subscript[list, i]).

(RefLink[Classify, paclet:ref/Classify])[(RefLink[Dataset, paclet:ref/Dataset])[…]->part,…] can be used to specify that classes are given by the value of part of each row of the dataset.

(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])[…,(RefLink[FeatureExtractor, paclet:ref/FeatureExtractor])->"Minimal"] indicates that the internal preprocessing should be as simple as possible.

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]) can be used on the (RefLink[ClassifierFunction, paclet:ref/ClassifierFunction])[…] obtained.

OpenersPrimaryExamplesSection(Spacer1)Examples  (47)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

Train a classifier function on labeled examples:

trainingset={1->"A",2->"A",3.5->"B",4->"B"};

c=Classify[trainingset]

Use the classifier function to classify a new unlabeled example:

c[2.6]

Obtain classification probabilities for this example:

c[2.6,"Probabilities"]

Classify multiple examples:

c[{2.6,5.1,-7.2}]

Plot the probability that the class of an example is "A" as a function of the feature:

Plot[c[x,"Probability"->"A"],{x,0,5}]

The training and the classification can be performed in one step:

Classify[trainingset,2.6]

Classify[trainingset,2.6,"Probabilities"]

Train a classifier with 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"}]

Classify 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  (16)

OpenersExampleSubsection0(Spacer1)Custom Classifiers  (8)

Train a classifier on textual data:

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

Classify new examples:

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

Train a classifier on image examples, gathered by their class:

c=Classify[<|Red-> {,,,,},Green->{,,,,}, Blue-> {,,,,}|>]

Classify new examples:

c[{,,}]

Obtain probabilities of a given class for new examples:

c[{,,},{"Probability",Blue}]

Train a classifier on data where the feature is a sequence of tokens:

c=Classify[{{"butter","sugar","flour"}->"bad",{"flour","butter"}->"good",{"tomato","salt"}->"good"}]

Classify a new example:

c[{"butter","tomato","apple"}]

Train a classifier on a dataset with features and classes in separate lists:

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

Obtain information about the classifier with (RefLink[Information, paclet:ref/Information]):

Information[c]

Generate a (RefLink[ClassifierMeasurements, paclet:ref/ClassifierMeasurements])[…] object of the classifier 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 classifier measurements object:

cm["Accuracy"]

Visualize the confusion matrix:

cm["ConfusionMatrixPlot"]

Train a classifier on a dataset with missing features:

c=Classify[{{2.3,"male"}->"A",{4.8,Missing[]}->"B",{Missing[],"female"}->"B",{5.2,"female"}->"C",{Missing[],"male"}->"B",{1.3,"male"}->"A"}]

Classify a new example:

c[{1.2,"male"}]

Classify examples containing missing features:

c[{{1.2,Missing[]}, {Missing[],"female"}}]

Train a classifier on a dataset with named features. The order of the keys does not matter. Keys can be missing:

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"}]

Classify a new example:

c[<|"age"->12,"height"->120|>]

Classify examples containing missing features:

c[{<|"height"->190|>, <|"age"->90|>,<|"height"->160|> , <||>}]

Construct a (RefLink[Dataset, paclet:ref/Dataset]) with a list of associations:

d=Dataset[{<|"age"->32,"height"->160, "gender"-> "female"|>,
<|"height"->183,"age"->41,"gender"-> "female"|>,
<|"height"->123,"age"->30,"gender"-> "female"|>,
<|"height"->175,"age"->21,"gender"-> "male"|>,
<|"height"->150,"age"->11,"gender"-> "male"|>,
<|"age"->52,"height"->164,"gender"-> "female"|>}]

Train a classifier to predict the feature "gender" as function of the other features:

c=Classify[d->"gender"]

Once the classifier is trained, any input format can be used. Classify an example formatted as an association:

c[<|"height"->120,"age"->12|>]

Find out the order of the features, and classify an example formatted as a list:

Information[c, FeatureNames]

c[{12,120}]

Classify examples in a (RefLink[Dataset, paclet:ref/Dataset]):

dnew = Dataset[{<|"height"->120,"age"->12|>, <|"height"->150,"age"->10|>,<|"height"->80,"age"->8|>}]

c[dnew]

Create an artificial dataset from three normally distributed clusters:

sampledata[center_]:=RandomVariate[MultinormalDistribution[center,IdentityMatrix[2]],200];

clusters=sampledata/@ {{1.5,1}, {-1.5,1},{0,-3}};

ListPlot[clusters, PlotStyle->Darker@{Yellow,Blue,Green}]

Train a classifier on this dataset:

c=Classify[<|Yellow->clusters[[1]],Blue->clusters[[2]],Green->clusters[[3]]|>]

Plot the training set and the probability distribution of each class as a function of the features:

Show[
Plot3D[{
c[{x,y},"Probability"->Yellow],c[{x,y},"Probability"->Blue],c[{x,y},"Probability"-> Green]
},{x,-4,4},{y,-5,4}, Exclusions->None],ListPointPlot3D[Map[Append[#,1]&,clusters,{2}], PlotStyle->{Yellow,Blue,Green}]]

OpenersExampleSubsection1(Spacer1)Built-in Classifiers  (8)

Use the "Language" built-in classifier to detect the language in which a text is written:

Classify["Language","the house is blue"]

Use it to detect the language of examples:

Classify["Language", {"the house is blue", "la maison est bleue", "la casa es azul", "das Haus ist blau", "房子是蓝色的", "المنزل باللون الأزرق", "будинок синій"}]

Obtain the probabilities for the most likely languages:

Classify["Language", "the house is blue, la maison est bleue", "TopProbabilities"]

Restrict the classifier to some languages with the option (RefLink[ClassPriors, paclet:ref/ClassPriors]):

Classify["Language",{"What is this language?","¿Qué idioma es ese?","Quelle est cette langue?"},ClassPriors-><|(Entity["English", Entity["Language","English"], "Entity[\"Language\", \"English\"]", "language"])->0.5,(Entity["Spanish", Entity["Language","Spanish"], "Entity[\"Language\", \"Spanish\"]", "language"])->0.5|>]

Use the "FacebookTopic" built-in classifier to detect the topic of a Facebook post:

Classify["FacebookTopic","I love eating carrots in the morning"]

Classify multiple examples:

Classify["FacebookTopic", {"I bought a new computer", "happy birthday!", "this skirt looks nice"}]

Unrecognized topics or languages will return (RefLink[Indeterminate, paclet:ref/Indeterminate]):

Classify["FacebookTopic", {"what is this topic?","qwe niowqe mwoei!"}]

Use the "CountryFlag" built-in classifier to recognize a country from its flag:

Classify["CountryFlag",{,,,,}]

Use the "NameGender" built-in classifier to get the probable sex of a person from their first name:

Classify["NameGender",{"Tom","Stacy", "John", "Natalie"}]

Use the "NotablePerson" built-in classifier to determine what notable person is depicted in the given image:

Classify["NotablePerson",]

Use the "Sentiment" built-in classifier to infer the sentiment of a social-media message:

Classify["Sentiment",{"I love this movie", "I am so sad", "My phone broke again"}]

Use the "Profanity" built-in classifier to return (RefLink[True, paclet:ref/True]) if a text contains strong language:

Classify["Profanity",Import["http://www.urbandictionary.com/random.php"]]

Use the "Spam" built-in classifier to detect if an email is spam from its content:

Classify["Spam", "Dear recipient,*** Technologies announces the beginning of a new unprecendented global employment campaign.reviser yeller winers butchery twenties
Due to company's exploding growth *** is expanding business to the European region.During last employment campaign over 1500 people worldwide took part in ***'s business
and more than half of them are currently employed by the company.And now we are offering you
one more opportunity to earn extra money working with *** Technologies.druggists blame classy gentry Aladdin

We are looking for honest,responsible,hard-working people that can dedicate 2-4 hours of their
time per day and earn extra Â$300-500 weekly.All offered positions are currently part-time
and give you a chance to work mainly from home.lovelies hockey Malton meager reordered

Please visit ***'s corporate web site (http://www.***.com/sta/home/0077.htm) for more details regarding these vacancies."]

OpenersExampleSection2(Spacer1)Options  (19)

OpenersExampleSubsection2(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 training 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 instead of the imbalanced training prior:

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 can be specified during the training:

c2=Classify[data,Method->"LogisticRegression", ClassPriors->\[LeftAssociation]False->0.5,True->0.5\[RightAssociation]]

c2[5]

c2[5,"Probabilities"]

c2[5,ClassPriors->\[LeftAssociation]False->0.2,True->0.8\[RightAssociation]]

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

c3=Classify[c2, ClassPriors->\[LeftAssociation]False->1/6,True->5/6\[RightAssociation]]

c2[5, "Probabilities"]

c3[5, "Probabilities"]

OpenersExampleSubsection3(Spacer1)FeatureExtractor  (3)

Train a (RefLink[FeatureExtractorFunction, paclet:ref/FeatureExtractorFunction]) on a simple dataset:

dataset={{1.4,"A"},{1.5,"A"},{2.3,"B"},{5.4,"B"}};

fe=FeatureExtraction[dataset]

Use the feature extractor function as a preprocessing step in (RefLink[Classify, paclet:ref/Classify]):

Classify[dataset->{"Yes","No","No","No"},FeatureExtractor->fe]

Train a classifier on texts preprocessed by custom functions and an extractor method:

c=Classify[{"The cat is grey."->,"My cat is fast."->,"This dog is scary."-> ,"The big dog."->},FeatureExtractor->{ToUpperCase, RemoveDiacritics,"SegmentedWords"}]

c[{"Nice CAT","What a dög"}]

Create a feature extractor and extract features from a dataset of texts:

{features,fe}=FeatureExtraction[{"The cat is grey.","My cat is fast.","This dog is scary.","The big dog."},{ToUpperCase, RemoveDiacritics,"SegmentedWords"},{"ExtractedFeatures","ExtractorFunction"}]

Train a classifier on the extracted features:

c=Classify[features->{,,,}]

Join the feature extractor to the classifier:

c2=Classify[c,FeatureExtractor->fe]

The classifier can now be used on the initial input type:

c2["Nice CAT"]

OpenersExampleSubsection4(Spacer1)FeatureNames  (2)

Train a classifier and give a name to each feature:

c=Classify[{{2.3,"male"}->"a",{4.8,Missing[]}->"b",{Missing[],"female"}->"a",{5.2,"female"}->"b"},FeatureNames->{"age","gender"}]

Use the association format to predict a new example:

c[<|"age"->3.3,"gender"->"male"|>]

The list format can still be used:

c[{3.3,"male"}]

Train a classifier on a training set with named features and use (RefLink[FeatureNames, paclet:ref/FeatureNames]) to set their order:

c=Classify[{<|"age"->2.3,"gender"->"male"|>->"a",<|"age"->4.6|>->"b",<|"gender"->"female"|>->"a",<|"gender"->"female","age"->5.2|>->"b"}, FeatureNames->{"gender","age"}]

Features are ordered as specified:

ClassifierInformation[c, FeatureNames]

Classify a new example from a list:

c[{"female", 6.5}]

OpenersExampleSubsection5(Spacer1)FeatureTypes  (2)

Train a classifier on data where the feature is intended to be a sequence of tokens:

c=Classify[{{"butter","sugar"}->"bad",{"flour","butter"}->"good",{"tomato","salt"}->"good"}]

(RefLink[Classify, paclet:ref/Classify]) wrongly assumed that examples contained two different text features:

Information[c,FeatureTypes]

The following classification will output an error message:

c[{"butter","tomato","apple"}]

Force (RefLink[Classify, paclet:ref/Classify]) to interpret the feature as a "NominalSequence":

c2=Classify[{{"butter","sugar"}->"bad",{"flour","butter"}->"good",{"tomato","salt"}->"good"}, FeatureTypes->"NominalSequence"]

Information[c2,FeatureTypes]

Classify a new example:

c2[{"butter","tomato","apple"}]

Train a classifier with named features:

trainingset={
<|"age"->32,"gender"->1|>->"tall",
<|"age"->41,"gender"->2|>->"short",
<|"age"->17,"gender"->2|>->"short",
<|"age"->11,"gender"->1|>->"tall"};

c=Classify[trainingset]

Both features have been considered numerical:

Information[c, FeatureTypes]

Specify that the feature "gender" should be considered nominal:

c=Classify[trainingset,FeatureTypes-><|"gender"->"Nominal"|>]

Information[c, FeatureTypes]

OpenersExampleSubsection6(Spacer1)IndeterminateThreshold  (1)

Specify a probability threshold when training the classifier:

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

c=Classify[data,IndeterminateThreshold->0.9]

Obtain class probabilities for an example:

c[5,"Probabilities"]

As there are no class probabilities above 0.9, no prediction is made:

c[5]

Specifying a threshold when classifying supersedes the trained threshold:

c[5,IndeterminateThreshold-> 0.5]

Update the value of the threshold in the classifier:

c2=Classify[c,IndeterminateThreshold-> 0.5]

c2[5]

OpenersExampleSubsection7(Spacer1)Method  (3)

Train a logistic classifier:

trainingset={1,2,3,4,5,6,7}->{"a","a", "b", "a", "b","b","b"};

logistic=Classify[trainingset,Method->"LogisticRegression"]

Train a random forest classifier:

rf=Classify[trainingset,Method->"RandomForest"]

Plot the probability of class "a" given the feature for both classifiers:

Plot[{
logistic[x,"Probability"->"a"],
rf[x,"Probability"->"a"]
}, {x,0,8}, Exclusions->None]

Train a nearest neighbors classifier:

trainingset=ExampleData[{"MachineLearning", "UCILetter"},"TrainingData"];

c1=Classify[trainingset,Method->"NearestNeighbors"]

Find the classification accuracy on a test set:

testset=ExampleData[{"MachineLearning", "UCILetter"},"TestData"];

ClassifierMeasurements[c1,testset,"Accuracy"]

In this example, using a naive Bayes classifier reduces the classification accuracy:

c2=Classify[trainingset,Method->"NaiveBayes"]

ClassifierMeasurements[c2,testset,"Accuracy"]

However, using a naive Bayes classifier reduces the classification time:

First[AbsoluteTiming[c1[testset[[All,1]]]]]

First[AbsoluteTiming[c2[testset[[All,1]]]]]

MONK's problems consist of synthetic binary classification datasets used for comparing the performance of different classifiers. Generate the dataset for the second MONK problem:

data=Map[#->Count[#,1]==1&,Tuples[{{1,2,3},{1,2,3},{1,2},{1,2,3},{1,2,3,4},{1,2}}]];

Test the accuracy of each available classifier by training on 169 examples and testing on the entire dataset:

trainingset=RandomSample[data,169];

AssociationMap[ ClassifierMeasurements[
Classify[trainingset,Method->#],data, "Accuracy"]&,{"RandomForest", "NaiveBayes", "SupportVectorMachine", "NearestNeighbors","LogisticRegression"}]

OpenersExampleSubsection8(Spacer1)PerformanceGoal  (1)

Train a classifier with an emphasis on training speed:

trainingset=ExampleData[{"MachineLearning","Satellite"},"TrainingData"];

c1=Classify[trainingset,PerformanceGoal->"TrainingSpeed"]

Information[c1, "TrainingTime"]

Compute the classification accuracy on a test set:

testset=ExampleData[{"MachineLearning","Satellite"},"TestData"];

ClassifierMeasurements[c1,testset,"Accuracy"]

By default, a compromise between classification speed and performance is sought:

c2=Classify[trainingset]

ClassifierInformation[c2, "TrainingTime"]

ClassifierMeasurements[c2,testset,"Accuracy"]

With the same data, train a classifier with an emphasis on training speed and memory:

c3=Classify[trainingset,PerformanceGoal->{"TrainingSpeed", "Memory"}]

The classifier uses less memory, but is also less accurate:

ByteCount /@ {c2,c3}

ClassifierMeasurements[c3,testset,"Accuracy"]

OpenersExampleSubsection9(Spacer1)TargetDevice  (1)

Train a classifier on the system's default GPU using a neural network and look at the (RefLink[AbsoluteTiming, paclet:ref/AbsoluteTiming]):

n=10000;
trainingData=RandomReal[1,{n,4}]->RandomChoice[{1,2,3},n];
AbsoluteTiming[classifier = Classify[trainingData, Method->"NeuralNetwork", TargetDevice->"GPU"]]

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

AbsoluteTiming[classifier = Classify[trainingData, Method->"NeuralNetwork"]]

OpenersExampleSubsection10(Spacer1)TimeGoal  (2)

Train a classifier while specifying a total training time of 5 seconds:

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

Information[c,"TrainingTime"]

Load the "Mushroom" dataset:

dataset=ExampleData[{"MachineLearning","Mushroom"},"Data"];

Train a classifier while specifying a target training time of 0.1 seconds:

c=Classify[dataset,TimeGoal->.1]

The classifier reached an accuracy of about 90%:

Information[c]

Train a classifier while specifying a target training time of 5 seconds:

c=Classify[dataset,TimeGoal->5]

The classifier reached an accuracy of about 99%:

Information[c]

OpenersExampleSubsection11(Spacer1)TrainingProgreessReporting  (1)

Load the "UCILetter" dataset:

dataset=ExampleData[{"MachineLearning","UCILetter"},"Data"];

Show training progress interactively during training of a classifier:

Classify[dataset,TrainingProgressReporting->"Panel"];

Show training progress interactively without plots:

Classify[dataset,TrainingProgressReporting->"SimplePanel"];

Print training progress periodically during training:

Classify[dataset,TrainingProgressReporting->"Print"];

Show a simple progress indicator:

Classify[dataset,TrainingProgressReporting->"ProgressIndicator"];

Do not report progress:

Classify[dataset,TrainingProgressReporting->None];

OpenersExampleSubsection12(Spacer1)UtilityFunction  (1)

Train a classifier:

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

c1=Classify[trainingset]

By default, the most probable class is predicted:

c1[2.55,"Probabilities"]

c1[2.55]

This corresponds to the following utility specification:

Information[c1,UtilityFunction]

Train a classifier that penalizes examples of class "yes" being misclassified as "no":

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

The classifier decision is different despite the probabilities being unchanged:

c2[2.55,"Probabilities"]

c2[2.55]

Specifying a utility function when classifying supersedes the utility function specified at training:

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

Update the value of the utility function in the classifier:

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

c3[2.55]

OpenersExampleSubsection13(Spacer1)ValidationSet  (1)

Train a logistic regression classifier on the Fisher iris data:

trainingset=ExampleData[{"MachineLearning", "FisherIris"},"TrainingData"];

c1=Classify[trainingset,Method-> "LogisticRegression"]

Obtain the L2 regularization coefficient of the trained classifier:

ClassifierInformation[c1,"L2Regularization"]

Specify a validation set:

validationset=ExampleData[{"MachineLearning", "FisherIris"},"TestData"][[;;10]];

c2=Classify[trainingset,ValidationSet->validationset,Method-> "LogisticRegression"]

A different L2 regularization coefficient has been selected:

ClassifierInformation[c2,"L2Regularization"]

OpenersExampleSection3(Spacer1)Applications  (7)

Train a digit recognizer on 100 examples from the MNIST database of handwritten digits:

digit=Classify[
{->2,->5,->8,->0,->2,->7,->5,->1,->3,->0,->3,->9,->6,->2,->8,->2,->0,->6,->6,->1,->1,->7,->8,->5,->0,->4,->7,->6,->0,->2,->5,->3,->1,->5,->6,->7,->5,->4,->1,->9,->3,->6,->8,->0,->9,->3,->0,->3,->7,->4,->4,->3,->8,->0,->4,->1,->3,->7,->6,->4,->7,->2,->7,->2,->5,->2,->0,->9,->8,->9,->8,->1,->6,->4,->8,->5,->8,->0,->6,->7,->4,->5,->8,->4,->3,->1,->5,->1,->9,->9,->9,->2,->4,->7,->3,->1,->9,->2,->9,->6}]

Use the classifier to recognize unseen digits:

digit[{,,,,,,,,,}]

Analyze probabilities of a misclassified example:

digit[, "TopProbabilities"]

Train a classifier on 32 images of legendary creatures:

legendary=Classify[<|
"Griffin"->{,,,,,,,},
"Centaur"->{,,,,,,,},
"Dragon"->{,,,,,,,},
"Unicorn"->{,,,,,,,}|>]

Use the classifier to recognize unseen creatures:

legendary[{,,,}]

Train a classifier to recognize daytime from nighttime:

daynight=Classify[
{->"Night",->"Day",->"Night",->"Night",->"Day",->"Night",->"Day",->"Day",->"Night",->"Night",->"Day",->"Night",->"Night",->"Day",->"Night",->"Night",->"Day",->"Day",->"Day",->"Day",->"Night",->"Night",->"Day",->"Night",->"Night",->"Day",->"Day",->"Day",->"Night",->"Day"}]

Test it on examples:

daynight[{,,,,,}]

Train a classifier on the Fisher iris dataset to predict the species of Iris:

c=Classify[ExampleData[{"MachineLearning","FisherIris"},"TrainingData"]]

Predict the species of Iris from a list of features:

c[{4.3,3.1,1.2,0.3}]

Test the accuracy of the classifier on a test set:

cm=ClassifierMeasurements[c,ExampleData[{"MachineLearning","FisherIris"},"TestData"]]

cm["Accuracy"]

Generate a confusion matrix of the classifier on this test set:

cm["ConfusionMatrixPlot"]

Load the "Titanic" dataset, which contains a list of Titanic passengers with their age, sex, ticket class, and survival:

dataset=ExampleData[{"MachineLearning","Titanic"},"Data"];

Visualize a sample of the dataset:

RandomSample[dataset, 10] // TableForm

Train a logistic classifier on this dataset:

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

Calculate the survival probability of a 10-year-old girl traveling in third class:

c[{"3rd", 10, "female"},"Probability"->"survived"]

Plot the survival probability as a function of age for some combinations of "class" and "sex":

p[class_,age_,sex_]:=c[{class,age,sex},{"Probability","survived"}];

Plot[{p["1st",x,"female"],p["3rd",x,"female"],p["1st",x,"male"],p["3rd",x,"male"]}, {x,0,100},PlotLegends->{"female, 1st class", "female, 3rd class", "male, 1st class", "male, 3rd class"}, Frame->True,FrameLabel-> {"Age (years)", "Survival probability"}, Exclusions->None]

Train a classifier that classifies movie review snippets as "positive" or "negative":

c=Classify[ExampleData[{"MachineLearning","MovieReview"},"TrainingData"]]

Classify an unseen movie review snippet:

c["the gorgeously elaborate continuation of \" the lord of the rings \" trilogy is so huge that a column of words cannot adequately describe co-writer/director peter jackson's expanded vision of j . r . r . tolkien's middle-earth . "]

Test the accuracy of the classifier on a test set:

ClassifierMeasurements[c,ExampleData[{"MachineLearning","MovieReview"},"TestData"],"Accuracy"]

Import examples of the writings of Shakespeare, Oscar Wilde, and Victor Hugo to train a classifier:

Othello = Import["http://www.gutenberg.org/cache/epub/2267/pg2267.txt"];
Hamlet= Import["http://www.gutenberg.org/cache/epub/2265/pg2265.txt"];
Macbeth = Import["http://www.gutenberg.org/cache/epub/2264/pg2264.txt"];

TheImportanceOfBeingEarnest = Import["http://www.gutenberg.org/cache/epub/844/pg844.txt"];
ThePictureofDorianGray = Import["http://www.gutenberg.org/cache/epub/174/pg174.txt"];
AnIdealHusband = Import["http://www.gutenberg.org/files/885/885-0.txt"];

LesMiserables = Import["http://www.gutenberg.org/cache/epub/135/pg135.txt"];
NotreDamedeParis = Import["http://www.gutenberg.org/cache/epub/2610/pg2610.txt"];
TheManWhoLaughs=Import["http://www.gutenberg.org/cache/epub/12587/pg12587.txt"];

Generate an author classifier from these texts:

author = Classify[<|"William Shakespeare"-> {Othello, Hamlet},"Oscar Wilde"-> {TheImportanceOfBeingEarnest, ThePictureofDorianGray}, "Victor Hugo"-> {LesMiserables, NotreDamedeParis}|>]

Find which author new texts are from:

author[{Macbeth,AnIdealHusband,TheManWhoLaughs}]

OpenersExampleSection4(Spacer1)Possible Issues  (1)

The (RefLink[RandomSeeding, paclet:ref/RandomSeeding]) option does not always guarantee reproducibility of the result:

Train several classifiers on the "Titanic" dataset:

dataset=ExampleData[{"MachineLearning","Titanic"},"TrainingData"];

classifiers = Table[Classify[dataset], 4];

Compare the results when tested on a test set:

testset=ExampleData[{"MachineLearning","Titanic"},"TestData"];

SameQ@@(#[testset[[All, 1]]]&/@classifiers)

OpenersExampleSection5(Spacer1)Neat Examples  (2)

Define and plot clusters sampled from normal distributions:

gaussian[μ_,σ_,n_]:=RandomVariate[MultinormalDistribution[μ,{{σ,0},{0,σ}}],n];
positions={{4,2},{-2,2},{0,-3},{3,0}};
sizes={2,1,5,0.5};
colors={, , ,};
nums={100,100,50,20};

clusters=MapThread[gaussian,{positions,sizes,nums}];
clusters=Table[RandomVariate[BinormalDistribution[
RandomReal[{-3,3},2],
RandomReal[{0.5,2},2],
RandomReal[{0.2,0.8}]],RandomInteger[{30,40}]],{4}];
plot=ListPlot[clusters, PlotStyle->Darker[colors,0.1],ImageSize->200,PlotRange->{{-5,5},{-5,5}},Frame->True,AspectRatio->1,PlotLabel->"data"]

Blend colors to reflect the probability density of the different classes for each method:

line=Range[-5,5,0.25];
points=Tuples[line,2];

makecolormap[probs_] :=Transpose @ Partition[
Map[Blend[Keys[#],Values[#]]&, probs],
Length[line]];

data=AssociationThread[colors,clusters];
methods={"LogisticRegression","NaiveBayes","NearestNeighbors","NeuralNetwork","RandomForest","SupportVectorMachine"};
Table[
ArrayPlot[
makecolormap @ Classify[data,points,"Probabilities",Method->method],
PlotLabel->method,DataReversed->True,ImageSize->150],
{method,methods}]~Multicolumn~2 ~Legended~plot

Draw in the box to test a logistic classifier trained on the dataset (RefLink[ExampleData, paclet:ref/ExampleData])[{"MachineLearning","MNIST"}]:

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[ClassifierFunction, paclet:ref/ClassifierFunction])  ▪  (RefLink[ClassifierMeasurements, paclet:ref/ClassifierMeasurements])  ▪  (RefLink[Predict, paclet:ref/Predict])  ▪  (RefLink[ActiveClassification, paclet:ref/ActiveClassification])  ▪  (RefLink[SequencePredict, paclet:ref/SequencePredict])  ▪  (RefLink[ClusterClassify, paclet:ref/ClusterClassify])  ▪  (RefLink[LogitModelFit, paclet:ref/LogitModelFit])  ▪  (RefLink[Nearest, paclet:ref/Nearest])  ▪  (RefLink[FindClusters, paclet:ref/FindClusters])  ▪  (RefLink[Interpreter, paclet:ref/Interpreter])  ▪  (RefLink[DimensionReduce, paclet:ref/DimensionReduce])  ▪  (RefLink[ExampleData, paclet:ref/ExampleData])  ▪  (RefLink[LanguageIdentify, paclet:ref/LanguageIdentify])  ▪  (RefLink[ImageIdentify, paclet:ref/ImageIdentify])  ▪  (RefLink[TextRecognize, paclet:ref/TextRecognize])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Machine Learning, paclet:guide/MachineLearning])}, {▪(RefLinkPlain[Computer Vision, paclet:guide/ComputerVision])}, {▪(RefLinkPlain[Linguistic Data, paclet:guide/LinguisticData])}, {▪(RefLinkPlain[Text Analysis, paclet:guide/TextAnalysis])}, {▪(RefLinkPlain[Neural Networks, paclet:guide/NeuralNetworks])}, {▪(RefLinkPlain[Image Computation for Microscopy, paclet:guide/ImageComputationForMicroscopy])}, {▪(RefLinkPlain[Audio Processing, paclet:guide/AudioProcessing])}, {▪(RefLinkPlain[Text Manipulation, paclet:guide/ProcessingTextualData])}, {▪(RefLinkPlain[Scientific Data Analysis, paclet:guide/ScientificDataAnalysis])}, {▪(RefLinkPlain[Internet and Computer Systems Data, paclet:guide/InternetAndWebRelatedData])}})}})

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