Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A Quiz
Introduction to Supervised Learning
Supervised learning is one of the most common types of
machine learning and forms the foundation for many predictive tasks in data
science. In supervised learning, the model is trained on a labeled dataset,
meaning the input data is paired with the correct output, or "label".
The algorithm’s task is to learn the mapping between the input and the output
so that it can predict the label for new, unseen data.
Supervised learning can be classified into two main types:
In this chapter, we’ll dive deep into supervised learning
algorithms, focusing on Linear Regression for regression problems and Logistic
Regression, K-Nearest Neighbors (KNN), and Support Vector
Machines (SVM) for classification problems. We will explore how these
algorithms work, their mathematical foundations, and how to implement them
using Python libraries like Scikit-learn and NumPy.
1. Linear Regression: Predicting Continuous Values
Linear regression is one of the simplest and most commonly
used algorithms in machine learning for predicting continuous values. The
primary goal of linear regression is to find the best-fitting line (or
hyperplane in higher dimensions) that predicts the output variable from the
input variables.
Mathematics Behind Linear Regression
Linear regression assumes a linear relationship between the
input variables and the target variable. The mathematical model for linear
regression is:
y=β0+β1x1+β2x2+...+βnxn+ϵy = \beta_0 + \beta_1 x_1 + \beta_2
x_2 + ... + \beta_n x_n + \epsilony=β0+β1x1+β2x2+...+βnxn+ϵ
Where:
The goal is to find the values of the coefficients (β) that
minimize the error, typically using least squares.
Code Example: Linear Regression
Here’s how to implement linear regression in Python using Scikit-learn:
import
numpy as np
import
matplotlib.pyplot as plt
from
sklearn.linear_model import LinearRegression
from
sklearn.model_selection import train_test_split
from
sklearn.metrics import mean_squared_error
#
Sample data
X
= np.array([[1], [2], [3], [4], [5]]) #
Input feature
y
= np.array([1, 2, 3, 4, 5]) # Target
variable
#
Split the data into training and testing sets
X_train,
X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
#
Create the model
model
= LinearRegression()
#
Train the model
model.fit(X_train,
y_train)
#
Predict on test data
y_pred
= model.predict(X_test)
#
Evaluate the model
mse
= mean_squared_error(y_test, y_pred)
print("Mean
Squared Error:", mse)
#
Plotting the data and the regression line
plt.scatter(X,
y, color='blue')
plt.plot(X,
model.predict(X), color='red')
plt.title("Linear
Regression")
plt.xlabel("X")
plt.ylabel("y")
plt.show()
Output:
2. Logistic Regression: Predicting Categorical Outcomes
Logistic regression is used for classification tasks,
especially when the outcome is binary (0 or 1, Yes or No). Unlike linear
regression, which is used to predict continuous values, logistic regression
predicts the probability that a given input point belongs to a particular
class.
Mathematics Behind Logistic Regression
The logistic function, also known as the sigmoid function,
is used to map the predicted value to a probability between 0 and 1. The
formula for logistic regression is:
p=11+e−(β0+β1x1+...+βnxn)p = \frac{1}{1 + e^{-(\beta_0 +
\beta_1 x_1 + ... + \beta_n x_n)}}p=1+e−(β0+β1x1+...+βnxn)1
Where:
Code Example: Logistic Regression
from
sklearn.linear_model import LogisticRegression
from
sklearn.metrics import accuracy_score
#
Example data (binary classification)
X
= np.array([[1], [2], [3], [4], [5], [6]])
# Input features
y
= np.array([0, 0, 0, 1, 1, 1]) # Binary
target
#
Split the data
X_train,
X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
#
Logistic Regression model
model
= LogisticRegression()
#
Train the model
model.fit(X_train,
y_train)
#
Predict on test data
y_pred
= model.predict(X_test)
#
Evaluate the model
accuracy
= accuracy_score(y_test, y_pred)
print("Accuracy:",
accuracy)
3. K-Nearest Neighbors (KNN): Classification Based on
Similarity
K-Nearest Neighbors (KNN) is a simple but powerful algorithm
for classification. It works by finding the 'k' training examples that are
closest to a test point and predicting the class based on the majority class of
these neighbors.
How KNN Works
Code Example: KNN Classification
from
sklearn.neighbors import KNeighborsClassifier
from
sklearn.metrics import accuracy_score
#
Sample data for KNN
X
= np.array([[1], [2], [3], [4], [5], [6]])
y
= np.array([0, 0, 0, 1, 1, 1])
#
Split the data
X_train,
X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
#
KNN model
knn
= KNeighborsClassifier(n_neighbors=3)
#
Train the model
knn.fit(X_train,
y_train)
#
Predict on test data
y_pred
= knn.predict(X_test)
#
Evaluate the model
accuracy
= accuracy_score(y_test, y_pred)
print("Accuracy:",
accuracy)
4. Support Vector Machines (SVM): Finding the Optimal
Hyperplane
Support Vector Machines (SVM) are powerful supervised
learning algorithms used for both classification and regression tasks. SVM
works by finding a hyperplane that best divides a dataset into two classes. The
goal is to maximize the margin between the two classes.
How SVM Works
Code Example: SVM for Classification
from
sklearn.svm import SVC
from
sklearn.metrics import accuracy_score
#
Sample data for SVM
X
= np.array([[1], [2], [3], [4], [5], [6]])
y
= np.array([0, 0, 0, 1, 1, 1])
#
Split the data
X_train,
X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
#
SVM model
svm
= SVC(kernel='linear')
#
Train the model
svm.fit(X_train,
y_train)
#
Predict on test data
y_pred
= svm.predict(X_test)
#
Evaluate the model
accuracy
= accuracy_score(y_test, y_pred)
print("Accuracy:",
accuracy)
5. Performance Evaluation Metrics
To evaluate the performance of supervised learning models,
we use various metrics that help us understand how well our model is
performing. For classification tasks, some common evaluation metrics include:
For regression tasks, some common metrics include:
Conclusion
In this chapter, we have explored key supervised learning
algorithms, including Linear Regression for regression tasks and Logistic
Regression, KNN, and SVM for classification tasks. These
algorithms form the backbone of many machine learning systems and are
fundamental to understanding and solving predictive problems. By mastering
these techniques, you’ll be equipped to tackle a wide range of problems in data
science and machine learning.
Machine learning is a branch of artificial intelligence that allows computers to learn from data and make predictions or decisions without being explicitly programmed
Classification involves predicting a categorical outcome (e.g., spam or not spam), while regression involves predicting a continuous numerical value (e.g., predicting house prices).
Features are the input variables (data) used to predict an outcome, and labels are the output or target variable we want to predict (in supervised learning).
Overfitting occurs when a model learns the training data too well, including its noise and outliers, making it perform poorly on unseen data
Cross-validation is a technique used to assess the performance of a machine learning model by splitting the data into multiple subsets and training the model on different combinations of the subsets
Training data is used to train the machine learning model, while testing data is used to evaluate the model's performance after training.
Hyperparameters are the settings or configurations used to control the training process of a machine learning model, such as learning rate, number of epochs, and batch size.
Feature engineering is the process of selecting, modifying, or creating new features from raw data to improve the performance of machine learning algorithms. It involves tasks like normalizing values, handling missing data, encoding categorical variables, and creating new features based on domain knowledge to better represent the underlying patterns in the data.
o Classification
involves predicting a categorical label (e.g., spam or not spam, dog or cat)
based on input features. Common algorithms for classification include Logistic
Regression, Decision Trees, and SVM.
o Regression
involves predicting a continuous value (e.g., predicting house prices or stock
prices). Common algorithms for regression include Linear Regression, Ridge
Regression, and Random Forest Regression.
Please log in to access this content. You will be redirected to the login page shortly.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Comments(0)