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.

Wednesday, September 2, 2026

TYBCS- DS & DA Assignment 5

 Assignment 5
Supervised Machine Learning Models for Classification (KNN, Random Forest)

 Lab Assignment
SET A:
1. Implement the K-Nearest Neighbors (KNN) algorithm on the Wine dataset. Perform data preprocessing, model training, testing, and performance evaluation. Compare the results for different values of K.

pip install pandas numpy matplotlib seaborn scikit-learn

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import (
    accuracy_score,
    precision_score,
    recall_score,
    f1_score,
    confusion_matrix,
    classification_report
)

# =========================================================
# STEP 1: LOAD WINE DATASET
# =========================================================

wine = load_wine()

# Convert dataset into DataFrame
df = pd.DataFrame(
    wine.data,
    columns=wine.feature_names
)

# Add target column
df["Target"] = wine.target

print("========== WINE DATASET ==========")
print(df.head())

# =========================================================
# STEP 2: DATASET INFORMATION
# =========================================================

print("\n========== DATASET INFORMATION ==========")
df.info()

print("\n========== DATASET SHAPE ==========")
print(df.shape)

print("\n========== STATISTICAL DESCRIPTION ==========")
print(df.describe())

print("\n========== MISSING VALUES ==========")
print(df.isnull().sum())

print("\n========== CLASS DISTRIBUTION ==========")
print(df["Target"].value_counts())

# =========================================================
# STEP 3: DEFINE FEATURES AND TARGET
# =========================================================

X = df.drop("Target", axis=1)
y = df["Target"]

print("\nNumber of Features:", X.shape[1])
print("Number of Classes:", len(np.unique(y)))

# =========================================================
# STEP 4: SPLIT DATA INTO TRAINING AND TESTING
# =========================================================

X_train, X_test, y_train, y_test = train_test_split(
    X,
    y,
    test_size=0.20,
    random_state=42,
    stratify=y
)

print("\n========== DATA SPLIT ==========")
print("Training Records:", len(X_train))
print("Testing Records :", len(X_test))

# =========================================================
# STEP 5: FEATURE SCALING
# =========================================================

scaler = StandardScaler()

X_train_scaled = scaler.fit_transform(X_train)

X_test_scaled = scaler.transform(X_test)

print("\nFeature scaling completed.")

# =========================================================
# STEP 6: APPLY KNN FOR DIFFERENT VALUES OF K
# =========================================================

k_values = [1, 3, 5, 7, 9, 11, 13, 15]

results = []

print("\n========== KNN PERFORMANCE ==========")

for k in k_values:

    # Create KNN model
    knn = KNeighborsClassifier(
        n_neighbors=k
    )

    # Train model
    knn.fit(
        X_train_scaled,
        y_train
    )

    # Predict test data
    y_pred = knn.predict(
        X_test_scaled
    )

    # Calculate metrics
    accuracy = accuracy_score(
        y_test,
        y_pred
    )

    precision = precision_score(
        y_test,
        y_pred,
        average="weighted",
        zero_division=0
    )

    recall = recall_score(
        y_test,
        y_pred,
        average="weighted",
        zero_division=0
    )

    f1 = f1_score(
        y_test,
        y_pred,
        average="weighted",
        zero_division=0
    )

    results.append({
        "K": k,
        "Accuracy": accuracy,
        "Precision": precision,
        "Recall": recall,
        "F1-Score": f1
    })

    print(
        "K =", k,
        "| Accuracy =", round(accuracy, 4),
        "| Precision =", round(precision, 4),
        "| Recall =", round(recall, 4),
        "| F1 =", round(f1, 4)
    )

# =========================================================
# STEP 7: CREATE COMPARISON TABLE
# =========================================================

results_df = pd.DataFrame(results)

print("\n========== K VALUE COMPARISON ==========")
print(
    results_df.round(4)
)

# =========================================================
# STEP 8: IDENTIFY BEST K
# =========================================================

best_index = results_df["Accuracy"].idxmax()

best_k = results_df.loc[
    best_index,
    "K"
]

best_accuracy = results_df.loc[
    best_index,
    "Accuracy"
]

print("\n========== BEST K VALUE ==========")

print(
    "Best K:",
    int(best_k)
)

print(
    "Best Accuracy:",
    round(best_accuracy, 4)
)

# =========================================================
# STEP 9: PLOT K VS ACCURACY
# =========================================================

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

plt.plot(
    results_df["K"],
    results_df["Accuracy"],
    marker="o",
    linewidth=2
)

plt.xlabel("Value of K")
plt.ylabel("Accuracy")

plt.title(
    "KNN Accuracy for Different Values of K"
)

plt.xticks(k_values)

plt.grid(
    True,
    linestyle="--",
    alpha=0.5
)

plt.tight_layout()
plt.show()

# =========================================================
# STEP 10: TRAIN FINAL MODEL USING BEST K
# =========================================================

final_model = KNeighborsClassifier(
    n_neighbors=int(best_k)
)

final_model.fit(
    X_train_scaled,
    y_train
)

final_pred = final_model.predict(
    X_test_scaled
)

# =========================================================
# STEP 11: CONFUSION MATRIX
# =========================================================

cm = confusion_matrix(
    y_test,
    final_pred
)

print("\n========== CONFUSION MATRIX ==========")
print(cm)

plt.figure(figsize=(7, 5))

sns.heatmap(
    cm,
    annot=True,
    fmt="d",
    xticklabels=wine.target_names,
    yticklabels=wine.target_names
)

plt.xlabel("Predicted Class")
plt.ylabel("Actual Class")

plt.title(
    "KNN Confusion Matrix"
)

plt.tight_layout()
plt.show()

# =========================================================
# STEP 12: CLASSIFICATION REPORT
# =========================================================

print("\n========== CLASSIFICATION REPORT ==========")

print(
    classification_report(
        y_test,
        final_pred,
        target_names=wine.target_names,
        zero_division=0
    )
)

# =========================================================
# STEP 13: PREDICT A NEW WINE SAMPLE
# =========================================================

# Example wine sample
new_wine = [[
    13.2,     # alcohol
    2.7,      # malic acid
    2.4,      # ash
    18.5,     # alcalinity of ash
    100,      # magnesium
    2.5,      # total phenols
    2.3,      # flavanoids
    0.3,      # nonflavanoid phenols
    1.7,      # proanthocyanins
    4.0,      # color intensity
    1.0,      # hue
    3.0,      # od280/od315
    800       # proline
]]

