0% found this document useful (0 votes)
6 views47 pages

Multiple Regression Implementation in Python

The document outlines practical exercises in Python for implementing multiple regression, statistical operations, data visualization, data transformation techniques, and dataset splitting. It covers key concepts, goals, and implementations for each topic, including the use of libraries like pandas, sklearn, and seaborn. Each section provides code snippets and explanations to facilitate understanding and application of these data analysis techniques.

Uploaded by

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

Multiple Regression Implementation in Python

The document outlines practical exercises in Python for implementing multiple regression, statistical operations, data visualization, data transformation techniques, and dataset splitting. It covers key concepts, goals, and implementations for each topic, including the use of libraries like pandas, sklearn, and seaborn. Each section provides code snippets and explanations to facilitate understanding and application of these data analysis techniques.

Uploaded by

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

Practical: 02

Objective : WAP in Python to implement multiple regression

Description: Multiple Regression is a statistical method used to model the relationship between a
dependent variable and two or more independent variables. It aims to find the best-fitting hyperplane that
predicts the dependent variable based on the multiple independent [Link]

Multiple Linear Regression Equation:


y=m1x1+m2x2+⋯+mnxn+by = m_1 x_1 + m_2 x_2 + \dots + m_n x_n + by=m1
x1+m2x2+⋯+mnxn+b
Where:

● yyy: Dependent variable (what you want to predict)

● x1,x2,…,xnx_1, x_2, \dots, x_nx1,x2,…,xn: Independent variables (inputs)

● m1,m2,…,mnm_1, m_2, \dots, m_nm1,m2,…,mn: Coefficients (weights) of the independent


variables

● bbb: Y-intercept (value of yyy when all xi=0x_i = 0xi=0)

Goal:
The goal is to find the values of m1,m2,…,mnm_1, m_2, \dots, m_nm1,m2,…,mn and bbb that minimize
the error between the predicted and actual values.

Key Concepts:

● Best Fit Hyperplane: The hyperplane that minimizes the sum of squared errors (differences
between actual and predicted values of yyy).

● Loss Function: Typically, Mean Squared Error (MSE) is used:

● R-squared: A metric used to evaluate how well the model explains the variance in the data.
Ranges from 0 to 1.

Abhimanyu Patel 0187CS221008 CS1


Flow Chart:

Implementation:
import pandas as pd
import [Link] as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from [Link] import LabelEncoder
from [Link] import mean_squared_error, r2_score

# Load the dataset


df = pd.read_csv("Dataset/[Link]")

Abhimanyu Patel 0187CS221008 CS1


# Encode categorical feature: furnishingstatus
le = LabelEncoder()
df['furnishingstatus'] = le.fit_transform(df['furnishingstatus']) # 0 = furnished, 1 = semi-furnished, 2 =
unfurnished

# Select relevant features for multiple regression


features = ['area', 'bedrooms', 'bathrooms', 'furnishingstatus']
X = df[features]
y = df['price']

# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train model
model = LinearRegression()
[Link](X_train, y_train)

# Predict
y_pred = [Link](X_test)

# Evaluate
print("Mean Squared Error:", mean_squared_error(y_test, y_pred))
print("R² Score:", r2_score(y_test, y_pred))

# Coefficients
coeff_df = [Link]({
'Feature': features,
'Coefficient': model.coef_
})

# Visualization: Actual vs Predicted


[Link](figsize=(8, 6))
[Link](y_test, y_pred, alpha=0.7, color='green')
[Link]([[Link](), [Link]()], [[Link](), [Link]()], color='red', linestyle='--', linewidth=2)
[Link]('Actual Price')
[Link]('Predicted Price')
[Link]('Multiple Linear Regression: Actual vs Predicted Prices')
[Link](True)
plt.tight_layout()
[Link]()

Abhimanyu Patel 0187CS221008 CS1


Output:

Abhimanyu Patel 0187CS221008 CS1


Practical: 03

Objective : WAP in Python to perform statistical operations on real-world data.

Description: Statistical operations on real-world data involve applying various statistical techniques to
analyze and interpret data collected from real-world phenomena. These operations help in understanding
patterns, making predictions, and drawing conclusions from empirical data. The goal is to summarize,
infer, and model the underlying processes governing the data.

Key Statistical Operations:

● Descriptive Statistics: Techniques used to summarize and describe the main features of a
dataset, such as:
○ Mean: The average value of a dataset.
○ Median: The middle value of the dataset when arranged in ascending order.
○ Mode: The most frequent value in the dataset.
○ Standard Deviation: A measure of the spread or dispersion of the data.
○ Variance: The square of the standard deviation, showing the degree of variation in the
data.

● Inferential Statistics: Methods that use sample data to make inferences about a larger
population, such as:

○ Hypothesis Testing: Determining whether there is enough evidence to reject a null


hypothesis.
○ Confidence Intervals: A range of values, derived from the sample data, that is likely to
contain the population parameter.
○ T-tests and Z-tests: Statistical tests to compare means between groups or check
population means.

● Regression Analysis: Modeling relationships between variables to predict outcomes. This


includes:

○ Linear Regression: As described earlier, used for predicting a dependent variable based
on one or more independent variables.
○ Logistic Regression: Used for binary classification tasks (e.g., predicting whether an
event will happen or not).

● Correlation Analysis: A method to determine the strength and direction of the relationship
between two variables, typically using:

○ Pearson Correlation Coefficient: Measures the linear relationship between two


continuous variables.

Abhimanyu Patel 0187CS221008 CS1


