Wednesday, September 2, 2026

TYBCS DS & DA Assignment 4

 Assignment 4
Supervised Machine Learning Models for Regression
(Linear Regression, Polynomial Regression, Logistic Regression)

Lab Assignment
SET A
1. Apply Linear Regression on a YouTube Video dataset to predict video views using relevant video attributes. Visualize the relationship between the predictor and target variable using a scatter plot and regression line.

 import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score

# Create YouTube video dataset
data = {
    "Video": [
        "Video 1", "Video 2", "Video 3", "Video 4", "Video 5",
        "Video 6", "Video 7", "Video 8", "Video 9", "Video 10",
        "Video 11", "Video 12", "Video 13", "Video 14", "Video 15"
    ],
    "Likes": [
        1200, 2500, 3200, 4500, 5200,
        6800, 7500, 8200, 9500, 11000,
        12500, 14000, 15500, 17000, 19000
    ],
    "Views": [
        15000, 28000, 35000, 50000, 62000,
        75000, 82000, 95000, 110000, 125000,
        145000, 160000, 180000, 195000, 220000
    ]
}

df = pd.DataFrame(data)

print("YouTube Dataset:")
print(df)

# Save dataset
df.to_csv("youtube_videos.csv", index=False)

# Independent variable (Predictor)
X = df[["Likes"]]

# Dependent variable (Target)
y = df["Views"]

# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

# Create Linear Regression model
model = LinearRegression()

# Train the model
model.fit(X_train, y_train)

# Predict views
y_pred = model.predict(X_test)

# Display model parameters
print("\nLinear Regression Equation:")
print("Views =", model.intercept_, "+", model.coef_[0], "* Likes")

# Display predictions
result = pd.DataFrame({
    "Actual Views": y_test,
    "Predicted Views": y_pred
})

print("\nActual vs Predicted Views:")
print(result)

# Model evaluation
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)

print("\nMean Squared Error:", mse)
print("R2 Score:", r2)

# Predict views for a new video
new_likes = [[10000]]
predicted_views = model.predict(new_likes)

print("\nPredicted views for 10,000 likes:",
      round(predicted_views[0], 2))

# Scatter plot with regression line
plt.figure(figsize=(9, 6))

plt.scatter(
    df["Likes"],
    df["Views"],
    label="Actual Data"
)

plt.plot(
    df["Likes"],
    model.predict(df[["Likes"]]),
    linewidth=2,
    label="Regression Line"
)

plt.xlabel("Number of Likes")
plt.ylabel("Number of Views")
plt.title("YouTube Likes vs Video Views - Linear Regression")
plt.legend()
plt.grid(True, linestyle="--", alpha=0.5)

plt.tight_layout()
plt.show()

 

2. Apply Logistic Regression on an Online Gaming dataset to classify players as Casual or Professional gamers based on features such as daily play time, achievements unlocked, in-game purchases, and gaming level. Evaluate the model using Accuracy, Precision, Recall, F1-Score, and Confusion Matrix.

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

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import (
    accuracy_score,
    precision_score,
    recall_score,
    f1_score,
    confusion_matrix,
    classification_report
)

# --------------------------------------------------
# Step 1: Create Online Gaming Dataset
# --------------------------------------------------

data = {
    "Player": [
        "P1", "P2", "P3", "P4", "P5",
        "P6", "P7", "P8", "P9", "P10",
        "P11", "P12", "P13", "P14", "P15",
        "P16", "P17", "P18", "P19", "P20"
    ],

    "Daily_Play_Time": [
        1.0, 1.5, 2.0, 2.5, 3.0,
        3.5, 4.0, 4.5, 5.0, 5.5,
        6.0, 6.5, 7.0, 7.5, 8.0,
        8.5, 9.0, 9.5, 10.0, 10.5
    ],

    "Achievements_Unlocked": [
        5, 8, 12, 15, 20,
        25, 30, 35, 40, 45,
        50, 55, 60, 65, 70,
        75, 80, 85, 90, 95
    ],

    "In_Game_Purchases": [
        200, 300, 500, 600, 700,
        900, 1000, 1200, 1500, 1700,
        2000, 2200, 2500, 2800, 3000,
        3200, 3500, 3800, 4000, 4500
    ],

    "Gaming_Level": [
        3, 5, 7, 9, 12,
        15, 18, 21, 24, 27,
        30, 33, 36, 39, 42,
        45, 48, 51, 54, 57
    ],

    # 0 = Casual, 1 = Professional
    "Player_Type": [
        0, 0, 0, 0, 0,
        0, 0, 0, 0, 0,
        1, 1, 1, 1, 1,
        1, 1, 1, 1, 1
    ]
}

df = pd.DataFrame(data)

print("Online Gaming Dataset:")
print(df)

# Save dataset
df.to_csv("online_gaming.csv", index=False)

# --------------------------------------------------
# Step 2: Select Features and Target
# --------------------------------------------------

features = [
    "Daily_Play_Time",
    "Achievements_Unlocked",
    "In_Game_Purchases",
    "Gaming_Level"
]

X = df[features]
y = df["Player_Type"]

# --------------------------------------------------
# Step 3: Split Dataset
# --------------------------------------------------

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

# --------------------------------------------------
# Step 4: Create and Train Logistic Regression Model
# --------------------------------------------------

model = LogisticRegression(max_iter=1000)

model.fit(X_train, y_train)

# --------------------------------------------------
# Step 5: Make Predictions
# --------------------------------------------------

y_pred = model.predict(X_test)

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

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

# --------------------------------------------------
# Step 6: Evaluate Model
# --------------------------------------------------

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----- Model Evaluation -----")
print("Accuracy  :", round(accuracy, 4))
print("Precision :", round(precision, 4))
print("Recall    :", round(recall, 4))
print("F1-Score  :", round(f1, 4))

# --------------------------------------------------
# Step 7: Classification Report
# --------------------------------------------------

print("\nClassification Report:")
print(
    classification_report(
        y_test,
        y_pred,
        target_names=["Casual", "Professional"],
        zero_division=0
    )
)

# --------------------------------------------------
# Step 8: Confusion Matrix
# --------------------------------------------------

cm = confusion_matrix(y_test, y_pred)

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

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

sns.heatmap(
    cm,
    annot=True,
    fmt="d",
    xticklabels=["Casual", "Professional"],
    yticklabels=["Casual", "Professional"]
)

plt.xlabel("Predicted Class")
plt.ylabel("Actual Class")
plt.title("Confusion Matrix - Online Gaming Classification")

plt.tight_layout()
plt.show()

# --------------------------------------------------
# Step 9: Predict a New Player
# --------------------------------------------------

new_player = [[6.5, 55, 2300, 34]]

prediction = model.predict(new_player)

if prediction[0] == 0:
    print("\nNew Player Classification: Casual Gamer")
else:
    print("\nNew Player Classification: Professional Gamer")

 
3. Download the Salary Dataset. Write a Python program to read the dataset and display its information. Preprocess the data if required and split it into training and testing sets. Apply Simple Linear Regression to predict salary based on years of experience. Plot the regression line and evaluate the model using appropriate metrics such as Mean Squared Error.
Apply Polynomial Regression to predict song popularity using audio features such as danceability, energy, and tempo. Evaluate the model using MAE, RMSE, and R² Score, and visualize the fitted curve.

pip install pandas matplotlib scikit-learn

 import pandas as pd
import matplotlib.pyplot as plt

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score

# --------------------------------------------------
# Step 1: Read the Salary Dataset
# --------------------------------------------------

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

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

# --------------------------------------------------
# Step 2: Display Dataset Information
# --------------------------------------------------

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

print("\nDataset Description:")
print(df.describe())

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

# --------------------------------------------------
# Step 3: Preprocess the Data
# --------------------------------------------------

# Remove missing values
df = df.dropna()

# Convert columns to numeric if required
df["YearsExperience"] = pd.to_numeric(
    df["YearsExperience"], errors="coerce"
)

df["Salary"] = pd.to_numeric(
    df["Salary"], errors="coerce"
)

# Remove rows containing invalid values
df = df.dropna()

print("\nCleaned Dataset:")
print(df.head())

# --------------------------------------------------
# Step 4: Select Independent and Dependent Variables
# --------------------------------------------------

X = df[["YearsExperience"]]
y = df["Salary"]

# --------------------------------------------------
# Step 5: Split Dataset into Training and Testing Sets
# --------------------------------------------------

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

print("\nTraining Records:", len(X_train))
print("Testing Records:", len(X_test))

# --------------------------------------------------
# Step 6: Create Linear Regression Model
# --------------------------------------------------

model = LinearRegression()

# Train the model
model.fit(X_train, y_train)

# --------------------------------------------------
# Step 7: Predict Salary
# --------------------------------------------------

y_pred = model.predict(X_test)

print("\nActual and Predicted Salaries:")

result = pd.DataFrame({
    "YearsExperience": X_test["YearsExperience"].values,
    "Actual Salary": y_test.values,
    "Predicted Salary": y_pred
})

print(result)

# --------------------------------------------------
# Step 8: Display Regression Equation
# --------------------------------------------------

print("\nRegression Equation:")
print(
    "Salary =",
    round(model.intercept_, 2),
    "+",
    round(model.coef_[0], 2),
    "* YearsExperience"
)

