Index
[Link]. Date Experiment Page Signature Remarks
no.
1 08/07/25 Visualization
2 15/07/25 Pandas
3 22/07/25 Manipulating and
Rescaling Data
4 29/07/25 K-nearest Neighbours
5 05/08/25 Linear Transformation
6 12/08/25 Spam Filter using
Naïve Bayes’ classifier
7 19/08/25 Multiple Linear
Regression
8 02/09/25 Entropy of Partition
9 09/09/25 Simpson’s Paradox
10 16/09/25 Clustering
1. Data Visualization:
Aim: To visualize data using various types of graphs in Python.
import [Link] as plt
import numpy as np
# Sample data
x = [Link](1, 6)
y = [Link]([2, 4, 2, 17, 11])
a. Line Plot
Code: [Link](figsize=(6,4))
[Link](x, y, marker='o', linestyle='-', color='b')
[Link]("Line Plot")
[Link]("X-axis")
[Link]("Y-axis")
[Link]()
Result:
Program Executed Successfully
b. Pie Chart
Code: data = [Link](1000)
[Link](figsize=(6,4))
[Link](data, bins=20, color='g', edgecolor='black')
[Link]("Histogram")
[Link]()
Result:
Program Executed Successfully
c. Histogram
Code: sizes = [20, 30, 25, 25]
labels = ['A', 'B', 'C', 'D']
[Link](figsize=(6,6))
[Link](sizes, labels=labels, autopct='%1.1f%%', startangle=90)
[Link]("Pie Chart")
[Link]()
Result:
Program Executed Successfully
d. Bar Chart
Code: [Link](figsize=(6,4))
[Link](x, y, color='orange')
[Link]("Bar Chart")
[Link]("X-axis")
[Link]("Y-axis")
[Link]()
Result:
Program Executed Successfully
e. Scatter Plot
Code [Link](figsize=(6,4))
[Link](x, y, color='r', marker='x')
[Link]("Scatter Plot")
[Link]("X-axis")
[Link]("Y-axis")
[Link]()
Result:
Program Executed Successfully
f. Box and Whisker Plot
Code: [Link](figsize=(6,4))
[Link](data)
[Link]("Box and Whisker Plot")
[Link]()
Result:
Program Executed Successfully
2. Pandas Data Cleaning
Aim: To clean a dataset using Pandas by handling missing values and duplicates.
Concept: Data cleaning involves identifying and correcting errors or inconsistencies in data to
improve its quality. It's an essential preprocessing step in any data analysis or machine learning
project.
Real-world datasets often contain missing values or duplicate records. Data cleaning with
Pandas ensures that the dataset is consistent and ready for analysis or machine learning.
Functions like fillna(), drop_duplicates(), and rename() make it easy to clean and structure the
data for better results.
Objective:
• Remove duplicate records.
• Handle missing data using mean.
• Rename columns for better understanding.
• Export cleaned data to a CSV file.
Code: import pandas as pd
import numpy as np
df = [Link]({
'Name': ['kunal', 'kamal', [Link], 'akshit', 'shanky', 'harshit', 'Meera', [Link], 'Dev',
'Ananya'],
'Age': [25, [Link], 30, 22, 28, [Link], 35, 40, [Link], 27],
'Salary': [50000, 60000, None, 55000, 70000, 65000, None, 72000, 58000, None],
'Department': ['HR', 'IT', 'Finance', None, 'IT', 'HR', 'Finance', 'IT', None, 'HR']
})
print("Original DataFrame with Missing Values:\n")
print(df)
#df['Age'].fillna(df['Age'].mean(), inplace=True)
df['Age'] = df['Age'].mean()
#df['Salary'].fillna(df['Salary'].median(), inplace=True)
df['Salary'] = df['Salary'].mean()
#df['Name'].fillna('Unknown', inplace=True)
[Link]({'Name': 'Unknown'}, inplace=True)
[Link]({'Department': 'Unassigned'}, inplace=True)
print("\nCleaned DataFrame:\n")
print(df)
Result:
Program Executed Successfully
3. Manipulating and Rescaling data
Aim: To perform data manipulation and normalization on numerical features.
Concept: Data manipulation allows us to create new features like totals or averages. Rescaling
(normalization) ensures that values in different columns are within the same range, which
improves the accuracy and efficiency of many machine learning models, especially those based
on distance.
Feature scaling ensures that all numeric features are on the same scale, which improves the
performance of many machine learning models. MinMaxScaler rescales values between 0 and
1.
Objective:
• Create a new column by adding two features.
• Normalize values using MinMaxScaler.
Code: from [Link] import MinMaxScaler, StandardScaler
data = [Link]([[10, 200], [20, 300], [30, 400]], dtype=float)
# Min-Max Scaling (0 to 1)
scaler = MinMaxScaler()
scaled_data = scaler.fit_transform(data)
print("Min-Max Scaled Data:\n", scaled_data)
scaler_std = StandardScaler()
standardized_data = scaler_std.fit_transform(data)
print("Standardized Data:\n", standardized_data)
n, k = 3, 2
A = [Link](n, k)
x = [Link]([1, 2])
y=A@x
print("Matrix A:\n", A)
print("Input vector x:\n", x)
print("Mapped output y:\n", y)
Result:
Program Executed Successfully
4. Finding k-nearest neighbor
Aim: To find the nearest neighbors of a query point using the KNN algorithm.
Concept: The K-Nearest Neighbors (KNN) algorithm is based on the assumption that similar
things exist in close proximity. It uses Euclidean distance (or other metrics) to find the closest
points.
It is used for:
• Classification: Predict the label based on the majority class of neighbors.
• Recommendation systems: Find similar users/items.
• Anomaly detection: Identify outliers by distance.
• In this practical, NearestNeighbors from sklearn is used to locate the k closest data
points to a given query point — demonstrating the concept of similarity-based learning.
Objective:
• Use scikit-learn’s NearestNeighbors to compute closest points.
• Display distances and indices of neighbors.
Code: import numpy as np
from [Link] import NearestNeighbors
[Link](42)
X = [Link](1, 50, size=(20, 2))
print("Dataset (X):\n", X)
k=3
nbrs = NearestNeighbors(n_neighbors=k, algorithm='auto').fit(X)
query_point = [Link]([[25, 30]])
distances, indices = [Link](query_point)
print("\nQuery Point:", query_point)
print("Indices of Nearest Neighbors:", indices)
print("Distances to Neighbors:", distances)
print("\nNearest Neighbor Points:\n", X[indices])
Program Executed Successfully
Result:
Program Executed Successfully
5. Create a n*k matrix to represent a linear function that maps k-
dimensional vectors to n-dimensional vectors :
Aim: To represent a linear transformation using an n×k matrix.
Concept: Matrix operations are at the core of many ML algorithms.
A linear transformation maps vectors from one space to another using matrix multiplication.
This concept underlies:
• Linear Regression: The model is essentially a matrix transformation.
• Neural Networks: Each layer performs a transformation using weights (matrices).
• Dimensionality reduction (e.g., PCA): Transforms highdimensional data into a
lower-dimensional space.
By multiplying a matrix A (n×k) with a vector x (k×1), the output is a transformed vector y
(n×1). This simulates how inputs are processed in layers of a neural network or projected into
another space.
Objective:
• Use matrix multiplication to transform a vector.
• Understand how matrix shapes impact transformation.
Code: import numpy as np
from [Link] import NearestNeighbors
X = [Link]([[1,2],[3,4],[5,6],[7,8]])
print("Original Points (2D):\n", X)
A = [Link]([[2, 1],
[0, 3],
[4, 5]]) # (3x2 matrix)
X_transformed = X @ A.T # shape (4,3)
print("\nTransformed Points (3D):\n", X_transformed)
k=2
nbrs = NearestNeighbors(n_neighbors=k, algorithm='auto').fit(X_transformed)
query = [Link]([[2,3]]) @ A.T
print("\nQuery Point (Transformed):", query)
distances, indices = [Link](query)
print("\nIndices of Nearest Neighbors:", indices)
print("Distances to Neighbors:", distances)
Result:
Program Executed Successfully
6. Spam Filter using Naive Baye's :
Aim: To build a spam detection model using Naive Bayes.
Concept: Naive Bayes is a popular and fast classification algorithm based on Bayes' Theorem,
commonly used for text classification problems such as spam detection. It works by calculating
the probability that a given message belongs to a certain class (spam or ham) based on the
words it contains. It assumes that the presence of one word is independent of the presence of
others — hence the name "naive."
In this practical, we use CountVectorizer to convert text into numerical form and
MultinomialNB, which is suitable for text data represented as word frequencies.
Algorithm:
The algorithm uses Bayes’ Theorem:
𝑷(𝑩│𝑨).𝑷(𝑨)
𝑃(𝐴|𝐵) =
𝑷(𝑩)
Where:
• P(A∣B): Probability of class A (e.g. spam) given input B (message).
• P(B∣A): Probability of input B given class A.
• P(A): Prior probability of class A.
• P(B): Probability of the input message.
Objective:
• Load and preprocess a labeled SMS dataset.
• Convert text to numeric features using CountVectorizer.
• Train and evaluate a MultinomialNB model.
• Predict labels for new messages.
Code: import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
from [Link] import accuracy_score, classification_report
df = pd.read_csv("spam_ham_dataset.csv")
df = [Link](columns=['Unnamed: 0'])
X = df['text']
y = df['label_num']
vectorizer = CountVectorizer(stop_words='english')
X_vec = vectorizer.fit_transform(X)
X_train, X_test, y_train, y_test = train_test_split(X_vec, y, test_size=0.3,
random_state=42)
model = MultinomialNB()
[Link](X_train, y_train)
y_pred = [Link](X_test)
print(" Accuracy:", accuracy_score(y_test, y_pred))
print("\nClassification Report:\n", classification_report(y_test, y_pred,
target_names=['Ham','Spam']))
new_messages = [
"Congratulations! You won a free iPhone, claim your prize now!",
"Hey, are we still on for the meeting tomorrow?",
"Urgent: Your account has been suspended, click here to verify details!"
]
X_new = [Link](new_messages)
predictions = [Link](X_new)
print("\n Predictions for New Messages:")
for msg, label in zip(new_messages, predictions):
print(f"Message: '{msg}' --> {'Spam' if label==1 else 'Ham'}")
Result:
Program Executed Successfully.
7. Multiple Linear Regression
Aim: To implement and understand Multiple Linear Regression for predicting continuous
target variables, using multiple independent features, enabling accurate predictions and
insights into feature relationships.
Objective:
Understand the mathematical foundation of Multiple Linear Regression
Implement MLR from scratch and using scikit-learn
Perform comprehensive data preprocessing and feature engineering
Evaluate model performance using various metrics
Concept: Multiple Linear Regression (MLR) extends simple linear regression to handle
multiple independent variables to predict a dependent variable.
Code:
import numpy as np
import pandas as pd
import [Link] as plt
from sklearn.linear_model import LinearRegression
[Link](10)
X1 = [Link](50) * 10
X2 = [Link](50) * 5
y = 2*X1 + 3*X2 + [Link](50)*2
df = [Link]({'X1': X1, 'X2': X2, 'y': y})
model = LinearRegression()
[Link](df[['X1', 'X2']], df['y'])
df['y_pred'] = [Link](df[['X1', 'X2']])
print("Coefficients:", model.coef_)
print("Intercept:", model.intercept_)
[Link](figsize=(7,6))
[Link](df['y'], df['y_pred'], color='purple', edgecolor='k', alpha=0.7)
[Link]([df['y'].min(), df['y'].max()],
[df['y'].min(), df['y'].max()],
color='red', linestyle='--', label='Perfect Fit')
[Link]("Multiple Linear Regression - Actual vs Predicted (2D)")
[Link]("Actual y")
[Link]("Predicted y")
[Link]()
[Link](True)
[Link]()
Result:
Coefficients: [1.81964913 3.01970074]
Intercept: 1.1081727421557694
Program Executed Successfully.
8. Entropy of Partition
Aim: To calculate the entropy of a dataset partition in order to measure the purity/uncertainty
of data and understand how it is used in building decision trees.
Objective:
To understand the concept of entropy in information theory.
To calculate entropy for a dataset partition.
To apply entropy in decision tree learning (splitting criteria).
To interpret how entropy helps in finding the “best split” for classification problems.
Concept:
Entropy is a measure of uncertainty or impurity in a dataset.
Formula:
Hμ(P)=∑P∈P−μ(P)logμ(P),
where we assume 0log0=0 for convenience.
Entropy values:
H=0H = 0H=0 → perfectly pure (all samples in one class).
H=1H = 1H=1 → maximum uncertainty (equal probability for each class).
In Decision Trees (ID3, C4.5, etc.), entropy is used to calculate Information Gain:
Code:
import numpy as np
from math import log2
labels = ['Yes','Yes','No','Yes','No','No','Yes','No','Yes','Yes']
def entropy(labels):
values, counts = [Link](labels, return_counts=True)
probs = counts / [Link]()
return -[Link](probs * np.log2(probs))
H = entropy(labels)
print(f"Entropy of Partition: {H:.4f}")
values, counts = [Link](labels, return_counts=True)
[Link](values, counts, color=['green','red'])
[Link]("Class Distribution")
[Link]("Class")
[Link]("Frequency")
[Link]()
Result:
Entropy of Partition: 0.9710
Program Executed Successfully.
9. Solving problem generated due to Simpson’s Paradox.
Aim: To understand Simpson’s Paradox and how grouping/aggregating data differently can
lead to misleading conclusions, and to resolve it using proper statistical analysis.
Objective:
Show how Simpson’s Paradox arises in data.
Demonstrate with a simple Python program.
Concept:
1. Identify Hidden Variables
Check if there is a lurking variable (e.g., gender, age, income, department) influencing
the trend.
2. Disaggregate Data
Instead of only analyzing totals, analyze subgroup data separately.
Example: Compare treatment success rates within each gender group rather than only
overall.
3. Use Weighted Averages / Stratification
Apply weights based on group sizes to avoid bias from unequal sample sizes.
4. Causal Analysis (Not Just Correlation)
Use methods like causal inference, propensity score matching, or randomized
controlled trials (RCTs) to remove hidden variable effects.
5. Visualization
Plot subgroup trends separately before combining.
Helps in seeing whether the paradox is influencing the overall trend.
Code:
import pandas as pd
data = [Link]({
'Group': ['A']*3 + ['B']*3,
'Subgroup': ['X','Y','Z','X','Y','Z'],
'Success': [80, 30, 10, 20, 50, 90],
'Trials': [100, 50, 20, 100, 50, 20]
})
group_success = [Link]('Group').sum()
group_success['Rate'] = group_success['Success']/group_success['Trials']
print("\nAggregated Success Rate:\n", group_success['Rate'])
data['Rate'] = data['Success']/data['Trials']
print("\nSubgroup Rates:\n", data[['Group','Subgroup','Rate']])
fig, ax = [Link](1,2, figsize=(12,5))
group_success['Rate'].plot(kind='bar', ax=ax[0], color=['orange','purple'])
ax[0].set_title("Aggregated Success Rate by Group")
ax[0].set_ylabel("Success Rate")
for grp in data['Group'].unique():
subset = data[data['Group']==grp]
ax[1].bar(subset['Subgroup']+"-"+grp, subset['Rate'])
ax[1].set_title("Subgroup Success Rates")
ax[1].set_ylabel("Success Rate")
plt.tight_layout()
[Link]()
Result:
Aggregated Success Rate:
Group
A 0.705882
B 0.941176
Name: Rate, dtype: float64
Subgroup Rates:
Group Subgroup Rate
0 A X 0.8
1 A Y 0.6
2 A Z 0.5
3 B X 0.2
4 B Y 1.0
5 B Z 4.5
Program Executed Successfully.
10. Clustering
Aim: To understand and implement various clustering algorithms for unsupervised learning
tasks, enabling pattern discovery and data segmentation without labeled data.
Objective:
Understand fundamental clustering concepts and algorithms
Implement popular clustering techniques (K-Means, Hierarchical, DBSCAN)
Learn how to evaluate clustering performance
Concept: Clustering is an unsupervised learning technique that groups similar data points
together into clusters while keeping dissimilar points in different groups.
MAIN ALGORITHMS:
1. K-Means: Partitions data into k clusters
2. Hierarchical: Creates tree-like cluster structure
3. DBSCAN: Groups dense regions, handles noise
4. Gaussian Mixture Model: Probabilistic clustering
Code:
import numpy as np
import [Link] as plt
from [Link] import KMeans
[Link](42)
X1 = [Link](50,2) + [Link]([0,0])
X2 = [Link](50,2) + [Link]([5,5])
X3 = [Link](50,2) + [Link]([0,5])
X = [Link]([X1,X2,X3])
kmeans = KMeans(n_clusters=3, random_state=0)
[Link](X)
labels = kmeans.labels_
centroids = kmeans.cluster_centers_
[Link](figsize=(8,6))
[Link](X[:,0], X[:,1], c=labels, cmap='viridis', s=50)
[Link](centroids[:,0], centroids[:,1], c='red', marker='X', s=200, label='Centroids')
[Link]("K-Means Clustering")
[Link]("Feature 1")
[Link]("Feature 2")
[Link]()
[Link]()
Result:
Program Executed Successfully.