○ Spearman’s Rank Correlation: Measures the relationship between two ranked
variables.

Goal:
The goal of performing statistical operations on real-world data is to gain actionable insights, make
informed decisions, and provide evidence to support hypotheses or predictions.

Key Concepts:

● Data Cleaning: Ensuring that the data is free from errors, missing values, or outliers before
applying statistical methods.
● Sampling: The process of selecting a subset of data from a larger population for analysis.
● Modeling and Prediction: Using the results of statistical operations to build models that can
predict future outcomes based on historical data.

Flow Chart:

Abhimanyu Patel 0187CS221008 CS1


Implementation:
import pandas as pd
import seaborn as sns
import [Link] as plt
# Load real-world dataset
df = pd.read_csv("Dataset/[Link]")

# Convert categorical columns to numerical for stats (if needed)


from [Link] import LabelEncoder

categorical_cols = ['mainroad', 'guestroom', 'basement',


'hotwaterheating', 'airconditioning',
'prefarea', 'furnishingstatus']
label_encoders = {}
for col in categorical_cols:
le = LabelEncoder()
df[col] = le.fit_transform(df[col])
label_encoders[col] = le

# Example: Statistical operations on 'price' column


price = df['price']

print("\n--- Price Statistics ---")


print("Mean:", [Link]())
print("Median:", [Link]())
print("Mode:", [Link]().values)
print("Standard Deviation:", [Link]())
print("Variance:", [Link]())
print("Minimum:", [Link]())
print("Maximum:", [Link]())

[Link](figsize=(7, 5))
[Link]([Link](), annot=True, cmap='coolwarm')
[Link]("Correlation Matrix Heatmap")
plt.tight_layout()
[Link]()

Abhimanyu Patel 0187CS221008 CS1


Output:

Abhimanyu Patel 0187CS221008 CS1


Practical: 04

Objective : Implement Data Visualization.

Description:

Data Visualization is the graphical representation of data to help understand trends, patterns, and outliers
in a dataset. It enables users to interpret complex data more easily and make decisions based on visual
insights. Visualization techniques are commonly used to communicate information clearly and effectively
to stakeholders or in reports.

Common Data Visualization Techniques:

● Bar Charts: Represent categorical data with rectangular bars, where the length of each bar
corresponds to the value of the category.
● Line Graphs: Show trends over time by connecting data points with a line. They are ideal for
visualizing continuous data.
● Pie Charts: Display proportions of a whole using slices. Each slice represents a category's
contribution to the total.
● Scatter Plots: Show the relationship between two continuous variables by plotting points on a
Cartesian plane. They help in identifying correlations or patterns.
● Histograms: Show the distribution of a single continuous variable by dividing the range of data
into bins and plotting the frequency of data points in each bin.
● Heatmaps: Use color gradients to represent data values in a matrix form. Useful for identifying
patterns in large datasets, such as correlation matrices.
● Box Plots: Display the distribution of a dataset through its quartiles and highlight outliers. They
are useful for comparing distributions across different groups.

Goal:
The goal of data visualization is to present data in a way that is easy to interpret, facilitates
understanding, and allows stakeholders to make decisions quickly based on clear insights from the data.

Key Concepts:

● Clarity: Visualizations should simplify complex data and make it accessible and understandable.
● Design Principles: Proper use of color, scale, and layout to make visualizations effective.
Avoiding misleading visuals is key.
● Interactivity: Interactive visualizations allow users to explore the data by zooming, filtering, or
hovering over elements for more details.
● Dashboarding: Combining multiple visualizations into a single interface to provide an overview
of key metrics, trends, and insights.

Tools for Data Visualization:

● Matplotlib / Seaborn (Python): Popular libraries for static and interactive plots.

Abhimanyu Patel 0187CS221008 CS1


Flow Chart:

Implementation:
import pandas as pd
import [Link] as plt
import seaborn as sns
from [Link] import LabelEncoder

# Load dataset
df = pd.read_csv("Dataset/[Link]")

# Encode categorical columns

Abhimanyu Patel 0187CS221008 CS1


categorical_cols = ['mainroad', 'guestroom', 'basement',
'hotwaterheating', 'airconditioning',
'prefarea', 'furnishingstatus']
for col in categorical_cols:
le = LabelEncoder()
df[col] = le.fit_transform(df[col])

# Set Seaborn style


[Link](style='whitegrid')

# 1. Histogram of House Prices


[Link](figsize=(8, 5))
[Link](df['price'], kde=True, color='skyblue')
[Link]("Distribution of House Prices")
[Link]("Price")
[Link]("Count")
[Link]()

# 2. Box Plot: Price by Furnishing Status


[Link](figsize=(8, 5))
[Link](x='furnishingstatus', y='price', data=df)
[Link]("Price vs Furnishing Status")
[Link]("Furnishing Status (Encoded)")
[Link]("Price")
[Link]()

# 3. Bar Plot: Average Price by Number of Bedrooms


[Link](figsize=(8, 5))
[Link](x='bedrooms', y='price', data=df, estimator='mean', color='skyblue')
[Link]("Average Price by Number of Bedrooms")
[Link]("Bedrooms")
[Link]("Average Price")
[Link]()

# 4. Scatter Plot: Area vs Price


[Link](figsize=(8, 5))
[Link](x='area', y='price', data=df, hue='airconditioning')
[Link]("Area vs Price Colored by Air Conditioning")
[Link]("Area")
[Link]("Price")
[Link]()

Abhimanyu Patel 0187CS221008 CS1


Output:

Abhimanyu Patel 0187CS221008 CS1


