0% found this document useful (0 votes)
15 views14 pages

Practical File DL

The document is a practical file detailing various experiments in deep learning, submitted by Shraddha Kapoor to Dr. Sachin Sharma. It includes four labs focusing on Python basics with NumPy, implementing learning models using open-source libraries, building a Random Forest classifier, and applying Logistic Regression while exploring its neural network equivalence. Each lab outlines objectives, methodologies, code snippets, results, and conclusions, emphasizing the application of machine learning techniques on the Iris dataset.

Uploaded by

hridyensharma
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)
15 views14 pages

Practical File DL

The document is a practical file detailing various experiments in deep learning, submitted by Shraddha Kapoor to Dr. Sachin Sharma. It includes four labs focusing on Python basics with NumPy, implementing learning models using open-source libraries, building a Random Forest classifier, and applying Logistic Regression while exploring its neural network equivalence. Each lab outlines objectives, methodologies, code snippets, results, and conclusions, emphasizing the application of machine learning techniques on the Iris dataset.

Uploaded by

hridyensharma
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
You are on page 1/ 14

PRACTICAL FILE

DEEP LEARNING

SUBMITTED TO: DR. SACHIN SHARMA


SUBMITTED BY: SHRADDHA KAPOOR
ENROLLMENT NO: A25305221061
COURSE: BTECH. CSE

1
S.No Name of Experiment Page No. Signature

1. To implement and practice Python Basics with 3-6


Numpy.
2. To implement the learning models using open-source 7-9
libraries.
3. To build a simple classifier in Python. 10-11

4. To implement Logistic Regression with a neural 12-14


network mindset.

2
Lab 1. To implement and practice Python Basics with Numpy.

Objective:

The objective of this experiment is to implement and practice fundamental Python concepts
using the NumPy library. This includes working with arrays, mathematical operations,
indexing, slicing, and basic statistical functions.

Introduction: Background

Python is a high-level, versatile programming language widely used in scientific computing


and data analysis. NumPy (Numerical Python) is a powerful library that provides support for
multi-dimensional arrays, mathematical functions, and linear algebra operations. It is a
fundamental package for numerical computing in Python and is extensively used in data
science, artificial intelligence, and machine learning applications.

Key features of NumPy:


• Efficient handling of large datasets with ndarray (N-dimensional array).
• Built-in mathematical and statistical functions.

• Broadcasting and vectorization for fast computations.

• Support for linear algebra, Fourier transforms, and random number generation.

Methods: Flowchart/Code Snippets

Flowchart:

Below is a simple flowchart illustrating the steps involved in implementing Python basics
with NumPy:

Step1. Import NumPy library

Step2. Create NumPy arrays

Step3. Perform Array Properties

Step4. Creating Special Arrays

Step5️. Indexing and Slicing

Step6. Reshaping Arrays

Step7. Dot Product

3
Code Snippets

1. Creating Arrays

import numpy as np

arr1 = np.array([1, 2, 3, 4, 5️])


arr2 = np.array([[1, 2, 3], [4, 5️, 6]])

print("1D Array:", arr1)

print("2D Array:\n", arr2)

2. Array Properties

print("Shape of arr2:", arr2.shape)

print("Data type of arr1:", arr1.dtype)

3. Creating Special Arrays

zeros_arr = np.zeros((2, 3))

ones_arr = np.ones((3, 3))

random_arr = np.random.rand(2, 2)

print("Zeros Array:\n", zeros_arr)

print("Ones Array:\n", ones_arr)

print("Random Array:\n", random_arr)

4. Indexing and Slicing

print("Element at index 2 in arr1:", arr1[2])

print("First row of arr2:", arr2[0, :])

print("First column of arr2:", arr2[:, 0])

5️. Reshaping Arrays


reshaped_arr = arr2.reshape(3, 2)

print("Reshaped Array:\n", reshaped_arr)

4
6. Dot Product

arr4 = np.array([[1, 2], [3, 4]])

arr5️ = np.array([[5️, 6], [7, 8]])

dot_product = np.dot(arr4, arr5️)


print("Dot Product:\n", dot_product)

Results:

Results Discussion:

• The code successfully demonstrates the creation of NumPy arrays and performs basic
arithmetic operations like addition and multiplication.