# --------------------------------------------------
# Step 9: Evaluate the Model
# --------------------------------------------------

mse = mean_squared_error(y_test, y_pred)
mae = mean_absolute_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)

print("\n----- Model Evaluation -----")
print("Mean Squared Error (MSE):", round(mse, 2))
print("Mean Absolute Error (MAE):", round(mae, 2))
print("R2 Score:", round(r2, 4))

# --------------------------------------------------
# Step 10: Plot Regression Line
# --------------------------------------------------

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

# Actual data
plt.scatter(
    X,
    y,
    label="Actual Salary"
)

# Regression line
plt.plot(
    X,
    model.predict(X),
    linewidth=2,
    label="Regression Line"
)

plt.xlabel("Years of Experience")
plt.ylabel("Salary")
plt.title("Salary Prediction using Simple Linear Regression")

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

plt.tight_layout()
plt.show()

# --------------------------------------------------
# Step 11: Predict Salary for New Experience
# --------------------------------------------------

years = [[7]]

predicted_salary = model.predict(years)

print(
    "\nPredicted salary for 7 years of experience:",
    round(predicted_salary[0], 2)
)

 

Part B: Polynomial Regression for Song Popularity 

 Part B: Polynomial Regression for Song Popularity

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

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import make_pipeline
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score

# --------------------------------------------------
# Step 1: Create / Read Song Dataset
# --------------------------------------------------

data = {
    "Song": [
        "Song 1", "Song 2", "Song 3", "Song 4", "Song 5",
        "Song 6", "Song 7", "Song 8", "Song 9", "Song 10",
        "Song 11", "Song 12", "Song 13", "Song 14", "Song 15",
        "Song 16", "Song 17", "Song 18", "Song 19", "Song 20"
    ],

    "Danceability": [
        0.40, 0.45, 0.50, 0.55, 0.60,
        0.62, 0.65, 0.68, 0.70, 0.72,
        0.74, 0.76, 0.78, 0.80, 0.82,
        0.84, 0.86, 0.88, 0.90, 0.92
    ],

    "Energy": [
        0.30, 0.35, 0.40, 0.42, 0.45,
        0.50, 0.52, 0.55, 0.58, 0.60,
        0.62, 0.65, 0.68, 0.70, 0.72,
        0.75, 0.78, 0.80, 0.83, 0.85
    ],

    "Tempo": [
        80, 85, 90, 95, 100,
        105, 110, 115, 120, 125,
        128, 130, 135, 140, 145,
        150, 155, 160, 165, 170
    ],

    "Popularity": [
        35, 38, 42, 45, 48,
        52, 55, 58, 61, 64,
        66, 69, 72, 75, 78,
        80, 83, 86, 89, 92
    ]
}

df = pd.DataFrame(data)

print("Song Dataset:")
print(df)

# Save dataset
df.to_csv("song_popularity.csv", index=False)

# --------------------------------------------------
# Step 2: Check Dataset Information
# --------------------------------------------------

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

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

# --------------------------------------------------
# Step 3: Preprocess Dataset
# --------------------------------------------------

features = [
    "Danceability",
    "Energy",
    "Tempo"
]

target = "Popularity"

# Convert features to numeric
for column in features + [target]:
    df[column] = pd.to_numeric(
        df[column],
        errors="coerce"
    )

# Remove missing values
df = df.dropna()

# --------------------------------------------------
# Step 4: Select Features and Target
# --------------------------------------------------

X = df[features]
y = df[target]

# --------------------------------------------------
# Step 5: Split Dataset
# --------------------------------------------------

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

# --------------------------------------------------
# Step 6: Create Polynomial Regression Model
# --------------------------------------------------

degree = 2

model = make_pipeline(
    PolynomialFeatures(degree=degree),
    LinearRegression()
)

# Train model
model.fit(X_train, y_train)

# --------------------------------------------------
# Step 7: Predict Popularity
# --------------------------------------------------

y_pred = model.predict(X_test)

result = pd.DataFrame({
    "Actual Popularity": y_test.values,
    "Predicted Popularity": y_pred
})

print("\nActual vs Predicted Popularity:")
print(result)

# --------------------------------------------------
# Step 8: Evaluate Model
# --------------------------------------------------

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----- Polynomial Regression Evaluation -----")

print("Mean Absolute Error (MAE):", round(mae, 2))

print("Root Mean Squared Error (RMSE):", round(rmse, 2))

print("R2 Score:", round(r2, 4))

# --------------------------------------------------
# Step 9: Visualize Fitted Polynomial Curve
# --------------------------------------------------

# For visualization, use Danceability as the
# main predictor while keeping the other features
# fixed at their mean values.

danceability_range = np.linspace(
    df["Danceability"].min(),
    df["Danceability"].max(),
    100
)

visual_data = pd.DataFrame({
    "Danceability": danceability_range,
    "Energy": df["Energy"].mean(),
    "Tempo": df["Tempo"].mean()
})

predicted_curve = model.predict(visual_data)

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

# Actual observations
plt.scatter(
    df["Danceability"],
    df["Popularity"],
    label="Actual Songs"
)

# Polynomial fitted curve
plt.plot(
    danceability_range,
    predicted_curve,
    linewidth=2,
    label="Polynomial Regression Curve"
)

plt.xlabel("Danceability")
plt.ylabel("Song Popularity")
plt.title(
    "Polynomial Regression: Danceability vs Song Popularity"
)

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

plt.tight_layout()
plt.show() 

 
SET B
1. Apply Logistic Regression to classify smartphones into different price categories using specifications such as RAM, storage, battery capacity, and camera resolution. Assess the model using Accuracy, F1-Score, and Confusion Matrix. 

pip install pandas matplotlib seaborn scikit-learn

 

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

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, f1_score, confusion_matrix, classification_report

# ---------------------------------------------------------
# Step 1: Create Smartphone Dataset
# ---------------------------------------------------------

data = {
    "Smartphone": [
        "Phone1", "Phone2", "Phone3", "Phone4", "Phone5",
        "Phone6", "Phone7", "Phone8", "Phone9", "Phone10",
        "Phone11", "Phone12", "Phone13", "Phone14", "Phone15",
        "Phone16", "Phone17", "Phone18", "Phone19", "Phone20",
        "Phone21", "Phone22", "Phone23", "Phone24", "Phone25",
        "Phone26", "Phone27", "Phone28", "Phone29", "Phone30"
    ],

    "RAM_GB": [
        2, 3, 3, 4, 4, 4, 6, 6, 6, 8,
        8, 8, 8, 12, 12, 12, 12, 16, 16, 16,
        2, 3, 4, 6, 8, 12, 16, 4, 8, 12
    ],

    "Storage_GB": [
        32, 32, 64, 64, 128, 128, 128, 128, 256, 256,
        256, 128, 256, 256, 256, 512, 512, 512, 512, 512,
        32, 64, 64, 128, 256, 256, 512, 128, 256, 512
    ],

    "Battery_mAh": [
        3000, 3500, 4000, 4000, 4500, 4500, 5000, 5000, 4800, 5000,
        5000, 4500, 5000, 5000, 5200, 5000, 5500, 5000, 5200, 5500,
        3200, 3800, 4200, 4500, 5000, 5200, 5500, 4500, 5000, 5200
    ],

    "Camera_MP": [
        8, 12, 13, 16, 16, 20, 24, 32, 32, 48,
        48, 50, 50, 64, 64, 64, 108, 108, 200, 200,
        12, 16, 20, 32, 48, 64, 108, 32, 50, 108
    ],

    # 0 = Budget, 1 = Mid-Range, 2 = Premium
    "Price_Category": [
        0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
        1, 1, 2, 2, 2, 2, 2, 2, 2, 2,
        0, 0, 1, 1, 1, 2, 2, 1, 1, 2
    ]
}

df = pd.DataFrame(data)

print("----- Smartphone Dataset -----")
print(df)

# Save dataset
df.to_csv("smartphone_price.csv", index=False)

# ---------------------------------------------------------
# Step 2: Display Dataset Information
# ---------------------------------------------------------

print("\n----- Dataset Information -----")
df.info()

print("\n----- Missing Values -----")
print(df.isnull().sum())

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

features = [
    "RAM_GB",
    "Storage_GB",
    "Battery_mAh",
    "Camera_MP"
]

X = df[features]
y = df["Price_Category"]

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

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

print("\nTraining Records:", len(X_train))
print("Testing Records:", len(X_test))

# ---------------------------------------------------------
# Step 5: Apply Logistic Regression
# ---------------------------------------------------------

model = LogisticRegression(
    max_iter=1000,
    multi_class="auto"
)

model.fit(X_train, y_train)

# Predict test data
y_pred = model.predict(X_test)

print("\nActual Price Categories:")
print(y_test.values)

print("\nPredicted Price Categories:")
print(y_pred)

# ---------------------------------------------------------
# Step 6: Evaluate the Model
# ---------------------------------------------------------

accuracy = accuracy_score(y_test, y_pred)

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

print("\n----- Model Evaluation -----")
print("Accuracy :", round(accuracy, 4))
print("F1-Score :", round(f1, 4))

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

print(
    classification_report(
        y_test,
        y_pred,
        target_names=["Budget", "Mid-Range", "Premium"],
        zero_division=0
    )
)