new_wine_scaled = scaler.transform(
    new_wine
)

prediction = final_model.predict(
    new_wine_scaled
)

print("\n========== NEW WINE PREDICTION ==========")

print(
    "Predicted Wine Class:",
    prediction[0]
)

print(
    "Wine Type:",
    wine.target_names[prediction[0]]
)


2. Apply the Random Forest algorithm on the Heart Disease dataset to predict the presence of heart disease. Preprocess the data, evaluate the model using accuracy and confusion matrix, and identify important features affecting the prediction.

 
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report

# ------------------------------------------------------------
# Step 1: Load the Heart Disease Dataset
# ------------------------------------------------------------

df = pd.read_csv("heart.csv")

print("========== FIRST 5 RECORDS ==========")
print(df.head())

print("\n========== DATASET INFORMATION ==========")
df.info()

print("\n========== DATASET SHAPE ==========")
print(df.shape)

print("\n========== STATISTICAL DESCRIPTION ==========")
print(df.describe())

# ------------------------------------------------------------
# Step 2: Check Missing Values
# ------------------------------------------------------------

print("\n========== MISSING VALUES ==========")
print(df.isnull().sum())

# Remove missing records if present
df = df.dropna()

# ------------------------------------------------------------
# Step 3: Separate Features and Target
# ------------------------------------------------------------

X = df.drop("target", axis=1)
y = df["target"]

print("\n========== FEATURES ==========")
print(X.columns.tolist())

print("\n========== TARGET DISTRIBUTION ==========")
print(y.value_counts())

# ------------------------------------------------------------
# Step 4: Split Dataset into Training and Testing Data
# ------------------------------------------------------------

X_train, X_test, y_train, y_test = train_test_split(
    X,
    y,
    test_size=0.20,
    random_state=42,
    stratify=y
)

print("\nTraining samples:", X_train.shape[0])
print("Testing samples :", X_test.shape[0])

# ------------------------------------------------------------
# Step 5: Create Random Forest Model
# ------------------------------------------------------------

model = RandomForestClassifier(
    n_estimators=100,
    random_state=42
)

# ------------------------------------------------------------
# Step 6: Train the Model
# ------------------------------------------------------------

model.fit(X_train, y_train)

print("\nRandom Forest model trained successfully.")

# ------------------------------------------------------------
# Step 7: Make Predictions
# ------------------------------------------------------------

y_pred = model.predict(X_test)

print("\n========== PREDICTIONS ==========")
print(y_pred)

# ------------------------------------------------------------
# Step 8: Calculate Accuracy
# ------------------------------------------------------------

accuracy = accuracy_score(y_test, y_pred)

print("\n========== MODEL ACCURACY ==========")
print("Accuracy:", round(accuracy, 4))
print("Accuracy (%):", round(accuracy * 100, 2), "%")

# ------------------------------------------------------------
# Step 9: Confusion Matrix
# ------------------------------------------------------------

cm = confusion_matrix(y_test, y_pred)

print("\n========== CONFUSION MATRIX ==========")
print(cm)

plt.figure(figsize=(7, 5))

sns.heatmap(
    cm,
    annot=True,
    fmt="d",
    cmap="Blues",
    xticklabels=["No Disease", "Disease"],
    yticklabels=["No Disease", "Disease"]
)

plt.xlabel("Predicted Class")
plt.ylabel("Actual Class")
plt.title("Random Forest - Confusion Matrix")

plt.tight_layout()
plt.show()

# ------------------------------------------------------------
# Step 10: Classification Report
# ------------------------------------------------------------

print("\n========== CLASSIFICATION REPORT ==========")

print(
    classification_report(
        y_test,
        y_pred,
        target_names=["No Disease", "Disease"],
        zero_division=0
    )
)

# ------------------------------------------------------------
# Step 11: Calculate Feature Importance
# ------------------------------------------------------------

feature_importance = pd.DataFrame({
    "Feature": X.columns,
    "Importance": model.feature_importances_
})

feature_importance = feature_importance.sort_values(
    by="Importance",
    ascending=False
)

print("\n========== FEATURE IMPORTANCE ==========")
print(feature_importance)

# ------------------------------------------------------------
# Step 12: Visualize Important Features
# ------------------------------------------------------------

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

sns.barplot(
    data=feature_importance,
    x="Importance",
    y="Feature"
)

plt.xlabel("Importance Score")
plt.ylabel("Features")
plt.title("Important Features for Heart Disease Prediction")

plt.tight_layout()
plt.show()

# ------------------------------------------------------------
# Step 13: Display Top 5 Important Features
# ------------------------------------------------------------

print("\n========== TOP 5 IMPORTANT FEATURES ==========")

print(feature_importance.head(5))

# ------------------------------------------------------------
# Step 14: Predict a New Patient
# ------------------------------------------------------------

new_patient = [[
    55,     # age
    1,      # sex
    1,      # cp
    130,    # trestbps
    250,    # chol
    0,      # fbs
    1,      # restecg
    150,    # thalach
    0,      # exang
    1.2,    # oldpeak
    1,      # slope
    0,      # ca
    2       # thal
]]

prediction = model.predict(new_patient)

print("\n========== NEW PATIENT PREDICTION ==========")

if prediction[0] == 1:
    print("Prediction: Presence of Heart Disease")
else:
    print("Prediction: Absence of Heart Disease")

heart.csv

