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

 

TYBCS DS & DA Assignment 2

 Assignment 2
Basic Data Visualization Tools

 Lab Assignment
SET A
1. Generate a dataset representing the daily temperatures (in °C) recorded over 50 days. Visualize the dataset using a Line Chart and Scatter Plot. Apply appropriate colors, titles, axis labels, and styling
options. [Attributes for dataset – (Date, Temperature (°C))]

 

# ==========================================
# SET A - Q1
# Daily Temperature Visualization
# ==========================================

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

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

np.random.seed(42)

dates = pd.date_range(
    start="2026-01-01",
    periods=50
)

temperature = np.random.randint(
    18,
    36,
    50
)

df = pd.DataFrame({
    "Date": dates,
    "Temperature": temperature
})

print("Temperature Dataset:")
print(df)

# ------------------------------------------
# Step 2: Line Chart
# ------------------------------------------

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

plt.plot(
    df["Date"],
    df["Temperature"],
    marker="o",
    linewidth=2
)

plt.title(
    "Daily Temperature for 50 Days"
)

plt.xlabel("Date")

plt.ylabel("Temperature (°C)")

plt.xticks(rotation=45)

plt.grid(True)

plt.tight_layout()

plt.show()

# ------------------------------------------
# Step 3: Scatter Plot
# ------------------------------------------

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

plt.scatter(
    df["Date"],
    df["Temperature"],
    s=60
)

plt.title(
    "Daily Temperature Scatter Plot"
)

plt.xlabel("Date")

plt.ylabel("Temperature (°C)")

plt.xticks(rotation=45)

plt.grid(True)

plt.tight_layout()

plt.show()

2. Consider the following dataset representing the number of books available in different sections of a library:
sections = ["Science", "Literature", "History", "Technology", "Commerce"]
books = [450, 380, 250, 520, 310]
Create a Bar Chart to visualize the number of books available in each section. Use different bar colors, add an appropriate title, and label both axes.

 

# ==========================================
# SET A - Q2
# Library Books Bar Chart
# ==========================================

import matplotlib.pyplot as plt

sections = [
    "Science",
    "Literature",
    "History",
    "Technology",
    "Commerce"
]

books = [
    450,
    380,
    250,
    520,
    310
]

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

plt.bar(
    sections,
    books
)

plt.title(
    "Number of Books in Different Library Sections"
)

plt.xlabel(
    "Library Section"
)

plt.ylabel(
    "Number of Books"
)

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

plt.tight_layout()

plt.show()

 

3. Generate a suitable dataset containing the ages of 100 gym members. Create a Histogram to visualize the age distribution of the members. Customize the graph using appropriate bins, colors,
title, and labels. [Attributes for dataset – Member ID, Age]

 

# ==========================================
# SET A - Q3
# Gym Members Age Histogram
# ==========================================

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

# ------------------------------------------
# Step 1: Generate Dataset
# ------------------------------------------

np.random.seed(10)

member_id = np.arange(
    1,
    101
)

age = np.random.randint(
    18,
    61,
    100
)

df = pd.DataFrame({
    "Member_ID": member_id,
    "Age": age
})

print("Gym Members Dataset:")
print(df.head())

print("\nTotal Members:")
print(len(df))

# ------------------------------------------
# Step 2: Histogram
# ------------------------------------------

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

plt.hist(
    df["Age"],
    bins=10,
    edgecolor="black"
)

plt.title(
    "Age Distribution of Gym Members"
)

plt.xlabel(
    "Age"
)

plt.ylabel(
    "Number of Members"
)

plt.grid(
    axis="y",
    alpha=0.4
)

plt.tight_layout()

plt.show()

4. A company conducted a survey to determine the preferred mode of transportation among employees as shown below:
transport = ["Bus", "Train", "Car", "Bike", "Bicycle"], employees = [120, 80, 60, 90, 30]
Create a Pie Chart to represent the percentage of employees using each mode of transportation. Display the percentage contribution of each category.
 

# ==========================================
# SET A - Q4
# Transportation Preference Pie Chart
# ==========================================

import matplotlib.pyplot as plt

transport = [
    "Bus",
    "Train",
    "Car",
    "Bike",
    "Bicycle"
]

employees = [
    120,
    80,
    60,
    90,
    30
]

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

plt.pie(
    employees,
    labels=transport,
    autopct="%1.1f%%",
    startangle=90
)

plt.title(
    "Employee Transportation Preference"
)

plt.show()
 

 SET B
1. The environmental department collected the following information for six cities:
cities = ["A", "B", "C", "D", "E", "F"],
population = [5, 8, 12, 15, 20, 25]
pollution = [40, 48, 55, 63, 72, 85],
green_cover = [350, 300, 250, 220, 180, 150]
Create a Bar Chart showing pollution levels and a Bubble Plot showing the relationship among population, pollution, and green cover.
 

Required File

mpg.csv

Expected columns:

MPG
Horsepower
Weight
Cylinders
Acceleration


---

Complete Code

# ==========================================
# SET B - Q1
# Auto MPG Visualization
# ==========================================

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

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

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

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

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

# ------------------------------------------
# Step 2: Convert Columns to Numeric
# ------------------------------------------

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

for col in columns:

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

# ------------------------------------------
# Step 3: Handle Missing Values
# ------------------------------------------

df = df.dropna(
    subset=columns
)

print("\nCleaned Dataset:")
print(df[columns].head())

# ------------------------------------------
# Step 4: Box Plot - MPG
# ------------------------------------------

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

plt.boxplot(
    df["MPG"]
)

plt.title(
    "Box Plot of MPG"
)