# ---------------------------------------------------------
# Step 7: 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",
    xticklabels=["Budget", "Mid-Range", "Premium"],
    yticklabels=["Budget", "Mid-Range", "Premium"]
)

plt.xlabel("Predicted Category")
plt.ylabel("Actual Category")
plt.title("Confusion Matrix - Smartphone Price Classification")

plt.tight_layout()
plt.show()

# ---------------------------------------------------------
# Step 8: Predict Price Category for a New Smartphone
# ---------------------------------------------------------

new_phone = [[
    8,       # RAM in GB
    256,     # Storage in GB
    5000,    # Battery in mAh
    64       # Camera in MP
]]

prediction = model.predict(new_phone)

categories = {
    0: "Budget",
    1: "Mid-Range",
    2: "Premium"
}

print("\n----- New Smartphone Prediction -----")
print("RAM          :", new_phone[0][0], "GB")
print("Storage      :", new_phone[0][1], "GB")
print("Battery      :", new_phone[0][2], "mAh")
print("Camera       :", new_phone[0][3], "MP")
print("Predicted Price Category:", categories[prediction[0]]) 


2. Use a Gaming Dataset containing gameplay hours, achievements unlocked, in-game purchases, and friend count. Develop a Multiple Linear Regression model to predict player engagement score. Evaluate the model using MAE, MSE, RMSE, and R² Score.

pip install pandas numpy matplotlib scikit-learn

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

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import (
    mean_absolute_error,
    mean_squared_error,
    r2_score
)

# ---------------------------------------------------------
# Step 1: Create Gaming Dataset
# ---------------------------------------------------------

data = {
    "Player": [
        "P1", "P2", "P3", "P4", "P5",
        "P6", "P7", "P8", "P9", "P10",
        "P11", "P12", "P13", "P14", "P15",
        "P16", "P17", "P18", "P19", "P20",
        "P21", "P22", "P23", "P24", "P25"
    ],

    "Gameplay_Hours": [
        5, 8, 10, 12, 15,
        18, 20, 22, 25, 28,
        30, 32, 35, 38, 40,
        42, 45, 48, 50, 52,
        55, 58, 60, 65, 70
    ],

    "Achievements_Unlocked": [
        5, 8, 12, 15, 20,
        25, 28, 32, 36, 40,
        45, 48, 52, 55, 60,
        65, 68, 72, 76, 80,
        84, 88, 92, 96, 100
    ],

    "In_Game_Purchases": [
        100, 200, 300, 250, 500,
        600, 700, 800, 900, 1000,
        1200, 1300, 1500, 1600, 1800,
        2000, 2200, 2400, 2600, 2800,
        3000, 3200, 3500, 3800, 4000
    ],

    "Friend_Count": [
        5, 8, 10, 12, 15,
        18, 20, 22, 25, 28,
        30, 32, 35, 38, 40,
        42, 45, 48, 50, 55,
        58, 60, 65, 70, 75
    ],

    "Engagement_Score": [
        18, 24, 29, 33, 38,
        44, 47, 51, 56, 60,
        64, 67, 72, 76, 79,
        82, 85, 88, 91, 94,
        96, 97, 98, 99, 100
    ]
}

df = pd.DataFrame(data)

print("----- Gaming Dataset -----")
print(df)

# Save dataset
df.to_csv("gaming_dataset.csv", index=False)

# ---------------------------------------------------------
# Step 2: Display Dataset Information
# ---------------------------------------------------------

print("\n----- Dataset Information -----")
df.info()

print("\n----- Statistical Description -----")
print(df.describe())

print("\n----- Missing Values -----")
print(df.isnull().sum())

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

features = [
    "Gameplay_Hours",
    "Achievements_Unlocked",
    "In_Game_Purchases",
    "Friend_Count"
]

X = df[features]
y = df["Engagement_Score"]

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

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

print("\nTraining Records:", len(X_train))
print("Testing Records:", len(X_test))

# ---------------------------------------------------------
# Step 5: Apply Multiple Linear Regression
# ---------------------------------------------------------

model = LinearRegression()

model.fit(X_train, y_train)

# Predict test data
y_pred = model.predict(X_test)

# ---------------------------------------------------------
# Step 6: Display Actual and Predicted Values
# ---------------------------------------------------------

result = pd.DataFrame({
    "Actual Engagement Score": y_test.values,
    "Predicted Engagement Score": y_pred
})

print("\n----- Actual vs Predicted Values -----")
print(result)

# ---------------------------------------------------------
# Step 7: Regression Coefficients
# ---------------------------------------------------------

print("\n----- Regression Coefficients -----")

for feature, coefficient in zip(features, model.coef_):
    print(feature, ":", round(coefficient, 4))

print("\nIntercept:", round(model.intercept_, 4))

# ---------------------------------------------------------
# Step 8: Model Evaluation
# ---------------------------------------------------------

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 Evaluation -----")
print("Mean Absolute Error (MAE) :", round(mae, 4))
print("Mean Squared Error (MSE)  :", round(mse, 4))
print("Root Mean Squared Error (RMSE) :", round(rmse, 4))
print("R2 Score                  :", round(r2, 4))

# ---------------------------------------------------------
# Step 9: Actual vs Predicted Plot
# ---------------------------------------------------------

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

plt.scatter(
    y_test,
    y_pred,
    s=70
)

plt.xlabel("Actual Engagement Score")
plt.ylabel("Predicted Engagement Score")

plt.title(
    "Actual vs Predicted Player Engagement Score"
)

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

plt.tight_layout()
plt.show()

# ---------------------------------------------------------
# Step 10: Predict Engagement for a New Player
# ---------------------------------------------------------

new_player = [[
    35,      # Gameplay Hours
    55,      # Achievements
    1500,    # In-Game Purchases
    40       # Friend Count
]]

predicted_score = model.predict(new_player)

print("\n----- New Player Prediction -----")

print("Gameplay Hours       :", new_player[0][0])
print("Achievements         :", new_player[0][1])
print("In-Game Purchases    :", new_player[0][2])
print("Friend Count         :", new_player[0][3])

print(
    "Predicted Engagement Score:",
    round(predicted_score[0], 2)
)


3. Use a Sports Performance dataset containing attributes such as practice hours, fitness score, match  experience, and training sessions. Apply Simple Linear Regression and Multiple Linear Regression to predict player performance. Compare the models using MAE, MSE, RMSE, and R² Score, and identify the most suitable regression model.

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

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import (
    mean_absolute_error,
    mean_squared_error,
    r2_score
)

# ---------------------------------------------------------
# Step 1: Create Sports Performance Dataset
# ---------------------------------------------------------

data = {
    "Player": [
        "P1", "P2", "P3", "P4", "P5",
        "P6", "P7", "P8", "P9", "P10",
        "P11", "P12", "P13", "P14", "P15",
        "P16", "P17", "P18", "P19", "P20",
        "P21", "P22", "P23", "P24", "P25",
        "P26", "P27", "P28", "P29", "P30"
    ],

    "Practice_Hours": [
        2, 3, 4, 5, 6,
        7, 8, 9, 10, 11,
        12, 13, 14, 15, 16,
        17, 18, 19, 20, 21,
        22, 23, 24, 25, 26,
        27, 28, 29, 30, 32
    ],

    "Fitness_Score": [
        45, 48, 50, 52, 55,
        57, 60, 62, 64, 66,
        68, 70, 72, 74, 76,
        78, 80, 82, 84, 85,
        87, 88, 89, 91, 92,
        93, 94, 95, 96, 98
    ],

    "Match_Experience": [
        2, 3, 4, 5, 6,
        8, 9, 10, 12, 13,
        15, 16, 18, 20, 22,
        24, 25, 27, 28, 30,
        32, 35, 36, 38, 40,
        42, 45, 48, 50
    ],

    "Training_Sessions": [
        10, 12, 15, 18, 20,
        22, 25, 28, 30, 32,
        35, 38, 40, 42, 45,
        48, 50, 52, 55, 58,
        60, 62, 65, 68, 70,
        72, 75, 78, 80
    ],

    "Performance_Score": [
        42, 45, 48, 51, 54,
        57, 60, 62, 65, 67,
        70, 72, 74, 76, 78,
        80, 82, 84, 86, 87,
        89, 90, 91, 93, 94,
        95, 96, 97, 98, 99
    ]
}

df = pd.DataFrame(data)

print("----- Sports Performance Dataset -----")
print(df)

# Save dataset
df.to_csv("sports_performance.csv", index=False)

# ---------------------------------------------------------
# Step 2: Display Dataset Information
# ---------------------------------------------------------

print("\n----- Dataset Information -----")
df.info()

print("\n----- Statistical Description -----")
print(df.describe())

print("\n----- Missing Values -----")
print(df.isnull().sum())

# ---------------------------------------------------------
# Step 3: Define Variables
# ---------------------------------------------------------

features = [
    "Practice_Hours",
    "Fitness_Score",
    "Match_Experience",
    "Training_Sessions"
]

target = "Performance_Score"

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

X = df[features]
y = df[target]

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

print("\nTraining Records:", len(X_train))
print("Testing Records:", len(X_test))

# =========================================================
# MODEL 1: SIMPLE LINEAR REGRESSION
# =========================================================

# Use Practice Hours as the single predictor

X_simple = df[["Practice_Hours"]]

