Tuesday, September 8, 2026

TYBCS - DS & DA Assignment 6

Assignment No-6
Unsupervised Machine Learning Models for Clustering (K-means clustering) and Association Rule Mining (Apriori Algorithm) 

 

Lab Assignment
SET A:
1. Generate a dataset using built-in libraries. Plot the data points and observe their distribution. Apply the K-Means clustering algorithm with a predefined number of clusters (K=3 or K=4). Visualize the clustered data along with cluster centroids.

# --------------------------------------------------
# 1. Import Required Libraries
# --------------------------------------------------

import numpy as np
import matplotlib.pyplot as plt

from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans


# --------------------------------------------------
# 2. Generate Dataset using Built-in Library
# --------------------------------------------------

X, y = make_blobs(
    n_samples=300,
    centers=3,
    n_features=2,
    cluster_std=1.2,
    random_state=42
)


# --------------------------------------------------
# 3. Display Dataset Information
# --------------------------------------------------

print("Dataset Shape:")
print(X.shape)

print("\nFirst 10 Data Points:")
print(X[:10])


# --------------------------------------------------
# 4. Plot Original Data Distribution
# --------------------------------------------------

plt.figure(figsize=(8, 6))

plt.scatter(
    X[:, 0],
    X[:, 1]
)

plt.xlabel("Feature 1")
plt.ylabel("Feature 2")

plt.title("Original Data Distribution")

plt.show()


# --------------------------------------------------
# 5. Apply K-Means Clustering
# --------------------------------------------------

kmeans = KMeans(
    n_clusters=3,
    random_state=42,
    n_init=10
)

kmeans.fit(X)


# --------------------------------------------------
# 6. Get Cluster Labels
# --------------------------------------------------

labels = kmeans.labels_


# --------------------------------------------------
# 7. Get Cluster Centroids
# --------------------------------------------------

centroids = kmeans.cluster_centers_

print("\nCluster Centroids:")
print(centroids)


# --------------------------------------------------
# 8. Display Cluster Labels
# --------------------------------------------------

print("\nFirst 20 Cluster Labels:")
print(labels[:20])


# --------------------------------------------------
# 9. Visualize Clusters and Centroids
# --------------------------------------------------

plt.figure(figsize=(8, 6))

plt.scatter(
    X[:, 0],
    X[:, 1],
    c=labels
)

plt.scatter(
    centroids[:, 0],
    centroids[:, 1],
    marker='X',
    s=200,
    label='Centroids'
)

plt.xlabel("Feature 1")
plt.ylabel("Feature 2")

plt.title("K-Means Clustering (K=3)")

plt.legend()

plt.show()


# --------------------------------------------------
# 10. Calculate Inertia
# --------------------------------------------------

print("\nInertia:")
print(kmeans.inertia_)


2. Create the following dataset in python
TID         Items
1            Python, DBMS
2            Python, Java, AI
3            DBMS, Data Science
4            Python, DBMS, Java
5            AI, Data Science
6            Python, AI
7            Java, DBMS
8            Python, Data Science
9            Python, Java, DBMS, AI
10            DBMS, Data Science, AI
Create the following dataset in Python. Apply the Apriori algorithm to generate frequent itemsets and association rules. Repeat the process with different minimum support values (0.2, 0.3, 0.4, and 0.5).

1. Create the Dataset

import pandas as pd
from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import apriori, association_rules

# Transaction dataset
transactions = [
    ['Python', 'DBMS'],
    ['Python', 'Java', 'AI'],
    ['DBMS', 'Data Science'],
    ['Python', 'DBMS', 'Java'],
    ['AI', 'Data Science'],
    ['Python', 'AI'],
    ['Java', 'DBMS'],
    ['Python', 'Data Science'],
    ['Python', 'Java', 'DBMS', 'AI'],
    ['DBMS', 'Data Science', 'AI']
]

# Display original transactions
print("Transaction Dataset:")
for i, transaction in enumerate(transactions, start=1):
    print(i, ":", ", ".join(transaction))


# Convert transactions into one-hot encoded format
te = TransactionEncoder()
te_array = te.fit(transactions).transform(transactions)

df = pd.DataFrame(te_array, columns=te.columns_)

print("\nOne-Hot Encoded Dataset:")
print(df)

2. Apply Apriori for Different Minimum Support Values

# Different minimum support values
support_values = [0.2, 0.3, 0.4, 0.5]