plt.ylabel(
    "MPG"
)

plt.grid(
    axis="y",
    alpha=0.3
)

plt.show()

# ------------------------------------------
# Step 5: Box Plot - Horsepower
# ------------------------------------------

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

plt.boxplot(
    df["Horsepower"]
)

plt.title(
    "Box Plot of Horsepower"
)

plt.ylabel(
    "Horsepower"
)

plt.grid(
    axis="y",
    alpha=0.3
)

plt.show()

# ------------------------------------------
# Step 6: Identify Outliers using IQR
# ------------------------------------------

def find_outliers(data):

    Q1 = data.quantile(0.25)

    Q3 = data.quantile(0.75)

    IQR = Q3 - Q1

    lower = Q1 - 1.5 * IQR

    upper = Q3 + 1.5 * IQR

    outliers = data[
        (data < lower) |
        (data > upper)
    ]

    return outliers, lower, upper


mpg_outliers, mpg_lower, mpg_upper = \
    find_outliers(df["MPG"])

hp_outliers, hp_lower, hp_upper = \
    find_outliers(df["Horsepower"])

print("\nMPG Outliers:")
print(mpg_outliers)

print("\nHorsepower Outliers:")
print(hp_outliers)

# ------------------------------------------
# Step 7: Correlation Matrix
# ------------------------------------------

corr = df[columns].corr()

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

# ------------------------------------------
# Step 8: Heat Map
# ------------------------------------------

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

sns.heatmap(
    corr,
    annot=True,
    fmt=".2f",
    cmap="coolwarm"
)

plt.title(
    "Correlation Heat Map - Auto MPG"
)

plt.tight_layout()

plt.show()

How to Run

Folder:

Assignment_2/
├── Assignment_2.ipynb
└── mpg.csv

दोन्ही files same folder मध्ये ठेवा.

Result

Box plots मधून MPG आणि Horsepower मधील distribution आणि possible outliers दिसतील. 
Heat map मधून numerical variables मधील correlation समजेल.

2. Load the Titanic dataset (titanic.csv)
Create a histogram showing the number of passengers in each passenger class. Create a Pie Chart showing the survival distribution.
 
import pandas as pd
import matplotlib.pyplot as plt

# Load Titanic dataset
df = pd.read_csv("titanic.csv")

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

# -------------------------------
# 1. Histogram: Passengers in each class
# -------------------------------

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

plt.hist(df["Pclass"], bins=[0.5, 1.5, 2.5, 3.5],
         edgecolor="black", rwidth=0.8)

plt.xticks([1, 2, 3])
plt.xlabel("Passenger Class")
plt.ylabel("Number of Passengers")
plt.title("Number of Passengers in Each Passenger Class")

plt.show()


# -------------------------------
# 2. Pie Chart: Survival Distribution
# -------------------------------

survival_counts = df["Survived"].value_counts()

labels = ["Not Survived", "Survived"]

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

plt.pie(
    survival_counts,
    labels=labels,
    autopct="%1.1f%%",
    startangle=90
)

plt.title("Titanic Survival Distribution")
plt.show()

3. Write a Python program to draw scatter plots to compare two features of the iris dataset.
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())

# Create scatter plot
plt.figure(figsize=(8, 5))

plt.scatter(df["sepal_length"], df["petal_length"])

# Add labels and title
plt.xlabel("Sepal Length")
plt.ylabel("Petal Length")
plt.title("Scatter Plot: Sepal Length vs Petal Length")

# Display grid
plt.grid(True)

# Show plot
plt.show()


SET C
1. Banking Loan Analysis
Generate a dataset containing loan information.
Attributes: Customer Age, Loan Amount, Annual Income, Credit Score
Perform the following tasks:
a) Create Histograms for Loan Amount and Credit Score.
b) Generate Scatter Plots for Income vs Loan Amount.
c) Create Bubble Plots using Income, Credit Score, and Loan Amount.
 

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

# -----------------------------------------
# Generate Banking Loan Dataset
# -----------------------------------------

np.random.seed(42)

data = {
    "Customer Age": np.random.randint(21, 61, 50),
    "Loan Amount": np.random.randint(50000, 1000000, 50),
    "Annual Income": np.random.randint(200000, 2000000, 50),
    "Credit Score": np.random.randint(500, 850, 50)
}

df = pd.DataFrame(data)

# Display dataset
print("Banking Loan Dataset:")
print(df)

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


# -----------------------------------------
# a) Histograms for Loan Amount
#    and Credit Score
# -----------------------------------------

# Histogram for Loan Amount
plt.figure(figsize=(8, 5))
plt.hist(df["Loan Amount"], bins=10, edgecolor="black")

plt.xlabel("Loan Amount")
plt.ylabel("Number of Customers")
plt.title("Distribution of Loan Amount")

plt.show()


# Histogram for Credit Score
plt.figure(figsize=(8, 5))
plt.hist(df["Credit Score"], bins=10, edgecolor="black")

plt.xlabel("Credit Score")
plt.ylabel("Number of Customers")
plt.title("Distribution of Credit Score")

plt.show()


# -----------------------------------------
# b) Scatter Plot: Income vs Loan Amount
# -----------------------------------------

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

plt.scatter(df["Annual Income"], df["Loan Amount"])

plt.xlabel("Annual Income")
plt.ylabel("Loan Amount")
plt.title("Annual Income vs Loan Amount")

plt.grid(True)
plt.show()


# -----------------------------------------
# c) Bubble Plot using Income,
#    Credit Score and Loan Amount
# -----------------------------------------

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