X_train_s, X_test_s, y_train_s, y_test_s = train_test_split(
    X_simple,
    y,
    test_size=0.20,
    random_state=42
)

simple_model = LinearRegression()

simple_model.fit(X_train_s, y_train_s)

simple_pred = simple_model.predict(X_test_s)

# ---------------------------------------------------------
# Simple Regression Evaluation
# ---------------------------------------------------------

simple_mae = mean_absolute_error(
    y_test_s,
    simple_pred
)

simple_mse = mean_squared_error(
    y_test_s,
    simple_pred
)

simple_rmse = np.sqrt(simple_mse)

simple_r2 = r2_score(
    y_test_s,
    simple_pred
)

print("\n========================================")
print("SIMPLE LINEAR REGRESSION")
print("========================================")

print("Regression Equation:")
print(
    "Performance =",
    round(simple_model.intercept_, 2),
    "+",
    round(simple_model.coef_[0], 2),
    "* Practice Hours"
)

print("\nMAE  :", round(simple_mae, 4))
print("MSE  :", round(simple_mse, 4))
print("RMSE :", round(simple_rmse, 4))
print("R2   :", round(simple_r2, 4))

# ---------------------------------------------------------
# Simple Regression Plot
# ---------------------------------------------------------

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

plt.scatter(
    X_simple,
    y,
    label="Actual Data"
)

plt.plot(
    X_simple,
    simple_model.predict(X_simple),
    linewidth=2,
    label="Regression Line"
)

plt.xlabel("Practice Hours")
plt.ylabel("Performance Score")
plt.title("Simple Linear Regression")
plt.legend()
plt.grid(True, linestyle="--", alpha=0.5)

plt.tight_layout()
plt.show()

# =========================================================
# MODEL 2: MULTIPLE LINEAR REGRESSION
# =========================================================

multiple_model = LinearRegression()

multiple_model.fit(
    X_train,
    y_train
)

multiple_pred = multiple_model.predict(X_test)

# ---------------------------------------------------------
# Multiple Regression Evaluation
# ---------------------------------------------------------

multiple_mae = mean_absolute_error(
    y_test,
    multiple_pred
)

multiple_mse = mean_squared_error(
    y_test,
    multiple_pred
)

multiple_rmse = np.sqrt(multiple_mse)

multiple_r2 = r2_score(
    y_test,
    multiple_pred
)

print("\n========================================")
print("MULTIPLE LINEAR REGRESSION")
print("========================================")

print("\nRegression Coefficients:")

for feature, coefficient in zip(
    features,
    multiple_model.coef_
):
    print(
        feature,
        ":",
        round(coefficient, 4)
    )

print(
    "\nIntercept:",
    round(multiple_model.intercept_, 4)
)

print("\nMAE  :", round(multiple_mae, 4))
print("MSE  :", round(multiple_mse, 4))
print("RMSE :", round(multiple_rmse, 4))
print("R2   :", round(multiple_r2, 4))

# ---------------------------------------------------------
# Actual vs Predicted Plot
# ---------------------------------------------------------

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

plt.scatter(
    y_test,
    multiple_pred,
    s=70
)

plt.xlabel("Actual Performance Score")
plt.ylabel("Predicted Performance Score")

plt.title(
    "Multiple Linear Regression: "
    "Actual vs Predicted Performance"
)

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

plt.tight_layout()
plt.show()

# =========================================================
# MODEL COMPARISON
# =========================================================

comparison = pd.DataFrame({
    "Model": [
        "Simple Linear Regression",
        "Multiple Linear Regression"
    ],

    "MAE": [
        simple_mae,
        multiple_mae
    ],

    "MSE": [
        simple_mse,
        multiple_mse
    ],

    "RMSE": [
        simple_rmse,
        multiple_rmse
    ],

    "R2 Score": [
        simple_r2,
        multiple_r2
    ]
})

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

print(
    comparison.round(4)
)

# ---------------------------------------------------------
# Identify Best Model
# ---------------------------------------------------------

if multiple_r2 > simple_r2:
    best_model = "Multiple Linear Regression"
else:
    best_model = "Simple Linear Regression"

print("\nMost Suitable Model:", best_model)

# ---------------------------------------------------------
# Predict Performance of a New Player
# ---------------------------------------------------------

new_player = [[
    15,     # Practice Hours
    80,     # Fitness Score
    20,     # Match Experience
    45      # Training Sessions
]]

predicted_performance = multiple_model.predict(
    new_player
)

print("\n----- New Player Prediction -----")

print("Practice Hours     :", new_player[0][0])
print("Fitness Score      :", new_player[0][1])
print("Match Experience   :", new_player[0][2])
print("Training Sessions  :", new_player[0][3])

print(
    "Predicted Performance Score:",
    round(predicted_performance[0], 2)
)


SET C
1. Download a Mobile Phone Specifications dataset. Apply Multiple Linear Regression to predict mobile prices using features such as RAM, storage, battery capacity, and camera resolution. Categorize mobiles into Budget and Premium segments and build a Logistic Regression model for  classification. Compare the performance of both models using appropriate regression and classification metrics.

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.linear_model import LinearRegression, LogisticRegression
from sklearn.metrics import (
    mean_absolute_error,
    mean_squared_error,
    r2_score,
    accuracy_score,
    precision_score,
    recall_score,
    f1_score,
    confusion_matrix,
    classification_report
)

# =========================================================
# STEP 1: READ MOBILE PHONE DATASET
# =========================================================

# Downloaded dataset file
df = pd.read_csv("mobile_phone.csv")

print("========== MOBILE PHONE DATASET ==========")
print(df.head())

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

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

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

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

# =========================================================
# STEP 3: PREPROCESS DATA
# =========================================================

# Change these names if your dataset uses different names
features = [
    "RAM",
    "Storage",
    "Battery",
    "Camera"
]

target = "Price"

# Convert selected columns to numeric
for column in features + [target]:
    df[column] = pd.to_numeric(
        df[column],
        errors="coerce"
    )

# Remove missing values
df = df.dropna()

print("\n========== CLEANED DATASET ==========")
print(df[features + [target]].head())

# =========================================================
# PART A: MULTIPLE LINEAR REGRESSION
# =========================================================

print("\n\n==========================================")
print("MULTIPLE LINEAR REGRESSION")
print("==========================================")

X = df[features]
y = df[target]

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

print("\nTraining Records:", len(X_train))
print("Testing Records :", len(X_test))

# Create model
linear_model = LinearRegression()

# Train model
linear_model.fit(X_train, y_train)

# Predict prices
y_pred = linear_model.predict(X_test)

# ---------------------------------------------------------
# Regression Coefficients
# ---------------------------------------------------------

print("\nRegression Coefficients:")

for feature, coefficient in zip(
    features,
    linear_model.coef_
):
    print(
        feature,
        ":",
        round(coefficient, 4)
    )

print(
    "\nIntercept:",
    round(linear_model.intercept_, 4)
)

# ---------------------------------------------------------
# Actual vs Predicted Price
# ---------------------------------------------------------

result = pd.DataFrame({
    "Actual Price": y_test.values,
    "Predicted Price": y_pred
})

print("\n========== ACTUAL VS PREDICTED PRICE ==========")
print(result)

# ---------------------------------------------------------
# Regression Metrics
# ---------------------------------------------------------

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========== REGRESSION EVALUATION ==========")
print("MAE  :", round(mae, 2))
print("MSE  :", round(mse, 2))
print("RMSE :", round(rmse, 2))
print("R2 Score :", round(r2, 4))

# ---------------------------------------------------------
# Actual vs Predicted Price Plot
# ---------------------------------------------------------

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

plt.scatter(
    y_test,
    y_pred,
    s=70
)

# Reference line
minimum = min(y_test.min(), y_pred.min())
maximum = max(y_test.max(), y_pred.max())

plt.plot(
    [minimum, maximum],
    [minimum, maximum],
    linewidth=2
)

plt.xlabel("Actual Mobile Price")
plt.ylabel("Predicted Mobile Price")

plt.title(
    "Multiple Linear Regression: "
    "Actual vs Predicted Price"
)

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

plt.tight_layout()
plt.show()

# =========================================================
# PART B: CREATE BUDGET / PREMIUM CATEGORY
# =========================================================

print("\n\n==========================================")
print("MOBILE PRICE CATEGORIZATION")
print("==========================================")

# Find median price as the classification threshold
threshold = df["Price"].median()

print(
    "Price Classification Threshold:",
    threshold
)

# 0 = Budget
# 1 = Premium
df["Price_Category"] = np.where(
    df["Price"] <= threshold,
    0,
    1
)

print("\nPrice Categories:")
print("0 = Budget")
print("1 = Premium")

print("\nCategory Distribution:")
print(
    df["Price_Category"].value_counts()
)

# =========================================================
# PART C: LOGISTIC REGRESSION
# =========================================================

print("\n\n==========================================")
print("LOGISTIC REGRESSION")
print("==========================================")

X_class = df[features]
y_class = df["Price_Category"]

# Split data
X_train_c, X_test_c, y_train_c, y_test_c = train_test_split(
    X_class,
    y_class,
    test_size=0.20,
    random_state=42,
    stratify=y_class
)

print("\nTraining Records:", len(X_train_c))
print("Testing Records :", len(X_test_c))