Practical: 05

Objective : Implement various Data Transformation Techniques. Perform data scaling or normalization
on numerical features. Encode categorical variables using techniques like one-hot encoding or label
encoding.

Description:
Data transformation techniques are crucial preprocessing steps in any machine learning pipeline. These
techniques help prepare raw data into a suitable format for training models. The main goals are to ensure
that numerical values are on a comparable scale, to handle categorical variables effectively, and to
improve model performance and convergence speed.

Key Data Transformation Techniques:

1. Scaling / Normalization (for Numerical Features): Scaling techniques bring all numerical
values into a uniform range, which helps algorithms (especially those based on distance metrics
or gradient descent) perform optimally.
2. Encoding Categorical Variables: Many machine learning models cannot work with raw
categorical data. Encoding converts categorical values into a numerical format.
Label Encoding:
Converts each category to a unique integer. Useful for ordinal data (e.g., "Low" < "Medium" <
"High").
One-Hot Encoding:
Creates binary columns for each category. Useful for nominal data (e.g., colors, product types).
For example, Color = Red becomes:
Red: 1, Blue: 0, Green: 0.

Key Concepts:

● Why Scale?
Algorithms like KNN, SVM, and Gradient Descent-based models are sensitive to the scale of
input features.
● Why Encoding?
Categorical features must be numeric for models to interpret them. Encoding allows the model to
handle both ordinal and nominal data effectively.

Goal:
To transform raw data into a numerical and scaled format that can be efficiently processed by machine
learning models. Proper transformation improves model accuracy, convergence, and generalization.

Abhimanyu Patel 0187CS221008 CS1


Flow Chart:

Implementation:
import pandas as pd
from [Link] import StandardScaler, MinMaxScaler, Normalizer, LabelEncoder,
OneHotEncoder

# Load dataset
df = pd.read_csv("Dataset/[Link]")

# Show original data


print("Original Data Sample:\n", [Link](5))

# ============================
# 1. Encode Categorical Columns
# ============================

# Label Encoding (ordinal conversion)


label_enc_cols = ['mainroad', 'guestroom', 'basement', 'hotwaterheating', 'airconditioning', 'prefarea']
label_encoders = {}
for col in label_enc_cols:

Abhimanyu Patel 0187CS221008 CS1


le = LabelEncoder()
df[col] = le.fit_transform(df[col])
label_encoders[col] = le

# One-Hot Encoding (nominal features)


df = pd.get_dummies(df, columns=['furnishingstatus'], drop_first=True)

# ============================
# 2. Scale Numerical Features
# ============================

numerical_cols = ['area', 'bedrooms', 'bathrooms', 'stories', 'parking', 'price']

# Create copies for different scalings


df_standard = [Link]()
df_minmax = [Link]()
df_normalized = [Link]()

# Standard Scaling (mean=0, std=1)


scaler_standard = StandardScaler()
df_standard[numerical_cols] = scaler_standard.fit_transform(df_standard[numerical_cols])

# Min-Max Scaling (range 0-1)


scaler_minmax = MinMaxScaler()
df_minmax[numerical_cols] = scaler_minmax.fit_transform(df_minmax[numerical_cols])

# Normalization (L2 norm)


scaler_norm = Normalizer()
df_normalized[numerical_cols] = scaler_norm.fit_transform(df_normalized[numerical_cols])

# ============================
# 3. Show Results
# ============================
print("\n--- Label Encoded + One-Hot Encoded Data Sample ---\n", [Link]())
print("\n--- Standard Scaled Data Sample ---\n", df_standard[numerical_cols].head())
print("\n--- Min-Max Scaled Data Sample ---\n", df_minmax[numerical_cols].head())
print("\n--- Normalized Data Sample ---\n", df_normalized[numerical_cols].head())

Abhimanyu Patel 0187CS221008 CS1


Output:

Abhimanyu Patel 0187CS221008 CS1


Practical: 06

Objective : Split the dataset into training and testing sets.

Description:

Splitting a dataset into training and testing sets is a fundamental step in the machine learning pipeline. It
helps to evaluate the performance of a model by training it on one subset (training set) and testing it on
another (testing set), ensuring that the model generalizes well to unseen data. Typically, the dataset is split
in such a way that the model can learn from a portion of the data and be evaluated on a separate portion to
check for overfitting.

Steps to Split the Dataset:

3. Shuffling: Randomly shuffle the dataset before splitting to ensure that the training and testing
sets are representative of the overall data distribution.
4. Splitting Ratio: Common splitting ratios are:
○ 70% / 30%: 70% of the data for training and 30% for testing.
○ 80% / 20%: 80% of the data for training and 20% for testing.
○ 90% / 10%: 90% of the data for training and 10% for testing.
The exact ratio depends on the dataset size and the problem.
5. Stratified Splitting (if applicable): For classification problems, it's important to maintain the
distribution of the classes in both the training and testing sets. This ensures that the model is
trained on a balanced representation of each class.

Key Concepts:

● Training Set: The subset of data used to train the model. It teaches the model to learn patterns
and relationships.
● Testing Set: The subset of data used to evaluate the model's performance. It tests how well the
model generalizes to new, unseen data.
● Random State: A random seed for reproducibility, ensuring that the data split is the same every
time the code is run.
● Stratified Split: Ensures that the class distribution in the training and testing sets is similar,
especially in imbalanced datasets.

Goal:
The goal of splitting the dataset is to ensure that the model is not overfitting to the training data and can
generalize effectively when faced with new data during testing.