age,sex,cp,trestbps,chol,fbs,restecg,thalach,exang,oldpeak,slope,ca,thal,target
67,0,2,130,309,0,0,124,0,3.4,1,2,1,1
57,1,3,113,310,1,0,176,0,1.7,2,0,1,1
43,1,0,161,216,0,0,182,0,1.1,2,1,2,0
71,1,2,113,277,1,2,196,1,0.9,2,1,2,1
36,0,1,114,167,0,2,164,1,1.1,0,3,2,1
49,0,0,165,174,1,0,107,1,1.2,2,1,3,1
67,0,0,146,203,0,2,200,1,0.9,0,0,1,0
47,0,0,127,207,0,0,165,1,4.0,1,2,3,1
51,1,2,134,216,0,0,98,1,3.4,2,1,3,1
39,0,1,133,253,1,1,163,1,0.2,0,3,2,1
39,0,0,176,323,0,2,147,1,1.2,0,1,2,1
52,0,3,95,173,0,0,197,1,0.1,2,0,0,0
64,0,0,105,263,1,0,106,0,2.2,1,3,0,1
68,0,0,151,181,1,2,191,1,2.1,1,1,3,1
52,1,2,144,324,0,1,96,1,3.7,1,3,1,1
31,0,2,117,235,0,1,135,1,1.4,0,0,2,1
50,1,1,125,300,1,0,102,1,3.7,0,2,2,1
30,0,3,136,343,1,2,129,1,4.4,0,0,3,1
52,1,2,101,276,1,1,131,1,0.4,0,0,0,1
72,0,0,110,304,1,2,98,1,3.6,1,0,1,1
58,0,1,154,279,1,2,139,1,2.7,2,0,0,1
66,1,0,96,166,0,0,116,1,2.2,0,2,0,0
30,1,0,95,253,1,1,155,1,1.9,0,2,0,1
49,1,2,142,310,0,0,94,1,3.5,1,3,0,1
61,0,1,106,286,0,1,118,0,2.9,2,1,2,1
40,1,3,163,192,0,1,126,0,3.6,2,2,2,1
50,0,3,131,325,1,1,127,1,4.1,1,2,0,1
72,0,2,126,188,0,1,172,1,2.8,1,1,1,1
53,1,2,103,319,0,1,97,0,4.4,2,2,3,1
77,1,1,113,175,1,2,198,1,2.7,0,0,1,1
55,0,3,142,248,0,0,154,0,2.9,2,0,1,1
70,0,3,174,199,0,2,175,0,2.5,1,1,1,1
56,1,3,97,302,1,1,106,0,0.4,0,3,0,1
44,1,0,114,301,0,0,160,0,3.3,0,3,2,1
43,1,3,118,162,0,1,178,0,2.5,1,1,1,1
75,0,0,148,209,0,0,134,1,2.0,1,1,0,1
72,0,1,127,284,1,2,93,0,4.1,1,0,1,1
31,0,0,118,206,1,0,125,1,1.3,2,3,1,1
65,0,1,169,185,1,0,159,1,2.4,2,2,0,1
35,0,3,166,322,0,0,120,0,3.1,0,0,2,1
49,0,3,130,169,0,0,108,0,3.6,1,1,2,1
37,1,1,132,214,1,0,150,1,2.1,0,3,3,1
67,0,2,178,157,0,1,197,0,3.8,0,2,0,1
46,0,1,119,293,0,0,143,0,3.5,0,2,3,1
32,0,2,112,291,1,0,128,1,0.3,0,0,3,1
53,1,0,176,264,1,2,180,1,0.2,1,3,3,1
42,0,0,160,292,0,0,163,0,2.8,0,3,2,1
37,0,0,148,241,0,2,179,1,1.6,1,1,0,0
54,1,3,129,247,0,1,108,0,0.9,1,3,3,1
30,0,0,174,215,1,0,128,0,2.6,2,3,0,1
48,0,2,155,181,1,2,156,0,1.5,1,0,0,1
56,0,0,135,340,1,2,134,1,2.4,2,3,2,1
75,0,1,127,235,1,1,102,1,2.1,1,2,3,1
35,0,1,162,200,0,0,181,1,2.6,1,0,3,1
72,1,3,127,302,1,0,147,1,1.8,1,0,0,1
36,1,1,108,335,1,2,109,1,3.1,2,1,1,1
75,1,2,115,212,1,0,181,0,0.8,2,3,2,1
63,0,0,142,339,1,2,161,0,3.1,0,1,2,1
42,0,3,114,274,1,0,150,0,1.9,2,2,2,1
45,0,0,102,299,0,2,194,0,3.9,2,0,2,0
64,1,0,101,207,0,0,128,1,2.3,2,0,3,1
68,0,2,161,207,1,1,90,0,4.4,2,0,3,1
32,0,1,111,235,0,0,92,1,2.7,0,2,1,1
30,1,1,127,198,1,0,166,0,1.0,2,2,2,0
34,0,0,142,329,1,1,181,1,3.7,1,1,2,1
70,1,3,170,319,0,0,151,1,1.6,1,1,0,1
32,1,1,153,219,0,1,152,0,1.6,2,1,3,0
57,1,3,180,164,1,0,114,0,0.1,2,3,2,1
46,0,1,116,203,1,2,145,0,2.5,1,0,1,0
54,0,3,124,337,1,2,122,0,2.4,1,0,1,1
72,0,2,132,250,0,0,127,1,1.6,0,1,3,1
62,0,3,145,157,1,0,95,0,4.0,1,0,1,1
38,0,2,148,202,1,0,147,0,0.6,1,1,1,1
64,0,2,102,209,1,2,133,1,1.5,2,3,3,1
42,0,3,121,257,0,0,134,0,1.4,1,3,2,1
59,1,2,121,154,1,2,121,0,0.4,0,2,2,1
76,0,0,115,252,1,1,134,0,2.2,1,3,1,1
43,1,2,124,345,0,2,150,0,3.1,2,1,1,1
36,0,1,122,155,1,2,136,0,2.3,2,3,2,1
42,0,3,158,258,1,1,110,1,0.7,1,2,3,1
51,0,0,163,265,0,2,169,0,1.7,2,0,1,0
68,1,3,155,243,1,0,174,0,0.0,0,1,0,1
49,1,1,142,196,0,0,164,0,3.9,0,0,3,1
44,1,1,113,248,0,2,190,0,0.4,1,2,1,1
73,1,1,98,204,1,0,125,0,2.7,1,2,1,1
46,0,2,129,317,1,2,188,1,4.4,1,2,0,1
75,1,2,158,201,1,0,108,0,2.4,2,0,0,1
52,0,3,143,293,1,0,109,1,4.2,0,0,2,1
54,0,3,111,162,0,2,146,1,1.1,0,0,3,1
53,1,0,138,263,1,0,107,0,3.4,2,0,3,1
73,1,0,124,273,0,2,136,0,2.4,0,2,2,1
69,1,3,140,255,1,0,194,0,3.2,1,2,3,1
57,1,2,100,307,1,0,138,0,0.3,1,1,3,1
43,1,1,131,296,1,1,103,1,0.7,1,1,3,1
73,1,3,118,294,0,1,104,0,0.6,0,0,3,1
29,1,0,140,269,1,2,120,0,3.1,1,3,3,1
53,1,2,147,212,1,1,90,0,3.8,2,3,0,1
35,0,3,154,168,1,2,143,0,3.4,2,2,3,1
37,1,3,157,241,1,1,92,0,0.1,0,2,0,1
52,1,2,179,207,0,0,105,0,3.9,2,2,3,1