# Create Logistic Regression model
logistic_model = LogisticRegression(
    max_iter=2000
)

# Train model
logistic_model.fit(
    X_train_c,
    y_train_c
)

# Predict categories
y_pred_c = logistic_model.predict(X_test_c)

# ---------------------------------------------------------
# Classification Results
# ---------------------------------------------------------

print("\nActual Categories:")
print(y_test_c.values)

print("\nPredicted Categories:")
print(y_pred_c)

# ---------------------------------------------------------
# Classification Metrics
# ---------------------------------------------------------

accuracy = accuracy_score(
    y_test_c,
    y_pred_c
)

precision = precision_score(
    y_test_c,
    y_pred_c,
    zero_division=0
)

recall = recall_score(
    y_test_c,
    y_pred_c,
    zero_division=0
)

f1 = f1_score(
    y_test_c,
    y_pred_c,
    zero_division=0
)

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

print("Accuracy  :", round(accuracy, 4))
print("Precision :", round(precision, 4))
print("Recall    :", round(recall, 4))
print("F1-Score  :", round(f1, 4))

# ---------------------------------------------------------
# Classification Report
# ---------------------------------------------------------

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

print(
    classification_report(
        y_test_c,
        y_pred_c,
        target_names=[
            "Budget",
            "Premium"
        ],
        zero_division=0
    )
)

# =========================================================
# STEP 4: CONFUSION MATRIX
# =========================================================

cm = confusion_matrix(
    y_test_c,
    y_pred_c
)

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

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

sns.heatmap(
    cm,
    annot=True,
    fmt="d",
    xticklabels=[
        "Budget",
        "Premium"
    ],
    yticklabels=[
        "Budget",
        "Premium"
    ]
)

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

plt.title(
    "Confusion Matrix - Mobile Price Classification"
)

plt.tight_layout()
plt.show()

# =========================================================
# STEP 5: COMPARE THE MODELS
# =========================================================

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

comparison = pd.DataFrame({
    "Model": [
        "Multiple Linear Regression",
        "Logistic Regression"
    ],

    "MAE": [
        mae,
        np.nan
    ],

    "MSE": [
        mse,
        np.nan
    ],

    "RMSE": [
        rmse,
        np.nan
    ],

    "R2 Score": [
        r2,
        np.nan
    ],

    "Accuracy": [
        np.nan,
        accuracy
    ],

    "F1 Score": [
        np.nan,
        f1
    ]
})

print(
    comparison.round(4)
)

# =========================================================
# STEP 6: PREDICT A NEW MOBILE
# =========================================================

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

# Example:
# RAM = 8 GB
# Storage = 256 GB
# Battery = 5000 mAh
# Camera = 64 MP

new_mobile = [[
    8,
    256,
    5000,
    64
]]

# Predict actual price
predicted_price = linear_model.predict(
    new_mobile
)

# Predict category
predicted_category = logistic_model.predict(
    new_mobile
)

print(
    "Predicted Mobile Price:",
    round(predicted_price[0], 2)
)

if predicted_category[0] == 0:
    print("Predicted Category: Budget")
else:
    print("Predicted Category: Premium")


2. Use a YouTube Video Statistics dataset to predict video views using Simple Linear Regression based on the number of likes. Convert the target variable into Popular and Non-Popular categories using a suitable threshold and apply Logistic Regression. Evaluate the regression model using MAE, MSE, RMSE, and R² Score, and evaluate the classification model using Accuracy, Precision, Recall, F1-Score, and Confusion Matrix. 

 

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.linear_model import LinearRegression, LogisticRegression
from sklearn.metrics import (
    mean_absolute_error,
    mean_squared_error,
    r2_score,
    accuracy_score,
    precision_score,
    recall_score,
    f1_score,
    confusion_matrix,
    classification_report
)

# =========================================================
# STEP 1: READ YOUTUBE DATASET
# =========================================================

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

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

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

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

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

# =========================================================
# STEP 2: PREPROCESSING
# =========================================================

# Change these column names if your dataset uses different names
# Example: views, likes

df["Views"] = pd.to_numeric(
    df["Views"],
    errors="coerce"
)

df["Likes"] = pd.to_numeric(
    df["Likes"],
    errors="coerce"
)

# Remove missing values
df = df.dropna(
    subset=["Views", "Likes"]
)

print("\n========== CLEANED DATA ==========")
print(df[["Likes", "Views"]].head())

# =========================================================
# PART A: SIMPLE LINEAR REGRESSION
# =========================================================

print("\n==========================================")
print("SIMPLE LINEAR REGRESSION")
print("==========================================")

# Independent variable = Likes
# Dependent variable = Views

X = df[["Likes"]]
y = df["Views"]

# 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
)

print("\nTraining Records:", len(X_train))
print("Testing Records :", len(X_test))

# Create Linear Regression model
linear_model = LinearRegression()

# Train model
linear_model.fit(
    X_train,
    y_train
)

# Predict views
y_pred = linear_model.predict(
    X_test
)

# =========================================================
# REGRESSION EQUATION
# =========================================================

print("\nRegression Equation:")

print(
    "Views =",
    round(linear_model.intercept_, 2),
    "+",
    round(linear_model.coef_[0], 2),
    "* Likes"
)

# =========================================================
# ACTUAL VS PREDICTED VALUES
# =========================================================

result = pd.DataFrame({
    "Likes": X_test["Likes"].values,
    "Actual Views": y_test.values,
    "Predicted Views": y_pred
})

print("\n========== ACTUAL VS PREDICTED ==========")
print(result)

# =========================================================
# REGRESSION EVALUATION
# =========================================================

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========== REGRESSION EVALUATION ==========")

print("MAE  :", round(mae, 2))
print("MSE  :", round(mse, 2))
print("RMSE :", round(rmse, 2))
print("R2 Score :", round(r2, 4))

# =========================================================
# REGRESSION PLOT
# =========================================================

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

plt.scatter(
    df["Likes"],
    df["Views"],
    label="Actual Data"
)

# Sort values so regression line is displayed correctly
sorted_df = df.sort_values("Likes")

plt.plot(
    sorted_df["Likes"],
    linear_model.predict(
        sorted_df[["Likes"]]
    ),
    linewidth=2,
    label="Regression Line"
)

plt.xlabel("Number of Likes")
plt.ylabel("Number of Views")

plt.title(
    "Simple Linear Regression: "
    "Likes vs Views"
)

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

plt.tight_layout()
plt.show()

# =========================================================
# PART B: CREATE POPULAR / NON-POPULAR CATEGORY
# =========================================================

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

# Use median views as the popularity threshold
threshold = df["Views"].median()

print(
    "\nPopularity Threshold:",
    threshold
)

# 0 = Non-Popular
# 1 = Popular

df["Popularity"] = np.where(
    df["Views"] >= threshold,
    1,
    0
)

print("\nCategory Definition:")
print("0 = Non-Popular")
print("1 = Popular")

print("\nPopularity Distribution:")
print(
    df["Popularity"].value_counts()
)

# =========================================================
# PART C: LOGISTIC REGRESSION
# =========================================================

print("\n==========================================")
print("LOGISTIC REGRESSION")
print("==========================================")

# Use Likes to classify popularity
X_class = df[["Likes"]]
y_class = df["Popularity"]

# Split dataset
X_train_c, X_test_c, y_train_c, y_test_c = train_test_split(
    X_class,
    y_class,
    test_size=0.20,
    random_state=42,
    stratify=y_class
)

print("\nTraining Records:", len(X_train_c))
print("Testing Records :", len(X_test_c))

# Create Logistic Regression model
logistic_model = LogisticRegression(
    max_iter=2000
)

# Train model
logistic_model.fit(
    X_train_c,
    y_train_c
)

# Predict popularity
y_pred_c = logistic_model.predict(
    X_test_c
)

# =========================================================
# CLASSIFICATION RESULTS
# =========================================================

print("\nActual Categories:")
print(y_test_c.values)

print("\nPredicted Categories:")
print(y_pred_c)

# =========================================================
# CLASSIFICATION METRICS
# =========================================================

accuracy = accuracy_score(
    y_test_c,
    y_pred_c
)

precision = precision_score(
    y_test_c,
    y_pred_c,
    zero_division=0
)

recall = recall_score(
    y_test_c,
    y_pred_c,
    zero_division=0
)

f1 = f1_score(
    y_test_c,
    y_pred_c,
    zero_division=0
)

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

print("Accuracy  :", round(accuracy, 4))
print("Precision :", round(precision, 4))
print("Recall    :", round(recall, 4))
print("F1-Score  :", round(f1, 4))

# =========================================================
# CLASSIFICATION REPORT
# =========================================================

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

print(
    classification_report(
        y_test_c,
        y_pred_c,
        target_names=[
            "Non-Popular",
            "Popular"
        ],
        zero_division=0
    )
)

# =========================================================
# CONFUSION MATRIX
# =========================================================

cm = confusion_matrix(
    y_test_c,
    y_pred_c
)

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

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

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

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

plt.title(
    "Confusion Matrix - YouTube Popularity Classification"
)

plt.tight_layout()
plt.show()

# =========================================================
# PART D: PREDICT A NEW VIDEO
# =========================================================

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

# Example video with 10,000 likes
new_video = [[10000]]

# Predict number of views using Linear Regression
predicted_views = linear_model.predict(
    new_video
)

