MLmodule 3
MLmodule 3
messages.pdf_cover_qr_code_label
messages.studocu_not_sponsored_or_endorsed_by_college
messages.downloaded_by
lOMoARcPSD|69216461
MACHINE LEARNING
&
DATA ANALYTICS USING PYTHON
[MMC201]
(2024-26)
messages.downloaded_by
lOMoARcPSD|69216461
Sl.
Experiments
NO
Implement and demonstrate the FIND-S algorithm for finding the most specific hypothesis
1
based on a given set of training data samples. Read the training data from a .CSV file.
For a given set of training data examples stored in a .CSV file, implement and demonstrate
2 the Candidate- Elimination algorithm to output a description of the set of all hypotheses
consistent with the training examples.
Write a program to demonstrate the working of the decision tree based ID3 algorithm. Use
3 an appropriate da set for building the decision tree and apply this knowledge to classify a
new sample.
Write a program to implement the naïve Bayesian classifier for a sample training data set
4
stored as a .CSV fil Compute the accuracy of the classifier, considering few test data sets.
Write a program to implement k-Nearest Neighbour algorithm to classify the iris data set.
5
Print both correct a wrong predictions.
Build an Artificial Neural Network by implementing the Backpropagation algorithm and
6
test the same using appropriate data sets.
7 Write a program to demonstrate Regression analysis with residual plots on a given data set.
Write a program to compute summary statistics such as mean, median, mode, standard
8
deviation and variance the given different types of data.
Write a program to implement k-Means clustering algorithm to cluster the set of data stored
9
in .CSV file.
MODULE 3
UNSUPERVISED LEARNING
INTRODUCTION
Unsupervised learning is a branch of machine learning that deals with unlabeled data.
• Unlike supervised learning, where the data is labeled with a specific category or outcome,
• unsupervised learning algorithms are tasked with finding patterns and relationships within the data
without any prior knowledge of the data's meaning.
• Unsupervised machine learning algorithms find hidden patterns and data without any human
intervention, i.e., we don't give output to our model.
• The training model has only input parameter values and discovers the groups or patterns on its own.
• Natural Language Processing (NLP): Topic modeling (categorizing news articles), text
summarization, word embeddings.
• Image and Video Analysis: Image compression, object recognition (learning features without
labeled examples), video surveillance.
• Cybersecurity: Detecting unusual network traffic patterns indicating a cyberattack.
• Recommendation Systems: Personalizing recommendations for online shoppers or content
consumers.
• Scientific Research: Classifying galaxies in astronomy, grouping weather patterns in climate
science.
Advantages of Unsupervised Learning:
• Handles Unlabeled Data: Its biggest strength is its ability to extract insights from data where
labeling is expensive, time-consuming, or simply not feasible.
• Discovers Hidden Patterns: Can uncover patterns and relationships that human analysts might
miss.
• Exploratory Analysis: Excellent for initial data exploration and understanding the underlying
structure of a dataset.
• Scalability: Can be applied to large and diverse datasets.
Limitations of Unsupervised Learning:
• Lack of Ground Truth: Without labels, it can be challenging to objectively evaluate the
performance of unsupervised models or interpret the meaning of the discovered patterns.
• Subjectivity in Interpretation: The interpretation of clusters or associations often requires domain
expertise and can be subjective.
• Computational Complexity: Some unsupervised learning algorithms can be computationally
intensive, especially for very large datasets.
• Irrelevant Patterns: The algorithm might discover patterns that are not meaningful or relevant to
the problem at hand.
CLUSTERING
Clustering in unsupervised machine learning is the process of grouping unlabeled data into clusters based
on their similarities. The goal of clustering is to identify patterns and relationships in the data without any
prior knowledge of the data's meaning.
Broadly this technique is applied to group data based on different patterns, such as similarities or
differences, our machine model finds. These algorithms are used to process raw, unclassified data objects
into groups.
Think of it as you have a dataset of customers shopping habits. Clustering can help you group customers
with similar purchasing behaviours, which can then be used for targeted marketing, product
recommendations, or customer segmentation.
For Example, In the graph given below, we can clearly see that there are 3 circular clusters forming on the
basis of distance.
Now it is not necessary that the clusters formed must be circular in shape. The shape of clusters can be
arbitrary. There are many algorithms that work well with detecting arbitrary shaped clusters.
For example, In the below given graph we can see that the clusters formed are not circular in shape.
K-Means Clustering
K-Means Clustering is an Unsupervised Machine Learning algorithm which groups unlabeled dataset into
different clusters. It is used to organize data into groups based on their similarity.
How k-means clustering works?
Consider the data set of items with certain features and values for these features like a vector. The task is
to categorize those items into groups. To achieve this we will use the K-means algorithm. 'K' in the name
of the algorithm represents the number of groups/clusters we want to classify our items into.
k_means_clustering
The algorithm will categorize the items into k groups or clusters of similarity. To calculate that similarity
we will use the Euclidean distance as a measurement. The algorithm works as follows:
Step 1: Determine the number of clusters before the algorithm is started. This is called k.
Step 2: Choose k instances randomly. These are initial cluster centers.
Step 3: Compute the mean of the initial clusters and assign the remaining sample to the closest
cluster based on Euclidean distance or any other distance measure between the instances and the
centroid of the clusters.
Step 4: Compute new centroid again considering the newly added samples.
Step 5: Perform the steps 3-4 till the algorithm becomes stable with no more changes in
assignment of instances and clusters.
Example: Consider the following set of data given in below Table. Cluster it using k-means algorithm with
the initial value of objects 2 and 5 with the coordinate values (4, 6) and (12, 4) as initial seeds.
Solution:
As per the problem, choose the objects 2 and 5 with the coordinate values. Hereafter, the objects' id
is not important. The samples or data points (4, 6) and (12, 4) are started as two clusters as shown in Table
Cluster 1 Cluster 2
(4,6) (12,4)
Centroid 1 (4,6) Centroid 2 (12,4)
Iteration 1: Compare all the data points or samples with the centroid and assign to the nearest sample. Take
the sample object 1 (2, 4) from Table and compare with the centroid of the clusters in the table. The distance
is 0. Therefore, it remains in the same cluster. Similarly, consider the remaining samples. For the object 1
(2, 4), 3 (6, 8), 4 (10, 4) the Euclidean distance between it and the centroid is given as:
Cluster 1 Cluster 2
(4,6) (10,4)
(2,4) (12,4)
(6,8)
4 + 2 + 6, 6 + 4 + 8 10 + 12, 4 + 4
𝑐𝑒𝑛𝑡𝑟𝑜𝑖𝑑 1 = ( ) = (4,6) 𝑐𝑒𝑛𝑡𝑟𝑜𝑖𝑑 2 = ( ) = (11,4)
3 2
Cluster 1 Cluster 2
(4,6) (10,4)
(2,4) (12,4)
(6,8)
4 + 2 + 6, 6 + 4 + 8 10 + 12, 4 + 4
𝑐𝑒𝑛𝑡𝑟𝑜𝑖𝑑 1 = ( ) = (4,6) 𝑐𝑒𝑛𝑡𝑟𝑜𝑖𝑑 2 = ( ) = (11,4)
3 2
There is no change in the cluster Table 1 & 2. It is exactly the same; therefore, the k-means algorithm
terminates with two clusters with data points as shown in the Table 2.
Core Strengths:
• Simplicity and Interpretability: K-Means is easy to understand and implement. The cluster
centroids provide a simple representation of each cluster.
• Efficiency: It is computationally efficient and scales relatively well to large datasets, especially
compared to hierarchical methods, when K is small. Its complexity is approximately O(n⋅K⋅i⋅d),
where n is data points, K clusters, i iterations, d dimensions.
• Effectiveness: Often produces good results for spherical clusters that are well-separated.
• Guaranteed Convergence: The algorithm is guaranteed to converge to a local optimum.
The algorithm iteratively moves centroids and reassigns points to reduce this total sum of squared distances,
aiming for compact and well-separated clusters
• Group similar data points together: Data points within the same cluster should be as similar as
possible to each other.
• Separate dissimilar data points: Data points in different clusters should be as dissimilar as possible.
• Minimize the distance between data points and their cluster centroid: Each cluster is represented
by a "centroid" (its mean), and the algorithm aims to make data points as close as possible to their
assigned cluster's centroid. This effectively makes the clusters compact.
• Maximize the distance between different cluster centroids: This ensures that the clusters are well-
separated and distinct from one another.
Drawback (Curse)
Curse of Dimensionality: This refers to various phenomena that arise when analysing and organizing data
in high-dimensional spaces that do not occur in low-dimensional settings. For K-Means, its impact includes:
• Distance Metric Effectiveness: In high dimensions, the concept of "distance" becomes less
meaningful. The relative difference between the nearest and farthest points becomes less pronounced.
Most points tend to be roughly equidistant from each other, making it hard for algorithms like K-Means
(which rely on distance) to distinguish between similar and dissimilar points.
• Sparsity: Data points become extremely sparse in high-dimensional spaces. There's less "local"
density, making it difficult to form compact clusters.
• Increased Computational Cost: Calculating distances in high dimensions takes more time.
• Increased Noise Sensitivity: Irrelevant dimensions can dominate the distance calculations, masking
the true underlying clusters.
• Requires Pre-defined Number of Clusters (K): This is often the biggest challenge. Determining
the optimal K is subjective and can require external methods (like the Elbow method or Silhouette
analysis).
• Sensitive to Initial Centroid Placement: Can converge to suboptimal local optima depending on
the initial centroid selection. Running multiple initializations is often recommended.
• Struggles with Non-Globular Cluster Shapes: K-Means implicitly assumes clusters are
spherical and equal in size and density. It performs poorly with clusters of irregular shapes (e.g.,
elongated, crescent-shaped) or varying densities, as it tries to find circular boundaries around
centroids.
• Sensitive to Outliers: Outliers can significantly skew the centroid positions, leading to
misleading clusters, as squared distances amplify their effect.
• Requires Numerical Data: Typically works only with numerical data. Categorical or mixed-type
data requires specific encoding or specialized K-Means variants.
Hierarchical clustering
Hierarchical clustering is used to group similar data points together based on their similarity creating
a hierarchy or tree-like structure. The key idea is to begin with each data point as its own separate cluster
and then progressively merge or split them based on their similarity. Let’s understand this with the help of
an example
Imagine you have four fruits with different weights: an apple (100g), a banana (120g), a cherry (50g) and
a grape (30g). Hierarchical clustering starts by treating each fruit as its own group.
• It then merges the closest groups based on their weights.
• First the cherry and grape are grouped together because they are the lightest.
• Next the apple and banana are grouped together.
Finally, all the fruits are merged into one large group, showing how hierarchical clustering progressively
combines the most similar data points.
Hierarchical clustering is an unsupervised machine learning algorithm that groups similar data points into
clusters, forming a hierarchy of clusters rather than a flat partitioning (like K-means). The result of
hierarchical clustering is typically visualized as a dendrogram, a tree-like diagram that illustrates the
arrangement of clusters and the sequence in which they were merged or split.
Dendrogram
A dendrogram is like a family tree for clusters. It shows how individual data points or groups of data
merge together. The bottom shows each data point as its own group, and as you move up, similar groups
are combined. The lower the merge point, the more similar the groups are. It helps you see how things are
grouped step by step. The working of the dendrogram can be explained using the below diagram:
In the above image on the left side there are five points labeled P, Q, R, S and T. These represent individual
data points that are being clustered. On the right side there’s a dendrogram which show how these points
are grouped together step by step.
• At the bottom of the dendrogram the points P, Q, R, S and T are all separate.
• As you move up, the closest points are merged into a single group.
• The lines connecting the points show how they are progressively merged based on similarity.
• The height at which they are connected shows how similar the points are to each other; the shorter
the line the more similar they are.
5. Repeat steps 3 and 4: Keep merging the closest clusters and updating the distance matrix until
you have only one cluster left.
6. Create a dendrogram: As the process continues you can visualize the merging of clusters using a
tree-like diagram called a dendrogram. It shows the hierarchy of how clusters are merged.
Python implementation of the above algorithm using the scikit-learn library:
from [Link] import AgglomerativeClustering
import numpy as np
print(clustering.labels_)
Output :
[1, 1, 1, 0, 0, 0]
This means:
• Points [1,0], [1,2], [1,4] → assigned to cluster 1
• Points [4,0], [4,2], [4,4] → assigned to cluster 0
Silhouette Score
The Silhouette Score is a way to measure how good the clusters are in a dataset. It helps us understand
how well the data points have been grouped. The score ranges from -1 to 1.
• A score close to 1 means a point fits really well in its group (cluster) and is far from other groups.
• A score close to 0 means the point is on the border between two clusters.
• A score close to -1 means the point might be in the wrong cluster.
Silhouette Score (S) for a data point i is calculated as:
𝑏(𝑖) − 𝑎(𝑖)
𝑆(𝑖) =
max( 𝑎(𝑖), 𝑏(𝑖) )
where,
• 𝑎(𝑖) is the average distance from i to other data points in the same cluster.
• 𝑏(𝑖) is the smallest average distance from i to data points in a different cluster.
DIMENSIONALITY REDUCTION
Dimensionality reduction is a fundamental technique in machine learning and data analysis that involves
transforming data from a high-dimensional space into a low-dimensional space while retaining as much
meaningful information as possible. It's crucial for addressing various challenges posed by high-
dimensional datasets.
Dimensionality reduction is a process that simplifies complex dataset by combining similar or correlated
features. It helps in improving analysis and computational efficiency.
Example: when you are building a model to predict house prices with features like bedrooms, square
footage and location. If you add too many features such as room condition or flooring type, the dataset
becomes large and complex.
Why is Dimensionality Reduction Necessary?
• Curse of Dimensionality: As the number of dimensions (features) in a dataset increases, the data
points become increasingly sparse. This makes it difficult for machine learning algorithms to find
meaningful patterns, leading to poor model performance and generalization.
• Computational Efficiency: High-dimensional data requires more computational resources (time
and memory) for storage, processing, and model training. Reducing dimensions can significantly
speed up these processes.
• Overfitting Prevention: Models trained on high-dimensional data are more prone to overfitting,
where they learn the noise in the training data rather than the true underlying patterns.
Dimensionality reduction can help mitigate overfitting by removing redundant or irrelevant features.
• Visualization Challenges: It's practically impossible to visualize data with more than three
dimensions. Dimensionality reduction allows us to project data into 2D or 3D, making it easier to
explore patterns, clusters, and relationships.
• Noise Reduction: High-dimensional datasets often contain noisy or irrelevant features that can
obscure the true signal. Dimensionality reduction can help filter out this noise, improving the data's
signal-to-noise ratio.
• Requires Standardization: As mentioned, data must be standardized before applying PCA, which
adds an extra preprocessing step.
• Information Loss: While PCA aims to minimize information loss, some information is inevitably
lost when reducing dimensions, especially if not enough components are retained.
Applications:
PCA is widely used across various domains:
• Image Compression and Processing: Reducing the number of pixels or features in images while
preserving visual quality.
• Face Recognition (Eigenfaces): Representing faces as a combination of principal components.
• Bioinformatics: Analysing gene expression data, which often has thousands of features.
• Financial Data Analysis: Identifying principal factors driving stock market movements.
• Anomaly Detection: Detecting unusual patterns in high-dimensional data by projecting it into a
lower-dimensional space where anomalies might become more apparent.
• Exploratory Data Analysis: Gaining insights into the structure and relationships within complex
datasets.
In essence, PCA is a powerful and versatile tool for simplifying complex datasets, making them more
manageable for analysis, visualization, and machine learning tasks.
For example we have two classes that need to be separated efficiently. Each class may have multiple features
and using a single feature to classify them may result in overlapping. To solve this LDA is used as it uses
multiple features to improve classification accuracy. LDA works by some assumptions and we are
required to understand them so that we have a better understanding of its working.
such a way that it maximizes the distance between the means of the two classes while minimizing the
variation within each class. This transforms the dataset into a space where the classes are better separated.
After transforming the data points along a new axis LDA maximizes the class separation. This new axis
allows for clearer classification by projecting the data along a line that enhance the distance between the
means of the two classes.
Perpendicular distance between the decision boundary and the data points helps us to visualize how LDA
works by reducing class variation and increasing separability.
After generating this new axis using the above-mentioned criteria all the data points of the classes are
plotted on this new axis and are shown in the figure given below.
It shows how LDA creates a new axis to project the data and separate the two classes effectively along a
linear path. But it fails when the mean of the distributions are shared as it becomes impossible for LDA to
find a new axis that makes both classes linearly separable. In such cases we use non-linear discriminant
analysis.
Advantages of LDA
• Simple and computationally efficient.
• Works well even when the number of features is much larger than the number of training samples.
• Can handle multicollinearity.
Disadvantages of LDA
• Assumes Gaussian distribution of data which may not always be the case.
• Assumes equal covariance matrices for different classes which may not hold in all datasets.
• Assumes linear separability which is not always true.
• May not always perform well in high-dimensional feature spaces.
Applications of LDA
1. Face Recognition: It is used to reduce the high-dimensional feature space of pixel values in face
recognition applications helping to identify faces more efficiently.
2. Medical Diagnosis: It classifies disease severity in mild, moderate or severe based on patient
parameters helping in decision-making for treatment.
3. Customer Identification: It can help identify customer segments most likely to purchase a specific
product based on survey data.
points are mapped to nearby points in the low-dimensional space, and dissimilar points are mapped
far apart.
• For each data point, t-SNE calculates the probability that other data points are its neighbors. This
is done using a Gaussian distribution, where points closer to the central point have a higher
probability of being considered neighbors.
• The "perplexity" parameter plays a crucial role here. It can be thought of as a smooth measure of
the effective number of neighbors each point has. A higher perplexity value considers more
neighbors and balances attention between local and global aspects of the data.
2. Creating a Low-Dimensional Embedding:
• t-SNE then creates a similar probability distribution over the points in a low-dimensional map
(e.g., 2D).
• It uses a Student's t-distribution (which has heavier tails than a Gaussian) to measure similarities
in the low-dimensional space. The heavy tails help to alleviate the "crowding problem" (where
distant points in high-dimensional space might be squeezed together in low-dimensional space).
• The algorithm then iteratively adjusts the positions of the points in the low-dimensional space to
minimize the difference (Kullback-Leibler divergence) between the two probability distributions
(high-dimensional and low-dimensional). This optimization is typically performed using gradient
descent.
• The goal is to ensure that similar objects in high-dimensional space are modelled by nearby points
in the low-dimensional map, and dissimilar objects are modelled by distant points.
Advantages of t-SNE
• Great for Visualization: t-SNE is particularly used to convert complex high-dimensional data into
2D or 3D for visualization making patterns and clusters easy to observe.
• Preserve Local Structure: Unlike linear techniques like PCA t-SNE focus on maintaining the local
relationships between data points meaning similar data points remain close in the lower-dimensional
space.
• Non-Linear Capability: It captures non-linear dependencies in the data which makes it suitable for
complex datasets where linear methods fail.
• Cluster Separation: Helps in clearly visualizing clusters and class separability in datasets like
MNIST making it easier for interpretation and exploration.
Disadvantages of t-SNE
• Computationally Intensive: t-SNE is slower and more computationally expensive compared to
linear methods especially on large datasets.
• Non-deterministic Output: The output can vary with each run due to its randomness unless a fixed
random_state is used.
• Not Scalable for Large Datasets: It struggles with very large datasets (e.g., millions of points)
unless optimized or approximated versions are used.
• No Global Structure Preservation: It keeps nearby things close together but might make far-apart
things seem weirdly spaced.
Association Rule Learning is an unsupervised machine learning technique used for discovering interesting
relationships and patterns between variables in large datasets. It's a type of unsupervised learning that aims
to find patterns and correlations within data. It's particularly powerful for identifying "if-then" relationships,
often expressed as rules of the form X→Y, where X and Y are disjoint sets of items. This means that if a
customer buys items in set X, they are likely to also buy items in set Y
The primary goal is to find strong rules that reveal how items frequently co-occur in transactions or
observations.
How it Works:
Association rule learning typically involves three main stages:
1. Identifying Frequent Itemsets: This first step involves finding all combinations of items (itemsets)
that appear together in a dataset with a frequency above a predefined threshold. This threshold is
called Support.
• Support: For an itemset, support is the proportion of transactions in the dataset that contain
that itemset. A higher support means the itemset appears more frequently.
2. Generating Association Rules: Once the frequent itemsets are identified, association rules are
generated from these itemsets. To determine the "strength" or "interestingness" of these rules,
additional metrics are used:
3. Evaluate Rule Strength: Metrics like confidence and lift are used to evaluate the reliability and
strength of the generated rules.
• Confidence: For a rule X→Y, confidence measures the reliability of the inference. It's the
conditional probability that Y will be in a transaction, given that X is already in the transaction.
In simpler terms, it tells you how often itemset Y is purchased when itemset X is also
purchased.
• Lift: Lift measures how much more likely the consequent item (Y) is to be purchased when
the antecedent item (X) is present, compared to its individual occurrence rate.
▪ A lift value of 1 indicates that X and Y are independent.
▪ A lift value greater than 1 suggests a positive association (the presence of X increases the
likelihood of Y).
▪ A lift value less than 1 indicates a negative association (the presence of X decreases the
likelihood of Y).
Key Algorithms:
Several algorithms are used to efficiently find association rules, most important is:
• Apriori Algorithm: This is one of the most well-known algorithms. It uses an iterative, breadth-
first search approach. Its core principle (Apriori property) is that if an itemset is frequent, then all
of its subsets must also be frequent. This helps to prune the search space and reduce computational
cost.
Applications of Association Rule Learning:
Association Rule Learning has a wide range of practical applications across various domains:
• Market Basket Analysis: This is the most classic application. Retailers use it to understand
customer purchasing habits by identifying which products are frequently bought together (e.g.,
"customers who buy bread and butter also tend to buy milk"). This information is used for:
• Web Usage Mining (Clickstream Analysis): Analysing user navigation patterns on websites to
improve website design, personalize content, and make recommendations.
• Healthcare and Medical Diagnosis: Identifying co-occurrence patterns in symptoms, diseases, and
treatments to aid in diagnosis, predict complications, and understand drug interactions.
• Fraud Detection: Detecting unusual patterns in transactions (e.g., credit card fraud) that deviate
from established norms.
• Recommendation Systems: Suggesting items to users based on their past behaviour or the
behaviour of similar users (e.g., "people who watched this movie also watched...").
• Bioinformatics: Discovering relationships between genes, proteins, or other biological entities.
• Customer Segmentation: Grouping customers based on their purchasing habits to tailor marketing
campaigns.
Apriori algorithm
Apriori Algorithm is a basic method used in data analysis to find groups of items that often appear together
in large sets of data. It helps to discover useful patterns or rules about how items are related which is
particularly valuable in market basket analysis.
Like in a grocery store if many customers buy bread and butter together, the store can use this information
to place these items closer or create special offers. This helps the store sell more and make customers happy.
• Because of this, the algorithm does not check those larger groups. This way it avoids wasting time
looking at groups that won’t be important make the whole process faster.
4. Generating Association Rules
• The algorithm makes rules to show how items are related.
• It checks these rules using support, confidence and lift to find the strongest ones.
Example:
Let’s understand the concept of Apriori Algorithm with the help of an example. Consider the following
dataset and we will find frequent itemsets and generate association rules for them:
Frequent 1-Itemsets
All items have support% ≥ 50%, so they qualify as frequent 1-itemsets. if any item has support% < 50%, It
will be omitted out from the frequent 1- itemsets.
Step 3: Generate Candidate 2-Itemsets
Combine the frequent 1-itemsets into pairs and calculate their support. For this use case we will get 3 item
pairs ( bread,butter) , (bread,ilk) and (butter,milk) and will calculate the support similar to step 2
Candidate 2-Itemsets
Frequent 2-itemsets: {Bread, Milk} meet the 50% threshold but {Butter, Milk} and {Bread ,Butter}
doesn't meet the threshold, so will be committed out.
Step 4: Generate Candidate 3-Itemsets
Combine the frequent 2-itemsets into groups of 3 and calculate their support. for the triplet we have only
got one case i.e {bread,butter,milk} and we will calculate the support.
Candidate 3-Itemsets
Since this does not meet the 50% threshold, there are no frequent 3-itemsets.
Step 5: Generate Association Rules
Now we generate rules from the frequent itemsets and calculate confidence.
Rule 1: If Bread → Butter (if customer buys bread, the customer will buy butter also)
Advantages of Apriori:
• Simplicity: Easy to understand and implement.
• Well-defined: The process is systematic and clear.
• Foundation: Forms the basis for many other association rule mining algorithms.
Disadvantages of Apriori:
• High Computational Cost: Can be very slow and computationally expensive when dealing with
large datasets and a large number of unique items, as it generates a huge number of candidate
itemsets.
• Multiple Scans of Database: Requires multiple passes over the database to count the support of
candidate itemsets, which can be inefficient.
• Memory Intensive: Storing candidate itemsets can consume a lot of memory.
Example:
• Data mining concepts are in use for Sales and marketing to provide better customer service, to
improve cross-selling opportunities, to increase direct mail response rates.
• Customer Retention in the form of pattern identification and prediction of likely defections is
possible by Data mining.
• Risk Assessment and Fraud area also use the data-mining concept for identifying inappropriate or
unusual behaviour etc.
Market basket analysis mainly works with the ASSOCIATION RULE {IF} -> {THEN}.
• IF means Antecedent: An antecedent is an item found within the data
• THEN means Consequent: A consequent is an item found in combination with the antecedent.
Let's see ASSOCIATION RULE {IF} -> {THEN} rules used in Market Basket Analysis in Data Mining.
For example, customers buying a domain means they definitely need extra plugins/extensions to make it
easier for the users.
Like we said above Antecedent is the item sets that are available in data. By formulating from the rules
means {if} component and from the example is the domain.
Same as Consequent is the item that is found with the combination of Antecedents. By formulating from
the rules means {THEN} component and from the example is extra plugins/extensions.
With the help of these, we are able to predict customer behavioural patterns. From this, we are able to make
certain combinations with offers that customers will probably buy those products. That will automatically
increase the sales and revenue of the company.
With the help of the Apriori Algorithm, we can further classify and simplify the item sets which are
frequently bought by the consumer.
There are three components in APRIORI ALGORITHM:
• SUPPORT
• CONFIDENCE
• LIFT
Now take an example, suppose 5000 transactions have been made through a popular eCommerce website.
Now they want to calculate the support, confidence, and lift for the two products, let's say pen and notebook
for example out of 5000 transactions, 500 transactions for pen, 700 transactions for notebook, and 1000
transactions for both.
SUPPORT: It is been calculated with the number of transactions divided by the total number of transactions
made,
CONFIDENCE: It is been calculated for whether the product sales are popular on individual sales or
through combined sales. That is calculated with combined transactions/individual transactions.
Confidence=freq(A,B)/freq(A)Confidence=freq(A,B)/freq(A)
Confidence = combine transactions/individual transactions
i.e confidence-> 1000/500=20 percent
LIFT: Lift is calculated for knowing the ratio for the sales.
Lift=confidencepercent/supportpercentLift=confidencepercent/supportpercent
Lift-> 20/10=2
When the Lift value is below 1 means the combination is not so frequently bought by consumers. But in
this case, it shows that the probability of buying both the things together is high when compared to the
transaction for the individual items sold.
With this, we come to an overall view of the Market Basket Analysis in Data Mining and how to calculate
the sales for combination products.
Types of Market Basket Analysis
There are three types of Market Basket Analysis. They are as follow:
1. Descriptive market basket analysis: This sort of analysis looks for patterns and connections in the
data that exist between the components of a market basket. This kind of study is mostly used to
understand consumer behavior, including what products are purchased in combination and what the
most typical item combinations. Retailers can place products in their stores more profitably by
understanding which products are frequently bought together with the aid of descriptive market
basket analysis.
2. Predictive Market Basket Analysis: Market basket analysis that predicts future purchases based
on past purchasing patterns is known as predictive market basket analysis. Large volumes of data
are analysed using machine learning algorithms in this sort of analysis in order to create predictions
about which products are most likely to be bought together in the future. Retailers may make data-
driven decisions about which products to carry, how to price them, and how to optimize shop layouts
with the use of predictive market basket research.
3. Differential Market Basket Analysis: Differential market basket analysis analyses two sets of
market basket data to identify variations between them. Comparing the behavior of various client
segments or the behavior of customers over time is a common usage for this kind of study. Retailers
can respond to shifting consumer behavior by modifying their marketing and sales tactics with the
help of differential market basket analysis.
Benefits of Market Basket Analysis
1. Enhanced Customer Understanding: Market basket research offers insights into customer
behavior, including what products they buy together and which products they buy the most
frequently. Retailers can use this information to better understand their customers and make
informed decisions.
2. Improved Inventory Management: By examining market basket data, retailers can determine
which products are sluggish sellers and which ones are commonly bought together. Retailers can
use this information to make well-informed choices about what products to stock and how to manage
their inventory most effectively.
3. Better Pricing Strategies: A better understanding of the connection between product prices and
consumer behavior might help merchants develop better pricing strategies. Using this knowledge,
pricing plans that boost sales and profitability can be created.
4. Sales Growth: Market basket analysis can assist businesses in determining which products are most
frequently bought together and where they should be positioned in the store to grow sales. Retailers
may boost revenue and enhance customer shopping experiences by improving store layouts and
product positioning.
Applications of Market Basket Analysis
1. Retail: Market basket research is frequently used in the retail sector to examine consumer buying
patterns and inform decisions about product placement, inventory management, and pricing tactics.
Retailers can utilize market basket research to identify which items are sluggish sellers and which
ones are commonly bought together, and then modify their inventory management strategy
accordingly.
2. E-commerce: Market basket analysis can help online merchants better understand the customer
buying habits and make data-driven decisions about product recommendations and targeted
advertising campaigns. The behaviour of visitors to a website can be examined using market basket
analysis to pinpoint problem areas.
3. Finance: Market basket analysis can be used to evaluate investor behaviour and forecast the types
of investment items that investors will likely buy in the future. The performance of investment
portfolios can be enhanced by using this information to create tailored investment strategies.
4. Telecommunications: To evaluate consumer behaviour and make data-driven decisions about
which goods and services to provide, the telecommunications business might employ market basket
analysis. The usage of this data can enhance client happiness and the shopping experience.
5. Manufacturing: To evaluate consumer behaviour and make data-driven decisions about which
products to produce and which materials to employ in the production process, the manufacturing
sector might use market basket analysis. Utilizing this knowledge will increase effectiveness and
cut costs.
• Interpretation:
• High support: Indicates that the itemset (A∪B) appears frequently in the dataset. It means
the rule is applicable to a substantial portion of the transactions.
• Low support: The itemset is rare. Rules with very low support might be statistically
significant but may not be practically actionable due to infrequent occurrence.
• Purpose: Primarily used to identify frequent itemsets. A minimum support threshold (min_sup) is
set to filter out infrequent items and itemsets, reducing the search space for rules.
2. Confidence
• Definition: The conditional probability that a transaction contains the consequent (B), given that it
already contains the antecedent (A).
• Interpretation:
o High confidence: Suggests a strong likelihood that if a customer buys A, they will also buy
B. It measures the reliability of the rule.
o Low confidence: The occurrence of A does not strongly imply the occurrence of B.
• Purpose: To assess the reliability or predictive power of an association rule. A minimum confidence
threshold (min_conf) is set to filter out unreliable rules.
3. Lift
• Definition: Measures how much more likely the consequent (B) is to be purchased when the
antecedent (A) is purchased, compared to the general likelihood of purchasing B. It compares the
observed support of A∪B with the expected support if A and B were statistically independent.
• Interpretation:
• Lift = 1: A and B are independent. The occurrence of A has no impact on the occurrence of
B.
• Lift > 1: Positive association. A and B occur together more often than expected by chance.
The higher the lift, the stronger the positive association.
• Lift < 1: Negative association. A and B occur together less often than expected by chance.
The presence of A decreases the likelihood of B.
• Purpose: To determine the true strength and significance of the association, beyond just co-
occurrence. It helps filter out rules that might have high support and confidence simply because the
individual items are very popular.
Considerations for Evaluation:
• Thresholds: The interpretation of these metrics heavily depends on the chosen minimum support
and confidence thresholds. What's "high" or "low" is relative to the domain and dataset.
• Domain Knowledge: Statistical significance doesn't always equate to business usefulness. Domain
experts are crucial for interpreting rules and identifying truly actionable insights.
• Redundancy: A large number of rules can be generated. Pruning redundant or very similar rules is
important for focusing on the most valuable ones.
• Actionability: The ultimate goal of MBA is to find actionable insights. A rule might have high
metrics but be impractical to implement. Conversely, a rule with slightly lower metrics might be
highly actionable.
By using a combination of these evaluation metrics, analysts can gain a comprehensive understanding of
the strength, reliability, and interestingness of the association rules discovered, leading to more effective
business strategies.
In brief,
Rules are evaluated using:
• Lift: How much more likely B is bought when A is bought compared to random chance
𝑆𝑢𝑝𝑝𝑜𝑟𝑡(𝐴 𝑎𝑛𝑑 𝐵)
𝐿𝑖𝑓𝑡 (𝐴 → 𝐵) =
𝑆𝑢𝑝𝑝𝑜𝑟𝑡(𝐴) × 𝑆𝑢𝑝𝑝𝑜𝑟𝑡(𝐵)