• Indexing and slicing allow us to extract specific portions of an array efficiently.


• NumPy's built-in statistical functions provide quick and efficient calculations of mean
and standard deviation.
5
• The use of broadcasting in NumPy enables element-wise operations without writing
explicit loops, making computations faster and more efficient compared to traditional
Python lists.

Conclusion: Outcome

The experiment successfully demonstrated the basic operations of Python with NumPy. The
implementation of array creation, mathematical operations, indexing, slicing, reshaping and
other features highlights NumPy’s efficiency in handling numerical computations.
Understanding these fundamentals is essential for further exploration of data science,
machine learning, and scientific computing applications.

6
Lab 2: To implement the learning models using open-source libraries.

Objective

The objective of this experiment is to implement learning models using open-source libraries
such as Scikit-learn, TensorFlow, and PyTorch. These libraries provide pre-built functions
and tools for training machine learning models efficiently.

Introduction: Background

Machine learning models are built using datasets to make predictions or classify information.
Open-source libraries like Scikit-learn, TensorFlow, and PyTorch simplify the
implementation of these models by providing optimized mathematical functions, data
handling tools, and pre-trained models.

Common Open-Source Libraries for Machine Learning:

1. Scikit-learn – Ideal for traditional ML algorithms (e.g., regression, SVM, decision


trees).
2. TensorFlow – Best for deep learning models, supports both CPU and GPU
computation.

3. PyTorch – A flexible deep learning framework with dynamic computation graphs.

Methods: Flowchart/Code Snippets

Flowchart:

Step1. Import necessary libraries

Step2. Load dataset (e.g., Iris, MNIST)

Step3. Preprocess data (e.g., scaling, encoding)

Step4. Train machine learning models (e.g., Decision Tree, Neural Network)

Step5️. Evaluate model performance

Step6. Display results and accuracy

Code Snippets:

1. Implementing a Machine Learning Model using Scikit-learn


Example: Decision Tree Classifier on the Iris dataset

from sklearn.datasets import load_iris

from sklearn.model_selection import train_test_split


from sklearn.tree import DecisionTreeClassifier

7
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix

# Load dataset

iris = load_iris()
X, y = iris.data, iris.target

# Split dataset into training and testing sets

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

# Train the Decision Tree model

model = DecisionTreeClassifier(random_state=42)
model.fit(X_train, y_train)

# Make predictions

y_pred = model.predict(X_test)

# Evaluate the model

accuracy = accuracy_score(y_test, y_pred)

print(f"Decision Tree Model Accuracy: {accuracy:.2f}")

# Print classification report

print("\nClassification Report:")

print(classification_report(y_test, y_pred))

8
Results

Results and Discussion

After executing the above Decision Tree classification model on the Iris dataset, the
following results are obtained:

1. Accuracy: The model achieves an accuracy of approximately 1.00 (100%), indicating


that all test samples were correctly classified.

2. Classification Report:
The classification report provides a detailed breakdown of precision, recall, and F1-
score for each class (Setosa, Versicolor, and Virginica). Given the high accuracy,
precision and recall values for all classes are expected to be 1.00.

Conclusion: Outcome

The experiment successfully implemented machine learning models using open-source


libraries. Scikit-learn proved effective for traditional ML models, while TensorFlow
handled deep learning tasks efficiently. This implementation helps in understanding how
learning models work and can be further optimized for real-world applications.

9
Lab 3. To build a simple classifier in Python.

Objective:

The objective of the code is to train a Random Forest Classifier on the Iris dataset, evaluate
its performance, and calculate the accuracy of the model on the test data.

Introduction: Background

The Iris dataset is a well-known dataset in machine learning, consisting of 15️0 samples from
three species of iris flowers (Setosa, Versicolour, and Virginica). Each sample has four
features: sepal length, sepal width, petal length, and petal width. The goal of this experiment
is to use the dataset to classify iris species using the Random Forest algorithm, a robust and
versatile machine learning technique that performs well in many classification tasks.

Methods: Flowchart/Code Snippets

Flowchart:

1. Load the dataset: Import the Iris dataset.

2. Split the data: Divide the dataset into training and test sets.