# Predict popularity using Logistic Regression
predicted_category = logistic_model.predict(
    new_video
)

print(
    "Number of Likes:",
    new_video[0][0]
)

print(
    "Predicted Views:",
    round(predicted_views[0], 2)
)

if predicted_category[0] == 1:
    print("Predicted Category: Popular")
else:
    print("Predicted Category: Non-Popular") 

Saturday, August 29, 2026

TYBCS DS & DA Assignment 3

 Assignment 3
Advanced Data Visualization tools

 Lab Assignment
SET A
1. A retail store has recorded the daily sales (₹) for two months. Create a suitable dataset and generate a Box Plot to identify any outliers. Customize the graph with appropriate title and labels.

import pandas as pd
import matplotlib.pyplot as plt

# -----------------------------------------
# Create Daily Sales Dataset for Two Months
# -----------------------------------------

data = {
    "Day": list(range(1, 61)),
    "Sales": [
        12500, 13200, 11800, 14500, 13900, 15200, 12800, 13500, 14200, 15000,
        15500, 14700, 13800, 12900, 14100, 13600, 14800, 15300, 13200, 12700,
        14500, 15100, 13900, 14300, 15700, 14900, 13500, 12800, 14000, 14600,
        15200, 13700, 14400, 15800, 14900, 13600, 14200, 15100, 14700, 13900,
        15500, 16000, 14800, 13400, 14100, 15300, 15900, 14600, 13800, 15000,
        16200, 14500, 13900, 14700, 15400, 16800, 14200, 13600, 15100, 17500,

        # Unusually high sales values (potential outliers)
        45000, 52000
    ]
}

df = pd.DataFrame(data)

# Display dataset
print("Daily Sales Dataset:")
print(df)

# Save dataset as CSV
df.to_csv("retail_sales.csv", index=False)


# -----------------------------------------
# Create Box Plot
# -----------------------------------------

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

plt.boxplot(
    df["Sales"],
    patch_artist=True,
    showmeans=True
)

plt.title("Box Plot of Daily Retail Store Sales")
plt.ylabel("Daily Sales (₹)")
plt.xlabel("Retail Store")

plt.grid(axis="y", linestyle="--", alpha=0.7)

plt.show()


# -----------------------------------------
# Identify Outliers using IQR Method
# -----------------------------------------

Q1 = df["Sales"].quantile(0.25)
Q3 = df["Sales"].quantile(0.75)

IQR = Q3 - Q1

lower_limit = Q1 - 1.5 * IQR
upper_limit = Q3 + 1.5 * IQR

outliers = df[
    (df["Sales"] < lower_limit) |
    (df["Sales"] > upper_limit)
]

print("\nQ1:", Q1)
print("Q3:", Q3)
print("IQR:", IQR)

print("\nLower Limit:", lower_limit)
print("Upper Limit:", upper_limit)

print("\nOutliers:")
print(outliers)


2. Write a Python program to generate a box plot to show the Interquartile range and outliers for the three species for each feature using IRIS data set.

import pandas as pd
import matplotlib.pyplot as plt

# Load Iris dataset
df = pd.read_csv("iris.csv")

# Display first five records
print(df.head())

# Features of Iris dataset
features = [
    "sepal_length",
    "sepal_width",
    "petal_length",
    "petal_width"
]

# Species
species = ["setosa", "versicolor", "virginica"]

# -----------------------------------------
# Create Box Plots for Each Feature
# -----------------------------------------

fig, axes = plt.subplots(2, 2, figsize=(12, 8))

for ax, feature in zip(axes.ravel(), features):

    data = [
        df[df["species"] == s][feature]
        for s in species
    ]

    ax.boxplot(
        data,
        labels=species,
        patch_artist=True
    )

    ax.set_title("Box Plot of " + feature)
    ax.set_xlabel("Species")
    ax.set_ylabel(feature)
    ax.grid(axis="y", linestyle="--", alpha=0.5)

plt.suptitle(
    "Iris Dataset: IQR and Outliers for Each Feature",
    fontsize=14
)

plt.tight_layout()
plt.show()


# -----------------------------------------
# Calculate IQR and Outliers
# -----------------------------------------

print("\nIQR and Outlier Analysis")
print("=" * 50)

for feature in features:

    print("\nFeature:", feature)

    for s in species:

        values = df[df["species"] == s][feature]

        Q1 = values.quantile(0.25)
        Q3 = values.quantile(0.75)

        IQR = Q3 - Q1

        lower_limit = Q1 - 1.5 * IQR
        upper_limit = Q3 + 1.5 * IQR

        outliers = values[
            (values < lower_limit) |
            (values > upper_limit)
        ]

        print("\nSpecies:", s)
        print("Q1:", Q1)
        print("Q3:", Q3)
        print("IQR:", IQR)
        print("Lower Limit:", lower_limit)
        print("Upper Limit:", upper_limit)
        print("Number of Outliers:", len(outliers)) 


3. Create a dataset containing monthly values of Temperature, Humidity, Rainfall, and Wind Speed.Generate a Heat Map to visualize the correlation between these weather parameters. 

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

# -----------------------------------------
# Create Monthly Weather Dataset
# -----------------------------------------

data = {
    "Month": [
        "January", "February", "March", "April",
        "May", "June", "July", "August",
        "September", "October", "November", "December"
    ],

    "Temperature": [
        18, 21, 25, 30, 34, 29,
        27, 28, 27, 25, 21, 18
    ],

    "Humidity": [
        55, 52, 48, 45, 42, 65,
        75, 78, 72, 65, 58, 54
    ],

    "Rainfall": [
        5, 8, 12, 20, 35, 180,
        250, 220, 190, 80, 25, 10
    ],

    "Wind Speed": [
        8, 9, 10, 12, 14, 18,
        20, 19, 17, 13, 10, 8
    ]
}

df = pd.DataFrame(data)

# Display dataset
print("Monthly Weather Dataset:")
print(df)

# Save dataset as CSV
df.to_csv("monthly_weather.csv", index=False)


# -----------------------------------------
# Calculate Correlation Matrix
# -----------------------------------------

correlation = df[
    ["Temperature", "Humidity", "Rainfall", "Wind Speed"]
].corr()

print("\nCorrelation Matrix:")
print(correlation)


# -----------------------------------------
# Generate Heat Map
# -----------------------------------------

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

sns.heatmap(
    correlation,
    annot=True,
    cmap="coolwarm",
    fmt=".2f",
    linewidths=0.5
)

plt.title("Correlation Heat Map of Weather Parameters")
plt.xlabel("Weather Parameters")
plt.ylabel("Weather Parameters")

plt.tight_layout()
plt.show()


4. Create a dataset containing information about different products such as Price, Rating, and Number of Sales. Generate a Dendrogram to group similar products based on their characteristics. 

import pandas as pd
import matplotlib.pyplot as plt

from scipy.cluster.hierarchy import dendrogram, linkage
from sklearn.preprocessing import StandardScaler

# -----------------------------------------
# Create Product Dataset
# -----------------------------------------

data = {
    "Product": [
        "Laptop", "Mobile", "Tablet", "Headphones",
        "Smartwatch", "Camera", "Television",
        "Speaker", "Keyboard", "Monitor"
    ],

    "Price": [
        60000, 25000, 35000, 5000, 8000,
        45000, 55000, 7000, 3000, 15000
    ],

    "Rating": [
        4.5, 4.2, 4.4, 4.0, 4.1,
        4.3, 4.6, 4.0, 4.2, 4.4
    ],

    "Number of Sales": [
        1200, 3500, 2200, 5000, 4200,
        1500, 1000, 3800, 5500, 2000
    ]
}

df = pd.DataFrame(data)

# Display dataset
print("Product Dataset:")
print(df)

# Save dataset as CSV
df.to_csv("product_data.csv", index=False)


# -----------------------------------------
# Select Numerical Features
# -----------------------------------------

features = [
    "Price",
    "Rating",
    "Number of Sales"
]

X = df[features]


# -----------------------------------------
# Standardize the Data
# -----------------------------------------

scaler = StandardScaler()

X_scaled = scaler.fit_transform(X)


# -----------------------------------------
# Perform Hierarchical Clustering
# -----------------------------------------

linked = linkage(
    X_scaled,
    method="ward"
)


# -----------------------------------------
# Generate Dendrogram
# -----------------------------------------

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

dendrogram(
    linked,
    labels=df["Product"].values,
    orientation="top",
    distance_sort="ascending"
)

plt.title("Dendrogram of Similar Products")
plt.xlabel("Products")
plt.ylabel("Euclidean Distance")

plt.xticks(rotation=45)

plt.tight_layout()
plt.show()
 

5. A survey was conducted to identify customers who use Netflix and Amazon Prime. Create a Venn Diagram showing - Customers using only Netflix, Customers using only Amazon Prime,Customers using both services
import pandas as pd  

 import matplotlib.pyplot as plt
from matplotlib_venn import venn2

# -----------------------------------------
# Create Customer Survey Dataset
# -----------------------------------------

data = {
    "Customer": [
        "C1", "C2", "C3", "C4", "C5",
        "C6", "C7", "C8", "C9", "C10",
        "C11", "C12"
    ],

    "Netflix": [
        "Yes", "Yes", "Yes", "Yes", "No",
        "No", "Yes", "No", "Yes", "No",
        "Yes", "No"
    ],

    "Amazon Prime": [
        "No", "Yes", "No", "Yes", "Yes",
        "Yes", "No", "Yes", "Yes", "No",
        "No", "Yes"
    ]
}

