PROG 25211
AI AND MACHINE
LEARNING - PYTHON
Dr. Gurleen Kaur
Gurleen.kaur2@sheridancollege.ca
UNSUPERVISED LEARNING
Overview
• Unsupervised Learning
• Image Regression
• Pickle Files
OUTCOME
By the end of this lesson you would have covered
various concepts of unsupervised learning.
CLUSTERING
Supposed you were asked to organize the group of shapes below:
CLUSTERING
There are a couple of different ways that might become apparent right
away. You could group them by colour, or you could group them by shape.
CLUSTERING
You might have come up with a more interesting grouping idea,
but these two options are probably the most likely. Without
more specific instructions other than grouping, both answers
would be correct. But both answers produce different
results. This is the idea of Unsupervised Learning. Unlike
Supervised Learning which provides details about the
information it is training on, unsupervised learning is given
no or minimal instructions, and is required to find some type
of pattern.
KNN VS K-MEANS
In a previous
lecture we made use
of K-Nearest
Neighbours to try
to determine
penguin species.
KNN VS K-MEANS
We had a lot of labelled information about our records,
and could use a supervised approach of K Nearest
Neighbours.
With unsupervised techniques, we might know how many
groups we might want our data to fall into, but like the
shapes example earlier we won’t necessarily know how our
data gets grouped.
KNN VS K-MEANS
When we compare it to K-Means, the weighted center of
each group is calculated and the new data is measured to
each cluster center. These clusters are then
reevaluated again and again until the distance to a
cluster center is minimized.
K-MEANS
ALGORITHM
WHAT IS UNSUPERVISED
LEARNING?
Unsupervised learning is used to analyze and cluster
unlabeled datasets. These algorithms discover hidden
patterns or data groupings without the need for human
intervention.
https://www.ibm.com/topics/unsupervised-learning
ADVANTAGES OF UNSUPERVISED
LEARNING
• Unsupervised learning is used for more complex tasks as
compared to supervised learning because, in unsupervised
learning, we don't have labeled input data.
• Unsupervised learning is preferable as it is easy to get
unlabeled data in comparison to labeled data.
DISADVANTAGES OF UNSUPERVISED
LEARNING
• Unsupervised learning is intrinsically more difficult
than supervised learning as it does not have
corresponding output.
• The result of the unsupervised learning algorithm might
be less accurate as input data is not labeled, and
algorithms do not know the exact output in advance.
https://www.javatpoint.com/unsupervised-machine-learning
CLUSTERING
Clustering can be subdivided into various groups.
HIERARCHICAL
Agglomerative clustering is a bottom up approach where
smaller groups are grouped together into larger groups
HIERARCHICAL
Divisive clustering is a top down approach where larger
groups are divided into smaller groups.
PARTITIONAL
K-Means: We have already
looked at an example of K-
Means, where we try to converge
our data around specific
centroids. The distinction
between partitional groups is
K-Means is that it is mutually
exclusive. A specific data
point can only belong to one
defined group.
PARTITIONAL
Fuzzy C-Means:
Alternatively, Fuzzy C-
Means does separate data
into groups, but does
allow for a specific
point to belong to
multiple groups.
UNSUPERVISED LEARNING
APPLICATIONS
• Natural language processing (NLP).
• Image and video analysis.
• Anomaly detection.
• Customer segmentation.
• Recommendation Engines.
https://www.datacamp.com/blog/introduction-to-unsupervised-learni
ng
IMAGE REDUCTION
We are going to start with our standard set of imports.
IMAGE REDUCTION
We are then going to load in a default image from sklearn of a flower.
IMAGE REDUCTION
IMAGE REDUCTION
IMAGE REDUCTION
IMAGE REDUCTION
We see that we get a 3-dimensional array of numbers. Each inner
array represents a pixel of RGB where each colour is in the range
0-255. With a little math that means we have 2^24 possible
pixels, or 16,777,216 colors.
We are going to convert our information into a 2-dimensional
array of data, and reduce each value to a scale of 0-1 so that it
is easier for our algorithm to process.
IMAGE REDUCTION
IMAGE REDUCTION
To look at our colour spectrum and see the range of colours that
we have we are going to plot our values of Red against Blue and
Red against Green.
The function below is going to randomly sample 10000 points in
our image and graph them in either the Red vs Blue graph or the
Red vs Green graph.
IMAGE REDUCTION
IMAGE REDUCTION
MINI-BATCH K-MEANS CLUSTERING
The Mini-batch K-means clustering algorithm is a version of the
K-means algorithm which can be used instead of the K-means
algorithm when clustering on huge datasets. Sometimes it performs
better than the standard K-means algorithm while working on huge
datasets because it doesn’t iterate over the entire dataset. It
creates random batches of data to be stored in memory, then a
random batch of data is collected on each iteration to update the
clusters.
https://thecleverprogrammer.com/2021/09/10/mini-batch-k-means-clu
stering-in-machine-learning/
IMAGE REDUCTION
We are going to use MiniBatchKMeans to convert our images from a 24 bit
colour image to a 4 bit colour image, which will only have 16 colours.
IMAGE REDUCTION
If we now try to graph our clustered data we get the following
IMAGE REDUCTION
We now only see 16 points on our graph. Or rather we see 10000
points clustered at 16 locations. These are the centroid
locations that were chosen by our k-Means model. All the points
have been recoloured to one of these 16 values.
IMAGE REDUCTION
We can look at the colour distribution of our image. You will
notice that the colours are still distributed the same, but we
don't have the same colour tones.
IMAGE REDUCTION
IMAGE REDUCTION
IMAGE REDUCTION
MAKING A PICKLE FILE
We have spent a lot of time learing how to train a model. But
what if we want to use our model in another program, or if we
want to use someone else's model? We are going to make use of
Pickle in this lesson, but first, let's quickly train a model.
This is just Week 4 Part 4.
MAKING A PICKLE FILE
MAKING A PICKLE FILE
MAKING A PICKLE FILE
READING A PICKLE FILE
READING A PICKLE FILE
READING A PICKLE FILE