plt.scatter(
    df["Annual Income"],
    df["Credit Score"],
    s=df["Loan Amount"] / 1000,
    alpha=0.6
)

plt.xlabel("Annual Income")
plt.ylabel("Credit Score")
plt.title("Bubble Plot: Income, Credit Score and Loan Amount")

plt.grid(True)
plt.show() 

2. Smart City Traffic Analysis
Generate a dataset showing monthly traffic statistics.
Attributes: Month, Cars, Two-Wheelers, Public Transport Users
Perform the following tasks:
a) Create Line Charts showing traffic trends.
b) Generate Area Plots for cumulative traffic volume.
c) Create Pie Charts showing transportation mode share.
d) Analyze urban mobility patterns and congestion trends

import pandas as pd
import matplotlib.pyplot as plt

# -----------------------------------------
# Generate Smart City Traffic Dataset
# -----------------------------------------

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

    "Cars": [
        45000, 47000, 48000, 50000,
        52000, 54000, 55000, 57000,
        59000, 61000, 63000, 65000
    ],

    "Two-Wheelers": [
        60000, 62000, 64000, 66000,
        68000, 70000, 72000, 74000,
        76000, 78000, 80000, 82000
    ],

    "Public Transport Users": [
        30000, 31000, 32000, 33000,
        35000, 36000, 37000, 38000,
        40000, 41000, 43000, 45000
    ]
}

df = pd.DataFrame(data)

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

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


# -----------------------------------------
# a) Line Chart - Traffic Trends
# -----------------------------------------

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

plt.plot(df["Month"], df["Cars"], marker="o", label="Cars")
plt.plot(df["Month"], df["Two-Wheelers"], marker="o",
         label="Two-Wheelers")
plt.plot(df["Month"], df["Public Transport Users"],
         marker="o", label="Public Transport Users")

plt.xlabel("Month")
plt.ylabel("Number of Users/Vehicles")
plt.title("Monthly Traffic Trends")
plt.xticks(rotation=45)
plt.legend()
plt.grid(True)

plt.tight_layout()
plt.show()


# -----------------------------------------
# b) Area Plot - Cumulative Traffic Volume
# -----------------------------------------

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

plt.stackplot(
    df["Month"],
    df["Cars"],
    df["Two-Wheelers"],
    df["Public Transport Users"],
    labels=[
        "Cars",
        "Two-Wheelers",
        "Public Transport Users"
    ],
    alpha=0.7
)

plt.xlabel("Month")
plt.ylabel("Traffic Volume")
plt.title("Cumulative Traffic Volume")
plt.xticks(rotation=45)
plt.legend(loc="upper left")

plt.tight_layout()
plt.show()


# -----------------------------------------
# c) Pie Chart - Transportation Mode Share
# -----------------------------------------

# Calculate total values for each transportation mode
cars_total = df["Cars"].sum()
two_wheeler_total = df["Two-Wheelers"].sum()
public_transport_total = df["Public Transport Users"].sum()

values = [
    cars_total,
    two_wheeler_total,
    public_transport_total
]

labels = [
    "Cars",
    "Two-Wheelers",
    "Public Transport"
]

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

plt.pie(
    values,
    labels=labels,
    autopct="%1.1f%%",
    startangle=90
)

plt.title("Transportation Mode Share")
plt.show()


# -----------------------------------------
# d) Analyze Urban Mobility Patterns
# -----------------------------------------

df["Total Traffic"] = (
    df["Cars"]
    + df["Two-Wheelers"]
    + df["Public Transport Users"]
)

print("\nMonthly Total Traffic:")
print(df[["Month", "Total Traffic"]])

# Find month with maximum traffic
max_month = df.loc[df["Total Traffic"].idxmax(), "Month"]
max_traffic = df["Total Traffic"].max()

# Find month with minimum traffic
min_month = df.loc[df["Total Traffic"].idxmin(), "Month"]
min_traffic = df["Total Traffic"].min()

print("\nUrban Mobility Analysis:")
print("Month with maximum traffic:", max_month)
print("Maximum traffic volume:", max_traffic)

print("Month with minimum traffic:", min_month)
print("Minimum traffic volume:", min_traffic)

# Calculate percentage increase in total traffic
first_month = df["Total Traffic"].iloc[0]
last_month = df["Total Traffic"].iloc[-1]

increase = ((last_month - first_month) / first_month) * 100

print("Overall traffic increase: {:.2f}%".format(increase)) 

TYBCS- DS & DA Assignmnet 1

Assignment 1
Data Pre-Processing Techniques – Cleaning, Integration, Transformation, Reduction, and Discretization  

Lab Assignment
SET A
 

Q.1. Write a Python program to create an “employee” DataFrame with the following attributes:Employee_ID, Age, Department, Experience, and Performance_Score.Perform data cleaning by handling missing values, removing duplicates, and detecting outliers using the IQR method.
Solution:

#install command #
pip3 install pandas numpy
import pandas as pd
import numpy as np
# -------------------------------
# Create Employee DataFrame
# -------------------------------
employee = {
"Employee_ID": [101, 102, 103, 104, 105, 106, 107, 108, 108, 110],
"Age": [25, 30, np.nan, 45, 28, 60, 29, 27, 27, 120],
"Department": ["HR", "IT", "Finance", "HR", None, "IT", "Sales", "Finance", "Finance",
"IT"],
"Experience": [2, 5, 3, 20, 4, 35, np.nan, 2, 2, 1],
"Performance_Score": [85, 90, 78, np.nan, 88, 95, 82, 87, 87, 99]
}
df = pd.DataFrame(employee)
print("\nOriginal DataFrame")
print(df)
# -------------------------------
# Handle Missing Values
# -------------------------------
# Fill missing Age with mean
df["Age"] = df["Age"].fillna(df["Age"].mean())
# Fill missing Experience with median
df["Experience"] = df["Experience"].fillna(df["Experience"].median())
# Fill missing Performance Score with mean