3. Train the model: Fit the Random Forest Classifier on the training data.

4. Make predictions: Use the trained model to make predictions on the test data.
5️. Evaluate accuracy: Calculate the accuracy of the model by comparing the predictions
to the actual labels of the test data.

Code Snippet:
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

from sklearn.ensemble import RandomForestClassifier

from sklearn.metrics import accuracy_score

# Load dataset

data = load_iris()
X, y = data.data, data.target

# Split data

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

10
# Train classifier

classifier = RandomForestClassifier()

classifier.fit(X_train, y_train)

# Predict and evaluate

y_pred = classifier.predict(X_test)

accuracy = accuracy_score(y_test, y_pred)

print("Accuracy:", accuracy)

Results

Results and Discussions

Results Discussion:
The accuracy score reflects how well the classifier predicts the correct species of the Iris
flowers. A high accuracy (like 1.0 or near 1.0) indicates that the model is performing well.
Factors that could affect accuracy include the complexity of the model, the quality of the
dataset, and the choice of features.

Conclusion: Outcome

The Random Forest classifier successfully trained and evaluated on the Iris dataset and
produced a high accuracy. This demonstrates that the Random Forest algorithm is capable of
efficiently handling this classification task with minimal tuning. Future improvements could
involve tuning the model parameters or trying different classifiers to compare performance.

11
Lab 4. To implement Logistic Regression while understanding its neural network
equivalence.

Objective:

The objective is to implement Logistic Regression on the Iris dataset and explore its
equivalence to a neural network with a single neuron (sigmoid activation function). Logistic
Regression can be interpreted as a neural network with no hidden layers, using a sigmoid
activation function in the output layer.
Introduction: Background

Logistic Regression is a supervised learning algorithm commonly used for binary


classification tasks. It predicts the probability of a class using the logistic function (sigmoid).
In neural networks, Logistic Regression corresponds to a neural network with one neuron in
the output layer, where the neuron applies a sigmoid activation function.

The neural network equivalent of logistic regression can be understood as a simple network
with:

1. Input layer: The features of the dataset.

2. No hidden layers: Direct mapping from inputs to outputs.


3. Output layer: Sigmoid activation function, mapping the result to a probability.

Methods: Flowchart/Code Snippets

Flowchart:

1. Load the dataset: Import the Iris dataset.

2. Split the data: Divide the dataset into training and test sets.

3. Train Logistic Regression model: Train the model using Logistic Regression.

4. Make predictions: Use the trained model to make predictions.

5️. Evaluate accuracy: Calculate the accuracy of the model by comparing predictions to
the actual labels.

Code Snippets
from sklearn.datasets import load_iris

from sklearn.model_selection import train_test_split

from sklearn.linear_model import LogisticRegression

from sklearn.metrics import accuracy_score

import numpy as np

12
# Load dataset

data = load_iris()

X, y = data.data, data.target

# For simplicity, we'll convert this into a binary classification problem (Setosa vs others)

y_binary = np.where(y == 0, 1, 0) # Classifying Setosa (1) vs non-Setosa (0)

# Split data

X_train, X_test, y_train, y_test = train_test_split(X, y_binary, test_size=0.2,


random_state=42)

# Train Logistic Regression classifier

classifier = LogisticRegression()

classifier.fit(X_train, y_train)

# Predict and evaluate

y_pred = classifier.predict(X_test)

accuracy = accuracy_score(y_test, y_pred)

print("Accuracy:", accuracy)

Results

Results and Discussion


The Logistic Regression classifier has likely achieved a perfect classification score (1.0
accuracy) on this dataset. This is not always guaranteed in practice, but Logistic Regression
often performs well on simple datasets like Iris when treated as a binary classification
problem. The high accuracy indicates that the logistic regression model can successfully
differentiate between the two classes.

Conclusion: Outcome

The Logistic Regression model has successfully classified the Iris dataset, treating the
classification task as binary. The neural network equivalent of Logistic Regression is
essentially a one-layer network with a sigmoid activation, where it performs a similar task of

13
mapping inputs to output probabilities. This implementation shows how Logistic Regression
can be viewed through the lens of neural networks, and it illustrates the effectiveness of
logistic regression in classification problems.

14

You might also like