0% found this document useful (0 votes)
19 views59 pages

Data Science & ML Experiments in Python

The document outlines a series of experiments conducted in a Data Science and Machine Learning course at Delhi Technological University, focusing on data generation, analysis, and visualization using Python. Key objectives include creating datasets, performing statistical analysis, handling missing data, and applying machine learning algorithms. Each experiment emphasizes the use of Python libraries such as Pandas and Matplotlib for data manipulation and visualization.

Uploaded by

arpitakshrwn
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views59 pages

Data Science & ML Experiments in Python

The document outlines a series of experiments conducted in a Data Science and Machine Learning course at Delhi Technological University, focusing on data generation, analysis, and visualization using Python. Key objectives include creating datasets, performing statistical analysis, handling missing data, and applying machine learning algorithms. Each experiment emphasizes the use of Python libraries such as Pandas and Matplotlib for data manipulation and visualization.

Uploaded by

arpitakshrwn
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DELHI TECHNOLOGICAL UNIVERSITY

DEPARTMENT OF INFORMATION
TECHNOLOGY

FUNDAMENTALS OF DATA SCIENCE AND


MACHINE LEARNING

RITD505a

Submitted To: Submitted By:

Prof. Dinesh K. Vishwakarma Ms. Arpita Kesharwani


INDEX

S. Objective Date Sign


No.
1 Generate a dataset of student marks using Python,
store the data in a CSV file, read and analyze the
dataset using statistical measures such as mean, me-
dian, mode, minimum, maximum, and standard de-
viation, and finally visualize the marks of students
in different subjects using bar plots, line plots, pie
charts, and histograms.
2 Load, explore, and analyze a high-resolution electric-
ity load dataset from smart meters across various
cities in Morocco using Python.
3 Handle missing and inconsistent data in a dataset
using Python and Pandas.
4 Visualize data using different types of plots in
Python.
5 Perform statistical analysis on a dataset containing
students’ marks in different subjects using Python.
6 Apply and compare machine learning classification al-
gorithms, specifically Naive Bayes and Random For-
est, on the Iris dataset using Python.
7 To implement a linear regression model on the Cal-
ifornia Housing dataset in order to predict the me-
dian house value based on various housing and de-
mographic features, and to evaluate the model’s per-
formance using metrics such as Mean Squared Error
(MSE), Mean Absolute Error (MAE), and R-squared
score.
8 To perform clustering on the Iris dataset using K-
Means and Hierarchical Agglomerative Clustering
(HAC) methods, visualize the resulting clusters, and
compare their outcomes to understand how different
algorithms group similar data points based on their
features.
9 Perform time series analysis on the Airline Passengers
dataset by checking stationarity, applying transfor-
mations such as differencing, decomposing the series
into trend and seasonality components, and building
an ARIMA model to forecast future passenger values.
10 Apply the Apriori algorithm on a transactional
dataset to identify frequent itemsets and generate as-
sociation rules.
EXPERIMENT 1

Objective
The objective of this experiment is to generate a dataset of student marks using Python,
store the data in a CSV file, read and analyze the dataset using statistical measures
such as mean, median, mode, minimum, maximum, and standard deviation, and finally
visualize the marks of students in different subjects using bar plots, line plots, pie charts,
and histograms.

Theory
Data analysis involves collecting, organizing, summarizing, and presenting data to extract
meaningful information. In this experiment, a CSV (Comma Separated Values) file is
used to store the marks of 20 students in three subjects: Maths, Science, and English.
Python’s csv module is used to generate and write data, while the pandas library is used
to load and analyze the dataset.
Descriptive statistics are applied to understand the distribution of Science marks:

• Mean: The average value of all observations.

• Median: The middle value when data is arranged in order.

• Mode: The most frequently occurring value.

• Minimum and Maximum: The smallest and largest values in the dataset.

• Standard Deviation: A measure of spread that shows how much the data varies
from the mean.

Data visualization is performed using matplotlib. Bar charts are used to compare
marks of different students across subjects, while line plots help observe trends when
the data is sorted. Pie charts show the distribution of total marks, and histograms help
analyze the frequency distribution of marks in each subject. These visualizations help
interpret the data more effectively and identify patterns or variations.
CODE and OUTPUT :
Assignment 1

In [33]: import csv


import random

file = "[Link]"

with open(file, "w", newline="") as csvfile:


w=[Link](csvfile)
[Link](["Student Name", "Maths", "Science", "English"])
for i in range (1,21):
r1=[Link](1,100)
r2=[Link](1,100)
r3=[Link](1,100)
[Link]([chr(64+i), r1, r2, r3])

In [35]: import pandas as pd

df = pd.read_csv("[Link]")
mean=df["Science"].mean()
print(mean)
median=df["Science"].median()
print(mean)
mode=df["Science"].mode()
print(mode)
min=df["Science"].min()
print(min)
max=df["Science"].max()
print(max)
std=df["Science"].std()
print(std)

48.7
48.7
0 37
1 53
Name: Science, dtype: int64
3
93
28.44033458978546

In [41]: import [Link] as plt

print("3(a)")
x=df["Student Name"]
y1=df["Science"]
y2=df["Maths"]
y3=df["English"]

[Link](x, y1, label="Science Marks")


[Link]("Names")
[Link]("Marks")
[Link]("Science marks of the students")
[Link]()
[Link]()
[Link](x, y2, label="Maths Marks")
[Link]("Names")
[Link]("Marks")
[Link]("Maths marks of the students")
[Link]()
[Link]()
[Link](x, y3, label="English Marks")
[Link]("Names")
[Link]("Marks")
[Link]("English marks of the students")
[Link]()
[Link]()