df["Performance_Score"] =
df["Performance_Score"].fillna(df["Performance_Score"].mean())
# Fill missing Department with mode
df["Department"] = df["Department"].fillna(df["Department"].mode()[0])
print("\nDataFrame After Handling Missing Values")
print(df)
# -------------------------------
# Remove Duplicate Records
# -------------------------------
df = df.drop_duplicates()
print("\nDataFrame After Removing Duplicates")
print(df)
# -------------------------------
# Detect Outliers Using IQR Method
# -------------------------------
numeric_columns = ["Age", "Experience", "Performance_Score"]
for col in numeric_columns:
Q1 = df[col].quantile(0.25)
Q3 = df[col].quantile(0.75)
IQR = Q3 - Q1
lower_limit = Q1 - 1.5 * IQR
upper_limit = Q3 + 1.5 * IQR
outliers = df[(df[col] < lower_limit) | (df[col] > upper_limit)]
print("\n--------------------------------")
print("Column:", col)
print("Lower Limit:", lower_limit)
print("Upper Limit:", upper_limit)
if len(outliers) > 0:
print("Outliers Found:")
print(outliers)
else:
print("No Outliers Found")


Q.2.Write a Python program to create dataframes student_info, student_result and perform data integration on them.
Solution:

#install command :pip3 install pandas #
student_integration.py
import pandas as pd
# ---------------------------------
# Create student_info DataFrame
# ---------------------------------
student_info = pd.DataFrame({
"Student_ID": [101, 102, 103, 104, 105],
"Name": ["Amit", "Priya", "Rahul", "Sneha", "Rohan"],
"Age": [20, 21, 19, 22, 20],
"Department": ["CS", "IT", "CS", "ENTC", "IT"]
})
# ---------------------------------
# Create student_result DataFrame
# ---------------------------------
student_result = pd.DataFrame({
"Student_ID": [101, 102, 103, 104, 105],
"Subject": ["Python", "Python", "Python", "Python", "Python"],
"Marks": [85, 92, 78, 88, 95],
"Grade": ["A", "A+", "B+", "A", "A+"]
})
print("Student Information")
print(student_info)
print("\nStudent Result")

print(student_result)
# ---------------------------------
# Data Integration (Merge)
# ---------------------------------
integrated_data = pd.merge(student_info, student_result, on="Student_ID")
print("\nIntegrated Student Data")
print(integrated_data)

$ python3 student_integration.py
Student Information
Student_ID Name Age Department
0 101 Amit 20 CS
1 102 Priya 21 IT
2 103 Rahul 19 CS
3 104 Sneha 22 ENTC
4 105 Rohan 20 IT
Student Result
Student_ID Subject Marks Grade
0 101 Python 85 A
1 102 Python 92 A+
2 103 Python 78 B+
3 104 Python 88 A
4 105 Python 95 A+
Integrated Student Data
Student_ID Name Age Department Subject Marks Grade
0 101 Amit 20 CS Python 85 A
1 102 Priya 21 IT Python 92 A+
2 103 Rahul 19 CS Python 78 B+
3 104 Sneha 22 ENTC Python 88 A
4 105 Rohan 20 IT Python 95 A+


Q.3. Write a Python program to create a DataFrame using sales_data.csv with the attributes Product_ID, Price, Quantity_Sold, Discount, and Revenue. Apply data transformation techniques on the DataFrame and display the transformed data.
a) Min-Max Scaling
b) Standardization
c) Normalization
Solution:

#install command: pip3 install pandas scikit-learn#

sales_data.csv


Product_ID,Price,Quantity_Sold,Discount,Revenue
101,500,20,10,9000
102,700,15,5,9975
103,300,30,15,7650
104,1000,10,8,9200
105,450,25,12,9900

sales_transformation.py
import pandas as pd
from sklearn.preprocessing import MinMaxScaler, StandardScaler, Normalizer
# ---------------------------------------
# Read CSV File
# ---------------------------------------
df = pd.read_csv("sales_data.csv")
print("Original DataFrame")
print(df)
# Select numerical columns
numeric_columns = ["Price", "Quantity_Sold", "Discount", "Revenue"]
# ---------------------------------------
# a) Min-Max Scaling
# ---------------------------------------
minmax_scaler = MinMaxScaler()
minmax_data = df.copy()
minmax_data[numeric_columns] = minmax_scaler.fit_transform(df[numeric_columns])
print("\nMin-Max Scaled Data")
print(minmax_data)
# ---------------------------------------
# b) Standardization (Z-score)
# ---------------------------------------
standard_scaler = StandardScaler()
standard_data = df.copy()

standard_data[numeric_columns] = standard_scaler.fit_transform(df[numeric_columns])
print("\nStandardized Data")
print(standard_data)
# ---------------------------------------
# c) Normalization
# ---------------------------------------
normalizer = Normalizer()
normalized_data = df.copy()
normalized_data[numeric_columns] = normalizer.fit_transform(df[numeric_columns])
print("\nNormalized Data")
print(normalized_data)


Q.4Write a Python program to create a dataframe using customer_data.csv and perform encoding techniques:
[Consider appropriate attributes for following operations]
a) Label Encoding
b) One-Hot Encoding
Solution:

#install command : pip3 install pandas scikit-learn#
customer_data.csv
Customer_ID,Gender,City,Membership,Purchase_Amount
101,Male,Pune,Gold,2500
102,Female,Mumbai,Silver,1800
103,Male,Nashik,Gold,3200
104,Female,Pune,Platinum,4500
105,Male,Mumbai,Silver,2100
customer_encoding.py
import pandas as pd
from sklearn.preprocessing import LabelEncoder
# ---------------------------------
# Read CSV File
# ---------------------------------
df = pd.read_csv("customer_data.csv")

print("Original DataFrame")
print(df)
# ---------------------------------
# a) Label Encoding
# ---------------------------------
label_df = df.copy()
le = LabelEncoder()
# Label Encode Gender
label_df["Gender"] = le.fit_transform(label_df["Gender"])
# Label Encode Membership
label_df["Membership"] = le.fit_transform(label_df["Membership"])
print("\nDataFrame after Label Encoding")
print(label_df)
# ---------------------------------
# b) One-Hot Encoding
# ---------------------------------
onehot_df = pd.get_dummies(
df,
columns=["City", "Membership"],
dtype=int
)
print("\nDataFrame after One-Hot Encoding")
print(onehot_df)
#OUTPUT#
python3 customer_encoding.py
Original DataFrame
Customer_ID Gender City Membership Purchase_Amount
0 101 Male Pune Gold 2500
1 102 Female Mumbai Silver 1800
2 103 Male Nashik Gold 3200
3 104 Female Pune Platinum 4500
4 105 Male Mumbai Silver 2100
DataFrame after Label Encoding
Customer_ID Gender City Membership Purchase_Amount
0 101 1 Pune 0 2500

1 102 0 Mumbai 2 1800
2 103 1 Nashik 0 3200
3 104 0 Pune 1 4500
4 105 1 Mumbai 2 2100
DataFrame after One-Hot Encoding
Customer_ID Gender Purchase_Amount City_Mumbai City_Nashik City_Pune
Membership_Gold Membership_Platinum Membership_Silver
0 101 Male 2500 0 0 1 1 0
0
1 102 Female 1800 1 0 0 0 0
1
2 103 Male 3200 0 1 0 1 0
0
3 104 Female 4500 0 0 1 0 1
0
4 105 Male 2100 1 0 0 0 0
1
Q.5.Write a Python program to create a dataframe using age_data.csv and perform Data Discretization
using:
a) Equal Width Binning
b) Equal Frequency Binning

#install command: pip3 install pandas #
age_data.csv
Person_ID,Name,Age
1,Amit,18
2,Priya,22
3,Rahul,25
4,Sneha,28
5,Rohan,31
6,Neha,35
7,Ajay,40
8,Pooja,45
9,Karan,50
10,Anita,55

age_discretization.py
import pandas as pd
# ---------------------------------
# Read CSV File
# ---------------------------------
df = pd.read_csv("age_data.csv")
print("Original DataFrame")
print(df)
# ---------------------------------
# a) Equal Width Binning
# ---------------------------------
df["Equal_Width_Bin"] = pd.cut(df["Age"], bins=3)
print("\nDataFrame after Equal Width Binning")
print(df)
# ---------------------------------
# b) Equal Frequency Binning
# ---------------------------------
df["Equal_Frequency_Bin"] = pd.qcut(df["Age"], q=3)
print("\nDataFrame after Equal Frequency Binning")
print(df)

#output#
python3 age_discretization.py
Original DataFrame
Person_ID Name Age
0 1 Amit 18
1 2 Priya 22
2 3 Rahul 25
3 4 Sneha 28
4 5 Rohan 31
5 6 Neha 35
6 7 Ajay 40
7 8 Pooja 45
8 9 Karan 50
9 10 Anita 55

DataFrame after Equal Width Binning
Person_ID Name Age Equal_Width_Bin
0 1 Amit 18 (17.963, 30.333]
1 2 Priya 22 (17.963, 30.333]
2 3 Rahul 25 (17.963, 30.333]
3 4 Sneha 28 (17.963, 30.333]
4 5 Rohan 31 (30.333, 42.667]
5 6 Neha 35 (30.333, 42.667]
6 7 Ajay 40 (30.333, 42.667]
7 8 Pooja 45 (42.667, 55.0]
8 9 Karan 50 (42.667, 55.0]
9 10 Anita 55 (42.667, 55.0]
DataFrame after Equal Frequency Binning
Person_ID Name Age Equal_Width_Bin Equal_Frequency_Bin
0 1 Amit 18 (17.963, 30.333] (17.999, 28.0]
1 2 Priya 22 (17.963, 30.333] (17.999, 28.0]
2 3 Rahul 25 (17.963, 30.333] (17.999, 28.0]
3 4 Sneha 28 (17.963, 30.333] (17.999, 28.0]
4 5 Rohan 31 (30.333, 42.667] (28.0, 40.0]
5 6 Neha 35 (30.333, 42.667] (28.0, 40.0]
6 7 Ajay 40 (30.333, 42.667] (28.0, 40.0]
7 8 Pooja 45 (42.667, 55.0] (40.0, 55.0]
8 9 Karan 50 (42.667, 55.0] (40.0, 55.0]
9 10 Anita 55 (42.667, 55.0] (40.0, 55.0]

SET B
1. Write a Python program to create a dataframe using iris flower dataset and reduce 4D data to 2D data using PCA.
Solution:

#Install Command : pip3 install pandas scikit-learn matplotlib #
iris_pca.py
# Import required libraries
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
# -------------------------------
# Load Iris Dataset
# -------------------------------
iris = load_iris()