df = pd.DataFrame(data)

# Display dataset
print("Customer Survey Dataset:")
print(df)


# -----------------------------------------
# Create Sets
# -----------------------------------------

netflix_customers = set(
    df[df["Netflix"] == "Yes"]["Customer"]
)

amazon_customers = set(
    df[df["Amazon Prime"] == "Yes"]["Customer"]
)


# -----------------------------------------
# Display Customer Groups
# -----------------------------------------

only_netflix = netflix_customers - amazon_customers
only_amazon = amazon_customers - netflix_customers
both_services = netflix_customers & amazon_customers

print("\nCustomers using only Netflix:")
print(only_netflix)

print("\nCustomers using only Amazon Prime:")
print(only_amazon)

print("\nCustomers using both services:")
print(both_services)


# -----------------------------------------
# Generate Venn Diagram
# -----------------------------------------

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

venn = venn2(
    [netflix_customers, amazon_customers],
    set_labels=("Netflix", "Amazon Prime")
)

plt.title("Netflix and Amazon Prime Customer Survey")

plt.show()

 

Set B
1. Download the Auto MPG Dataset (mpg.csv)[ MPG, Horsepower, Weight, Cylinders, Acceleration.] and perform the following tasks:
a. Create Box Plots for MPG and Horsepower, Identify outliers
b. Generate a Heat Map showing correlations among numerical attributes.

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

# -----------------------------------------
# Load Auto MPG Dataset
# -----------------------------------------

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

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

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


# -----------------------------------------
# Select Required Numerical Attributes
# -----------------------------------------

columns = [
    "MPG",
    "Horsepower",
    "Weight",
    "Cylinders",
    "Acceleration"
]

data = df[columns].copy()

# Convert columns to numeric
# Invalid values will be converted to NaN
for column in columns:
    data[column] = pd.to_numeric(data[column], errors="coerce")

# Remove missing values
data = data.dropna()


# =========================================
# a) BOX PLOTS
# =========================================

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

plt.subplot(1, 2, 1)
plt.boxplot(data["MPG"])
plt.title("Box Plot of MPG")
plt.ylabel("MPG")

plt.subplot(1, 2, 2)
plt.boxplot(data["Horsepower"])
plt.title("Box Plot of Horsepower")
plt.ylabel("Horsepower")

plt.tight_layout()
plt.show()


# -----------------------------------------
# Identify Outliers using IQR
# -----------------------------------------

def find_outliers(column):
    Q1 = data[column].quantile(0.25)
    Q3 = data[column].quantile(0.75)

    IQR = Q3 - Q1

    lower_limit = Q1 - 1.5 * IQR
    upper_limit = Q3 + 1.5 * IQR

    outliers = data[
        (data[column] < lower_limit) |
        (data[column] > upper_limit)
    ]

    print("\n", column)
    print("Q1 =", Q1)
    print("Q3 =", Q3)
    print("IQR =", IQR)
    print("Lower Limit =", lower_limit)
    print("Upper Limit =", upper_limit)
    print("Number of Outliers =", len(outliers))

    print("Outlier Values:")
    print(outliers[column].values)


# Find MPG outliers
find_outliers("MPG")

# Find Horsepower outliers
find_outliers("Horsepower")


# =========================================
# b) CORRELATION HEAT MAP
# =========================================

correlation = data[columns].corr()

print("\nCorrelation Matrix:")
print(correlation)

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

sns.heatmap(
    correlation,
    annot=True,
    cmap="coolwarm",
    fmt=".2f",
    linewidths=0.5
)

plt.title("Correlation Heat Map of Auto MPG Dataset")
plt.xlabel("Numerical Attributes")
plt.ylabel("Numerical Attributes")

plt.tight_layout()
plt.show()
 

2. Create a dataset containing movie titles available on Netflix, Amazon Prime, and Disney+ and perform the following tasks:
a. Create a Venn Diagram comparing Netflix and Amazon Prime movie collections.
b. Generate a Word Cloud using movie genres or titles.

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib_venn import venn2
from wordcloud import WordCloud

# -----------------------------------------
# Create Movie Dataset
# -----------------------------------------

data = {
    "Movie": [
        "Inception",
        "Avengers",
        "The Lion King",
        "Titanic",
        "Interstellar",
        "Frozen",
        "Joker",
        "Black Panther",
        "Toy Story",
        "Spider Man",
        "The Dark Knight",
        "Moana",
        "Avatar",
        "Iron Man",
        "Coco"
    ],

    "Netflix": [
        "Yes", "No", "Yes", "Yes", "Yes",
        "No", "Yes", "No", "No", "Yes",
        "Yes", "No", "Yes", "No", "No"
    ],

    "Amazon Prime": [
        "Yes", "Yes", "No", "Yes", "No",
        "Yes", "No", "Yes", "Yes", "Yes",
        "No", "Yes", "No", "Yes", "No"
    ],

    "Disney+": [
        "No", "Yes", "Yes", "No", "No",
        "Yes", "No", "Yes", "Yes", "No",
        "No", "Yes", "Yes", "Yes", "Yes"
    ]
}

df = pd.DataFrame(data)

# Display dataset
print("Movie Dataset:")
print(df)

# Save dataset
df.to_csv("streaming_movies.csv", index=False)


# =========================================
# a) VENN DIAGRAM
# =========================================

# Create Netflix movie set
netflix_movies = set(
    df[df["Netflix"] == "Yes"]["Movie"]
)

# Create Amazon Prime movie set
amazon_movies = set(
    df[df["Amazon Prime"] == "Yes"]["Movie"]
)

# Display movie groups
print("\nNetflix Movies:")
print(netflix_movies)

print("\nAmazon Prime Movies:")
print(amazon_movies)

print("\nMovies Available on Both:")
print(netflix_movies & amazon_movies)

print("\nMovies Only on Netflix:")
print(netflix_movies - amazon_movies)

print("\nMovies Only on Amazon Prime:")
print(amazon_movies - netflix_movies)


# Create Venn Diagram
plt.figure(figsize=(8, 6))

venn2(
    [netflix_movies, amazon_movies],
    set_labels=("Netflix", "Amazon Prime")
)

plt.title("Netflix vs Amazon Prime Movie Collection")

plt.show()


# =========================================
# b) WORD CLOUD USING MOVIE TITLES
# =========================================

# Combine all movie titles
text = " ".join(df["Movie"])

# Generate Word Cloud
wordcloud = WordCloud(
    width=1000,
    height=600,
    background_color="white",
    max_words=100
).generate(text)

# Display Word Cloud
plt.figure(figsize=(12, 7))

plt.imshow(wordcloud, interpolation="bilinear")

plt.axis("off")

plt.title("Word Cloud of Movie Titles")

plt.show()
 

3. Create a dataset containing information about different cities.
Attributes: City, Population, GDP, Literacy Rate
Perform the following tasks:
a. Generate a Treemap showing city-wise population distribution.
b. Create a 3D Scatter Plot using Population, GDP, and Literacy Rate.

import pandas as pd
import matplotlib.pyplot as plt
import squarify
from mpl_toolkits.mplot3d import Axes3D

# -----------------------------------------
# Create City Dataset
# -----------------------------------------

data = {
    "City": [
        "Mumbai",
        "Delhi",
        "Bengaluru",
        "Chennai",
        "Hyderabad",
        "Pune",
        "Ahmedabad",
        "Kolkata",
        "Jaipur",
        "Nashik"
    ],

    "Population": [
        20.7, 19.0, 13.6, 11.5, 10.8,
        7.0, 8.0, 14.8, 4.0, 2.2
    ],

    "GDP": [
        310, 293, 110, 78, 75,
        69, 47, 150, 30, 25
    ],

    "Literacy Rate": [
        89.7, 86.2, 88.7, 90.2, 83.3,
        89.4, 88.3, 87.1, 84.0, 82.3
    ]
}

df = pd.DataFrame(data)

# Display dataset
print("City Dataset:")
print(df)

# Save dataset
df.to_csv("city_data.csv", index=False)


# =========================================
# a) TREEMAP
# =========================================

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

squarify.plot(
    sizes=df["Population"],
    label=df["City"],
    alpha=0.8
)

plt.title("City-wise Population Distribution")
plt.axis("off")

plt.show()


# =========================================
# b) 3D SCATTER PLOT
# =========================================

fig = plt.figure(figsize=(10, 7))

ax = fig.add_subplot(111, projection="3d")

# Create 3D scatter plot
scatter = ax.scatter(
    df["Population"],
    df["GDP"],
    df["Literacy Rate"],
    s=80
)

# Add city names
for i in range(len(df)):
    ax.text(
        df["Population"][i],
        df["GDP"][i],
        df["Literacy Rate"][i],
        df["City"][i]
    )

# Labels
ax.set_xlabel("Population (Million)")
ax.set_ylabel("GDP (Billion)")
ax.set_zlabel("Literacy Rate (%)")

ax.set_title(
    "3D Scatter Plot: Population, GDP and Literacy Rate"
)

plt.show()
 