3(a)
In [42]: print("3b")
df1=df.sort_values("Science")
[Link](df1["Student Name"], df1["Science"], label="Science Marks")
[Link]("Names")
[Link]("Marks")
[Link]("Science marks o the students")
[Link]()
[Link]()

[Link]("Student Name").sum().plot(kind="pie", y="Science")

3b

Out[42]: <Axes: ylabel='Science'>


In [39]: [Link]()

Out[39]: array([[<Axes: title={'center': 'Maths'}>,


<Axes: title={'center': 'Science'}>],
[<Axes: title={'center': 'English'}>, <Axes: >]], dtype=object)
Conclusion
The experiment successfully demonstrated how to create a dataset using Python, store
it in a CSV file, and analyze it using descriptive statistics. The statistical values ob-
tained for the Science marks provided insights into the overall performance of students.
Various visualizations such as bar charts, line plots, pie charts, and histograms helped
in understanding the distribution and comparison of marks across subjects. This ex-
periment highlights the effectiveness of Python as a tool for data generation, statistical
computation, and graphical representation in data analysis.
EXPERIMENT 2

Objective
The objective of this experiment is to load, explore, and analyze a high-resolution elec-
tricity load dataset from smart meters across various cities in Morocco using Python.

Theory
Data analysis is an essential part of data science and energy analytics. In this experiment,
the Pandas library in Python is used to handle and analyze large datasets efficiently.
Pandas provides powerful data structures such as DataFrames, which allow users to store,
manipulate, and analyze tabular data with labeled axes (rows and columns).
The main steps involved in this analysis are:

1. Data Loading: Importing the dataset into a Pandas DataFrame using the read excel()
function.

2. Data Inspection: Using functions like head(), info(), and shape to view the first
few rows, column data types, number of entries, and overall structure.

3. Data Understanding: Identifying data columns — in this case, DateTime and en-
ergy readings from five zones (zone1 to zone5). This allows analysts to explore
temporal patterns in electricity usage and assess data completeness.

Such an exploratory analysis forms the foundation for more advanced data science
tasks like visualization, time series forecasting, and energy demand prediction.
CODE and OUTPUT:
In [1]: import pandas as pd