# Create DataFrame
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df["Species"] = iris.target
print("Original Iris Data (First 5 Rows)")
print(df.head())
# -------------------------------
# Apply PCA (4D to 2D)
# -------------------------------
X = iris.data
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X)
# Create DataFrame for PCA result
pca_df = pd.DataFrame(X_pca, columns=["Principal_Component_1",
"Principal_Component_2"])
pca_df["Species"] = iris.target
print("\n2D Data After PCA")
print(pca_df.head())
# -------------------------------
# Plot PCA Result
# -------------------------------
plt.figure(figsize=(8,6))
for i, species in enumerate(iris.target_names):
plt.scatter(
X_pca[iris.target == i, 0],
X_pca[iris.target == i, 1],
label=species
)
plt.title("PCA of Iris Dataset (4D to 2D)")
plt.xlabel("Principal Component 1")
plt.ylabel("Principal Component 2")
plt.legend()
plt.grid(True)
plt.show()

#OUTPUT#
kgdm@kgdm-OptiPlex-3000:~$ python3 iris_pca.py
Original Iris Data (First 5 Rows)
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) Species
0 5.1 3.5 1.4 0.2 0
1 4.9 3.0 1.4 0.2 0
2 4.7 3.2 1.3 0.2 0
3 4.6 3.1 1.5 0.2 0
4 5.0 3.6 1.4 0.2 0
2D Data After PCA
Principal_Component_1 Principal_Component_2 Species
0 -2.684126 0.319397 0
1 -2.714142 -0.177001 0
2 -2.888991 -0.144949 0
3 -2.745343 -0.318299 0
4 -2.728717 0.326755 0

Q.2.Write a Python program to perform data reduction techniques as
a) Feature Selection to identify important features for disease prediction.
b) Feature extraction (Use Patient_health.csv – Pno, Age, BP, Chol, Sugar(0 = No, 1 = Yes), BMI, Disease)
#install command : pip install pandas scikit-learn#

feature_reduction.py
# Import libraries
import pandas as pd
from sklearn.feature_selection import SelectKBest, chi2
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
# Load dataset
df = pd.read_csv("Patient_health.csv")
print("Original Dataset:")
print(df)
# Independent variables
X = df[['Age', 'BP', 'Chol', 'Sugar', 'BMI']]
# Target variable
y = df['Disease']
# -----------------------------
# (a) Feature Selection

# -----------------------------
selector = SelectKBest(score_func=chi2, k=3)
X_new = selector.fit_transform(X, y)
selected_features = X.columns[selector.get_support()]
print("\nSelected Important Features:")
print(selected_features)
print("\nFeature Scores:")
for feature, score in zip(X.columns, selector.scores_):
print(feature, ":", round(score, 2))
# -----------------------------
# (b) Feature Extraction (PCA)
# -----------------------------
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X_scaled)
print("\nData after PCA:")
pca_df = pd.DataFrame(X_pca, columns=['PC1', 'PC2'])
print(pca_df)
print("\nExplained Variance Ratio:")
print(pca.explained_variance_ratio_)

Patient_health.csv
Pno,Age,BP,Chol,Sugar,BMI,Disease
1,45,130,220,1,28.5,1
2,30,120,180,0,22.1,0
3,55,145,250,1,31.2,1
4,40,135,210,0,26.4,0
5,60,150,260,1,33.5,1
6,35,125,190,0,24.0,0
7,50,140,230,1,29.8,1
8,28,118,170,0,21.5,0
9,65,155,270,1,34.2,1
10,38,128,200,0,25.3,0
OUTPUT
kgdm@kgdm-OptiPlex-3000:~$ python3 feature_reduction.py
Original Dataset
Pno Age BP Chol Sugar BMI Disease
0 1 45 130 220 1 28.5 1
1 2 30 120 180 0 22.1 0
2 3 55 145 250 1 31.2 1
3 4 40 135 210 0 26.4 0

4 5 60 150 260 1 33.5 1
Selected Important Features:
Index(['Age', 'BP', 'Chol'], dtype='object')
Feature Scores
Age : 24.25
BP : 6.56
Chol : 35.96
Sugar : 5.0
BMI : 5.19
Reduced Data using PCA
Principal Component 1 Principal Component 2
0 0.376461 1.012546
1 -2.643857 0.097995
2 2.028674 0.162954
3 -0.820753 -0.776063
4 2.792598 -0.160510
5 -1.922656 -0.219351
6 1.220146 0.521821
7 -2.999687 0.251987
8 3.385629 -0.459506
9 -1.416554 -0.431872
Explained Variance Ratio
[0.94744106 0.04942025]


Q.3.Load Superstore dataset(Order_ID, Region, Category, Sales,Profit, Discount) and perform Data Cube Aggregation to analyze region-wise and category-wise sales performance.
#Install command: pip install pandas#

Superstore.csv
Order_ID,Region,Category,Sales,Profit,Discount
1001,East,Furniture,500,50,0.10
1002,West,Technology,800,120,0.05
1003,South,Office Supplies,300,40,0.00
1004,North,Furniture,700,80,0.15
1005,East,Technology,900,150,0.10
1006,West,Office Supplies,400,60,0.05
1007,South,Furniture,650,70,0.20
1008,North,Technology,1000,180,0.05
1009,East,Office Supplies,350,45,0.00
1010,West,Furniture,550,65,0.10