3. Use the KNN algorithm to classify emails as spam or non-spam based on selected features. Evaluate the classifier using suitable performance metrics. 
Attributes: [ World_Counr, Links_Count, Special_Characters, Sender_Reputation, Spam]


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import (
    accuracy_score,
    precision_score,
    recall_score,
    f1_score,
    confusion_matrix,
    classification_report
)

# ------------------------------------------------------------
# Step 1: Load Dataset
# ------------------------------------------------------------

df = pd.read_csv("email_spam.csv")

print("========== FIRST 5 RECORDS ==========")
print(df.head())

print("\n========== DATASET INFORMATION ==========")
df.info()

print("\n========== DATASET SHAPE ==========")
print(df.shape)

print("\n========== MISSING VALUES ==========")
print(df.isnull().sum())

# ------------------------------------------------------------
# Step 2: Data Preprocessing
# ------------------------------------------------------------

# Remove missing values
df = df.dropna()

# Convert categorical World_Counr into numerical values
df = pd.get_dummies(
    df,
    columns=["World_Counr"],
    drop_first=True
)

print("\n========== PREPROCESSED DATA ==========")
print(df.head())

# ------------------------------------------------------------
# Step 3: Separate Features and Target
# ------------------------------------------------------------

X = df.drop("Spam", axis=1)
y = df["Spam"]

print("\n========== FEATURES ==========")
print(X.columns.tolist())

print("\n========== TARGET DISTRIBUTION ==========")
print(y.value_counts())

# ------------------------------------------------------------
# Step 4: Split Dataset
# ------------------------------------------------------------

X_train, X_test, y_train, y_test = train_test_split(
    X,
    y,
    test_size=0.20,
    random_state=42,
    stratify=y
)

print("\nTraining records:", X_train.shape[0])
print("Testing records :", X_test.shape[0])

# ------------------------------------------------------------
# Step 5: Feature Scaling
# ------------------------------------------------------------

scaler = StandardScaler()

X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

# ------------------------------------------------------------
# Step 6: Train KNN Classifier
# ------------------------------------------------------------

k = 5

model = KNeighborsClassifier(n_neighbors=k)

model.fit(X_train_scaled, y_train)

print("\nKNN model trained successfully.")
print("Value of K:", k)

# ------------------------------------------------------------
# Step 7: Make Predictions
# ------------------------------------------------------------

y_pred = model.predict(X_test_scaled)

print("\n========== PREDICTIONS ==========")
print(y_pred)

# ------------------------------------------------------------
# Step 8: Performance Evaluation
# ------------------------------------------------------------

accuracy = accuracy_score(y_test, y_pred)

precision = precision_score(
    y_test,
    y_pred,
    zero_division=0
)

recall = recall_score(
    y_test,
    y_pred,
    zero_division=0
)

f1 = f1_score(
    y_test,
    y_pred,
    zero_division=0
)

print("\n========== PERFORMANCE METRICS ==========")
print("Accuracy  :", round(accuracy, 4))
print("Precision :", round(precision, 4))
print("Recall    :", round(recall, 4))
print("F1-Score  :", round(f1, 4))

# ------------------------------------------------------------
# Step 9: Classification Report
# ------------------------------------------------------------

print("\n========== CLASSIFICATION REPORT ==========")

print(
    classification_report(
        y_test,
        y_pred,
        target_names=["Non-Spam", "Spam"],
        zero_division=0
    )
)

# ------------------------------------------------------------
# Step 10: Confusion Matrix
# ------------------------------------------------------------

cm = confusion_matrix(y_test, y_pred)

print("\n========== CONFUSION MATRIX ==========")
print(cm)

plt.figure(figsize=(7, 5))

sns.heatmap(
    cm,
    annot=True,
    fmt="d",
    cmap="Blues",
    xticklabels=["Non-Spam", "Spam"],
    yticklabels=["Non-Spam", "Spam"]
)

plt.xlabel("Predicted Class")
plt.ylabel("Actual Class")
plt.title("KNN Email Spam Classification - Confusion Matrix")

plt.tight_layout()
plt.show()

# ------------------------------------------------------------
# Step 11: Compare Different Values of K
# ------------------------------------------------------------

k_values = [1, 3, 5, 7, 9, 11, 13]

accuracy_values = []

print("\n========== K VALUE COMPARISON ==========")

for k in k_values:

    knn = KNeighborsClassifier(n_neighbors=k)

    knn.fit(X_train_scaled, y_train)

    prediction = knn.predict(X_test_scaled)

    acc = accuracy_score(y_test, prediction)

    accuracy_values.append(acc)

    print(
        "K =", k,
        "| Accuracy =", round(acc, 4)
    )

# ------------------------------------------------------------
# Step 12: Plot K vs Accuracy
# ------------------------------------------------------------

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

plt.plot(
    k_values,
    accuracy_values,
    marker="o",
    linewidth=2
)

plt.xlabel("Value of K")
plt.ylabel("Accuracy")
plt.title("KNN: K Value vs Accuracy")
plt.xticks(k_values)
plt.grid(True)

plt.tight_layout()
plt.show()

# ------------------------------------------------------------
# Step 13: Find Best K
# ------------------------------------------------------------

best_index = np.argmax(accuracy_values)

best_k = k_values[best_index]
best_accuracy = accuracy_values[best_index]

print("\n========== BEST K ==========")
print("Best K:", best_k)
print("Best Accuracy:", round(best_accuracy, 4))
print("Best Accuracy (%):", round(best_accuracy * 100, 2), "%")

Sample email_spam.csv

If you need a dataset for the program, use this format:

World_Counr,Links_Count,Special_Characters,Sender_Reputation,Spam
USA,2,5,92,0
India,1,3,88,0
UK,3,6,85,0
Canada,2,4,90,0
Australia,1,2,95,0
Germany,2,5,87,0
France,1,3,91,0
Japan,2,4,93,0
India,8,18,25,1
USA,12,22,18,1
UK,10,20,30,1
Canada,9,17,35,1
Australia,11,25,15,1
Germany,7,16,40,1
France,13,28,12,1
Japan,9,21,32,1
India,3,7,82,0
USA,4,8,78,0
UK,2,5,89,0
Canada,1,4,94,0
Australia,10,19,22,1
Germany,8,15,38,1
France,12,24,20,1
Japan,7,18,42,1
India,2,6,86,0
USA,3,5,90,0
UK,1,3,96,0
Canada,4,7,80,0
Australia,9,20,28,1
Germany,11,23,17,1
France,8,19,33,1
Japan,10,22,24,1
India,1,2,93,0
USA,2,4,91,0
UK,3,6,84,0
Canada,2,5,88,0
Australia,12,26,14,1
Germany,9,18,31,1
France,11,21,19,1
Japan,8,17,36,1


4. Apply the Random Forest algorithm to predict house prices based on property attributes. Evaluate the model performance and analyze the importance of features influencing the prediction.

# Import required libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score

# ----------------------------------------------------
# 1. Load California Housing Dataset
# ----------------------------------------------------

housing = fetch_california_housing()

# Create DataFrame
X = pd.DataFrame(housing.data, columns=housing.feature_names)
y = pd.Series(housing.target, name="HousePrice")

print("First 5 records:")
print(X.head())

print("\nDataset Shape:", X.shape)

# ----------------------------------------------------
# 2. Display Dataset Information
# ----------------------------------------------------

print("\nDataset Information:")
print(X.info())

print("\nStatistical Summary:")
print(X.describe())

# ----------------------------------------------------
# 3. Check Missing Values
# ----------------------------------------------------

print("\nMissing Values:")
print(X.isnull().sum())

# ----------------------------------------------------
# 4. Split Dataset into Training and Testing Sets
# ----------------------------------------------------

X_train, X_test, y_train, y_test = train_test_split(
    X, y,
    test_size=0.20,
    random_state=42
)

print("\nTraining Data Shape:", X_train.shape)
print("Testing Data Shape:", X_test.shape)

# ----------------------------------------------------
# 5. Create Random Forest Regression Model
# ----------------------------------------------------

model = RandomForestRegressor(
    n_estimators=100,
    random_state=42,
    max_depth=None,
    n_jobs=-1
)

# ----------------------------------------------------
# 6. Train the Model
# ----------------------------------------------------

model.fit(X_train, y_train)

# ----------------------------------------------------
# 7. Make Predictions
# ----------------------------------------------------

y_pred = model.predict(X_test)

print("\nFirst 10 Actual Prices:")
print(y_test.head(10).values)

print("\nFirst 10 Predicted Prices:")
print(y_pred[:10])

# ----------------------------------------------------
# 8. Evaluate Model Performance
# ----------------------------------------------------

mae = mean_absolute_error(y_test, y_pred)
mse = mean_squared_error(y_test, y_pred)
rmse = np.sqrt(mse)
r2 = r2_score(y_test, y_pred)

print("\n----- Model Performance -----")
print("Mean Absolute Error (MAE):", mae)
print("Mean Squared Error (MSE):", mse)
print("Root Mean Squared Error (RMSE):", rmse)
print("R² Score:", r2)

# ----------------------------------------------------
# 9. Feature Importance
# ----------------------------------------------------

feature_importance = pd.DataFrame({
    "Feature": X.columns,
    "Importance": model.feature_importances_
})

feature_importance = feature_importance.sort_values(
    by="Importance",
    ascending=False
)

print("\n----- Feature Importance -----")
print(feature_importance)

# ----------------------------------------------------
# 10. Plot Feature Importance
# ----------------------------------------------------

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

plt.bar(
    feature_importance["Feature"],
    feature_importance["Importance"]
)

plt.xlabel("Features")
plt.ylabel("Importance")
plt.title("Feature Importance - Random Forest")
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()

# ----------------------------------------------------
# 11. Actual vs Predicted Prices
# ----------------------------------------------------

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

plt.scatter(y_test, y_pred, alpha=0.5)

plt.xlabel("Actual House Price")
plt.ylabel("Predicted House Price")
plt.title("Actual vs Predicted House Prices")

plt.tight_layout()
plt.show()


SET B:
1. Implement a KNN classifier on a Movie dataset. Preprocess the data, evaluate the model usingaccuracy and confusion matrix.

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

import pandas as pd
import matplotlib.pyplot as plt

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, confusion_matrix, ConfusionMatrixDisplay


# --------------------------------------------------
# 2. Create Movie Dataset
# --------------------------------------------------

data = {
    'Action': [8, 7, 9, 2, 3, 1, 8, 6, 2, 9, 4, 1],
    'Comedy': [2, 3, 1, 8, 9, 7, 2, 4, 8, 1, 6, 9],
    'Drama':  [3, 4, 2, 7, 8, 9, 4, 5, 7, 2, 6, 8],
    'Rating': [8.5, 8.0, 8.7, 7.5, 8.2, 7.8,
               8.8, 7.9, 7.4, 8.9, 7.6, 8.1],
    'Genre': [
        'Action',
        'Action',
        'Action',
        'Comedy',
        'Comedy',
        'Drama',
        'Action',
        'Comedy',
        'Drama',
        'Action',
        'Drama',
        'Comedy'
    ]
}

df = pd.DataFrame(data)

print("Movie Dataset:")
print(df)


# --------------------------------------------------
# 3. Check Missing Values
# --------------------------------------------------

print("\nMissing Values:")
print(df.isnull().sum())


# --------------------------------------------------
# 4. Separate Features and Target
# --------------------------------------------------

X = df[['Action', 'Comedy', 'Drama', 'Rating']]
y = df['Genre']


# --------------------------------------------------
# 5. Split Dataset
# --------------------------------------------------

X_train, X_test, y_train, y_test = train_test_split(
    X,
    y,
    test_size=0.25,
    random_state=42,
    stratify=y
)