Abhimanyu Patel 0187CS221008 CS1


Flow Chart:

Implementation:
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split

# read the dataset


df = pd.read_csv("Dataset/[Link]")

# get the locations


X = [Link][:, :-1]
y = [Link][:, -1]

# split the dataset


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.05, random_state=0)
print("Training Dataset X_train:")
print(X_train.head(5))

Abhimanyu Patel 0187CS221008 CS1


print("Training Dataset y_train:")
print(y_train.head(5))

Output:

Abhimanyu Patel 0187CS221008 CS1


Practical: 07

Objective : Build a classification model to predict the species of iris flowers using the famous Iris dataset
Logistic Regression

Description:

A classification model is a type of machine learning algorithm used to predict a categorical label or class
for a given input. In classification tasks, the goal is to assign each input data point to one of the
predefined classes based on its features. The model learns patterns in the training data and then applies
those patterns to predict the class of unseen data in the testing set.

Steps to Build a Classification Model:

1. Preprocessing the Data:


○ Handle missing values: Impute missing values or remove rows with missing data.
○ Encode categorical features: Convert non-numeric data (such as strings) into numeric
values using techniques like one-hot encoding or label encoding.
○ Scale numerical features: Standardize or normalize features to bring them to a common
scale, especially for distance-based algorithms like k-NN or SVM.

2. Splitting the Data:


○ Divide the dataset into training and testing sets (usually an 80/20 or 70/30 split).
3. Choose a Classification Algorithm:
○ Logistic Regression: A linear model for binary classification.
○ Decision Trees: A tree-like structure that splits the data based on feature values.
○ Random Forest: An ensemble of decision trees, providing better accuracy.
○ Support Vector Machines (SVM): A powerful classifier that works well for both linear
and non-linear data.
○ k-Nearest Neighbors (k-NN): A non-parametric method that classifies based on the
majority class of the nearest neighbors.
○ Naive Bayes: A probabilistic classifier based on Bayes' theorem, often used for text
classification.
4. Train the Model:
○ Fit the chosen model to the training data to learn the relationships between features and
classes.
5. Evaluate the Model:
○ Use metrics such as accuracy, precision, recall, F1-score, and confusion matrix to
evaluate the model's performance on the testing set.
○ Accuracy: The proportion of correct predictions (good for balanced datasets).
○ Precision and Recall: Useful for imbalanced datasets (e.g., in fraud detection or medical
diagnosis).
○ Confusion Matrix: A detailed breakdown of true positives, true negatives, false
positives, and false negatives.

Abhimanyu Patel 0187CS221008 CS1


Key Concepts:

● Training the Model: The process of teaching the model using labeled data so that it learns to
map inputs to correct class labels.
● Prediction: Using the trained model to classify new, unseen data into predefined classes.
● Evaluation Metrics:
○ Accuracy: The overall proportion of correct predictions.
○ Precision: The ratio of true positives to the total predicted positives.
○ Recall: The ratio of true positives to the total actual positives.
○ F1-score: The harmonic mean of precision and recall, useful when balancing the trade-
off between the two.

Goal:
The goal of building a classification model is to correctly predict the class label of new data based on the
learned patterns from the training data. This model is then evaluated and refined to improve accuracy and
generalization.

Flow Chart:

Implementation:

Abhimanyu Patel 0187CS221008 CS1


import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from [Link] import LabelEncoder
from sklearn.linear_model import LogisticRegression
from [Link] import classification_report, confusion_matrix, accuracy_score
import seaborn as sns
import [Link] as plt

# Load dataset
df = pd.read_csv("Dataset/[Link]")

# Drop 'Id' column if present


if 'Id' in [Link]:
[Link]('Id', axis=1, inplace=True)

# Separate features and target


X = [Link]('Species', axis=1)
y = df['Species']

# Encode target variable


le = LabelEncoder()
y_encoded = le.fit_transform(y) # Converts species names to 0, 1, 2

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

# Train Logistic Regression model


model = LogisticRegression(max_iter=200)
[Link](X_train, y_train)

# Predict
y_pred = [Link](X_test)

# Evaluation
print("Accuracy:", accuracy_score(y_test, y_pred))
print("\nClassification Report:\n", classification_report(y_test, y_pred, target_names=le.classes_))
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))

# Confusion Matrix Plot


[Link](figsize=(6, 4))
[Link](confusion_matrix(y_test, y_pred), annot=True, fmt="d", cmap="Blues",
xticklabels=le.classes_, yticklabels=le.classes_)
[Link]("Confusion Matrix")
[Link]("Predicted")
[Link]("Actual")
[Link]()

Abhimanyu Patel 0187CS221008 CS1


Output:

Abhimanyu Patel 0187CS221008 CS1


Practical: 08

Objective : Train and evaluate each model using appropriate metrics (e.g., accuracy, precision, recall, F1-
score).

Description:

Training and evaluating a machine learning model involves using appropriate metrics to assess its
performance. These metrics help determine how well the model is learning from the data and generalizing
to unseen examples. The choice of evaluation metrics depends on the type of model (e.g., classification or
regression) and the nature of the problem (e.g., balanced or imbalanced classes).

For classification tasks, metrics like accuracy, precision, recall, and F1-score are commonly used to
evaluate model performance.

Steps to Train and Evaluate a Model:

1. Train the Model:

○ Choose an appropriate algorithm for the problem (e.g., Logistic Regression, Random
Forest, SVM).

○ Split the dataset into training and testing sets.

○ Fit the model to the training data to learn the relationships between the features and the
target variable.