data_cube.py
import pandas as pd
# Load dataset
df = pd.read_csv("Superstore.csv")
print("Original Dataset")
print(df)
# -------------------------------
# 1. Region-wise Sales
# -------------------------------
print("\nRegion-wise Sales")
region_sales = df.groupby("Region")["Sales"].sum()
print(region_sales)
# -------------------------------
# 2. Category-wise Sales
# -------------------------------
print("\nCategory-wise Sales")
category_sales = df.groupby("Category")["Sales"].sum()
print(category_sales)
# -------------------------------
# 3. Region and Category-wise Sales & Profit
# -------------------------------
print("\nData Cube Aggregation (Region x Category)")
cube = df.pivot_table(
index="Region",
columns="Category",
values=["Sales", "Profit"],
aggfunc="sum",
fill_value=0,
margins=True
)
print(cube)

OUTPUT
$ python3 data_cube.py
Original Dataset
Order_ID Region Category Sales Profit Discount
0 1001 East Furniture 500 50 0.10
1 1002 West Technology 800 120 0.05
2 1003 South Office Supplies 300 40 0.00
3 1004 North Furniture 700 80 0.15
4 1005 East Technology 900 150 0.10

5 1006 West Office Supplies 400 60 0.05
6 1007 South Furniture 650 70 0.20
7 1008 North Technology 1000 180 0.05
8 1009 East Office Supplies 350 45 0.00
9 1010 West Furniture 550 65 0.10
Region-wise Sales
Region
East 1750
North 1700
South 950
West 1750
Name: Sales, dtype: int64
Category-wise Sales
Category
Furniture 2400
Office Supplies 1050
Technology 2700
Name: Sales, dtype: int64
Data Cube Aggregation (Region x Category)
Profit Sales
Category Furniture Office Supplies Technology All Furniture Office Supplies Technology All
Region
East 50 45 150 245 500 350 900 1750
North 80 0 180 260 700 0 1000 1700
South 70 40 0 110 650 300 0 950
West 65 60 120 245 550 400 800 1750
All 265 145 450 860 2400 1050 2700 6150

Q.4.Load Employee(eid, age, workclass, education, hours-per-week, income) dataset and perform Numerosity Reduction.
Solution:

#Install Command:pip install pandas scikit-learn#
Employee.csv
eid,age,workclass,education,hours-per-week,income
1,25,Private,Bachelors,40,<=50K
2,45,Government,Masters,50,>50K
3,35,Private,Bachelors,45,<=50K
4,50,Self-emp,PhD,60,>50K
5,28,Private,Graduate,38,<=50K
6,40,Government,Masters,48,>50K
7,32,Self-emp,Bachelors,42,<=50K
8,55,Private,PhD,55,>50K
9,29,Government,Graduate,40,<=50K
10,47,Private,Masters,52,>50K

numerosity_reduction.py

# Import libraries
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.cluster import KMeans
# Load dataset
df = pd.read_csv("Employee.csv")
print("Original Dataset:")
print(df)
# Encode categorical columns
le = LabelEncoder()
df['workclass'] = le.fit_transform(df['workclass'])
df['education'] = le.fit_transform(df['education'])
df['income'] = le.fit_transform(df['income'])
# Select features for clustering
X = df[['age', 'workclass', 'education', 'hours-per-week']]
# Apply K-Means (3 clusters)
kmeans = KMeans(n_clusters=3, random_state=42, n_init=10)
df['Cluster'] = kmeans.fit_predict(X)
print("\nDataset after Numerosity Reduction (Cluster Assignment):")
print(df)
print("\nCluster Centers:")
print(pd.DataFrame(kmeans.cluster_centers_,
columns=['age', 'workclass', 'education', 'hours-per-week']))

//OUTPUT//
$ python3 numerosity_reduction.py
Original Dataset:
eid age workclass education hours-per-week income
0 1 25 Private Bachelors 40 <=50K
1 2 45 Government Masters 50 >50K
2 3 35 Private Bachelors 45 <=50K
3 4 50 Self-emp PhD 60 >50K
4 5 28 Private Graduate 38 <=50K
5 6 40 Government Masters 48 >50K
6 7 32 Self-emp Bachelors 42 <=50K
7 8 55 Private PhD 55 >50K
8 9 29 Government Graduate 40 <=50K
9 10 47 Private Masters 52 >50K

Dataset after Numerosity Reduction (Cluster Assignment):
eid age workclass education hours-per-week income Cluster
0 1 25 1 0 40 0 1
1 2 45 0 2 50 1 2
2 3 35 1 0 45 0 1
3 4 50 2 3 60 1 0
4 5 28 1 1 38 0 1
5 6 40 0 2 48 1 2
6 7 32 2 0 42 0 1
7 8 55 1 3 55 1 0
8 9 29 0 1 40 0 1
9 10 47 1 2 52 1 2
Cluster Centers:
age workclass education hours-per-week
0 52.5 1.500000 3.0 57.5
1 29.8 1.000000 0.4 41.0
2 44.0 0.333333 2.0 50.0

 

Thursday, June 25, 2026

NEP-SYBCS -DBMS-I Assignment-2

 Assignment no.2 Data Definition Query (Create simple tables with referential integrity constraint).

Set A

Create tables for the information given below by giving appropriate integrity constraints as specified.

1. Create the following tables:

Table Name :-Property

Columns Column Name         Column Data Type         Constraints

1                 P-number             integer                             Primary key

2                 Description          varchar (50)                     Not null

3                 Area                     char(10)

Table Name:- Owner

Columns Column Name         Column Data Type         Constraints

1             Owner-name             varchar(50)                     Primary key

2             Address                     varchar (50)

3             Phone no                    integer