data=pd.read_excel("C:\\Users\\DELL\\Documents\\mtech\\1sem\\DS\\high-resolution+load+

In [2]: print([Link]())

DateTime zone1 zone2 zone3 zone4 \


0 2022-09-14 17:10:00 73.536915 120.920183 160.982021 122.236644
1 2022-09-14 17:20:00 74.403119 122.179697 159.403248 119.727278
2 2022-09-14 17:30:00 77.893555 124.701734 159.969683 118.508081
3 2022-09-14 17:40:00 78.317958 126.731194 161.969315 118.571429
4 2022-09-14 17:50:00 81.334845 125.741697 163.189366 120.688295

zone5
0 102.922162
1 101.747520
2 102.074112
3 102.911361
4 102.546117

In [3]: print([Link]())

<class '[Link]'>
RangeIndex: 88890 entries, 0 to 88889
Data columns (total 6 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 DateTime 88890 non-null datetime64[ns]
1 zone1 88890 non-null float64
2 zone2 88890 non-null float64
3 zone3 88890 non-null float64
4 zone4 88890 non-null float64
5 zone5 88890 non-null float64
dtypes: datetime64[ns](1), float64(5)
memory usage: 4.1 MB
None

In [5]: print([Link])

(88890, 6)
Conclusion
From this experiment, we successfully loaded and inspected a large electricity load dataset
consisting of 88,890 records and 6 columns. The dataset includes timestamped readings
for five energy consumption zones. The info() output confirmed that all entries are
complete, with no missing values, and that the data types are appropriate (datetime64[ns]
for time and float64 for numerical load readings).
This preliminary data analysis establishes a clear understanding of the dataset’s struc-
ture, enabling further analytical processes such as statistical analysis, visualization, and
forecasting of electricity demand patterns.
EXPERIMENT 3

Objective
The objective of this experiment is to handle missing and inconsistent data in a dataset
using Python and Pandas.

Theory
In real-world data analysis, missing and inconsistent data are common and can signif-
icantly affect the accuracy of any model or analysis. Data cleaning is the process of
detecting and correcting (or removing) corrupt or inaccurate records from a dataset.
In this experiment, the Pandas library in Python is used to perform data preprocess-
ing. The following concepts are applied:

1. Handling Missing Data: Missing values are imputed using the median of the re-
spective column through the fillna() function. The median is often preferred as
it is less sensitive to extreme values than the mean.

2. Outlier Detection and Removal: Outliers are detected using the standard devia-
tion method. Any value lying beyond three standard deviations from the mean is
considered an outlier and is removed using logical conditions.

3. Data Consistency: Once missing data and outliers are addressed, the resulting
dataset becomes more reliable and ready for further statistical or machine learning
analysis.

CODE and OUTPUT:


In [1]: import pandas as pd

data = {
"Age": [21, 23, None, 22, 45, 24, 22, 23, 21, None],
"Salary": [30000, 32000, 31000, None, 150000, 29000, 30500, 31500, None, 33000
}

df = [Link](data)

print("Original Data:")
print(df)

df['Age'].fillna(df['Age'].median(), inplace=True)
df['Salary'].fillna(df['Salary'].median(), inplace=True)

mean_salary = df['Salary'].mean()
std_salary = df['Salary'].std()

df_cleaned = df[(df['Salary'] >= mean_salary - 3*std_salary) &


(df['Salary'] <= mean_salary + 3*std_salary)]

print("\nCleaned Data:")
print(df_cleaned)

Original Data:
Age Salary
0 21.0 30000.0
1 23.0 32000.0
2 NaN 31000.0
3 22.0 NaN
4 45.0 150000.0
5 24.0 29000.0
6 22.0 30500.0
7 23.0 31500.0
8 21.0 NaN
9 NaN 33000.0

Cleaned Data:
Age Salary
0 21.0 30000.0
1 23.0 32000.0
2 22.5 31000.0
3 22.0 31250.0
4 45.0 150000.0
5 24.0 29000.0
6 22.0 30500.0
7 23.0 31500.0
8 21.0 31250.0
9 22.5 33000.0
Conclusion
In this experiment, a dataset containing missing and inconsistent values for Age and
Salary was successfully cleaned using Pandas. The missing values were replaced with the
median of each column, ensuring the dataset remained statistically balanced. Outliers
were removed using the standard deviation approach. The cleaned dataset provided a
more accurate and consistent representation of the data, forming a strong foundation for
subsequent analysis and visualization tasks.
EXPERIMENT 4

Objective
The objective of this experiment is to visualize data using different types of plots in
Python.

Theory
Data visualization is an important aspect of data analysis as it helps to understand
trends, patterns, and relationships in data. Python provides several libraries for creating
visualizations, with matplotlib, numpy, and seaborn being among the most widely used.
The following visualization techniques are demonstrated in this experiment:

1. Line Plot: Displays the relationship between two continuous variables, showing
trends over an interval.

2. Bar Chart: Represents categorical data using rectangular bars proportional to their
values.

3. Histogram: Shows the frequency distribution of numerical data, allowing for the
observation of data spread and skewness.

4. Scatter Plot: Displays relationships between two numerical variables, useful for
identifying correlations or clusters.

5. Pie Chart: Depicts the proportional contribution of different categories to a whole.

6. Heatmap: Uses color to represent the correlation or magnitude of variables in a


matrix form, providing quick visual cues.

Visual representation enhances interpretability, aids in detecting anomalies, and sim-


plifies complex datasets for better decision-making.
CODE and OUTPUT:
In [1]: import [Link] as plt
import numpy as np
import seaborn as sns

# 1. Line Plot
x = [Link](1, 11)
y1 = x ** 2
y2 = x ** 3
[Link](x, y1, label="x^2", color="blue")
[Link](x, y2, label="x^3", color="orange")
[Link]("Line Plot Example")
[Link]("X values")
[Link]("Y values")
[Link]()
[Link]()

# 2. Bar Chart
categories = ["A", "B", "C", "D"]
values = [23, 45, 56, 78]
[Link](categories, values, color="skyblue")
[Link]("Bar Chart Example")
[Link]("Categories")
[Link]("Values")
[Link]()

# 3. Histogram
data = [Link](1000)
[Link](data, bins=20, color="purple", alpha=0.7)
[Link]("Histogram Example")
[Link]("Value")
[Link]("Frequency")
[Link]()

# 4. Scatter Plot
x = [Link](100)
y = [Link](100)
[Link](x, y, color="green", alpha=0.6)
[Link]("Scatter Plot Example")
[Link]("X values")
[Link]("Y values")
[Link]()

# 5. Pie Chart
sizes = [20, 30, 25, 25]
labels = ["A", "B", "C", "D"]
[Link](sizes, labels=labels, autopct='%1.1f%%', startangle=140)
[Link]("Pie Chart Example")
[Link]()

# 6. Heatmap
data = [Link](6, 6)
corr_matrix = [Link](data)
[Link](corr_matrix, annot=True, cmap="coolwarm", fmt=".2f")
[Link]("Heatmap Example")
[Link]()
Conclusion
In this experiment, different data visualization techniques were implemented using Python
libraries such as Matplotlib, NumPy, and Seaborn. Each visualization type provided
unique insights into the data, from identifying trends and distributions to observing corre-
lations between variables. This experiment demonstrates the importance of visualization
in transforming raw data into meaningful information for analysis and interpretation.
EXPERIMENT 5

Objective
The objective of this experiment is to perform statistical analysis on a dataset containing
students’ marks in different subjects using Python.

Theory
Statistical analysis is a fundamental process in data science that helps summarize, inter-
pret, and draw insights from data. Descriptive statistics provide numerical summaries
that describe the main features of a dataset.
In this experiment, a dataset of students’ marks in subjects such as Maths, Science,
and English is analyzed using the Python libraries pandas and statistics. The key
measures used are:

1. Mean: Represents the average value of a dataset and indicates the central location
of the data.

2. Median: Refers to the middle value when data points are arranged in ascending
order, reducing the effect of outliers.

3. Mode: Denotes the most frequently occurring value in the dataset.

4. Variance: Measures the degree of spread in the data, showing how far values deviate
from the mean.

5. Standard Deviation: Indicates the average deviation from the mean and provides
insight into data variability.

These statistical parameters help in understanding the distribution and consistency


of students’ performance across different subjects.
CODE and OUTPUT:
In [2]: import pandas as pd
from statistics import mean, variance, stdev, median, mode

# === Step 1: Read data from Excel file ===


# Replace '[Link]' with your actual Excel file name
file_path = '[Link]'
df = pd.read_csv(file_path)

print("Dataset:\n")
print(df, "\n")

# === Step 2: Select only numeric columns (marks) ===


numeric_data = df.select_dtypes(include=['number'])

print("=== Statistical Analysis ===")

# === Step 3: Perform statistical analysis for each column ===


for col in numeric_data.columns:
values = numeric_data[col].dropna().tolist() # remove any missing values
print(f"\nSubject: {col}")
print(f"Mean: {mean(values):.2f}")
print(f"Variance: {variance(values) if len(values) > 1 else 'N/A (need ≥2 values)'
print(f"Standard Deviation: {stdev(values) if len(values) > 1 else 'N/A (need ≥2 v
print(f"Median: {median(values)}")
try:
print(f"Mode: {mode(values)}")
except:
print("Mode: No unique mode (all values distinct)")
Dataset:

Student Name Maths Science English


0 A 100 57 89
1 B 91 61 43
2 C 55 69 99
3 D 11 53 41
4 E 41 93 24
5 F 18 64 74
6 G 44 3 48
7 H 20 22 57
8 I 67 6 50
9 J 28 91 16
10 K 32 89 24
11 L 22 39 83
12 M 20 11 95
13 N 2 37 50
14 O 16 53 3
15 P 77 51 20
16 Q 70 26 40
17 R 94 24 92
18 S 61 37 78
19 T 74 88 12

=== Statistical Analysis ===

Subject: Maths
Mean: 47.15
Variance: 926.7657894736842
Standard Deviation: 30.44282821082306
Median: 42.5
Mode: 20

Subject: Science
Mean: 48.70
Variance: 808.8526315789474
Standard Deviation: 28.44033458978546
Median: 52.0
Mode: 53

Subject: English
Mean: 51.90
Variance: 917.4631578947368
Standard Deviation: 30.289654304642315
Median: 49.0
Mode: 24
Conclusion
In this experiment, the marks dataset was analyzed using statistical measures to un-
derstand student performance in Maths, Science, and English. The calculated mean,
median, mode, variance, and standard deviation for each subject revealed variations in
performance levels. The analysis demonstrated that Python provides an efficient and
reliable approach to performing statistical computations, enabling quick insights into the
underlying trends and spread of data.
EXPERIMENT 6

Objective
The objective of this experiment is to apply and compare machine learning classification
algorithms, specifically Naive Bayes and Random Forest, on the Iris dataset using Python.

Theory
Machine learning classification is a supervised learning technique used to categorize data
into predefined classes. In this experiment, two different algorithms are applied to the
Iris dataset, a well-known dataset containing measurements of sepal and petal dimensions
for three species of flowers.

1. Naive Bayes Classifier: Based on Bayes’ theorem, this probabilistic classifier as-
sumes independence among predictors. It calculates the posterior probability of
each class and assigns the class with the highest probability. The Gaussian Naive
Bayes model is used here, as it assumes that features follow a normal distribution.

2. Random Forest Classifier: An ensemble learning algorithm that constructs multiple


decision trees and combines their results to improve accuracy and control overfitting.
Each tree is trained on a random subset of data and features.

To evaluate model performance, the following metrics are used:

• Accuracy: Measures the overall correctness of the model.

• Confusion Matrix: Displays the number of correct and incorrect predictions for each
class.

• Classification Report: Provides detailed metrics such as precision, recall, and F1-
score.

The visual representation of confusion matrices and feature importance helps in better
understanding of model performance and the influence of different features.
CODE and OUTPUT:
In [8]: # Import necessary libraries
import numpy as np
import [Link] as plt
import seaborn as sns
from [Link] import load_iris
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from [Link] import RandomForestClassifier
from [Link] import accuracy_score, confusion_matrix, classification_report

# Load dataset
iris = load_iris()
X = [Link]
y = [Link]
feature_names = iris.feature_names
class_names = iris.target_names

# Split dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state

# ---------------------- Naive Bayes ----------------------


nb_model = GaussianNB()
nb_model.fit(X_train, y_train)
y_pred_nb = nb_model.predict(X_test)

print("=== Naive Bayes Classification ===")


print("Accuracy:", accuracy_score(y_test, y_pred_nb))
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred_nb))
print("Classification Report:\n", classification_report(y_test, y_pred_nb, target_name

# ---------------------- Random Forest ----------------------


rf_model = RandomForestClassifier(n_estimators=100, random_state=42)
rf_model.fit(X_train, y_train)
y_pred_rf = rf_model.predict(X_test)

print("\n=== Random Forest Classification ===")


print("Accuracy:", accuracy_score(y_test, y_pred_rf))
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred_rf))
print("Classification Report:\n", classification_report(y_test, y_pred_rf, target_name

# ---------------------- Visualization ----------------------

# Confusion Matrix (Naive Bayes)


[Link](figsize=(6,4))
[Link](confusion_matrix(y_test, y_pred_nb), annot=True, cmap="Blues", fmt=
xticklabels=class_names, yticklabels=class_names)
[Link]("Naive Bayes - Confusion Matrix")
[Link]("Predicted")
[Link]("Actual")
[Link]()

# Confusion Matrix (Random Forest)


[Link](figsize=(6,4))
[Link](confusion_matrix(y_test, y_pred_rf), annot=True, cmap="Greens", fmt
xticklabels=class_names, yticklabels=class_names)
[Link]("Random Forest - Confusion Matrix")
[Link]("Predicted")
[Link]("Actual")
[Link]()

# Feature Importance (Random Forest)


[Link](figsize=(8,5))
[Link](x=rf_model.feature_importances_, y=feature_names, palette="viridis"
[Link]("Feature Importance (Random Forest)")
[Link]("Importance Score")
[Link]("Features")
[Link]()

=== Naive Bayes Classification ===


Accuracy: 1.0
Confusion Matrix:
[[10 0 0]
[ 0 9 0]
[ 0 0 11]]
Classification Report:
precision recall f1-score support

setosa 1.00 1.00 1.00 10


versicolor 1.00 1.00 1.00 9
virginica 1.00 1.00 1.00 11

accuracy 1.00 30
macro avg 1.00 1.00 1.00 30
weighted avg 1.00 1.00 1.00 30

=== Random Forest Classification ===


Accuracy: 1.0
Confusion Matrix:
[[10 0 0]
[ 0 9 0]
[ 0 0 11]]
Classification Report:
precision recall f1-score support

setosa 1.00 1.00 1.00 10


versicolor 1.00 1.00 1.00 9
virginica 1.00 1.00 1.00 11

accuracy 1.00 30
macro avg 1.00 1.00 1.00 30
weighted avg 1.00 1.00 1.00 30
C:\Users\DELL\AppData\Local\Temp\ipykernel_20232\[Link]: FutureWarnin
g:

Passing `palette` without assigning `hue` is deprecated and will be removed in


v0.14.0. Assign the `y` variable to `hue` and set `legend=False` for the same e
ffect.

[Link](x=rf_model.feature_importances_, y=feature_names, palette="viridi


s")
Conclusion
In this experiment, both Naive Bayes and Random Forest classifiers were implemented
on the Iris dataset. The results showed that both models achieved an accuracy of 100%,
indicating perfect classification performance. The confusion matrices confirmed that all
samples were correctly classified. The feature importance plot from the Random Forest
model highlighted the most significant features influencing classification. This experiment
demonstrates that both algorithms can perform exceptionally well on structured and well-
separated datasets like Iris.
EXPERIMENT 7

Objective
To implement a linear regression model on the California Housing dataset in order to
predict the median house value based on various housing and demographic features, and
to evaluate the model’s performance using metrics such as Mean Squared Error (MSE),
Mean Absolute Error (MAE), and R-squared score.

Theory
Linear regression is a supervised machine learning algorithm used for predicting a contin-
uous dependent variable based on one or more independent variables. It assumes a linear
relationship between the input variables and the target variable, which can be expressed
as:
y = β0 + β1 x1 + β2 x2 + · · · + βn xn + ϵ

where y is the dependent variable, xi are the independent variables, βi are the coefficients,
and ϵ is the error term.
The goal of linear regression is to minimize the difference between the predicted and
actual values, typically achieved by minimizing the cost function — the Mean Squared
Error (MSE):
n
1X
M SE = (yi − ŷi )2
n i=1

where yi is the actual value and ŷi is the predicted value.


Once trained, the model’s performance can be evaluated using statistical measures
such as:

• Mean Squared Error (MSE): Average squared difference between actual and pre-
dicted values.

• Mean Absolute Error (MAE): Average absolute difference between actual and pre-
dicted values.

• R-squared (R2 ): Proportion of variance in the dependent variable explained by the


model.

In this experiment, the model uses the California Housing dataset, which includes features
like average income, house age, number of rooms, population, and geographic coordinates
to predict median house values.
CODE and OUTPUT:
In [1]: # Import libraries
import [Link] as plt
import seaborn as sns
from [Link] import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from [Link] import mean_squared_error, mean_absolute_error, r2_score
import pandas as pd

# Load dataset
data = fetch_california_housing()
X = [Link]
y = [Link]
feature_names = data.feature_names

# Convert to DataFrame for visualization


df = [Link](X, columns=feature_names)
df['Target'] = y

# Split into train and test


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

# ---------------------- Linear Regression ----------------------


lr_model = LinearRegression()
lr_model.fit(X_train, y_train)

# Predict
y_pred = lr_model.predict(X_test)

# ---------------------- Evaluation ----------------------


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

print("=== Linear Regression Results ===")


print(f"Mean Squared Error (MSE): {mse:.4f}")
print(f"Mean Absolute Error (MAE): {mae:.4f}")
print(f"R-squared (R2 Score): {r2:.4f}")

# ---------------------- Visualization ----------------------

# 1 ⯑ True vs Predicted
[Link](figsize=(6,6))
[Link](y_test, y_pred, alpha=0.6)
[Link]([y_test.min(), y_test.max()], [y_test.min(), y_test.max()], 'r--', lw=
[Link]("True Values")
[Link]("Predicted Values")
[Link]("Linear Regression: True vs Predicted")
[Link]()

# 2 ⯑ Feature Coefficients
coef_df = [Link]({'Feature': feature_names, 'Coefficient': lr_model.coef_
coef_df = coef_df.sort_values(by='Coefficient', key=abs, ascending=False)
[Link](figsize=(8,5))
[Link](x='Coefficient', y='Feature', data=coef_df, palette="coolwarm")
[Link]("Linear Regression Feature Coefficients")
[Link]()

=== Linear Regression Results ===


Mean Squared Error (MSE): 0.5559
Mean Absolute Error (MAE): 0.5332
R-squared (R2 Score): 0.5758

C:\Users\DELL\AppData\Local\Temp\ipykernel_9308\[Link]: FutureWarning:

Passing `palette` without assigning `hue` is deprecated and will be removed in


v0.14.0. Assign the `y` variable to `hue` and set `legend=False` for the same e
ffect.

[Link](x='Coefficient', y='Feature', data=coef_df, palette="coolwarm")


Conclusion
The linear regression model successfully predicted house prices with a Mean Squared
Error (MSE) of approximately 0.5559, Mean Absolute Error (MAE) of 0.5332, and an
R-squared value of 0.5758. These results indicate a moderate level of accuracy, suggesting
that while the model captures general trends in the data, it may not account for complex
non-linear relationships. The experiment demonstrates the basic principles of regression
modeling and the importance of feature selection and data normalization in predictive
analytics.
EXPERIMENT 8

Objective
To perform clustering on the Iris dataset using K-Means and Hierarchical Agglomerative
Clustering (HAC) methods, visualize the resulting clusters, and compare their outcomes
to understand how different algorithms group similar data points based on their features.

Theory
Clustering is an unsupervised machine learning technique used to group similar data
points together based on feature similarity. Unlike classification, clustering does not use
labeled data. In this experiment, two clustering algorithms — K-Means and Hierarchical
Agglomerative Clustering — are applied to the Iris dataset.
1. K-Means Clustering: K-Means aims to partition data into k clusters such that
each observation belongs to the cluster with the nearest mean (centroid). The objective
function minimizes the within-cluster sum of squares:

k X
X
J= ||xj − µi ||2
i=1 xj ∈Ci

where µi is the centroid of cluster Ci . The algorithm iteratively updates cluster assign-
ments and centroids until convergence.
2. Hierarchical Agglomerative Clustering (HAC): HAC builds a hierarchy of clusters
using a bottom-up approach, starting with each data point as an individual cluster and
successively merging the two closest clusters based on a linkage criterion (such as Ward’s
method). The hierarchy is visualized using a dendrogram, where the height represents
the distance at which clusters are merged.
Before applying both algorithms, the features are standardized to ensure equal con-
tribution of all variables. The clustering performance is visualized using scatter plots for
both K-Means and HAC, and their cluster labels are compared.
CODE and OUTPUT:
In [2]: import pandas as pd
import numpy as np
import [Link] as plt
import seaborn as sns
from [Link] import load_iris
from [Link] import StandardScaler
from [Link] import KMeans, AgglomerativeClustering
from [Link] import dendrogram, linkage

# Load the Iris dataset


iris = load_iris()
X = [Link]
df = [Link](X, columns=iris.feature_names)

print("First five rows of the dataset:")


print([Link]())

# -------------------------------
# Step 1: Data Standardization
# -------------------------------
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# -------------------------------
# Step 2: K-Means Clustering
# -------------------------------
kmeans = KMeans(n_clusters=3, random_state=42)
kmeans_labels = kmeans.fit_predict(X_scaled)

df['KMeans_Cluster'] = kmeans_labels

print("\nK-Means Cluster Centers:")


print(kmeans.cluster_centers_)

# Visualize K-Means Clusters


[Link](figsize=(8,5))
[Link](x=X_scaled[:, 0], y=X_scaled[:, 1], hue=kmeans_labels, palette=
[Link]("K-Means Clustering on Iris Dataset")
[Link]("Feature 1")
[Link]("Feature 2")
[Link]()

# -------------------------------
# Step 3: Hierarchical Clustering
# -------------------------------
# Perform linkage
linked = linkage(X_scaled, method='ward')

# Plot Dendrogram
[Link](figsize=(10, 6))
dendrogram(linked,
orientation='top',
distance_sort='descending',
show_leaf_counts=True)
[Link]("Hierarchical Clustering Dendrogram")
[Link]("Samples")
[Link]("Distance")
[Link]()

# Apply Agglomerative Clustering


hac = AgglomerativeClustering(n_clusters=3, metric='euclidean', linkage='ward')
hac_labels = hac.fit_predict(X_scaled)
df['HAC_Cluster'] = hac_labels

# Visualize HAC Clusters


[Link](figsize=(8,5))
[Link](x=X_scaled[:, 0], y=X_scaled[:, 1], hue=hac_labels, palette='Set2'
[Link]("Hierarchical Agglomerative Clustering on Iris Dataset")
[Link]("Feature 1")
[Link]("Feature 2")
[Link]()

# -------------------------------
# Step 4: Comparison of Clusters
# -------------------------------
print("\nCluster labels by both methods:")
print(df[['KMeans_Cluster', 'HAC_Cluster']].head())

First five rows of the dataset:


sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
0 5.1 3.5 1.4 0.2
1 4.9 3.0 1.4 0.2
2 4.7 3.2 1.3 0.2
3 4.6 3.1 1.5 0.2
4 5.0 3.6 1.4 0.2

K-Means Cluster Centers:


[[ 0.57100359 -0.37176778 0.69111943 0.66315198]
[-0.81623084 1.31895771 -1.28683379 -1.2197118 ]
[-1.32765367 -0.373138 -1.13723572 -1.11486192]]
Cluster labels by both methods:
KMeans_Cluster HAC_Cluster
0 1 1
1 2 1
2 2 1
3 2 1
4 1 1
Conclusion
The clustering experiment on the Iris dataset using K-Means and Hierarchical Agglom-
erative Clustering successfully grouped the samples into three distinct clusters. Both
methods showed similar grouping patterns, though minor differences in cluster assign-
ments were observed due to the differing algorithmic principles. K-Means efficiently par-
titions data based on distance from centroids, while HAC provides a detailed hierarchical
structure and visual interpretability through the dendrogram. The results demonstrate
the effectiveness of unsupervised learning in identifying natural patterns in data without
labeled inputs.
EXPERIMENT 9

Objective
The objective of this experiment is to perform time series analysis on the Airline Pas-
sengers dataset by checking stationarity, applying transformations such as differencing,
decomposing the series into trend and seasonality components, and building an ARIMA
model to forecast future passenger values.

Theory
Time series analysis involves studying data points collected or recorded at successive
points in time. It is used to identify underlying patterns such as trend, seasonality, and
cyclic behavior. A time series must be stationary for most forecasting models, meaning
its statistical properties remain constant over time.
The Augmented Dickey–Fuller (ADF) test is used to check stationarity. A high p-value
(greater than 0.05) indicates a non-stationary series. To achieve stationarity, differencing
is often applied:
Yt′ = Yt − Yt−1

Time series decomposition separates the data into:

• Trend: Long-term movement in the data,

• Seasonality: Repeating patterns at regular intervals,

• Residual: Random noise.

ARIMA (AutoRegressive Integrated Moving Average) is a widely used forecasting


model defined by three parameters:
(p, d, q)

where p is the autoregressive term, d is the degree of differencing, and q is the moving
average term. The ARIMA model forecasts future values by combining past observations
and past errors:
Yt = c + ϕ1 Yt−1 + · · · + θ1 ϵt−1 + · · ·

In this experiment, an ARIMA(1,1,1) model is fitted to the training portion of the


dataset. The forecasted values are compared with the test dataset, and accuracy is
measured using Mean Squared Error:
n
1X
M SE = (yi − ŷi )2
n i=1

CODE and OUTPUT:


In [4]: import pandas as pd
import numpy as np
import [Link] as plt
from [Link] import adfuller
from [Link] import seasonal_decompose
from [Link] import ARIMA
from [Link] import mean_squared_error
import warnings
[Link]('ignore')

url = "[Link]
data = pd.read_csv(url)

[Link] = ['Date', 'Value']


data['Date'] = pd.to_datetime(data['Date'])
data.set_index('Date', inplace=True)

print("✅ First 5 rows of dataset:")


print([Link]())

[Link](figsize=(10,5))
[Link](data, label='Monthly Airline Passengers', color='blue')
[Link]("Original Time Series Data (AirPassengers)")
[Link]("Date")
[Link]("Passengers (in thousands)")
[Link]()
[Link]()

def adf_test(series):
result = adfuller(series)
print('ADF Statistic:', result[0])
print('p-value:', result[1])
print('Critical Values:')
for key, value in result[4].items():
print(f' {key}, {value}')


if result[1] <= 0.05:
print(" Data is stationary (p < 0.05)")


else:

🔍
print(" Data is NOT stationary (p > 0.05)")
print("\n Stationarity Check:")
adf_test(data['Value'])

decomposition = seasonal_decompose(data, model='additive', period=12)


[Link]()
[Link]()

🔍
diff_data = [Link]().dropna()
print("\n Stationarity Check after Differencing:")
adf_test(diff_data['Value'])

[Link](figsize=(10,5))
[Link](diff_data, label='Differenced Data', color='green')
[Link]("Differenced Time Series")
[Link]()
[Link]()

train_size = int(len(data) * 0.8)


train, test = data[:train_size], data[train_size:]

model = ARIMA(train, order=(1,1,1))


model_fit = [Link]()
print(model_fit.summary())

forecast = model_fit.forecast(steps=len(test))
[Link](figsize=(10,5))
[Link](train, label='Train')
[Link](test, label='Test')
[Link]([Link], forecast, label='Forecast', color='red')
[Link]("ARIMA Forecast vs Actual (AirPassengers)")
[Link]()
[Link]()

📊
mse = mean_squared_error(test, forecast)
print(f" Mean Squared Error: {mse:.4f}")

✅ First 5 rows of dataset:


Value
Date
1949-01-01 112
1949-02-01 118
1949-03-01 132
1949-04-01 129
1949-05-01 121
🔍 Stationarity Check:
ADF Statistic: 0.8153688792060498
p-value: 0.991880243437641
Critical Values:
1%, -3.4816817173418295
5%, -2.8840418343195267


10%, -2.578770059171598
Data is NOT stationary (p > 0.05)

🔍 Stationarity Check after Differencing:


ADF Statistic: -2.8292668241700047
p-value: 0.05421329028382478
Critical Values:
1%, -3.4816817173418295
5%, -2.8840418343195267


10%, -2.578770059171598
Data is NOT stationary (p > 0.05)
SARIMAX Results
==============================================================================
Dep. Variable: Value No. Observations: 115
Model: ARIMA(1, 1, 1) Log Likelihood -526.123
Date: Wed, 29 Oct 2025 AIC 1058.246
Time: 10:10:04 BIC 1066.454
Sample: 01-01-1949 HQIC 1061.577
- 07-01-1958
Covariance Type: opg
==============================================================================
coef std err z P>|z| [0.025 0.975]
------------------------------------------------------------------------------
ar.L1 -0.5111 0.114 -4.488 0.000 -0.734 -0.288
ma.L1 0.9144 0.056 16.251 0.000 0.804 1.025
sigma2 592.7851 101.208 5.857 0.000 394.422 791.148
===============================================================================
====
Ljung-Box (L1) (Q): 0.09 Jarque-Bera (JB):
2.29
Prob(Q): 0.76 Prob(JB):
0.32
Heteroskedasticity (H): 5.22 Skew:
0.03
Prob(H) (two-sided): 0.00 Kurtosis:
2.31
===============================================================================
====

Warnings:
[1] Covariance matrix calculated using the outer product of gradients (complex-
step).
📊 Mean Squared Error: 9506.1758
Conclusion
The time series analysis of the Airline Passengers dataset revealed that the original se-
ries was non-stationary, as indicated by a high p-value in the ADF test. After applying
differencing, the series approached stationarity but still remained slightly above the sig-
nificance threshold. The ARIMA(1,1,1) model was successfully trained on the dataset,
and its forecasts followed the general upward trend of the original passenger data. How-
ever, the model produced a relatively high Mean Squared Error of 9506.17, indicating
that although it captured the general pattern, its predictive accuracy can be improved.
The experiment demonstrates the importance of stationarity, decomposition, and model
selection in time series forecasting.
EXPERIMENT 10

Objective
The objective of this experiment is to apply the Apriori algorithm on a transactional
dataset to identify frequent itemsets and generate association rules.

Theory
Association Rule Mining is a data mining technique used to discover interesting relation-
ships between items in large transactional datasets. It is widely applied in market basket
analysis to understand customer purchasing behaviour.
1. Apriori Algorithm: Apriori is a frequent itemset mining algorithm based on the
principle that if an itemset is frequent, all of its subsets must also be frequent. It uses
a bottom-up approach where frequent itemsets of size k are used to generate candidate
itemsets of size k + 1. The support of an itemset is given by:

Number of transactions containing A


Support(A) =
Total number of transactions

2. Association Rules: After identifying frequent itemsets, association rules of the form
A → B are generated, where A and B are disjoint itemsets. The strength of these rules
is evaluated using:

• Confidence:
Support(A ∪ B)
Confidence(A → B) =
Support(A)

• Lift:
Confidence(A → B)
Lift(A → B) =
Support(B)
Lift greater than 1 indicates a strong positive association.

In this experiment, the dataset is encoded using one-hot encoding and processed using
the Apriori algorithm with a minimum support of 0.6. Association rules are generated
based on confidence, and strong rules are filtered using lift.
CODE and OUTPUT :
In [1]: !pip install mlxtend

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

dataset = [
['Milk', 'Bread', 'Butter'],
['Bread', 'Diaper', 'Beer', 'Eggs'],
['Milk', 'Diaper', 'Beer', 'Cola'],
['Bread', 'Milk', 'Diaper', 'Beer'],
['Bread', 'Milk', 'Diaper', 'Cola']
]

from [Link] import TransactionEncoder


te = TransactionEncoder()
te_ary = [Link](dataset).transform(dataset)
df = [Link](te_ary, columns=te.columns_)

print("🔹 Transaction Dataset (One-Hot Encoded):\n")


print(df)

🔹
frequent_itemsets = apriori(df, min_support=0.6, use_colnames=True)
print("\n Frequent Itemsets:\n")
print(frequent_itemsets)

rules = association_rules(frequent_itemsets, metric="confidence", min_threshold

print("\n🔹 Association Rules:\n")


print(rules[['antecedents', 'consequents', 'support', 'confidence', 'lift']])

🔹
strong_rules = rules[rules['lift'] > 1]
print("\n Strong Association Rules (lift > 1):\n")
print(strong_rules[['antecedents', 'consequents', 'support', 'confidence', 'lift'
Requirement already satisfied: mlxtend in /usr/local/lib/python3.12/dist-packag
es (0.23.4)
Requirement already satisfied: scipy>=1.2.1 in /usr/local/lib/python3.12/dist-p
ackages (from mlxtend) (1.16.2)
Requirement already satisfied: numpy>=1.16.2 in /usr/local/lib/python3.12/dist-
packages (from mlxtend) (2.0.2)
Requirement already satisfied: pandas>=0.24.2 in /usr/local/lib/python3.12/dis
t-packages (from mlxtend) (2.2.2)
Requirement already satisfied: scikit-learn>=1.3.1 in /usr/local/lib/python3.1
2/dist-packages (from mlxtend) (1.6.1)
Requirement already satisfied: matplotlib>=3.0.0 in /usr/local/lib/python3.12/d
ist-packages (from mlxtend) (3.10.0)
Requirement already satisfied: joblib>=0.13.2 in /usr/local/lib/python3.12/dis
t-packages (from mlxtend) (1.5.2)
Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.12/di
st-packages (from matplotlib>=3.0.0->mlxtend) (1.3.3)
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.12/dist-p
ackages (from matplotlib>=3.0.0->mlxtend) (0.12.1)
Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.12/d
ist-packages (from matplotlib>=3.0.0->mlxtend) (4.60.1)
Requirement already satisfied: kiwisolver>=1.3.1 in /usr/local/lib/python3.12/d
ist-packages (from matplotlib>=3.0.0->mlxtend) (1.4.9)
Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.12/dis
t-packages (from matplotlib>=3.0.0->mlxtend) (25.0)
Requirement already satisfied: pillow>=8 in /usr/local/lib/python3.12/dist-pack
ages (from matplotlib>=3.0.0->mlxtend) (11.3.0)
Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.12/di
st-packages (from matplotlib>=3.0.0->mlxtend) (3.2.5)
Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.1
2/dist-packages (from matplotlib>=3.0.0->mlxtend) (2.9.0.post0)
Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.12/dist-p
ackages (from pandas>=0.24.2->mlxtend) (2025.2)
Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.12/dis
t-packages (from pandas>=0.24.2->mlxtend) (2025.2)
Requirement already satisfied: threadpoolctl>=3.1.0 in /usr/local/lib/python3.1
2/dist-packages (from scikit-learn>=1.3.1->mlxtend) (3.6.0)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.12/dist-packa

🔹
ges (from python-dateutil>=2.7->matplotlib>=3.0.0->mlxtend) (1.17.0)
Transaction Dataset (One-Hot Encoded):

Beer Bread Butter Cola Diaper Eggs Milk


0 False True True False False False True
1 True True False False True True False
2 True False False True True False True
3 True True False False True False True
4 False True False True True False True

🔹 Frequent Itemsets:
support itemsets
0 0.6 (Beer)
1 0.8 (Bread)
2 0.8 (Diaper)
3 0.8 (Milk)
4 0.6 (Beer, Diaper)
5 0.6 (Diaper, Bread)
6 0.6 (Bread, Milk)
7 0.6 (Diaper, Milk)

🔹 Association Rules:
antecedents consequents support confidence lift
0 (Beer) (Diaper) 0.6 1.00 1.2500
1 (Diaper) (Beer) 0.6 0.75 1.2500
2 (Diaper) (Bread) 0.6 0.75 0.9375
3 (Bread) (Diaper) 0.6 0.75 0.9375
4 (Bread) (Milk) 0.6 0.75 0.9375
5 (Milk) (Bread) 0.6 0.75 0.9375
6 (Diaper) (Milk) 0.6 0.75 0.9375
7 (Milk) (Diaper) 0.6 0.75 0.9375

🔹 Strong Association Rules (lift > 1):


antecedents consequents support confidence lift
0 (Beer) (Diaper) 0.6 1.00 1.25
1 (Diaper) (Beer) 0.6 0.75 1.25
Conclusion
The Apriori algorithm successfully identified frequent itemsets from the transactional
dataset, such as Beer, Bread, Diaper, and Milk, each with high support values. Associ-
ation rules were generated to reveal relationships between items, and strong rules with
lift greater than 1 highlighted meaningful correlations, such as the strong association
between Beer and Diaper. This experiment demonstrates how association rule mining
can be used to uncover valuable insights into purchasing patterns and support decision-
making in retail environments.

You might also like