# --------------------------------------------------
# 6. Feature Scaling
# --------------------------------------------------

scaler = StandardScaler()

X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)


# --------------------------------------------------
# 7. Create KNN Classifier
# --------------------------------------------------

knn = KNeighborsClassifier(n_neighbors=3)


# --------------------------------------------------
# 8. Train the Model
# --------------------------------------------------

knn.fit(X_train, y_train)


# --------------------------------------------------
# 9. Make Predictions
# --------------------------------------------------

y_pred = knn.predict(X_test)

print("\nActual Values:")
print(y_test.values)

print("\nPredicted Values:")
print(y_pred)


# --------------------------------------------------
# 10. Calculate Accuracy
# --------------------------------------------------

accuracy = accuracy_score(y_test, y_pred)

print("\nAccuracy:", accuracy)
print("Accuracy Percentage:", accuracy * 100, "%")


# --------------------------------------------------
# 11. Confusion Matrix
# --------------------------------------------------

cm = confusion_matrix(y_test, y_pred)

print("\nConfusion Matrix:")
print(cm)


# --------------------------------------------------
# 12. Display Confusion Matrix Graphically
# --------------------------------------------------

disp = ConfusionMatrixDisplay(
    confusion_matrix=cm,
    display_labels=knn.classes_
)

disp.plot()

plt.title("KNN - Confusion Matrix")
plt.show()

 

 2. Develop a Random Forest model to classify mobile phones into different price ranges based on their specifications. Evaluate the classifier and identify the most influential features.

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

import pandas as pd
import matplotlib.pyplot as plt

from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier

from sklearn.metrics import (
    accuracy_score,
    classification_report,
    confusion_matrix,
    ConfusionMatrixDisplay
)


# --------------------------------------------------
# 2. Load Dataset
# --------------------------------------------------

df = pd.read_csv("mobile_price.csv")

print("First 5 records:")
print(df.head())

print("\nDataset Shape:")
print(df.shape)


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

print("\nDataset Information:")
df.info()

print("\nStatistical Summary:")
print(df.describe())


# --------------------------------------------------
# 4. Check Missing Values
# --------------------------------------------------

print("\nMissing Values:")
print(df.isnull().sum())


# --------------------------------------------------
# 5. Separate Features and Target
# --------------------------------------------------

X = df.drop("price_range", axis=1)

y = df["price_range"]

print("\nFeatures:")
print(X.columns)

print("\nTarget Classes:")
print(y.unique())


# --------------------------------------------------
# 6. Split Dataset into Training and Testing
# --------------------------------------------------

X_train, X_test, y_train, y_test = train_test_split(
    X,
    y,
    test_size=0.20,
    random_state=42,
    stratify=y
)

print("\nTraining Data:", X_train.shape)
print("Testing Data:", X_test.shape)


# --------------------------------------------------
# 7. Create Random Forest Classifier
# --------------------------------------------------

rf_model = RandomForestClassifier(
    n_estimators=100,
    random_state=42,
    n_jobs=-1
)


# --------------------------------------------------
# 8. Train the Model
# --------------------------------------------------

rf_model.fit(X_train, y_train)


# --------------------------------------------------
# 9. Make Predictions
# --------------------------------------------------

y_pred = rf_model.predict(X_test)

print("\nActual Values:")
print(y_test.values[:20])

print("\nPredicted Values:")
print(y_pred[:20])


# --------------------------------------------------
# 10. Calculate Accuracy
# --------------------------------------------------

accuracy = accuracy_score(y_test, y_pred)

print("\n----- Model Accuracy -----")
print("Accuracy:", accuracy)
print("Accuracy Percentage:", accuracy * 100, "%")


# --------------------------------------------------
# 11. Classification Report
# --------------------------------------------------

print("\n----- Classification Report -----")

print(
    classification_report(
        y_test,
        y_pred,
        target_names=[
            "Low",
            "Medium",
            "High",
            "Very High"
        ]
    )
)


# --------------------------------------------------
# 12. Confusion Matrix
# --------------------------------------------------

cm = confusion_matrix(y_test, y_pred)

print("\n----- Confusion Matrix -----")
print(cm)


# --------------------------------------------------
# 13. Display Confusion Matrix
# --------------------------------------------------

disp = ConfusionMatrixDisplay(
    confusion_matrix=cm,
    display_labels=[
        "Low",
        "Medium",
        "High",
        "Very High"
    ]
)

disp.plot()

plt.title("Random Forest - Mobile Price Classification")

plt.show()


# --------------------------------------------------
# 14. Calculate Feature Importance
# --------------------------------------------------

feature_importance = pd.DataFrame({
    "Feature": X.columns,
    "Importance": rf_model.feature_importances_
})

feature_importance = feature_importance.sort_values(
    by="Importance",
    ascending=False
)


# --------------------------------------------------
# 15. Display Feature Importance
# --------------------------------------------------

print("\n----- Feature Importance -----")
print(feature_importance)


# --------------------------------------------------
# 16. Plot Feature Importance
# --------------------------------------------------

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

plt.bar(
    feature_importance["Feature"],
    feature_importance["Importance"]
)

plt.xlabel("Features")
plt.ylabel("Importance")
plt.title("Feature Importance in Random Forest")

plt.xticks(rotation=90)

plt.tight_layout()

plt.show() 

 
SET C:
1. Apply KNN and Random Forest algorithms to classify Instagram influencers into different engagement categories based on followers, likes, comments, and posts. Compare the performance of both classifiers.

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

import pandas as pd
import matplotlib.pyplot as plt

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier

from sklearn.metrics import (
    accuracy_score,
    confusion_matrix,
    ConfusionMatrixDisplay,
    classification_report
)


# --------------------------------------------------
# 2. Create Instagram Influencer Dataset
# --------------------------------------------------

