Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Supervised Learning Tutorial

Introduction

Supervised learning is a type of machine learning algorithm that is trained on labeled data. In supervised learning, the algorithm learns from a training dataset, which includes input data and the corresponding correct output. The goal is to make predictions or decisions based on new, unseen data by generalizing from the training data.

Key Concepts

Before diving into the specifics of supervised learning, it's important to understand a few key concepts:

  • Training Data: A set of data used to train the model. Each example in the training data includes input features and the corresponding correct output.
  • Labels: The correct output value for each example in the training data.
  • Features: The input variables used to make predictions.
  • Model: The algorithm that makes predictions or decisions based on the input data.
  • Loss Function: A function that measures how well the model's predictions match the actual labels. The goal is to minimize this loss function.

Types of Supervised Learning

There are two main types of supervised learning:

  • Classification: The goal is to predict a discrete label or category. Examples include spam detection and image recognition.
  • Regression: The goal is to predict a continuous value. Examples include predicting house prices and stock prices.

Example: Linear Regression

Let's walk through a simple example of linear regression, a type of regression algorithm.

Consider a dataset with two features: the number of hours studied and the corresponding exam score. We want to create a model that predicts the exam score based on the number of hours studied.

Here is some sample data:

Hours Studied: [1, 2, 3, 4, 5]
Exam Scores: [1.5, 2.5, 3.5, 4.5, 5.5]

We can use the following Python code to create a simple linear regression model using scikit-learn:

import numpy as np
from sklearn.linear_model import LinearRegression

# Training data
X = np.array([1, 2, 3, 4, 5]).reshape(-1, 1)
y = np.array([1.5, 2.5, 3.5, 4.5, 5.5])

# Create and train the model
model = LinearRegression()
model.fit(X, y)

# Make a prediction
hours_studied = np.array([6]).reshape(-1, 1)
predicted_score = model.predict(hours_studied)

print(f"Predicted exam score for 6 hours studied: {predicted_score[0]}")
                
Predicted exam score for 6 hours studied: 6.5

Example: Classification with Support Vector Machines (SVM)

Now, let's look at an example of classification using Support Vector Machines (SVM). We'll use a simple dataset to classify whether a point is above or below a certain line.

Consider a dataset with two features: x and y coordinates of a point. We want to classify whether each point is above or below the line y = x.

Here is some sample data:

X: [[1, 2], [2, 1], [3, 3], [4, 5], [5, 4]]
Labels: [1, 0, 1, 1, 0] (1: Above the line, 0: Below the line)

We can use the following Python code to create a SVM model using scikit-learn:

import numpy as np
from sklearn.svm import SVC

# Training data
X = np.array([[1, 2], [2, 1], [3, 3], [4, 5], [5, 4]])
y = np.array([1, 0, 1, 1, 0])

# Create and train the model
model = SVC(kernel='linear')
model.fit(X, y)

# Make a prediction
new_point = np.array([[3, 4]])
predicted_label = model.predict(new_point)

print(f"Predicted label for point (3, 4): {predicted_label[0]}")
                
Predicted label for point (3, 4): 1

Conclusion

Supervised learning is a powerful technique in machine learning that uses labeled data to train models for making predictions or decisions. By understanding the key concepts and types of supervised learning, you can apply these techniques to a wide range of problems. We explored two examples: linear regression for regression tasks and Support Vector Machines (SVM) for classification tasks. With these examples, you should have a good starting point for further exploration and application of supervised learning.