# Classifying Data with Neural Networks

## Classifying Vector Data

Create an artificial dataset from three normally distributed clusters:
 In[1]
 In[2]
Plot the dataset:
 In[3]
 Out[3]
The training data consists of rules mapping the point to the cluster it is in:
 In[4]
 Out[4]
Create a net to compute the probability of a point lying in each cluster, using a "Class" decoder to classify the input as Red, Green or Blue:
 In[5]
 Out[5]
Train the net on the data:
 In[6]
 Out[6]
Evaluate the net on the centers of each cluster:
 In[7]
 Out[7]
Show the contours in feature space in which each class reaches a posterior probability of 0.75:
 In[8]
 Out[8]
Plot the degree of uncertainty of the classifier as measured by entropy as a function of the position:
 In[9]
 Out[9]
Perform logistic regression on the Fisher Iris dataset. First, obtain the training data:
 In[10]
 In[11]
 Out[11]
Build a list of the unique labels to which each example is assigned:
 In[12]
 Out[12]
Create a NetChain to perform the classification, using a "Class" decoder to interpret the output of the net as probabilities for each class:
 In[13]
 Out[13]
NetTrain will automatically use a CrossEntropyLossLayer with a NetEncoder to interpret the class labels in the training data:
 In[14]
 Out[14]
Classify an input:
 In[15]
 Out[15]
Obtain the probabilities associated with the classification:
 In[16]
 Out[16]
Produce a ClassifierMeasurementsObject to test the accuracy of the trained net on the test set:
 In[17]
 In[18]
 Out[18]
 In[19]
 Out[19]

## Classifying Categorical Data

Perform logistic regression on a dataset containing both categorical and numeric values.

First, obtain the training data, and drop rows containing missing data:
 In[1]
 Out[1]
Split the data into training and test datasets:
 In[2]

Categorical variables cannot be used directly in neural networks and must be encoded as numeric tensors.

Create "Class" encoders that encode the categorical variables as one-hot encoded vectors:
 In[3]
 Out[3]
 Out[3]
Applying the encoders to class labels produces unit vectors:
 In[4]
 Out[4]
 In[5]
 Out[5]

Create a network with an input corresponding to each feature and using a "Boolean" decoder to interpret the output of the net as the probability of survival.

The input features are first concatenated together before being further processed:
 In[6]
 Out[6]
Train the net on the training data. NetTrain will automatically attach a CrossEntropyLossLayer["Binary"] layer to the output of the net:
 In[7]
 Out[7]
Predict whether a passenger will survive:
 In[8]
 Out[8]
Obtain the probability of survival associated with the input:
 In[9]
 Out[9]
Plot the survival probability as a function of age for some combinations of "class" and "sex":
 In[10]
 In[11]
 Out[11]
Use ClassifierMeasurements to test the accuracy of the trained net on the test set:
 In[12]
 Out[12]
The accuracy is typically comparable to that obtained using Classify when specifying the method "LogisticRegression":
 In[13]
 Out[13]

Perform multitask learning by creating a net that produces two separate classifications.

First, obtain training data:
 In[1]
 Out[1]
The training data consists of an image and the corresponding high-level and low-level labels:
 In[2]
 Out[2]
Extract the unique labels from the "Label" and "SubLabel" columns:
 In[3]
 Out[3]
 In[4]
 Out[4]
Create a base convolutional net that will produce a vector of 500 features:
 In[5]
 Out[5]
Create a NetGraph that will produce separate classifications for the high-level and low-level labels:
 In[6]
 Out[6]
Train the network. NetTrain will automatically attach CrossEntropyLossLayer objects to both outputs, taking the target values from the training data using the corresponding names "Label" and "SubLabel":
 In[7]
 Out[7]
Evaluate the trained network on a single image:
 In[8]
 Out[8]
Evaluate the trained network on several images, taking only the "SubLabel" output:
 In[9]
 Out[9]
Get a property of the class decoder of the "SubLabel" output on an input image:
 In[10]
 Out[10]
From a random sample, select the images for which the net produces highest and lowest entropy predictions for "Label":
 In[11]
 In[12]
 In[13]
 Out[13]
 Out[13]
Produce a subnetwork that computes only "SubLabel" predictions:
 In[14]
 Out[14]
Make a prediction on a single image:
 In[15]
 Out[15]