for min_support in support_values:

    print("\n" + "=" * 70)
    print("Minimum Support =", min_support)
    print("=" * 70)

    # Generate frequent itemsets
    frequent_itemsets = apriori(
        df,
        min_support=min_support,
        use_colnames=True
    )

    print("\nFrequent Itemsets:")
    
    if frequent_itemsets.empty:
        print("No frequent itemsets found.")
    else:
        # Add itemset length
        frequent_itemsets['Length'] = frequent_itemsets['itemsets'].apply(len)
        
        print(frequent_itemsets.to_string(index=False))

        # Generate association rules
        if len(frequent_itemsets) > 1:
            rules = association_rules(
                frequent_itemsets,
                metric="confidence",
                min_threshold=0.5
            )

            print("\nAssociation Rules (Confidence >= 0.5):")

            if rules.empty:
                print("No association rules found.")
            else:
                result = rules[
                    ['antecedents', 'consequents',
                     'support', 'confidence', 'lift']
                ].copy()

                # Convert frozenset to normal string
                result['antecedents'] = result['antecedents'].apply(
                    lambda x: ', '.join(sorted(x))
                )

                result['consequents'] = result['consequents'].apply(
                    lambda x: ', '.join(sorted(x))
                )

                print(result.to_string(index=False))
        else:
            print("\nNot enough frequent itemsets to generate rules.")

Below are the exact frequent itemsets and association rules for the given 10 transactions, assuming the same setting as the previous program:

  • Minimum support: 0.2, 0.3, 0.4, 0.5
  • Minimum confidence for rules: 0.5
  • Support is shown as both decimal and percentage where useful.

1. Minimum Support = 0.2

Since there are 10 transactions, support 0.2 means an itemset must occur in at least 2 transactions.

Frequent Itemsets

ItemsetSupportSupport %
{AI}0.5050%
{DBMS}0.6060%
{Data Science}0.4040%
{Java}0.4040%
{Python}0.6060%
{AI, DBMS}0.2020%
{AI, Data Science}0.2020%
{AI, Java}0.2020%
{AI, Python}0.3030%
{DBMS, Data Science}0.2020%
{DBMS, Java}0.3030%
{DBMS, Python}0.3030%
{Java, Python}0.3030%
{AI, Java, Python}0.2020%
{DBMS, Java, Python}0.2020%

Association Rules — Confidence ≥ 0.5

#RuleSupportConfidenceLift
1Data Science → AI0.200.501.00
2Java → AI0.200.501.00
3AI → Python0.300.601.00
4Python → AI0.300.501.00
5Data Science → DBMS0.200.500.83
6DBMS → Java0.300.501.25
7Java → DBMS0.300.751.25
8DBMS → Python0.300.500.83
9Python → DBMS0.300.500.83
10Java → Python0.300.751.25
11Python → Java0.300.501.25
12Java → AI, Python0.200.501.67
13AI, Java → Python0.201.001.67
14AI, Python → Java0.200.671.67
15Java, Python → AI0.200.671.33
16Java → DBMS, Python0.200.501.67
17DBMS, Java → Python0.200.671.11
18DBMS, Python → Java0.200.671.67
19Java, Python → DBMS0.200.671.11

2. Minimum Support = 0.3

Here an itemset must occur in at least 3 out of 10 transactions.

Frequent Itemsets

ItemsetSupportSupport %
{AI}0.5050%
{DBMS}0.6060%
{Data Science}0.4040%
{Java}0.4040%
{Python}0.6060%
{AI, Python}0.3030%
{DBMS, Java}0.3030%
{DBMS, Python}0.3030%
{Java, Python}0.3030%

Association Rules — Confidence ≥ 0.5

#RuleSupportConfidenceLift
1AI → Python0.300.601.00
2Python → AI0.300.501.00
3DBMS → Java0.300.501.25
4Java → DBMS0.300.751.25
5DBMS → Python0.300.500.83
6Python → DBMS0.300.500.83
7Java → Python0.300.751.25
8Python → Java0.300.501.25

3. Minimum Support = 0.4

Here an itemset must occur in at least 4 transactions.

Frequent Itemsets

ItemsetSupportSupport %
{AI}0.5050%
{DBMS}0.6060%
{Data Science}0.4040%
{Java}0.4040%
{Python}0.6060%

Association Rules

There are no association rules with confidence ≥ 0.5 because there is no frequent 2-itemset at support 0.4.