data = {
    'Followers': [
        5000, 8000, 12000, 15000,
        20000, 25000, 30000, 35000,
        45000, 50000, 60000, 70000,
        80000, 90000, 100000, 120000,
        150000, 180000, 200000, 250000
    ],

    'Likes': [
        300, 500, 800, 1000,
        1500, 2000, 2500, 3000,
        4000, 5000, 6000, 7000,
        8500, 10000, 12000, 15000,
        18000, 22000, 26000, 32000
    ],

    'Comments': [
        20, 30, 50, 70,
        90, 120, 150, 180,
        220, 250, 300, 350,
        400, 450, 500, 600,
        750, 900, 1100, 1300
    ],

    'Posts': [
        10, 15, 20, 25,
        30, 35, 40, 45,
        50, 55, 60, 65,
        70, 75, 80, 85,
        90, 95, 100, 110
    ],

    'Engagement': [
        'Low', 'Low', 'Low', 'Low',
        'Medium', 'Medium', 'Medium', 'Medium',
        'Medium', 'Medium', 'Medium', 'Medium',
        'High', 'High', 'High', 'High',
        'High', 'High', 'High', 'High'
    ]
}

df = pd.DataFrame(data)


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

print("Instagram Influencer Dataset:")
print(df)

print("\nDataset Shape:")
print(df.shape)


# --------------------------------------------------
# 4. Check Missing Values
# --------------------------------------------------

print("\nMissing Values:")
print(df.isnull().sum())


# --------------------------------------------------
# 5. Separate Features and Target
# --------------------------------------------------

X = df[['Followers', 'Likes', 'Comments', 'Posts']]

y = df['Engagement']


# --------------------------------------------------
# 6. Split Dataset
# --------------------------------------------------

X_train, X_test, y_train, y_test = train_test_split(
    X,
    y,
    test_size=0.30,
    random_state=42,
    stratify=y
)


# --------------------------------------------------
# 7. Feature Scaling for KNN
# --------------------------------------------------

scaler = StandardScaler()

X_train_scaled = scaler.fit_transform(X_train)

X_test_scaled = scaler.transform(X_test)


# ==================================================
# 8. KNN CLASSIFIER
# ==================================================

knn = KNeighborsClassifier(n_neighbors=3)

knn.fit(X_train_scaled, y_train)

y_pred_knn = knn.predict(X_test_scaled)


# --------------------------------------------------
# 9. KNN Accuracy
# --------------------------------------------------

knn_accuracy = accuracy_score(y_test, y_pred_knn)

print("\n========== KNN CLASSIFIER ==========")

print("KNN Accuracy:", knn_accuracy)

print("KNN Accuracy Percentage:",
      knn_accuracy * 100, "%")


# --------------------------------------------------
# 10. KNN Classification Report
# --------------------------------------------------

print("\nKNN Classification Report:")

print(
    classification_report(
        y_test,
        y_pred_knn,
        zero_division=0
    )
)


# --------------------------------------------------
# 11. KNN Confusion Matrix
# --------------------------------------------------

cm_knn = confusion_matrix(
    y_test,
    y_pred_knn,
    labels=['Low', 'Medium', 'High']
)

print("\nKNN Confusion Matrix:")
print(cm_knn)


# ==================================================
# 12. RANDOM FOREST CLASSIFIER
# ==================================================

rf = RandomForestClassifier(
    n_estimators=100,
    random_state=42
)

rf.fit(X_train, y_train)

y_pred_rf = rf.predict(X_test)


# --------------------------------------------------
# 13. Random Forest Accuracy
# --------------------------------------------------

rf_accuracy = accuracy_score(y_test, y_pred_rf)

print("\n========== RANDOM FOREST ==========")

print("Random Forest Accuracy:",
      rf_accuracy)

print("Random Forest Accuracy Percentage:",
      rf_accuracy * 100, "%")


# --------------------------------------------------
# 14. Random Forest Classification Report
# --------------------------------------------------

print("\nRandom Forest Classification Report:")

print(
    classification_report(
        y_test,
        y_pred_rf,
        zero_division=0
    )
)


# --------------------------------------------------
# 15. Random Forest Confusion Matrix
# --------------------------------------------------

cm_rf = confusion_matrix(
    y_test,
    y_pred_rf,
    labels=['Low', 'Medium', 'High']
)

print("\nRandom Forest Confusion Matrix:")
print(cm_rf)


# ==================================================
# 16. Compare Accuracy
# ==================================================

print("\n========== PERFORMANCE COMPARISON ==========")

print("KNN Accuracy:",
      knn_accuracy * 100, "%")

print("Random Forest Accuracy:",
      rf_accuracy * 100, "%")


if knn_accuracy > rf_accuracy:
    print("\nKNN performed better.")

elif rf_accuracy > knn_accuracy:
    print("\nRandom Forest performed better.")

else:
    print("\nBoth classifiers have the same accuracy.")


# ==================================================
# 17. Plot Confusion Matrix - KNN
# ==================================================

disp1 = ConfusionMatrixDisplay(
    confusion_matrix=cm_knn,
    display_labels=['Low', 'Medium', 'High']
)

disp1.plot()

plt.title("KNN - Instagram Engagement Classification")

plt.show()


# ==================================================
# 18. Plot Confusion Matrix - Random Forest
# ==================================================

disp2 = ConfusionMatrixDisplay(
    confusion_matrix=cm_rf,
    display_labels=['Low', 'Medium', 'High']
)

disp2.plot()

plt.title("Random Forest - Instagram Engagement Classification")

plt.show()


# ==================================================
# 19. Feature Importance - Random Forest
# ==================================================

importance = pd.DataFrame({
    'Feature': X.columns,
    'Importance': rf.feature_importances_
})

importance = importance.sort_values(
    by='Importance',
    ascending=False
)

print("\n========== FEATURE IMPORTANCE ==========")

print(importance)


# --------------------------------------------------
# 20. Plot Feature Importance
# --------------------------------------------------

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

plt.bar(
    importance['Feature'],
    importance['Importance']
)

plt.xlabel("Features")
plt.ylabel("Importance")

plt.title(
    "Random Forest Feature Importance"
)

plt.xticks(rotation=45)

plt.tight_layout()

plt.show()


2. Implement KNN and Random Forest algorithms to predict product rating categories based on customer reviews, purchase history, and product features. Compare the effectiveness of both classifiers.

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

import pandas as pd
import matplotlib.pyplot as plt

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier

from sklearn.metrics import (
    accuracy_score,
    confusion_matrix,
    ConfusionMatrixDisplay,
    classification_report
)


# --------------------------------------------------
# 2. Create Product Dataset
# --------------------------------------------------