2. Make Predictions:

○ Once the model is trained, use it to predict the labels for the testing set or new data.

3. Choose Evaluation Metrics:

○ Accuracy: Measures the overall correctness of the model, calculated as the proportion of
correct predictions.

○ Precision: The proportion of true positive predictions out of all positive predictions made
by the model. Useful when the cost of false positives is high (e.g., in spam detection).

○ Recall (Sensitivity): The proportion of true positive predictions out of all actual positive
instances in the dataset. Crucial when the cost of false negatives is high (e.g., in medical

Abhimanyu Patel 0187CS221008 CS1


diagnoses).

○ F1-Score: The harmonic mean of precision and recall, providing a balance between the
two. It is particularly useful when the classes are imbalanced.

○ Confusion Matrix: A table that shows the performance of the classification model by
comparing actual vs. predicted values, containing true positives (TP), false positives (FP),
true negatives (TN), and false negatives (FN).

4. Evaluate the Model:

○ Use the chosen metrics to evaluate the performance of the model on the testing set.

○ Consider using cross-validation to validate the model's performance on different subsets


of the data.

Key Concepts:

● Accuracy: Measures how often the model correctly predicts the class label.

● Precision: Important when false positives are costly; it ensures the model only predicts positives
when confident.

● Recall: Important when false negatives are costly; it ensures the model identifies as many
positives as possible.

● F1-Score: Balances precision and recall, providing a single metric for models with imbalanced
classes.

● Confusion Matrix: A detailed breakdown of how well the model performs in terms of true and
false positives and negatives.

Goal:
The goal of training and evaluating a model using appropriate metrics is to assess its effectiveness and
reliability. By using a combination of metrics, you can gain a deeper understanding of the model's
strengths and weaknesses and determine whether it is suitable for deployment in real-world applications.

Abhimanyu Patel 0187CS221008 CS1


Flow Chart:

Implementation:
from [Link] import load_iris
from sklearn.model_selection import train_test_split
from [Link] import StandardScaler
from [Link] import accuracy_score, precision_score, recall_score, f1_score
from sklearn.linear_model import LogisticRegression
from [Link] import DecisionTreeClassifier
from [Link] import RandomForestClassifier
from [Link] import SVC
from sklearn.naive_bayes import GaussianNB

Abhimanyu Patel 0187CS221008 CS1


#LOAD DATASET
iris = load_iris()
X, y = [Link], [Link]

#Split data into training and testing


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

#Standardize features
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = [Link](X_test)

#List of models to evaluate


models = {
'Logistic Regression': LogisticRegression(max_iter=500),
'Decision Tree': DecisionTreeClassifier(),
'Random Forest': RandomForestClassifier(),
'Support Vector Machine': SVC(),
'Naive Bayes': GaussianNB()
}

#Evaluate each mmodel


for name, model in [Link]():
[Link](X_train, y_train)
y_pred = [Link](X_test)
print(f"\n{name} Evaluation")
print(f"Accuracy: {accuracy_score(y_test, y_pred):}")
print(f"Precision: {precision_score(y_test, y_pred, average='macro'):}")
print(f"Recall: {recall_score(y_test, y_pred, average='macro'):}")
print(f"F1 Score: {f1_score(y_test, y_pred, average='macro'):}")

Abhimanyu Patel 0187CS221008 CS1


Output:

Abhimanyu Patel 0187CS221008 CS1


Practical: 09

Objective : WAP in python to implement multilayer perceptron

Description:

A Multilayer Perceptron (MLP) is a class of feedforward artificial neural network consisting of multiple
layers of nodes, often organized into an input layer, one or more hidden layers, and an output layer. It is a
supervised learning algorithm used for classification and regression tasks. MLPs can learn complex
relationships in data through backpropagation, making them powerful tools for modeling nonlinear data.

Structure of MLP:

● Input Layer: The first layer, where data is fed into the model. Each node in the input layer
corresponds to a feature in the dataset.

● Hidden Layers: One or more layers of neurons that perform computations and learn patterns in
the data. The number of neurons and layers can vary based on the problem and complexity of the
data.

● Output Layer: The final layer that produces predictions. For classification tasks, the output layer
typically uses a softmax (for multi-class problems) or sigmoid (for binary classification)
activation function.

Key Components:

1. Neurons (Nodes): The basic unit of an MLP. Each neuron computes a weighted sum of its inputs
and applies an activation function.

2. Weights: Parameters that define the importance of each input to the neuron. Weights are learned
during training through backpropagation.

3. Bias: An additional parameter added to the weighted sum before applying the activation function
to shift the activation.

4. Activation Function: A mathematical function that determines the output of a neuron. Common
activation functions include:

○ Sigmoid: Used for binary classification, squashes output to a value between 0 and 1.

○ ReLU (Rectified Linear Unit): Used in hidden layers, outputs zero if the input is less
than zero, and the input itself if it is greater than zero.

Abhimanyu Patel 0187CS221008 CS1


○ Softmax: Used in the output layer for multi-class classification, produces probabilities
for each class.

Training the MLP:

1. Forward Propagation: Input data is passed through the network, where it is processed by each
layer and the final output is produced.

2. Loss Function: The loss function calculates the error between the predicted output and the actual
label (e.g., Cross-Entropy for classification).

3. Backpropagation: The process of adjusting the weights and biases using the gradient of the loss
function. It aims to minimize the error by updating parameters.

4. Optimization: Typically, gradient descent or variants (like Adam) are used to minimize the loss
function by iteratively adjusting the model’s weights.

Key Concepts:

