Supervised learning

background image
Home / Learn / Machine Learning /
Supervised learning

Supervised learning is a type of machine learning in which a model is trained on labeled data. The model is presented with a set of labeled examples and their corresponding desired outputs, and the model learns to map the input data to the desired output. In supervised learning, the model is "supervised" by the labeled training data, which provides the model with the correct output for each example in the training set.

Some common applications of supervised learning include image classification, natural language processing, and speech recognition. In these cases, the model is trained on a large dataset of labeled examples, and is then able to make predictions about new, unseen examples.

There are many example use cases for supervised learning. Some common examples include:

  • Image classification: Supervised learning algorithms can be used to classify images into different categories, such as "dog," "cat," "car," etc. These algorithms are trained on a large dataset of labeled images, and can then be used to classify new, unseen images.

  • Natural language processing: Supervised learning algorithms can be used to classify text into different categories, such as spam or not spam, or positive or negative sentiment. These algorithms are trained on a large dataset of labeled text, and can then be used to classify new, unseen text.

  • Speech recognition: Supervised learning algorithms can be used to transcribe speech into text. These algorithms are trained on a large dataset of audio recordings and their corresponding transcriptions, and can then be used to transcribe new, unseen audio.

  • Credit fraud detection: Supervised learning algorithms can be used to classify transactions as fraudulent or not fraudulent. These algorithms are trained on a large dataset of labeled transactions, and can then be used to classify new, unseen transactions as either fraudulent or not fraudulent.

  • Medical diagnosis: Supervised learning algorithms can be used to predict the likelihood of a patient having a particular disease, based on a range of different features, such as age, gender, medical history, etc. These algorithms are trained on a large dataset of labeled patients, and can then be used to predict the likelihood of a new patient having a particular disease.

There are two main types of supervised learning: classification and regression. In classification, the output is a categorical label, such as "spam" or "not spam." In regression, the output is a continuous value, such as a price or a probability.

Imagine you are trying to build a model that can classify emails as spam or not spam. To do this, you would need a dataset of labeled emails, where each email is marked as "spam" or "not spam." You would use this dataset to train a model to classify new, unseen emails as either spam or not spam.

During training, the model would be presented with a series of emails and their corresponding labels (spam or not spam). The model would learn to recognize patterns in the data that are associated with spam emails, such as certain words or phrases that are commonly used in spam emails.

After training, the model could then be used to classify new emails as either spam or not spam. For example, if the model is presented with a new email that contains the word "viagra," which is commonly used in spam emails, it might classify the email as spam. On the other hand, if the model is presented with an email that contains words and phrases that are not commonly associated with spam emails, it might classify the email as not spam.

There are many different types of supervised learning algorithms, each with its own strengths and weaknesses. Some common algorithms include:

  • Linear regression: Used for predicting a continuous value, such as a price or a probability.

  • Logistic regression: Used for classification tasks, such as determining whether an email is spam or not spam.

  • Decision trees: Used for classification and regression tasks. The model makes predictions by creating a tree-like structure, where each internal node represents a decision based on the value of a feature, and each leaf node represents a prediction.

  • Random forests: An ensemble method that combines multiple decision trees to make more accurate predictions.

  • Support vector machines (SVMs): Used for classification tasks. The model makes predictions by finding the hyperplane in a high-dimensional space that maximizes the margin between different classes.

  • Neural networks: A flexible model that can be used for both classification and regression tasks. Neural networks are inspired by the structure and function of the brain, and are composed of multiple interconnected nodes, or "neurons."

This is just a small sampling of the many different types of supervised learning algorithms that are available. There are many more algorithms to choose from, and the best choice for a particular problem will depend on the characteristics of the data and the requirements of the task.

There are many real-life examples of supervised learning being used in various industries and applications. Here are a few examples:

  • In image classification, supervised learning algorithms are used to classify images into different categories, such as "dog," "cat," "car," etc. These algorithms are trained on a large dataset of labeled images, and can then be used to classify new, unseen images.

  • In natural language processing, supervised learning algorithms are used to classify text into different categories, such as spam or not spam, or positive or negative sentiment. These algorithms are trained on a large dataset of labeled text, and can then be used to classify new, unseen text.

  • In speech recognition, supervised learning algorithms are used to transcribe speech into text. These algorithms are trained on a large dataset of audio recordings and their corresponding transcriptions, and can then be used to transcribe new, unseen audio.

  • In credit fraud detection, supervised learning algorithms are used to classify transactions as fraudulent or not fraudulent. These algorithms are trained on a large dataset of labeled transactions, and can then be used to classify new, unseen transactions as either fraudulent or not fraudulent.

Here is an example of a simple supervised learning model using TensorFlow:

import tensorflow as tf

# Load the data
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()

# Normalize the data
x_train = x_train / 255.0
x_test = x_test / 255.0

# Flatten the data
x_train = x_train.reshape(-1, 28*28)
x_test = x_test.reshape(-1, 28*28)

# Build the model
model = tf.keras.models.Sequential([
  tf.keras.layers.Dense(128, input_shape=(28*28,), activation='relu'),
  tf.keras.layers.Dense(10, activation='softmax')
])

# Compile the model
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# Train the model
model.fit(x_train, y_train, epochs=5)

# Evaluate the model
test_loss, test_acc = model.evaluate(x_test, y_test)
print('Test accuracy:', test_acc)

This model is a simple feedforward neural network that is trained to classify images of handwritten digits from the MNIST dataset. The model is trained using the Adam optimizer and sparse categorical crossentropy loss, and is evaluated on the test set to measure its accuracy in predicting the correct labels for new examples.

In the example I provided includes a fully-connected (dense) layer with 128 units and an output layer with 10 units, one for each digit class. The input to the model is a flatten 28x28 image of a handwritten digit, and the output is a probability distribution over the 10 digit classes.

During training, the model is presented with a set of labeled training examples, consisting of input images and their corresponding correct output labels. The model uses these examples to learn the relationship between the input features (the pixel values of the images) and the output labels (the digits represented by the images).

The model is trained using the Adam optimizer, which is a gradient-based optimization algorithm that adjusts the model parameters based on the gradients of the loss function with respect to those parameters. The loss function used in this example is sparse categorical crossentropy, which is a measure of the difference between the predicted probability distribution and the true probability distribution.

After training, the model can be used to make predictions on new, unseen examples. To evaluate the model's performance, we can use the model.evaluate method to calculate the loss and accuracy on a separate test set. The test accuracy gives us an idea of how well the model is able to generalize to new examples.