data = {
    'Review_Score': [
        1, 2, 2, 3, 3,
        4, 4, 5, 5, 4,
        2, 3, 3, 4, 5,
        1, 2, 4, 5, 5,
        2, 3, 4, 4, 5
    ],

    'Purchase_Frequency': [
        1, 1, 2, 2, 3,
        3, 4, 5, 5, 4,
        1, 2, 3, 4, 5,
        1, 2, 4, 5, 5,
        2, 3, 4, 4, 5
    ],

    'Product_Quality': [
        2, 2, 3, 3, 3,
        4, 4, 5, 5, 4,
        2, 3, 3, 4, 5,
        1, 2, 4, 5, 5,
        2, 3, 4, 4, 5
    ],

    'Price': [
        500, 600, 700, 800, 900,
        1000, 1100, 1200, 1300, 1250,
        650, 750, 850, 1050, 1400,
        450, 550, 1150, 1350, 1500,
        600, 800, 1100, 1250, 1450
    ],

    'Rating_Category': [
        'Low', 'Low', 'Low', 'Medium', 'Medium',
        'High', 'High', 'High', 'High', 'High',
        'Low', 'Medium', 'Medium', 'High', 'High',
        'Low', 'Low', 'High', 'High', 'High',
        'Low', 'Medium', 'High', 'High', 'High'
    ]
}

df = pd.DataFrame(data)


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

print("Product Dataset:")
print(df)

print("\nDataset Shape:")
print(df.shape)


# --------------------------------------------------
# 4. Check Missing Values
# --------------------------------------------------

print("\nMissing Values:")
print(df.isnull().sum())


# --------------------------------------------------
# 5. Separate Features and Target
# --------------------------------------------------

X = df[
    [
        'Review_Score',
        'Purchase_Frequency',
        'Product_Quality',
        'Price'
    ]
]

y = df['Rating_Category']


# --------------------------------------------------
# 6. Split Dataset
# --------------------------------------------------

X_train, X_test, y_train, y_test = train_test_split(
    X,
    y,
    test_size=0.30,
    random_state=42,
    stratify=y
)


# ==================================================
# 7. FEATURE SCALING FOR KNN
# ==================================================

scaler = StandardScaler()

X_train_scaled = scaler.fit_transform(X_train)

X_test_scaled = scaler.transform(X_test)


# ==================================================
# 8. KNN CLASSIFIER
# ==================================================

knn = KNeighborsClassifier(n_neighbors=3)

knn.fit(X_train_scaled, y_train)

y_pred_knn = knn.predict(X_test_scaled)


# --------------------------------------------------
# 9. KNN Accuracy
# --------------------------------------------------

knn_accuracy = accuracy_score(
    y_test,
    y_pred_knn
)

print("\n========== KNN CLASSIFIER ==========")

print("KNN Accuracy:",
      knn_accuracy)

print("KNN Accuracy Percentage:",
      knn_accuracy * 100, "%")


# --------------------------------------------------
# 10. KNN Classification Report
# --------------------------------------------------

print("\nKNN Classification Report:")

print(
    classification_report(
        y_test,
        y_pred_knn,
        zero_division=0
    )
)


# --------------------------------------------------
# 11. KNN Confusion Matrix
# --------------------------------------------------

labels = ['Low', 'Medium', 'High']

cm_knn = confusion_matrix(
    y_test,
    y_pred_knn,
    labels=labels
)

print("\nKNN Confusion Matrix:")
print(cm_knn)


# ==================================================
# 12. RANDOM FOREST CLASSIFIER
# ==================================================

rf = RandomForestClassifier(
    n_estimators=100,
    random_state=42
)

rf.fit(X_train, y_train)

y_pred_rf = rf.predict(X_test)


# --------------------------------------------------
# 13. Random Forest Accuracy
# --------------------------------------------------

rf_accuracy = accuracy_score(
    y_test,
    y_pred_rf
)

print("\n========== RANDOM FOREST ==========")

print("Random Forest Accuracy:",
      rf_accuracy)

print("Random Forest Accuracy Percentage:",
      rf_accuracy * 100, "%")


# --------------------------------------------------
# 14. Random Forest Classification Report
# --------------------------------------------------

print("\nRandom Forest Classification Report:")

print(
    classification_report(
        y_test,
        y_pred_rf,
        zero_division=0
    )
)


# --------------------------------------------------
# 15. Random Forest Confusion Matrix
# --------------------------------------------------

cm_rf = confusion_matrix(
    y_test,
    y_pred_rf,
    labels=labels
)

print("\nRandom Forest Confusion Matrix:")
print(cm_rf)


# ==================================================
# 16. Compare Both Models
# ==================================================

print("\n========== PERFORMANCE COMPARISON ==========")

print("KNN Accuracy:",
      knn_accuracy * 100, "%")

print("Random Forest Accuracy:",
      rf_accuracy * 100, "%")


if knn_accuracy > rf_accuracy:

    print("\nKNN performed better.")

elif rf_accuracy > knn_accuracy:

    print("\nRandom Forest performed better.")

else:

    print("\nBoth models have the same accuracy.")


# ==================================================
# 17. Plot KNN Confusion Matrix
# ==================================================

disp1 = ConfusionMatrixDisplay(
    confusion_matrix=cm_knn,
    display_labels=labels
)

disp1.plot()

plt.title(
    "KNN - Product Rating Classification"
)

plt.show()


# ==================================================
# 18. Plot Random Forest Confusion Matrix
# ==================================================

disp2 = ConfusionMatrixDisplay(
    confusion_matrix=cm_rf,
    display_labels=labels
)

disp2.plot()

plt.title(
    "Random Forest - Product Rating Classification"
)

plt.show()


# ==================================================
# 19. Random Forest Feature Importance
# ==================================================

importance = pd.DataFrame({
    'Feature': X.columns,
    'Importance': rf.feature_importances_
})

importance = importance.sort_values(
    by='Importance',
    ascending=False
)

print("\n========== FEATURE IMPORTANCE ==========")

print(importance)


# ==================================================
# 20. Plot Feature Importance
# ==================================================

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

plt.bar(
    importance['Feature'],
    importance['Importance']
)

plt.xlabel("Features")
plt.ylabel("Importance")

plt.title(
    "Random Forest Feature Importance"
)

plt.xticks(rotation=45)

plt.tight_layout()

plt.show()