● Feedforward Network: The architecture where data flows in one direction, from input to output,
with no loops.

● Backpropagation: The process of updating weights and biases to minimize the model’s error.

● Gradient Descent: An optimization technique used to minimize the loss function by adjusting
weights based on the gradient of the error.

● Overfitting and Underfitting: Overfitting occurs when the model is too complex and fits the
training data too well, whereas underfitting occurs when the model is too simple and fails to
capture the underlying patterns in the data.

Goal:
The goal of using a Multilayer Perceptron is to model complex relationships between input data and
output labels. By learning from the data through backpropagation and optimizing weights, MLPs can be
applied to various machine learning tasks such as classification, regression, and pattern recognition.

Abhimanyu Patel 0187CS221008 CS1


Flow Chart:

Implementation:
import pandas as pd
from sklearn.model_selection import train_test_split
from [Link] import LabelEncoder, StandardScaler
from sklearn.neural_network import MLPClassifier
from [Link] import classification_report, confusion_matrix, accuracy_score

# Load dataset
df = pd.read_csv("Dataset/[Link]")

# Drop ID column if present


if 'Id' in [Link]:
[Link]('Id', axis=1, inplace=True)

# Features and target


X = [Link]('Species', axis=1)
y = df['Species']

# Encode target labels


le = LabelEncoder()
y = le.fit_transform(y) # Converts species to 0, 1, 2

Abhimanyu Patel 0187CS221008 CS1


# Feature scaling (very important for MLP)
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# Split into train and test sets


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

# Create and train MLP model


mlp = MLPClassifier(hidden_layer_sizes=(10, 10), activation='relu', max_iter=1000, random_state=42)
[Link](X_train, y_train)

# Predict
y_pred = [Link](X_test)

# Evaluate
print("Accuracy:", accuracy_score(y_test, y_pred))
print("\nClassification Report:\n", classification_report(y_test, y_pred, target_names=le.classes_))
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))

Output:

Abhimanyu Patel 0187CS221008 CS1


Practical: 10

Objective : WAP in python to implement ANN model for fashion MNIST dataset.

Description:

An Artificial Neural Network (ANN) is a computational model inspired by the structure and function of
the human brain. It is composed of interconnected nodes (or neurons) arranged in layers, which work
together to recognize patterns and make predictions. ANNs are used for a wide range of tasks, including
classification, regression, and pattern recognition, and are a key component of deep learning models.

Structure of an ANN:

1. Input Layer: The first layer that receives the input features. Each neuron in this layer
corresponds to one feature of the input data.

2. Hidden Layers: Layers between the input and output layers, where computation takes place. An
ANN can have multiple hidden layers, allowing the model to learn complex patterns.

3. Output Layer: The final layer that provides the model's predictions. For classification, it
produces class labels; for regression, it gives continuous values.

4. Neurons (Nodes): Basic computational units that process information. Each neuron receives
inputs, applies a weight to them, adds a bias, and then passes the result through an activation
function.

5. Weights: Parameters that control the strength of the connection between neurons. Weights are
learned during training to minimize the error.

6. Bias: A parameter added to the weighted sum of inputs to shift the activation function. Bias helps
the model adjust the output independently of the input.

7. Activation Function: A mathematical function that introduces non-linearity into the network.
Common activation functions include:

○ Sigmoid: Squashes outputs between 0 and 1, useful for binary classification.

○ ReLU (Rectified Linear Unit): Outputs zero for negative inputs and the input itself for
positive inputs, commonly used in hidden layers.

○ Softmax: Used in the output layer for multi-class classification, converting outputs into
probabilities.

Abhimanyu Patel 0187CS221008 CS1


Training an ANN:

1. Forward Propagation: Input data is passed through the network, layer by layer, to generate an
output.

2. Loss Function: The difference between the predicted output and the actual target is computed
using a loss function (e.g., Cross-Entropy for classification or Mean Squared Error for
regression).

3. Backpropagation: The process of updating the weights and biases using the gradient of the loss
function with respect to each parameter. This allows the model to minimize the error and improve
its predictions.

4. Optimization Algorithm: The weights are updated using optimization algorithms like Gradient
Descent, Stochastic Gradient Descent (SGD), or Adam to minimize the loss.

Key Concepts:

● Neural Network Architecture: The arrangement of neurons in layers (input, hidden, and output)
plays a crucial role in determining the model's capacity to learn from data.

● Forward Propagation: The process where input data flows through the network to produce an
output.

● Backpropagation: A technique used to minimize the error by adjusting weights based on the
gradient of the loss function.

● Activation Functions: Functions like ReLU, Sigmoid, and Softmax allow the network to model
complex, non-linear relationships in data.

● Optimization: Methods like Gradient Descent or Adam are used to update the model parameters
in order to reduce the loss.

Goal:
The goal of an Artificial Neural Network is to learn from the input data and make accurate predictions or
classifications. By adjusting weights and biases through backpropagation, ANNs improve their ability to
generalize to new, unseen data. ANNs are widely used in applications like image recognition, speech
recognition, natural language processing, and more.

Abhimanyu Patel 0187CS221008 CS1


Flow Chart:

Implementation:
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from [Link] import LabelEncoder, StandardScaler
from [Link] import Sequential
from [Link] import Dense

#Load the dataset


data = pd.read_csv('/content/[Link]')

#Preprocess the data


X = [Link](['Id','Species'],axis=1)
y = LabelEncoder().fit_transform(data['Species'])

#feature scaling
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

#split into training and testing sets


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

