SlideShare a Scribd company logo
PCA AND LDA (MACHINE
LEARNING)
Akhilesh Joshi
akhileshjoshi123@gmail
.com
PCA
PRINCIPAL COMPONENT ANALYSIS
DEFINED
The main idea of principal component analysis (PCA) is to reduce the
dimensionality of a data set consisting of many variables correlated
with each other, either heavily or lightly, while retaining the variation
present in the dataset, up to the maximum extent.
The same is done by transforming the variables to a new set of
variables, which are known as the principal components (or simply,
the PCs) and are orthogonal, ordered such that the retention of
variation present in the original variables decreases as we move down
in the order.
So, in this way, the 1st principal component retains maximum
variation that was present in the original components. The principal
components are the eigenvectors of a covariance matrix, and hence
they are orthogonal.
PCA and LDA in machine learning
PCA and LDA in machine learning
REFERENCES
StatQuest :
https://www.youtube.com/watch?v=_UVHneBUBW0&list=RD_UVHneB
UBW0
Dezyre :
https://www.dezyre.com/data-science-in-python-tutorial/principal-
component-analysis-tutorial
PCA
CONCEPT
ILLUSTRATI
ON
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
IMPLEMENTING PCA ON A 2-D
DATASET
Step 1: Normalize the data
Step 2: Calculate the covariance matrix
Step 3: Calculate the eigenvalues and eigenvectors
Step 4: Choosing components and forming a feature vector
Step 5: Forming Principal Components:
APPLICATIONS OF PRINCIPAL
COMPONENT ANALYSIS
PCA is predominantly used as a dimensionality reduction technique in
domains like facial recognition, computer vision and image
compression. It is also used for finding patterns in data of high
dimension in the field of finance, data mining, bioinformatics,
psychology, etc.
PCA and LDA in machine learning
LDA DEFINED
Linear Discriminant Analysis (LDA) is most commonly used as
dimensionality reduction technique in the pre-processing step for
pattern-classification and machine learning applications.
The goal is to project a dataset onto a lower-dimensional space with
good class-separability in order avoid overfitting (“curse of
dimensionality”) and also reduce computational costs.
PCA and LDA in machine learning
PCA and LDA in machine learning
LDA
CONCEPT
ILLUSTRATI
ON
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
PCA and LDA in machine learning
APPLICATIONS
OF
LDA
PCA VS LDA
PCA as a technique that finds the
directions of maximal variance
LDA attempts to find a feature
subspace that maximizes class
separability
PCA and LDA in machine learning
PYTHON
This Photo by Unknown Author is licensed under CC BY-SA
IMPORTING THE LIBRARIES
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
IMPORTING THE DATASET
dataset = pd.read_csv('Wine.csv')
X = dataset.iloc[:, 0:13].values
y = dataset.iloc[:, 13].values
SPLITTING THE DATASET INTO THE
TRAINING SET AND TEST SET
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2,
random_state = 0)
FEATURE SCALING
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
APPLYING PCA
from sklearn.decomposition import PCA
pca = PCA(n_components = None) #just for testing
pca = PCA(n_components = 2)
X_train = pca.fit_transform(X_train)
X_test = pca.transform(X_test)
explained_variance = pca.explained_variance_ratio_
Feature scaling is must
before any
dimensionality
reduction method
APPLYING KERNEL PCA
from sklearn.decomposition import KernelPCA
kpca = KernelPCA(n_components = 2, kernel = 'rbf')
X_train = kpca.fit_transform(X_train)
X_test = kpca.transform(X_test)
Feature scaling is must
before any
dimensionality
reduction method
APPLYING LDA
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
as LDA
lda = LDA(n_components = 2)
X_train = lda.fit_transform(X_train, y_train)
X_test = lda.transform(X_test)
Feature scaling is must
before any
dimensionality
reduction method
FITTING LOGISTIC REGRESSION
TO THE TRAINING SET
from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression(random_state = 0)
classifier.fit(X_train, y_train)
PREDICTING THE TEST SET
RESULTS
y_pred = classifier.predict(X_test)
# MAKING THE CONFUSION MATRIX
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
from matplotlib.colors import ListedColormap
X_set, y_set = X_train, y_train
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
alpha = 0.75, cmap = ListedColormap(('red', 'green', 'blue')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
c = ListedColormap(('red', 'green', 'blue'))(i), label = j)
plt.title('Logistic Regression (Training set)')
plt.xlabel('PC1')
plt.ylabel('PC2')
plt.legend()
plt.show()
# Visualising the Test set results
from matplotlib.colors import ListedColormap
X_set, y_set = X_test, y_test
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step
= 0.01),
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
alpha = 0.75, cmap = ListedColormap(('red', 'green', 'blue')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
c = ListedColormap(('red', 'green', 'blue'))(i), label = j)
plt.title('Logistic Regression (Test set)')
plt.xlabel('PC1')
plt.ylabel('PC2')
plt.legend()
plt.show()

