Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Supervised Learning Tutorial

Introduction to Supervised Learning

Supervised learning is a type of machine learning where the model is trained on labeled data. The algorithm learns from the training data, which includes input-output pairs, to make predictions or classifications based on new, unseen data.

Types of Supervised Learning

There are two main types of supervised learning:

  • Classification: The goal is to predict discrete class labels. Examples include spam detection, image recognition, etc.
  • Regression: The goal is to predict continuous values. Examples include house price prediction, stock price prediction, etc.

Steps in Supervised Learning

The typical steps involved in a supervised learning process are:

  1. Data Collection: Gather a dataset that includes input-output pairs.
  2. Data Preprocessing: Clean and prepare the data for training. This includes handling missing values, normalizing features, etc.
  3. Model Selection: Choose an appropriate model and algorithm for your task (e.g., Linear Regression, Decision Trees).
  4. Training: Train the model on the training dataset.
  5. Evaluation: Evaluate the model's performance using validation and test datasets.
  6. Prediction: Use the trained model to make predictions on new data.

Example: Linear Regression

Let's go through an example of using Linear Regression for predicting house prices.

Step 1: Data Collection

We will use a simple dataset containing information about houses: size (sq ft) and price ($).

Step 2: Data Preprocessing

For simplicity, let's assume our data is clean and ready for use.

Step 3: Model Selection

We will use Linear Regression for this task.

Step 4: Training

Training a Linear Regression model using Python's scikit-learn library:

import numpy as np
from sklearn.linear_model import LinearRegression

# Sample data: Size (sq ft) and Price ($)
X = np.array([[1500], [2000], [2500], [3000], [3500]])
y = np.array([300000, 400000, 500000, 600000, 700000])

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

Step 5: Evaluation

Evaluate the model's performance:

# Predicting prices for the training data
predictions = model.predict(X)

# Output the predictions
print(predictions)
                    
[300000. 400000. 500000. 600000. 700000.]

Step 6: Prediction

Use the trained model to predict the price of a new house with a size of 2800 sq ft:

# Predicting the price of a new house
new_house_size = np.array([[2800]])
predicted_price = model.predict(new_house_size)
print(predicted_price)
                    
[560000.]

Conclusion

Supervised learning is a powerful technique for making predictions and classifications based on labeled data. By following the steps outlined in this tutorial, you can build and deploy your own supervised learning models for various tasks.