Abhimanyu Patel 0187CS221008 CS1


#Build the ANN model
model = Sequential()
[Link](Dense(10, input_dim=4, activation='relu'))
[Link](Dense(8, activation='relu'))
[Link](Dense(3, activation='softmax'))

#Compile the model


[Link](loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

#Train the model


[Link](X_train, y_train, epochs=100, batch_size=10, verbose=1)

#Evaluate the model


loss, accuracy = [Link](X_test, y_test, verbose=0)
print('Test Loss:', loss)
print('Test Accuracy: {accuracy:.2f}')

Output:

Abhimanyu Patel 0187CS221008 CS1


Abhimanyu Patel 0187CS221008 CS1
Practical: 11

Objective : WAP in python to understand various activation functions and its uses.

Description:
Activation functions are mathematical functions used in neural networks to introduce non-linearity into
the model. They determine the output of a neuron based on its input, helping the network learn complex
patterns. Without activation functions, neural networks would only be able to model linear relationships,
limiting their ability to solve complex problems.

Types of Activation Functions:

1. Sigmoid (Logistic) Activation Function:

○ Formula: f(x)=11+e−xf(x) = \frac{1}{1 + e^{-x}}f(x)=1+e−x1

○ Range: (0, 1)

○ Commonly used in the output layer for binary classification tasks, where the output needs
to be a probability (between 0 and 1).

○ Pros: Smooth gradient, output is between 0 and 1, which makes it interpretable as a


probability.

○ Cons: Can cause vanishing gradients, where large negative or positive inputs result in
gradients close to zero, slowing down learning.

2. ReLU (Rectified Linear Unit) Activation Function:

○ Formula: f(x)=max⁡(0,x)f(x) = \max(0, x)f(x)=max(0,x)

○ Range: [0, ∞)

○ The most commonly used activation function in the hidden layers of neural networks.

○ Pros: Efficient and helps mitigate the vanishing gradient problem. It speeds up training in
deep networks.

○ Cons: Can lead to dead neurons (a phenomenon where neurons never activate, resulting
in no gradient for them during backpropagation). This is known as the "dying ReLU"
problem.

Abhimanyu Patel 0187CS221008 CS1


3. Leaky ReLU Activation Function:

○ Formula: f(x)=max⁡(αx,x)f(x) = \max(\alpha x, x)f(x)=max(αx,x) where α\alphaα is a


small constant (typically 0.01)

○ Range: (-∞, ∞)

○ A modified version of ReLU that allows a small, non-zero gradient when xxx is less than
zero, helping prevent dead neurons.

○ Pros: Helps solve the problem of dead neurons in ReLU.

○ Cons: Still not a perfect solution and can result in slightly slower training compared to
ReLU.

4. Tanh (Hyperbolic Tangent) Activation Function:

○ Formula: f(x)=ex−e−xex+e−xf(x) = \frac{e^x - e^{-x}}{e^x + e^{-


x}}f(x)=ex+e−xex−e−x

○ Range: (-1, 1)

○ A scaled version of the sigmoid function, where the output is centered around zero,
making it less likely to cause the vanishing gradient problem.

○ Pros: Output is centered around 0, making training easier in some cases.

○ Cons: Like sigmoid, it can still suffer from vanishing gradients for very large or very
small values of xxx.

5. Softmax Activation Function:

○ Formula: f(xi)=exi∑jexjf(x_i) = \frac{e^{x_i}}{\sum_{j} e^{x_j}}f(xi


)=∑jexjexi

○ Range: (0, 1) for each class, and the sum of all outputs equals 1.

○ Commonly used in the output layer of multi-class classification problems.

○ Pros: Converts raw network output into probability distributions over multiple classes,
making it suitable for multi-class classification tasks.

Abhimanyu Patel 0187CS221008 CS1


○ Cons: Computationally expensive as it requires calculating the exponential of every
output.

6. Swish Activation Function:

○ Formula: f(x)=x⋅sigmoid(x)f(x) = x \cdot \text{sigmoid}


(x)f(x)=x⋅sigmoid(x)

○ Range: (-∞, ∞)

○ Proposed by researchers at Google, it combines the benefits of ReLU and sigmoid to


create a smoother activation function.

○ Pros: Can outperform ReLU in some cases, especially in deeper networks, as it avoids
the dead neuron problem and has a smoother gradient.

○ Cons: Slightly more computationally expensive than ReLU.

7. ELU (Exponential Linear Unit) Activation Function:

○ Formula: f(x)=xf(x) = xf(x)=x if x>0x > 0x>0, α(ex−1)\alpha(e^x -


1)α(ex−1) if x≤0x \leq 0x≤0, where α\alphaα is a constant.

○ Range: (-α, ∞)

○ Pros: Solves the vanishing gradient problem better than ReLU by allowing negative
values. It can help improve learning in deeper networks.

○ Cons: More computationally expensive than ReLU, and the choice of α\alphaα can affect
performance

Key Concepts:

● Non-linearity: Activation functions allow neural networks to model complex relationships that
are not just linear.

● Vanishing Gradient Problem: Some activation functions like Sigmoid and Tanh can cause
gradients to become very small, making learning slow or even impossible in deep networks.

● Dead Neurons: ReLU can cause neurons to become "dead" (always outputting zero), making
them unable to contribute to learning.

Goal:
The goal of using activation functions is to enable the neural network to learn complex patterns in data.

Abhimanyu Patel 0187CS221008 CS1


By introducing non-linearity, these functions allow the model to approximate any function and generalize
better, making them essential components of deep learning architectures.

Flow Chart:

Implementation:
import numpy as np
import [Link] as plt

def step(x):
return [Link](x >= 0, 1, 0)

Abhimanyu Patel 0187CS221008 CS1


def sigmoid(x):
"""Sigmoid function: squashes values b/w 0 and 1"""
return 1/(1 + [Link](-x))

def tanh(x):
"""Tangent function: squashes values b/w -1 and 1"""
return [Link](x)

def relu(x):
"""ReLU function: outputs x if +ve, else 0"""
return [Link](0,x)

def leaky_relu(x, alpha=0.02):


"""Leaky ReLU: allows small gradient for -ve values"""
return [Link](x > 0, x, x*alpha)

#Prepare input range


x = [Link](-10, 10, 1000)

#Store fundtions for plotting


activations = {
"Step Function" : step,
"Sigmoid" : sigmoid,
"Tanh" : tanh,
"ReLU" : relu,
"Leaky ReLU" : leaky_relu
}

#Plot activation function


[Link](figsize=(12,8))

for i, (name, func) in enumerate([Link](), start=1):


[Link](2, 3, i)
y = func(x)
[Link](x, y, label=name)
[Link](name)
[Link](True)
[Link]()

plt.tight_layout()
[Link]()

Abhimanyu Patel 0187CS221008 CS1


Output:

Abhimanyu Patel 0187CS221008 CS1


Practical: 12

Objective : WAP in python to implement CNN model for image classification.

Description:

A Convolutional Neural Network (CNN) is a specialized type of deep neural network designed for
processing structured grid data, such as images, video, or audio. CNNs are widely used for image
recognition, classification, and processing tasks due to their ability to automatically learn spatial
hierarchies of features (edges, shapes, textures, etc.) from the data.

CNNs consist of multiple layers, including convolutional layers, pooling layers, and fully connected
layers. The architecture enables the model to capture both local and global patterns in data, making it
highly effective for tasks involving visual or temporal patterns.

Structure of CNN:

1. Input Layer:

○ The raw data (e.g., image) is input into the network. For images, this is typically a 3D
matrix (height, width, channels), where the channels represent the color channels (RGB).

2. Convolutional Layer:

○ The core building block of a CNN. This layer applies convolution operations using a set
of filters (kernels) that slide over the input image. The result is a set of feature maps that
capture spatial hierarchies in the data.

○ Filters (Kernels): Small, learnable weight matrices that are convolved with the input to
extract local features like edges, corners, or textures.

○ Stride and Padding: The stride determines how much the filter moves after each
operation, while padding is used to preserve the input dimensions by adding zeros around
the border.

3. Activation Layer (ReLU):

○ After each convolution operation, a non-linear activation function like ReLU is applied to
introduce non-linearity, enabling the network to learn more complex patterns.

Abhimanyu Patel 0187CS221008 CS1


4. Pooling Layer:

○ Typically used after a convolutional layer, pooling reduces the spatial dimensions (height
and width) of the feature maps while retaining the most important information. Common
pooling operations include:

■ Max Pooling: Takes the maximum value in a defined window.

■ Average Pooling: Takes the average value in a defined window.

5. Fully Connected (FC) Layer:

○ After several convolution and pooling layers, the CNN is usually followed by fully
connected layers (also known as dense layers), which are similar to those in regular
neural networks. These layers are responsible for combining features learned by the
convolutional layers to make predictions.

6. Output Layer:

○ For classification tasks, the output layer typically uses a softmax activation function (for
multi-class classification) or a sigmoid activation function (for binary classification) to
produce the final prediction probabilities.

Key Concepts:

● Convolution: A mathematical operation that involves sliding a filter over the input image to
extract local patterns. The result of a convolution operation is a feature map.

● Feature Maps: Output from the convolutional layer, representing the learned features at different
spatial locations.

● Stride: The number of pixels the filter shifts during convolution. Larger strides result in smaller
feature maps.

● Padding: Adding extra pixels around the input image to preserve its dimensions after
convolution.

● Pooling: A downsampling operation that reduces the dimensionality of feature maps, helping to
reduce computational complexity and mitigate overfitting.

Goal:
The goal of a CNN is to automatically and adaptively learn spatial hierarchies of features from input data,
making it particularly well-suited for tasks like image classification, object detection, and facial

Abhimanyu Patel 0187CS221008 CS1


recognition. CNNs eliminate the need for manual feature extraction and are highly effective in working
with visual data.
Flow Chart:

Implementation:
from [Link] import mnist
from [Link] import Sequential
from [Link] import Conv2D, MaxPool2D, Flatten, Dropout, Dense

#Loading dataset
(X_train, y_train), (X_test, y_test) = mnist.load_data()

#reshape data
X_train = X_train.reshape((X_train.shape[0],X_train.shape[1], X_train.shape[2],1))
X_test = X_test.reshape((X_test.shape[0], X_test.shape[1],X_test.shape[2],1))

Abhimanyu Patel 0187CS221008 CS1


# checking the shape after reshaping
print(X_train.shape)
print(y_train.shape)

#normalizing the pixel values


X_train = X_train /255.0
y_train = X_test / 255.0

#define model
model = Sequential()

#adding convolution layer


[Link](Convo2D(32,(3,3), activation='relu', input_shape=(28,28,1)))

#adding pooling layer


[Link](MaxPool2D(2,2))

#flattening and adding fully connected layers


[Link](Flatten())
[Link](Dense(100,activation='relu'))

Output:

Abhimanyu Patel 0187CS221008 CS1

You might also like