More Related Content

PDF
Dimensionality Reduction
PDF
Linear regression
PDF
Bias and variance trade off
PDF
Linear discriminant analysis
PDF
Decision trees in Machine Learning
PPT
Clustering
PPTX
Image segmentation using wvlt trnsfrmtn and fuzzy logic. ppt
PPTX
K-Means Clustering Algorithm.pptx
Dimensionality Reduction
Linear regression
Bias and variance trade off
Linear discriminant analysis
Decision trees in Machine Learning
Clustering
Image segmentation using wvlt trnsfrmtn and fuzzy logic. ppt
K-Means Clustering Algorithm.pptx

What's hot (20)

PPTX
K-Nearest Neighbor(KNN)
PDF
Principal Component Analysis
PPTX
Logistic regression
PPTX
Application of edge detection
PDF
Visual Explanation of Ridge Regression and LASSO
PPTX
Lect5 principal component analysis
PPTX
Data mining primitives
PPT
K mean-clustering
PPTX
Graph representation
PPTX
Decision Trees
PDF
Dimensionality Reduction
PPT
Bayseian decision theory
PPTX
Decision tree induction \ Decision Tree Algorithm with Example| Data science
PPTX
Chapter 9 morphological image processing
PPTX
Introduction to Linear Discriminant Analysis
PDF
Feature detection and matching
PPTX
Handling noisy data
PPTX
Machine Learning: Bias and Variance Trade-off
PPTX
Recurrent Neural Network (RNN) | RNN LSTM Tutorial | Deep Learning Course | S...
K-Nearest Neighbor(KNN)
Principal Component Analysis
Logistic regression
Application of edge detection
Visual Explanation of Ridge Regression and LASSO
Lect5 principal component analysis
Data mining primitives
K mean-clustering
Graph representation
Decision Trees
Dimensionality Reduction
Bayseian decision theory
Decision tree induction \ Decision Tree Algorithm with Example| Data science
Chapter 9 morphological image processing
Introduction to Linear Discriminant Analysis
Feature detection and matching
Handling noisy data
Machine Learning: Bias and Variance Trade-off
Recurrent Neural Network (RNN) | RNN LSTM Tutorial | Deep Learning Course | S...
Ad

Similar to PCA and LDA in machine learning (20)

PPTX
M5.pptx
PPTX
Unit 3-L6.pptx introdction to machine learning
PPTX
PCA-LDA-Lobo.pptxttvertyuytreiopkjhgftfv
PDF
Principal component analysis and lda
PPTX
DimensionalityReduction.pptx
PPTX
DATA MINING.pptx
PDF
1376846406 14447221
PDF
A Novel Algorithm for Design Tree Classification with PCA
PPTX
Singular Value Decomposition (SVD).pptx
PPTX
EDAB Module 5 Singular Value Decomposition (SVD).pptx
PDF
lec9_annotated.pdf ml csci 567 vatsal sharan
PDF
22PCOAM16_MACHINE_LEARNING_UNIT_IV_NOTES_with_QB
PDF
pca.pdf polymer nanoparticles and sensors
PDF
2012 mdsp pr09 pca lda
PPTX
PCA Algorithmthatincludespcathatispca.pptx
PPTX
Dimentionality reduction
PPTX
machine learning.pptx
PDF
Neural Networks: Principal Component Analysis (PCA)
PPTX
Dimensionality Reduction and feature extraction.pptx
PPT
Understandig PCA and LDA
M5.pptx
Unit 3-L6.pptx introdction to machine learning
PCA-LDA-Lobo.pptxttvertyuytreiopkjhgftfv
Principal component analysis and lda
DimensionalityReduction.pptx
DATA MINING.pptx
1376846406 14447221
A Novel Algorithm for Design Tree Classification with PCA
Singular Value Decomposition (SVD).pptx
EDAB Module 5 Singular Value Decomposition (SVD).pptx
lec9_annotated.pdf ml csci 567 vatsal sharan
22PCOAM16_MACHINE_LEARNING_UNIT_IV_NOTES_with_QB
pca.pdf polymer nanoparticles and sensors
2012 mdsp pr09 pca lda
PCA Algorithmthatincludespcathatispca.pptx
Dimentionality reduction
machine learning.pptx
Neural Networks: Principal Component Analysis (PCA)
Dimensionality Reduction and feature extraction.pptx
Understandig PCA and LDA
Ad