Relationship - A one-many relationships between owner and property. Define reference keys accordingly.

Queries:-

CREATE TABLE Owner (

    "Owner-name" VARCHAR(50) PRIMARY KEY,

    Address VARCHAR(50),

    "Phone no" INTEGER

);


CREATE TABLE Property (

    "P-number" INTEGER PRIMARY KEY,

    Description VARCHAR(50) NOT NULL,

    Area CHAR(10),

    "Owner-name" VARCHAR(50),

    FOREIGN KEY ("Owner-name") REFERENCES Owner("Owner-name")

);



2. Create the following tables:

Table Name:- Hospital

Columns Column Name         Column Data Type     Constraints

1             Hno                             integer                     Primary key

2             Name                         varchar (50)              Not null

3             City                             char(10)

Table Name:- Doctor

Columns         Column Name         Column Data Type         Constraints

1                     Dno                             integer                         Primary key

2                     Dname                         varchar (50)

3                     City                             char(10)

Relationship - A many-many relationships between hospital and doctor.

Query:-

CREATE TABLE Hospital (

    Hno INTEGER PRIMARY KEY,

    Name VARCHAR(50) NOT NULL,

    City CHAR(10)

);


CREATE TABLE Doctor (

    Dno INTEGER PRIMARY KEY,

    Dname VARCHAR(50),

    City CHAR(10)

);

CREATE TABLE Hospital_Doctor (

    Hno INTEGER,

    Dno INTEGER,

    PRIMARY KEY (Hno, Dno),

    FOREIGN KEY (Hno) REFERENCES Hospital(Hno),

    FOREIGN KEY (Dno) REFERENCES Doctor(Dno)

);


3. Create the following tables:

Table Name:- Patient

Columns    Column Name         Column Data Type         Constraints

1                 Pno                             integer                         Primary key

2                 Name                         varchar (50)                  Not null

3                 Address                      varchar(50)

Table Name:- Bed

Columns     Column Name         Column Data Type     Constraints

1                 Bedno                         integer                             Primary key

2                 Roomno                     integer                             Primary key

3                 Description                 varchar(50)

Relationship - a one–to-one relationship between Patient & Bed.

Query:-

CREATE TABLE Bed (

    Bedno INTEGER,

    Roomno INTEGER,

    Description VARCHAR(50),

    PRIMARY KEY (Bedno, Roomno)

);


CREATE TABLE Patient (

    Pno INTEGER PRIMARY KEY,

    Name VARCHAR(50) NOT NULL,

    Address VARCHAR(50),

    Bedno INTEGER,

    Roomno INTEGER UNIQUE,

    FOREIGN KEY (Bedno, Roomno) REFERENCES Bed(Bedno, Roomno)

);


Set B

1. Create the following tables:

Table Name:- Student

Columns     Column Name     Column Data Type         Constraints

1                 Sno                         integer                                 Primary key

2                 Name                     varchar (50)                         Not null

3                 Address                  varchar(50)

4                 Class                     varchar(10)

Table Name Teacher

Columns Column Name Column Data Type Constraints

1                 Tno                     integer                     Primary key

2                 Tname                integer                     Primary key

3                 Qualification     varchar(50)

4                 TotalExperience     float

5                 Salary                 float                         Should be greater than 0

Relationship - A Many–to-Many relationships between Student and Teacher with descriptive

attribute Marks.

Query:-

CREATE TABLE Student (

    Sno INTEGER PRIMARY KEY,

    Name VARCHAR(50) NOT NULL,

    Address VARCHAR(50),

    Class VARCHAR(10)

);


CREATE TABLE Teacher (

    Tno INTEGER PRIMARY KEY,

    Tname VARCHAR(50),

    Qualification VARCHAR(50),

    TotalExperience FLOAT,

    Salary FLOAT CHECK (Salary > 0)

);


CREATE TABLE Student_Teacher (

    Sno INTEGER,

    Tno INTEGER,

    Marks INTEGER,

    PRIMARY KEY (Sno, Tno),

    FOREIGN KEY (Sno) REFERENCES Student(Sno),

    FOREIGN KEY (Tno) REFERENCES Teacher(Tno)

);

2. Create the following tables:

Table Name Project

Columns       Column Name         Column Data Type         Constraints

1                     Pno                             integer                             Primary key

2                     Pname                         varchar (30)                     Not null

3                     Ptype                         char(20)

4                     Duration                     integer

Table Name Employee

Columns     Column Name         Column Data Type         Constraints

1                     Eno                             integer                         Primary key

2                     Ename                         varchar(20)

3                     Qualification               varchar(50)

4                     Join_Date                     date

5                     Salary                         float                     Should be greater than 0

Relationship -A Many–to-Many relationships between Project and Employee with descriptive

attribute Start date(date), no_of_hours_worked(integer).

Query:-

CREATE TABLE Project (

    Pno INTEGER PRIMARY KEY,

    Pname VARCHAR(30) NOT NULL,

    Ptype CHAR(20),

    Duration INTEGER

);

CREATE TABLE Employee (

    Eno INTEGER PRIMARY KEY,

    Ename VARCHAR(20),

    Qualification VARCHAR(50),

    Join_Date DATE,

    Salary FLOAT CHECK (Salary > 0)

);

CREATE TABLE Project_Employee (

    Pno INTEGER,

    Eno INTEGER,

    Start_Date DATE,

    No_of_Hours_Worked INTEGER,

    PRIMARY KEY (Pno, Eno),

    FOREIGN KEY (Pno) REFERENCES Project(Pno),

    FOREIGN KEY (Eno) REFERENCES Employee(Eno)

);