ResultValue
Frequent 1-itemsets5
Frequent 2-itemsets0
Association Rules0

4. Minimum Support = 0.5

Here an itemset must occur in at least 5 transactions.

Frequent Itemsets

ItemsetSupportSupport %
{AI}0.5050%
{DBMS}0.6060%
{Python}0.6060%

Association Rules

Again, there are no association rules, because no 2-itemset has support ≥ 0.5.

ResultValue
Frequent 1-itemsets3
Frequent 2-itemsets0
Association Rules0

Overall Comparison

Minimum SupportFrequent 1-itemsetsFrequent 2-itemsetsFrequent 3-itemsetsAssociation Rules
0.257219
0.35408
0.45000
0.53000

Important Observation

As minimum support increases, the number of frequent itemsets decreases:

0.2 → 0.3 → 0.4 → 0.5

Therefore:

  • At 0.2, we get the maximum number of patterns and rules.
  • At 0.3, several less-frequent combinations are eliminated.
  • At 0.4, only individual subjects remain frequent.
  • At 0.5, only AI, DBMS and Python remain frequent.
  • No association rules are possible at 0.4 and 0.5 with the chosen support/confidence thresholds.

 

 

3. Consider a dataset containing students' attendance percentage and examination marks. Apply the K-Means Clustering algorithm to group students into clusters based on their academic performance. Visualize the clusters using a scatter plot, identify the cluster centroids, and interpret the characteristics of each cluster.
. Apply the Apriori algorithm to discover frequently used learning resources and generate association rules among them.
TID        Items
1            Video Lecture, Quiz
2            Video Lecture, Assignment
3            Quiz, Discussion Forum
4            Video Lecture, Quiz, Assignment
5            Assignment, Discussion Forum
6            Video Lecture, Certificate
7            Quiz, Certificate
8            Video Lecture, Quiz
9            Assignment, Certificate
10         Video Lecture, Quiz, Certificate
 

 

Dataset

We consider the following student dataset:

StudentAttendance (%)Exam Marks
S15545
S26050
S35848
S46555
S56860
S67265
S77570
S87872
S98075
S108278
S118885
S129088
S139290
S149594
S159796

Complete Python Program

import pandas as pd
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans

# Create student dataset
data = {
    'Student': ['S1', 'S2', 'S3', 'S4', 'S5',
                'S6', 'S7', 'S8', 'S9', 'S10',
                'S11', 'S12', 'S13', 'S14', 'S15'],

    'Attendance': [55, 60, 58, 65, 68,
                   72, 75, 78, 80, 82,
                   88, 90, 92, 95, 97],

    'Exam_Marks': [45, 50, 48, 55, 60,
                   65, 70, 72, 75, 78,
                   85, 88, 90, 94, 96]
}

df = pd.DataFrame(data)

print("Student Dataset:")
print(df)

# Select features
X = df[['Attendance', 'Exam_Marks']]

# Apply K-Means
kmeans = KMeans(
    n_clusters=3,
    random_state=42,
    n_init=10
)

df['Cluster'] = kmeans.fit_predict(X)

# Get cluster centroids
centroids = kmeans.cluster_centers_

print("\nStudent Clusters:")
print(df)

print("\nCluster Centroids:")
for i, centroid in enumerate(centroids):
    print(
        f"Cluster {i}: "
        f"Attendance = {centroid[0]:.2f}%, "
        f"Exam Marks = {centroid[1]:.2f}"
    )

# Plot clusters
plt.figure(figsize=(8, 6))

plt.scatter(
    df['Attendance'],
    df['Exam_Marks'],
    c=df['Cluster'],
    s=100
)

# Plot centroids
plt.scatter(
    centroids[:, 0],
    centroids[:, 1],
    marker='X',
    s=250,
    label='Centroids'
)

# Add student labels
for i, student in enumerate(df['Student']):
    plt.annotate(
        student,
        (df['Attendance'][i], df['Exam_Marks'][i]),
        xytext=(5, 5),
        textcoords='offset points'
    )

plt.xlabel("Attendance Percentage")
plt.ylabel("Examination Marks")
plt.title("K-Means Clustering of Students")
plt.legend()
plt.grid(True)
plt.show()

Exact Cluster Centroids

With the above dataset and:

n_clusters = 3
random_state = 42
n_init = 10

the centroids are:

ClusterAverage AttendanceAverage Exam MarksInterpretation
Cluster 092.40%90.60High-performing students
Cluster 161.20%51.60Low-performing students
Cluster 277.40%72.00Average/Moderate-performing students

Cluster-wise Students

ClusterStudentsCharacteristics
Cluster 0S11, S12, S13, S14, S15High attendance and high marks
Cluster 1S1, S2, S3, S4, S5Low attendance and comparatively low marks
Cluster 2S6, S7, S8, S9, S10Moderate-to-high attendance and marks

Complete Python Program

import pandas as pd
from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import apriori, association_rules

# Create transaction dataset
transactions = [
    ['Video Lecture', 'Quiz'],
    ['Video Lecture', 'Assignment'],
    ['Quiz', 'Discussion Forum'],
    ['Video Lecture', 'Quiz', 'Assignment'],
    ['Assignment', 'Discussion Forum'],
    ['Video Lecture', 'Certificate'],
    ['Quiz', 'Certificate'],
    ['Video Lecture', 'Quiz'],
    ['Assignment', 'Certificate'],
    ['Video Lecture', 'Quiz', 'Certificate']
]

# Display original dataset
print("Learning Resource Transactions:")
for i, transaction in enumerate(transactions, start=1):
    print(i, ":", ", ".join(transaction))

# Convert transactions into one-hot encoded format
te = TransactionEncoder()
te_array = te.fit(transactions).transform(transactions)

df = pd.DataFrame(
    te_array,
    columns=te.columns_
)

print("\nOne-Hot Encoded Dataset:")
print(df)

# Minimum support
min_support = 0.2

# Generate frequent itemsets
frequent_itemsets = apriori(
    df,
    min_support=min_support,
    use_colnames=True
)

print("\nFrequent Itemsets:")
print(frequent_itemsets.to_string(index=False))

# Generate association rules
rules = association_rules(
    frequent_itemsets,
    metric="confidence",
    min_threshold=0.5
)

print("\nAssociation Rules:")
if rules.empty:
    print("No association rules found.")
else:
    result = rules[
        ['antecedents',
         'consequents',
         'support',
         'confidence',
         'lift']
    ].copy()

    result['antecedents'] = result['antecedents'].apply(
        lambda x: ', '.join(sorted(x))
    )

    result['consequents'] = result['consequents'].apply(
        lambda x: ', '.join(sorted(x))
    )

    print(result.to_string(index=False))

Frequent Itemsets at Minimum Support = 0.2

There are 10 transactions, so minimum support 0.2 means an itemset must appear in at least 2 transactions.

1-Itemsets

ItemsetSupportPercentage
{Assignment}0.4040%
{Certificate}0.4040%
{Discussion Forum}0.2020%
{Quiz}0.6060%
{Video Lecture}0.6060%

2-Itemsets

ItemsetSupportPercentage
{Assignment, Certificate}0.2020%
{Assignment, Discussion Forum}0.2020%
{Assignment, Video Lecture}0.2020%
{Certificate, Quiz}0.2020%
{Certificate, Video Lecture}0.2020%
{Quiz, Video Lecture}0.4040%

3-Itemsets

ItemsetSupportPercentage
{Certificate, Quiz, Video Lecture}0.1010%

This does not qualify because its support is below 0.2.

Therefore, the frequent itemsets are the 5 one-itemsets + 6 two-itemsets = 11 frequent itemsets.

 

SET B:
1. Download a real-world Customer Segmentation dataset (Mall Customers dataset). Preprocess the data by handling missing values and performing feature scaling. Apply the K-Means clustering algorithm to segment customers based on their annual income and spending score.


 

 

2. Download a Hotel Booking dataset. Perform preprocessing by handling missing values and selecting relevant numerical features such as booking duration, number of guests, and stay cost. Apply the K-Means clustering algorithm to group bookings into different categories.


3. Download the groceries dataset. Write a python program to read the dataset and display its information. Preprocess the data (drop null values etc.) Convert the categorical values into numeric  format. Apply the apriori algorithm on the above dataset to generate the frequent itemsets and association rules.
 

 

SET C:
1. Collect a stock market dataset containing features such as price, volume, and volatility. Preprocess the data by handling missing values and normalizing the features. Apply the K-Means clustering algorithm to group stocks based on performance patterns. Additionally, apply the Apriori algorithm to discover frequent patterns and relationships among stock attributes.

No comments:

Post a Comment