More from Akhilesh Joshi (20)

PPTX
random forest regression
PPTX
decision tree regression
PPTX
support vector regression
PPTX
polynomial linear regression
PPTX
multiple linear regression
PPTX
simple linear regression
PPTX
R square vs adjusted r square
PPTX
PPTX
Grid search (parameter tuning)
PPTX
svm classification
PPTX
knn classification
PPTX
logistic regression with python and R
PPTX
Data preprocessing for Machine Learning with R and Python
PPTX
Design patterns
PPTX
Bastion Host : Amazon Web Services
PDF
Design patterns in MapReduce
PPT
Google knowledge graph
DOCX
Machine learning (domingo's paper)
DOC
SoLoMo - Future of Marketing
PPTX
Webcrawler
random forest regression
decision tree regression
support vector regression
polynomial linear regression
multiple linear regression
simple linear regression
R square vs adjusted r square
Grid search (parameter tuning)
svm classification
knn classification
logistic regression with python and R
Data preprocessing for Machine Learning with R and Python
Design patterns
Bastion Host : Amazon Web Services
Design patterns in MapReduce
Google knowledge graph
Machine learning (domingo's paper)
SoLoMo - Future of Marketing
Webcrawler

Recently uploaded (20)

PPTX
Database Infoormation System (DBIS).pptx
PPTX
Modelling in Business Intelligence , information system
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PPTX
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
PPTX
Pilar Kemerdekaan dan Identi Bangsa.pptx
PDF
Microsoft Core Cloud Services powerpoint
PPTX
Topic 5 Presentation 5 Lesson 5 Corporate Fin
PDF
Lecture1 pattern recognition............
PPT
Predictive modeling basics in data cleaning process
PPTX
Microsoft-Fabric-Unifying-Analytics-for-the-Modern-Enterprise Solution.pptx
PDF
Data Engineering Interview Questions & Answers Batch Processing (Spark, Hadoo...
PPTX
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
PDF
Introduction to Data Science and Data Analysis
PPTX
(Ali Hamza) Roll No: (F24-BSCS-1103).pptx
PPTX
A Complete Guide to Streamlining Business Processes
PDF
Business Analytics and business intelligence.pdf
PDF
Oracle OFSAA_ The Complete Guide to Transforming Financial Risk Management an...
PDF
Galatica Smart Energy Infrastructure Startup Pitch Deck
PPTX
SAP 2 completion done . PRESENTATION.pptx
Database Infoormation System (DBIS).pptx
Modelling in Business Intelligence , information system
Acceptance and paychological effects of mandatory extra coach I classes.pptx
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
Pilar Kemerdekaan dan Identi Bangsa.pptx
Microsoft Core Cloud Services powerpoint
Topic 5 Presentation 5 Lesson 5 Corporate Fin
Lecture1 pattern recognition............
Predictive modeling basics in data cleaning process
Microsoft-Fabric-Unifying-Analytics-for-the-Modern-Enterprise Solution.pptx
Data Engineering Interview Questions & Answers Batch Processing (Spark, Hadoo...
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
Introduction to Data Science and Data Analysis
(Ali Hamza) Roll No: (F24-BSCS-1103).pptx
A Complete Guide to Streamlining Business Processes
Business Analytics and business intelligence.pdf
Oracle OFSAA_ The Complete Guide to Transforming Financial Risk Management an...
Galatica Smart Energy Infrastructure Startup Pitch Deck
SAP 2 completion done . PRESENTATION.pptx

PCA and LDA in machine learning

  • 1. PCA AND LDA (MACHINE LEARNING) Akhilesh Joshi akhileshjoshi123@gmail .com
  • 2. PCA
  • 3. PRINCIPAL COMPONENT ANALYSIS DEFINED The main idea of principal component analysis (PCA) is to reduce the dimensionality of a data set consisting of many variables correlated with each other, either heavily or lightly, while retaining the variation present in the dataset, up to the maximum extent. The same is done by transforming the variables to a new set of variables, which are known as the principal components (or simply, the PCs) and are orthogonal, ordered such that the retention of variation present in the original variables decreases as we move down in the order. So, in this way, the 1st principal component retains maximum variation that was present in the original components. The principal components are the eigenvectors of a covariance matrix, and hence they are orthogonal.
  • 97. IMPLEMENTING PCA ON A 2-D DATASET Step 1: Normalize the data Step 2: Calculate the covariance matrix Step 3: Calculate the eigenvalues and eigenvectors Step 4: Choosing components and forming a feature vector Step 5: Forming Principal Components:
  • 98. APPLICATIONS OF PRINCIPAL COMPONENT ANALYSIS PCA is predominantly used as a dimensionality reduction technique in domains like facial recognition, computer vision and image compression. It is also used for finding patterns in data of high dimension in the field of finance, data mining, bioinformatics, psychology, etc.
  • 100. LDA DEFINED Linear Discriminant Analysis (LDA) is most commonly used as dimensionality reduction technique in the pre-processing step for pattern-classification and machine learning applications. The goal is to project a dataset onto a lower-dimensional space with good class-separability in order avoid overfitting (“curse of dimensionality”) and also reduce computational costs.
  • 174. PCA VS LDA PCA as a technique that finds the directions of maximal variance LDA attempts to find a feature subspace that maximizes class separability
  • 176. PYTHON This Photo by Unknown Author is licensed under CC BY-SA
  • 177. IMPORTING THE LIBRARIES import numpy as np import matplotlib.pyplot as plt import pandas as pd
  • 178. IMPORTING THE DATASET dataset = pd.read_csv('Wine.csv') X = dataset.iloc[:, 0:13].values y = dataset.iloc[:, 13].values
  • 179. SPLITTING THE DATASET INTO THE TRAINING SET AND TEST SET from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)
  • 180. FEATURE SCALING from sklearn.preprocessing import StandardScaler sc = StandardScaler() X_train = sc.fit_transform(X_train) X_test = sc.transform(X_test)
  • 181. APPLYING PCA from sklearn.decomposition import PCA pca = PCA(n_components = None) #just for testing pca = PCA(n_components = 2) X_train = pca.fit_transform(X_train) X_test = pca.transform(X_test) explained_variance = pca.explained_variance_ratio_ Feature scaling is must before any dimensionality reduction method
  • 182. APPLYING KERNEL PCA from sklearn.decomposition import KernelPCA kpca = KernelPCA(n_components = 2, kernel = 'rbf') X_train = kpca.fit_transform(X_train) X_test = kpca.transform(X_test) Feature scaling is must before any dimensionality reduction method
  • 183. APPLYING LDA from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA lda = LDA(n_components = 2) X_train = lda.fit_transform(X_train, y_train) X_test = lda.transform(X_test) Feature scaling is must before any dimensionality reduction method
  • 184. FITTING LOGISTIC REGRESSION TO THE TRAINING SET from sklearn.linear_model import LogisticRegression classifier = LogisticRegression(random_state = 0) classifier.fit(X_train, y_train)
  • 185. PREDICTING THE TEST SET RESULTS y_pred = classifier.predict(X_test)
  • 186. # MAKING THE CONFUSION MATRIX from sklearn.metrics import confusion_matrix cm = confusion_matrix(y_test, y_pred)
  • 187. from matplotlib.colors import ListedColormap X_set, y_set = X_train, y_train X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01), np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01)) plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape), alpha = 0.75, cmap = ListedColormap(('red', 'green', 'blue'))) plt.xlim(X1.min(), X1.max()) plt.ylim(X2.min(), X2.max()) for i, j in enumerate(np.unique(y_set)): plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], c = ListedColormap(('red', 'green', 'blue'))(i), label = j) plt.title('Logistic Regression (Training set)') plt.xlabel('PC1') plt.ylabel('PC2') plt.legend() plt.show()
  • 188. # Visualising the Test set results from matplotlib.colors import ListedColormap X_set, y_set = X_test, y_test X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01), np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01)) plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape), alpha = 0.75, cmap = ListedColormap(('red', 'green', 'blue'))) plt.xlim(X1.min(), X1.max()) plt.ylim(X2.min(), X2.max()) for i, j in enumerate(np.unique(y_set)): plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], c = ListedColormap(('red', 'green', 'blue'))(i), label = j) plt.title('Logistic Regression (Test set)') plt.xlabel('PC1') plt.ylabel('PC2') plt.legend() plt.show()