Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Introduction to Supervised Learning

What is Supervised Learning?

Supervised learning is a type of machine learning where the model is trained on labeled data. In this approach, the algorithm learns from a training dataset that includes both the input features and the corresponding correct output. The goal is to learn a mapping from inputs to outputs that can be used to predict the output for new, unseen inputs.

Types of Supervised Learning

Supervised learning can be broadly categorized into two types:

  1. Regression: The output variable is continuous, and the goal is to predict the continuous value. For example, predicting house prices based on features like size and location.
  2. Classification: The output variable is categorical, and the goal is to predict the category label. For example, classifying emails as spam or not spam.

Common Algorithms in Supervised Learning

Several algorithms can be used for supervised learning, including:

  • Linear Regression
  • Logistic Regression
  • Decision Trees
  • Support Vector Machines (SVM)
  • k-Nearest Neighbors (k-NN)
  • Neural Networks

Example: Linear Regression

Linear regression is a simple algorithm used to model the relationship between a dependent variable and one or more independent variables. The relationship is modeled using a linear equation.

Suppose we have a dataset of house prices based on their size. The goal is to predict the price of a house given its size. The linear regression model can be represented as:

price = β0 + β1 * size

where β0 is the intercept and β1 is the slope of the line.

Implementing Linear Regression in Python

Let's implement a simple linear regression model using Python and the scikit-learn library.

import numpy as np
from sklearn.linear_model import LinearRegression

# Sample data
sizes = np.array([[1400], [1600], [1700], [1875], [1100], [1550], [2350], [2450], [1425], [1700]])
prices = np.array([245000, 312000, 279000, 308000, 199000, 219000, 405000, 324000, 319000, 255000])

# Create and train the model
model = LinearRegression()
model.fit(sizes, prices)

# Make a prediction
size_new = np.array([[1500]])
price_pred = model.predict(size_new)

print(f"Predicted price for a house with size 1500 sq ft: ${price_pred[0]:.2f}")
Predicted price for a house with size 1500 sq ft: $254812.50

Conclusion

Supervised learning is a fundamental concept in machine learning that involves training models on labeled data. By understanding the types of supervised learning, common algorithms, and practical implementation, you can start building your own predictive models. Linear regression is just one example; there are many other algorithms to explore and apply based on the problem at hand.