4. Download a publicly available Earthquake Dataset (CSV format) containing latitude, longitude, and magnitude information.
Perform the following tasks: 
a. Create a Geospatial Visualization showing earthquake locations on a map.
b. Use marker size or color to represent earthquake magnitude.

Required Libraries

Install the required libraries:

pip install pandas matplotlib seaborn geopandas

 

import pandas as pd
import matplotlib.pyplot as plt
import geopandas as gpd

# -----------------------------------------
# Load Earthquake Dataset
# -----------------------------------------

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

# Display first five records
print("Earthquake Dataset:")
print(df.head())

# Display column names
print("\nColumn Names:")
print(df.columns)


# -----------------------------------------
# Select Required Columns
# -----------------------------------------

earthquakes = df[
    ["latitude", "longitude", "magnitude"]
].dropna()

print("\nCleaned Earthquake Data:")
print(earthquakes.head())


# -----------------------------------------
# Create GeoDataFrame
# -----------------------------------------

geometry = gpd.points_from_xy(
    earthquakes["longitude"],
    earthquakes["latitude"]
)

gdf = gpd.GeoDataFrame(
    earthquakes,
    geometry=geometry,
    crs="EPSG:4326"
)


# -----------------------------------------
# Load World Map
# -----------------------------------------

world = gpd.read_file(
    gpd.datasets.get_path("naturalearth_lowres")
)


# -----------------------------------------
# Create Geospatial Visualization
# -----------------------------------------

fig, ax = plt.subplots(figsize=(14, 8))

# Plot world map
world.plot(
    ax=ax,
    edgecolor="black",
    facecolor="lightgray"
)

# Plot earthquake locations
gdf.plot(
    ax=ax,
    column="magnitude",
    cmap="Reds",
    markersize=gdf["magnitude"] ** 3,
    alpha=0.7,
    legend=True,
    legend_kwds={
        "label": "Earthquake Magnitude",
        "orientation": "vertical"
    }
)

plt.title("Global Earthquake Locations and Magnitudes")
plt.xlabel("Longitude")
plt.ylabel("Latitude")

plt.show()
 

SET C
1. Download climate data for multiple countries.
Attributes: Country, Average Temperature, CO₂ Emissions, Rainfall, Forest Area Perform the following tasks:
a. Generate a Heat Map showing feature correlations.
b. Create a 3D Scatter Plot using Temperature, CO₂ Emissions, and Forest Area.
c. Create a Geospatial Visualization displaying country-wise climate indicators.

Required Libraries

Install the required libraries:

pip install pandas matplotlib seaborn geopandas

 

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import geopandas as gpd

# -----------------------------------------
# Load Climate Dataset
# -----------------------------------------

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

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

print("\nColumn Names:")
print(df.columns)


# -----------------------------------------
# Select Numerical Features
# -----------------------------------------

features = [
    "Average Temperature",
    "CO2 Emissions",
    "Rainfall",
    "Forest Area"
]

# Convert numerical columns to numeric
for column in features:
    df[column] = pd.to_numeric(df[column], errors="coerce")

# Remove missing values
df_clean = df.dropna(subset=features)


# =========================================
# a) CORRELATION HEAT MAP
# =========================================

correlation = df_clean[features].corr()

print("\nCorrelation Matrix:")
print(correlation)

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

sns.heatmap(
    correlation,
    annot=True,
    cmap="coolwarm",
    fmt=".2f",
    linewidths=0.5
)

plt.title("Correlation Heat Map of Climate Indicators")
plt.xlabel("Climate Parameters")
plt.ylabel("Climate Parameters")

plt.tight_layout()
plt.show()


# =========================================
# b) 3D SCATTER PLOT
# =========================================

fig = plt.figure(figsize=(10, 7))

ax = fig.add_subplot(111, projection="3d")

scatter = ax.scatter(
    df_clean["Average Temperature"],
    df_clean["CO2 Emissions"],
    df_clean["Forest Area"],
    s=70,
    c=df_clean["Rainfall"],
    cmap="viridis",
    alpha=0.8
)

# Axis labels
ax.set_xlabel("Average Temperature")
ax.set_ylabel("CO2 Emissions")
ax.set_zlabel("Forest Area")

ax.set_title(
    "3D Scatter Plot: Temperature, CO2 Emissions and Forest Area"
)

# Color bar represents rainfall
cbar = plt.colorbar(scatter, ax=ax, pad=0.1)
cbar.set_label("Rainfall")

plt.tight_layout()
plt.show()


# =========================================
# c) GEOSPATIAL VISUALIZATION
# =========================================

# Load world map
world = gpd.read_file(
    "https://naturalearth.s3.amazonaws.com/110m_cultural/"
    "ne_110m_admin_0_countries.zip"
)

# Match country names
# Adjust this column if your dataset uses a different country name column.
world["Country"] = world["NAME"]

merged = world.merge(
    df_clean,
    on="Country",
    how="left"
)

# -----------------------------------------
# Map 1: Average Temperature
# -----------------------------------------

fig, ax = plt.subplots(figsize=(15, 8))

merged.plot(
    column="Average Temperature",
    cmap="coolwarm",
    linewidth=0.5,
    edgecolor="black",
    legend=True,
    ax=ax,
    missing_kwds={
        "color": "lightgray",
        "label": "No Data"
    }
)

ax.set_title("Country-wise Average Temperature")
ax.set_axis_off()

plt.show()


# -----------------------------------------
# Map 2: CO2 Emissions
# -----------------------------------------

fig, ax = plt.subplots(figsize=(15, 8))

merged.plot(
    column="CO2 Emissions",
    cmap="Oranges",
    linewidth=0.5,
    edgecolor="black",
    legend=True,
    ax=ax,
    missing_kwds={
        "color": "lightgray",
        "label": "No Data"
    }
)

ax.set_title("Country-wise CO2 Emissions")
ax.set_axis_off()

plt.show()


# -----------------------------------------
# Map 3: Forest Area
# -----------------------------------------

fig, ax = plt.subplots(figsize=(15, 8))

merged.plot(
    column="Forest Area",
    cmap="Greens",
    linewidth=0.5,
    edgecolor="black",
    legend=True,
    ax=ax,
    missing_kwds={
        "color": "lightgray",
        "label": "No Data"
    }
)

ax.set_title("Country-wise Forest Area")
ax.set_axis_off()

plt.show()
 

2. Create a dataset containing information about different colleges.
Attributes: College Name, Placement %, Research Publications, Student Strength, Average Package Perform the following tasks:
a. Create a Treemap showing student distribution.
b. Generate a 3D Scatter Plot using Placement %, Publications, and Average Package.
c. Create a Heat Map for feature correlation.

 

Required Libraries

Install the required libraries if necessary:

pip install pandas matplotlib seaborn squarify
 
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import squarify

# -----------------------------------------
# Create College Dataset
# -----------------------------------------

data = {
"College Name": [
"ABC College",
"XYZ University",
"PQR Institute",
"LMN College",
"Sunrise University",
"Modern Institute",
"Global College",
"National Institute",
"City College",
"Tech University"
],

"Placement %": [
82, 91, 76, 88, 95,
79, 85, 93, 72, 90
],

"Research Publications": [
45, 85, 32, 68, 110,
40, 55, 95, 25, 78
],

"Student Strength": [
1800, 3200, 1500, 2400, 4000,
1700, 2200, 3500, 1200, 2800
],

"Average Package": [
5.2, 8.5, 4.8, 7.2, 10.5,
5.5, 6.5, 9.2, 4.2, 8.0
]
}

df = pd.DataFrame(data)

# Display dataset
print("College Dataset:")
print(df)

# Save dataset
df.to_csv("college_data.csv", index=False)


# =========================================
# a) TREEMAP - STUDENT DISTRIBUTION
# =========================================

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

squarify.plot(
sizes=df["Student Strength"],
label=df["College Name"],
alpha=0.8
)

plt.title("Student Distribution Across Colleges")
plt.axis("off")

plt.show()


# =========================================
# b) 3D SCATTER PLOT
# =========================================

fig = plt.figure(figsize=(10, 7))

ax = fig.add_subplot(111, projection="3d")

scatter = ax.scatter(
df["Placement %"],
df["Research Publications"],
df["Average Package"],
s=80
)

# Add college names to points
for i in range(len(df)):
ax.text(
df["Placement %"][i],
df["Research Publications"][i],
df["Average Package"][i],
df["College Name"][i],
fontsize=8
)

ax.set_xlabel("Placement Percentage (%)")
ax.set_ylabel("Research Publications")
ax.set_zlabel("Average Package (LPA)")

ax.set_title(
"3D Scatter Plot of College Performance"
)

plt.tight_layout()
plt.show()


# =========================================
# c) CORRELATION HEAT MAP
# =========================================

# Select numerical columns
numerical_data = df[
[
"Placement %",
"Research Publications",
"Student Strength",
"Average Package"
]
]

# Calculate correlation
correlation = numerical_data.corr()

print("\nCorrelation Matrix:")
print(correlation)


# Generate heat map
plt.figure(figsize=(9, 7))

sns.heatmap(
correlation,
annot=True,
cmap="coolwarm",
fmt=".2f",
linewidths=0.5
)

plt.title("Correlation Heat Map of College Features")
plt.xlabel("Features")
plt.ylabel("Features")

plt.tight_layout()
plt.show()