ML LAB Mannual-1
ML LAB Mannual-1
Department of AIMLEngineering
Laboratory Manual
Machine Learning Lab
Subject Code: CSL604
Semester – VI
Prepared by
Machine Learning
Dombivli (E)
Semester-VI
INDEX
Sr. Topic Page
No. No.
1 Vision iii
2 Mission iii
6 Syllabus V
8 List of Experiments ix
VISION
• To impart quality technical education in the department of Artificial Intelligence and
Machine Learning for creating competent and ethically strong engineers with capabilities of
accepting new challenges.
MISSION
• To provide learners with the technical knowledge to build a life long learning career in
the Artificial Intelligence and Machine Learning domain.
• To develop ability among the learners to analyze,design implement engineering
problems and real world applications by providing novel Artificial Intelligence and
Machine Learning Solution.
• To promote close interaction among industry, faculty and learners to enrich the learning
process and enhance career opportunities for learners.
• PSO1 : Ability to understand the concepts and key issues in artificial intelligence and
its associated fields to achieve adequate perspectives in real time applications
• PSO2 : Ability to design, implement solutions for various domains using Machine
learning and Deep Learning techniques.
Useful Links:
1 https://www.learndatasci.com/out/edx-columbia-machine-learning/
2 https://www.learndatasci.com/out/oreilly-hands-machine-learning-scikit-learn-keras-and-
ten sorflow-2nd-edition/
3 https://www.learndatasci.com/out/google-machine-learning-crash-course/
4 https://www.learndatasci.com/out/edx-columbia-machine-learning/
Term Work:
1 Term work should consist of 10 experiments.
2 Journal must include at least 2 assignments.
3 The final certification and acceptance of term work ensures that satisfactory performance of
laboratory work and minimum passing marks in term work.
4 Total 25 Marks (Experiments: 15-marks, Attendance Theory & Practical: 05-marks,
Assignments: 05-marks)
Oral & Practical exam
Based on the entire syllabus of CSL604and CSC604
Course Outcomes:
After successful completion of the course students will be able to:
1 Comprehend basics of Machine Learning
2 Build Mathematical foundation for machine learning
3 Understand various Machine learning models
4 Select suitable Machine learning models for a given problem
5 Build Neural Network based models
6 Apply Dimensionality Reduction techniques
LIST OF EXPERIMENTS
Expt. Name of the Experiment Page COs
No. No.
1. Getting introduced to platforms such as Anaconda, COLAB. 1 CO1
PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12
CO1
CO2
CO3
CO4
CO5
CO6
CO-PSO Mapping Matrix
PSO1 PSO2
CO1
CO2
CO3
CO4
CO5
CO6
EXPERIMENT NO- 1
AIM: Introduction to platforms Anaconda, Google COLAB.
THEORY:
Introduction to Anaconda
Anaconda distribution, a comprehensive platform for data science and scientific computing
in Python. Anaconda simplifies the process of setting up and working with various libraries
and tools commonly used in data science, machine learning, and scientific computing.
Experiment Steps
1. Installation of Anaconda
1. Download Anaconda:
5. Conclusion
This lab document provides a structured outline for conducting an introduction to Anaconda
and Google Colab experiment.In this experiment, we have been introduced to the Anaconda
distribution and its capabilities. and learned how to install Anaconda, create and manage
Python environments, and use Jupyter Notebooks for interactive coding.
Additional Resources
• Anaconda Documentation: Anaconda Documentation
• Jupyter Notebook Documentation: Jupyter Notebook Documentation
In this topic, we will learn to install Python and an IDE with the help of Anaconda
distribution.
Anaconda distribution provides installation of Python with various IDE's such as Jupyter
Notebook, Spyder, Anaconda prompt, etc. Hence it is a very convenient packaged solution
which you can easily download and install in your computer. It will automatically install
Python and some basic IDEs and libraries with it.
Below some steps are given to show the downloading and installing process of Anaconda and
IDE:
• To download Anaconda in your system, firstly, open your favorite browser and type
Download Anaconda Python, and then click on the first link as given in the below
image. Alternatively, you can directly download it by clicking on this link,
https://www.anaconda.com/distribution/#download-section.
• After clicking on the first link, you will reach to download page of Anaconda, as
shown in the below image:
• Since, Anaconda is available for Windows, Linux, and Mac OS, hence, you can
download it as per your OS type by clicking on available options shown in below
image. It will provide you Python 2.7 and Python 3.7 versions, but the latest version
is 3.7, hence we will download Python 3.7 version. After clicking on the download
option, it will start downloading on your computer.
Note: In this topic, we are downloading Anaconda for Windows you can choose it as per your
OS.
Once the downloading process gets completed, go to downloads → double click on the ".exe"
file (Anaconda3-2019.03-Windows-x86_64.exe) of Anaconda. It will open a setup window
for Anaconda installations as given in below image, then click on Next.
• It will open a License agreement window click on "I Agree" option and move further.
• In the next window, you will get two options for installations as given in the below
image. Select the first option (Just me) and click on Next.
• Now you will get a window for installing location, here, you can leave it as default or
change it by browsing a location, and then click on Next. Consider the below image:
• Now installation is completed, tick the checkbox if you want to learn more about
Anaconda and Anaconda cloud. Click on Finish to end the process.
Note: Here, we will use the Spyder IDE to run Python programs.
Step- 3: Open Anaconda Navigator
• After opening the navigator, launch the Spyder IDE by clicking on the Launch button
given below the Spyder. It will install the Spyder IDE in your system.
• Write your first program, and save it using the .py extension.
• Run the program using the triangle Run button.
• You can check the program's output on console pane at the bottom right side.
Below some steps are given to show the downloading and installing process of Anaconda and
IDE:
• To download Anaconda in your system, firstly, open your favorite browser and type
Download Anaconda Python, and then click on the first link as given in the below
image. Alternatively, you can directly download it by clicking on this link,
https://www.anaconda.com/distribution/#download-section.
• After clicking on the first link, you will reach to download page of Anaconda, as
shown in the below image:
• Since, Anaconda is available for Windows, Linux, and Mac OS, hence, you can
download it as per your OS type by clicking on available options shown in below
image. It will provide you Python 2.7 and Python 3.7 versions, but the latest version
is 3.7, hence we will download Python 3.7 version. After clicking on the download
option, it will start downloading on your computer.
Note: In this topic, we are downloading Anaconda for Windows you can choose it as per your
OS.
Once the downloading process gets completed, go to downloads → double click on the ".exe"
file (Anaconda3-2019.03-Windows-x86_64.exe) of Anaconda. It will open a setup window
for Anaconda installations as given in below image, then click on Next.
• It will open a License agreement window click on "I Agree" option and move further.
• In the next window, you will get two options for installations as given in the below
image. Select the first option (Just me) and click on Next.
• Now you will get a window for installing location, here, you can leave it as default or
change it by browsing a location, and then click on Next. Consider the below image:
• Now installation is completed, tick the checkbox if you want to learn more about
Anaconda and Anaconda cloud. Click on Finish to end the process.
Note: Here, we will use the Spyder IDE to run Python programs.
Step- 3: Open Anaconda Navigator
• After opening the navigator, launch the Spyder IDE by clicking on the Launch button
given below the Spyder. It will install the Spyder IDE in your system.
• Write your first program, and save it using the .py extension.
• Run the program using the triangle Run button.
• You can check the program's output on console pane at the bottom right side.
On your first visit, you will see a Welcome To Colaboratory notebook with links to video
introductions and basic information on how to use Colab.
Create a workbook
If you are not yet logged in to a Google account, the system will prompt you to log in.
The notebook will by default have a generic name; click on the filename field to rename it.
The file type, IPYNB, is short for "IPython notebook" because IPython was the forerunner of
Jupyter Notebook.
The interface allows you to insert various kinds of cells, mainly text and code, which have
their own shortcut buttons under the menu bar via the Insert menu.
Because notebooks are meant for sharing, there are accommodations throughout for
structured documentation.
You can insert Python code to execute in a code cell. The code can be entirely standalone or
imported from various Python libraries.
A notebook can be treated as a rolling log of work, with earlier code snippets being no longer
executed in favor of later ones, or treated as an evolving set of code blocks intended for
ongoing execution. The Runtime menu offers execution options, such as Run all, Run
before or Run the focused cell, to match either approach.
Each code cell has a run icon on the left edge, as shown above. You can type code into a cell
and hit the run icon to execute it immediately.
If the code generates an error, the error output will appear beneath the cell. Correcting the
problem and hitting run again replaces the error info with program output. The first line of
code, in its own cell, imports the NumPy library, which is the source of the arange
function. Colab has many common libraries pre-loaded for easy import into programs.
A text cell provides basic rich text using Markdown formatting by default and allows for the
insertion of images, HTML code and LaTeX formatting.
As you add text on the left side of the text cell, the formatted output appears on the right.
Once you stop editing a block, only the final formatted version shows.
After getting comfortable with the interface and using it for initial test coding, you must
eventually provide the code with data to analyze or otherwise manipulate.
Colab can mount a user's Google Drive to the VM hosting their notebook using a code cell.
Once you hit run, Google will ask for permission to mount the drive.
If you allow it to connect, you will then have access to the files in your Google Drive via the
/my_drive path.
If you prefer not to grant access to your Drive space, you can upload files or any network file
space mounted as a drive from your local machine instead.
with file access, many functions are available to read data in various ways. For example,
importing the Pandas library gives access to functions such as read_csv and read_json.
By default, Colab puts notebooks in a Colab Notebooks folder under My Drive in Google
Drive.
The File menu enables notebooks to be saved as named revisions in the version history,
relocated using Move, or saved as a copy in Drive or GitHub. It also allows you to download
and upload notebooks. Tools based on Jupyter provide broad compatibilities, so you can
create notebooks in one place and then upload and use them in another.
You can use the Share button in the upper right to grant other Google users access to the
notebook and to copy links.
Google also provides example notebooks illustrating available resources, such as pre-trained
image classifiers and language transformers, as well as addressing common business
problems, such as working with BigQuery or performing time series analytics. It also
provides links to introductory Python coding notebooks.
Experiment No: 2
Aim: Study of Machine learning Libraries and Tools
Objective: The objective of this experiment is to provide students with hands-on experience
in using popular machine learning libraries and tools. Participants will explore libraries such
as scikit-learn, TensorFlow, and PyTorch, and familiarize themselves with essential machine
learning tasks.
Prerequisites
• Basic understanding of Python programming language.
• Familiarity with fundamental machine learning concepts.
RESOURCES REQUIRED: H/W :- P4 machine
S/W :- Jupyter Notebook
Theory:
Here are some of the best libraries of Python used for Machine Learning:
• NumPy
• SciPy
• Scikit-learn
• Pandas
• Matplotlib
• Seaborn
• TensorFlow
• Keras
• PyTorch
With NumPy, you can define arbitrary data types and easily integrate with most databases.
NumPy can also serve as an efficient multi-dimensional container for any generic data that is
in any datatype. The key features of NumPy include powerful N-dimensional array object,
broadcasting functions, and out-of-box tools to integrate C/C++ and Fortran code.
Advantages:
Disadvantages:
• Its data types are hardware-native and not Python-native, so it costs heavily when
NumPy entities have to be translated back to Python-equivalent entities and vice versa.
P = nup.array([10, 12])
S = nup.array([9, 11])
Output:
2) SciPy
SciPy stack, as SciPy library is one of the core packages which made up the SciPy stack. SciPy
library is used for image manipulation tasks.
Advantages:
Disadvantages:
Example 1:
Output:
Example 2:
Output:
3) Scikit-learn
The following features of scikit-learn make it one of the best machine learning libraries in
Python:
Example:
Output:
DecisionTreeClassifier()
precision recall f1-score support
[[50 0 0]
[ 0 50 0]
[ 0 0 50]]
4) Pandas
Pandas is a Python library that is mainly used for data analysis. The
users have to prepare the dataset before using it for training the
machine learning. Pandas make it easy for the developers as it is
developed specifically for data extraction. It has a wide variety of
tools for analysing data in detail, providing high-level data structures.
Advantages:
Disadvantages:
• Series (1-dimensional)
• DataFrame (2-dimensional)
These two put together can handle a vast majority of data requirements and use cases from
most sectors like science, statistics, social, finance, and of course, analytics and other areas of
engineering.
Pandas support and perform well with different kinds of data including the below :
• Tabular data with columns of heterogeneous data. For instance, consider the data
coming from the SQL table or Excel spreadsheet.
• Ordered and unordered time series data. The frequency of time series need not be fixed,
unlike other libraries and tools. Pandas is exceptionally robust in handling uneven time-
series data
• Arbitrary matrix data with the homogeneous or heterogeneous type of data in the rows
and columns
• Any other form of statistical or observational data sets. The data need not be labeled at
all. Pandas data structure can process it even without labeling.
It was launched as an open-source Python library in 2009. Currently, it has become one of the
favourite Python libraries for machine learning among many ML enthusiasts. The reason is
it offers some robust techniques for data analysis and data manipulation. This library is
extensively used in academia. Moreover, it supports different commercial domains like
business and web analytics, economics, statistics, neuroscience, finance, advertising, etc. It
also works as a foundational library for many advanced Python libraries.
Example:
Output:
5) Matplotlib
Matplotlib is a Python library that is used for data visualization. It is used by developers when
they want to visualize the data and its patterns. It is a 2-D plotting library that is used to create
2-D graphs and plots.
It has a module pyplot which is used for plotting graphs, and it provides different features for
control line styles, font properties, formatting axes and many more. Matplotlib provides
different types of graphs and plots such as histograms, error charts, bar charts and many more.
Example 1:
Output:
Example 2:
2.
3. # Creating dataset-1
4. K_1 = [8, 4, 6, 3, 5, 10,
5. 13, 16, 12, 21]
6.
7. R_1 = [11, 6, 13, 15, 17, 5,
8. 3, 2, 8, 19]
9.
10. # Creating dataset2
11. K_2 = [6, 9, 18, 14, 16, 15,
12. 11, 16, 12, 20]
13.
14. R_2 = [16, 4, 10, 13, 18,
15. 20, 6, 2, 17, 15]
16.
17. plot.scatter(K_1, R_1, c = "Black",
18. linewidths = 2,
19. marker = "s",
20. edgecolor = "Brown",
21. s = 50)
22.
23. plot.scatter(K_2, R_2, c = "Purple",
24. linewidths = 2,
25. marker = "^",
26. edgecolor = "Grey",
27. s = 200)
28.
29. plt.xlabel ("X-axis")
30. plt.ylabel ("Y-axis")
31. print ("Scatter Plot")
32. plt.show()
Output:
Matplotlib is a data visualization library that is used for 2D plotting to produce publication-
quality image plots and figures in a variety of formats. The library helps to generate histograms,
plots, error charts, scatter plots, bar charts with just a few lines of code.
It is the oldest Python machine learning library. However, it is still not obsolete. It is one of
the most innovative data visualization libraries for Python. So, the ML community admires it.
The following features of the Matplotlib library make it a famous Python machine learning
among the ML community:
Advantages:
Disadvantages:
• It has a strong dependency on NumPy and other such libraries for the SciPy stack.
• It has a high learning curve as its use takes quite a lot of knowledge and application
from the learner’s end.
• It can be confusing for developers as it provides two distinct frameworks, object-
oriented and MATLAB.
• It is primarily used for data visualization. It is not suitable for data analysis. To get both
data visualization and data analysis, we will have to integrate it with other libraries.
6) Seaborn
Advantages:
• It produces graphs that are more appealing than those created with Matplotlib.
• It has integrated packages that are unavailable in Matplotlib.
• It uses less code for visualizing graphs.
• It is integrated with pandas for visualizing and analyzing data.
Disadvantages:
7)TensorFlow
TensorFlow exposes a very stable Python and C++ APIs. It can expose, backward compatible
APIs for other languages too, but they might be unstable. TensorFlow has a flexible
architecture with which it can run on a variety of computational platforms CPUs, GPUs, and
TPUs. TPU stands for Tensor processing unit, a hardware chip built around TensorFlow for
machine learning and artificial intelligence.
The following key features of TensorFlow make it one of the best machine learning libraries
Python:
8) Keras
Keras works with neural-network building blocks like layers, objectives, activation functions,
and optimizers. Keras also have a bunch of features to work on images and text images that
comes handy when writing Deep Neural Network code.
Apart from the standard neural network, Keras supports convolutional and recurrent neural
networks.
It was released in 2015 and by now, it is a cutting-edge open-source Python deep learning
framework and API. It is identical to Tensorflow in several aspects. But it is designed with a
human-based approach to make DL and ML accessible and easy for everybody.
You can conclude that Keras is one of the versatile machine learning libraries Python
because it includes:
9) PyTorch
PyTorch has a range of tools and libraries that support
computer vision, machine learning, and natural language
processing. The PyTorch library is open-source and is
based on the Torch library. The most significant advantage
of PyTorch library is it’s ease of learning and using.
PyTorch can smoothly integrate with the python data science stack, including NumPy. You
will hardly make out a difference between NumPy and PyTorch. PyTorch also allows
developers to perform computations on Tensors. PyTorch has a robust framework to build
computational graphs on the go and even change them in runtime. Other advantages of PyTorch
include multi GPU support, simplified preprocessors, and custom data loaders.
Conclusion
In this Experiment, we have discussed about different libraries of Python and Machine learning
which are used for performing Machine learning tasks. We have also shown different examples
of each library.
Aim: To study & explore the performance of linear regression in various real-world scenarios.
Introduction:
Linear regression is a fundamental statistical technique widely used in various fields to model
the relationship between a dependent variable and one or more independent variables..
Linear regression serves as a powerful tool for predictive modeling and understanding the
relationship between variables. This study investigates its applicability in diverse scenarios,
addressing questions regarding its assumptions, accuracy, and potential challenges.
2. Experimental Design:
Dataset Selection: We selected a diverse set of datasets from different domains, ranging from
finance and healthcare to social sciences. Each dataset was preprocessed to handle missing
values and outliers.
Model Training: Linear regression models were trained on each dataset using standard
techniques. The performance metrics such as Mean Squared Error (MSE), R-squared, and
adjusted R-squared were employed to evaluate model fitness.
Theory:
Linear Regression
Simple linear regression is a type of regression analysis where the number of independent
variables is one and there is a linear relationship between the independent(x) and dependent(y)
variable. The red line in the above graph is referred to as the best fit straight line. Based on the
given data points, we try to plot a line that models the points the best. The line can be modelled
based on the linear equation shown below.
The motive of the linear regression algorithm is to find the best values for a_0 and a_1. Before
moving on to the algorithm, let’s have a look at two important concepts you must know to
better understand linear regression.
Cost Function
The cost function helps us to figure out the best possible values for a_0 and a_1 which would
provide the best fit line for the data points. Since we want the best values for a_0 and a_1, we
convert this search problem into a minimization problem where we would like to minimize the
error between the predicted value and the actual value.
We choose the above function to minimize. The difference between the predicted values and
ground truth measures the error difference. We square the error difference and sum over all
data points and divide that value by the total number of data points. This provides the average
squared error over all the data points. Therefore, this cost function is also known as the Mean
Squared Error(MSE) function. Now, using this MSE function we are going to change the
values of a_0 and a_1 such that the MSE value settles at the minima.
Gradient Descent
The next important concept needed to understand linear regression is gradient descent.
Gradient descent is a method of updating a_0 and a_1 to reduce the cost function(MSE). The
idea is that we start with some values for a_0 and a_1 and then we change these values
iteratively to reduce the cost. Gradient descent helps us on how to change the values.
Gradient Descent
To draw an analogy, imagine a pit in the shape of U and you are standing at the topmost point
in the pit and your objective is to reach the bottom of the pit. There is a catch, you can only
take a discrete number of steps to reach the bottom. If you decide to take one step at a time you
would eventually reach the bottom of the pit but this would take a longer time. If you choose
to take longer steps each time, you would reach sooner but, there is a chance that you could
overshoot the bottom of the pit and not exactly at the bottom. In the gradient descent algorithm,
the number of steps you take is the learning rate. This decides on how fast the algorithm
converges to the minima.
Sometimes the cost function can be a non-convex function where you could settle at a local
minima but for linear regression, it is always a convex function.
You may be wondering how to use gradient descent to update a_0 and a_1. To update a_0 and
a_1, we take gradients from the cost function. To find these gradients, we take partial
derivatives with respect to a_0 and a_1. Now, to understand how the partial derivatives are
found below you would require some calculus but if you don’t, it is alright. You can take it as
it is.
The partial derivates are the gradients and they are used to update the values of a_0 and a_1.
Alpha is the learning rate which is a hyperparameter that you must specify. A smaller learning
rate could get you closer to the minima but takes more time to reach the minima, a larger
learning rate converges sooner but there is a chance that you could overshoot the minima.
4. Results:
Assumption Validation: The experimental results demonstrated that while linear regression
assumes certain conditions, deviations from these assumptions did not always lead to
significant model deterioration. Robustness to violations of normality and homoscedasticity
was observed in specific scenarios.
Model Performance: The performance of linear regression varied across datasets. In some
cases, it provided accurate predictions, while in others, its simplicity led to underfitting. The
importance of feature selection and engineering emerged as crucial factors in improving model
performance.
PROGRAM:
OUTPUT:
Aim: To study & explore the performance of logistic regression in various real-world
scenarios.
Introduction:
Logistic regression is a statistical method used for binary classification, where the dependent
variable is categorical and has only two possible outcomes. It models the probability that an
instance belongs to a particular class based on one or more independent variables. Despite
its simplicity, logistic regression can provide valuable insights into the relationships between
predictors and outcomes, making it a versatile tool in machine learning.
Theory :
• Logistic regression is one of the most popular Machine Learning algorithms, which
comes under the Supervised Learning technique. It is used for predicting the
categorical dependent variable using a given set of independent variables.
• Logistic regression predicts the output of a categorical dependent variable. Therefore
the outcome must be a categorical or discrete value. It can be either Yes or No, 0 or 1,
true or False, etc. but instead of giving the exact value as 0 and 1, it gives the
probabilistic values which lie between 0 and 1.
• Logistic Regression is much similar to the Linear Regression except that how they are
used. Linear Regression is used for solving Regression problems, whereas Logistic
regression is used for solving the classification problems.
• In Logistic regression, instead of fitting a regression line, we fit an "S" shaped logistic
function, which predicts two maximum values (0 or 1).
• The curve from the logistic function indicates the likelihood of something such as
whether the cells are cancerous or not, a mouse is obese or not based on its weight,
etc.
• Logistic Regression is a significant machine learning algorithm because it has the
ability to provide probabilities and classify new data using continuous and discrete
datasets.
• Logistic Regression can be used to classify the observations using different types of
data and can easily determine the most effective variables used for the classification.
The below image is showing the logistic function:
• The sigmoid function is a mathematical function used to map the predicted values to
probabilities.
• It maps any real value into another value within a range of 0 and 1.
• The value of the logistic regression must be between 0 and 1, which cannot go
beyond this limit, so it forms a curve like the "S" form. The S-form curve is called the
Sigmoid function or the logistic function.
• In logistic regression, we use the concept of the threshold value, which defines the
probability of either 0 or 1. Such as values above the threshold value tends to 1, and
a value below the threshold values tends to 0.
The Logistic regression equation can be obtained from the Linear Regression equation. The
mathematical steps to get Logistic Regression equations are given below:
In Logistic Regression y can be between 0 and 1 only, so for this let's divide the above equation
by (1-y):
• But we need range between -[infinity] to +[infinity], then take logarithm of the
equation it will become:
On the basis of the categories, Logistic Regression can be classified into three types:
• Binomial: In binomial Logistic regression, there can be only two possible types of the
dependent variables, such as 0 or 1, Pass or Fail, etc.
• Multinomial: In multinomial Logistic regression, there can be 3 or more possible
unordered types of the dependent variable, such as "cat", "dogs", or "sheep"
• Ordinal: In ordinal Logistic regression, there can be 3 or more possible ordered types
of dependent variables, such as "low", "Medium", or "High".
# Prediction
y_pred = clf.predict(X_test)
OUTPUT:
Logistic Regression model accuracy (in %): 94.73684210526315
OUTPUT:
Aim: To study & explore the performance Implementation of Support Vector Machine
Regression & Classifier in Python.
Introduction:
Support Vector Machines (SVMs) are a class of supervised learning algorithms that analyze
data for classification and regression analysis. The primary objective of SVMs is to find the
optimal hyperplane that separates data points into different classes while maximizing the
margin, which is the distance between the hyperplane and the nearest data points of each
class. SVMs are particularly useful in scenarios where the data is not linearly separable, as
they can map the data to a higher-dimensional space using kernel functions to facilitate
separation.
Theory:
The goal of the SVM algorithm is to create the best line or decision boundary that can
segregate n-dimensional space into classes so that we can easily put the new data point in the
correct category in the future. This best decision boundary is called a hyperplane.
SVM chooses the extreme points/vectors that help in creating the hyperplane. These extreme
cases are called as support vectors, and hence algorithm is termed as Support Vector
Machine. Consider the below diagram in which there are two different categories that are
classified using a decision boundary or hyperplane:
Types of SVM
• Linear SVM: Linear SVM is used for linearly separable data, which means if a dataset
can be classified into two classes by using a single straight line, then such data is
termed as linearly separable data, and classifier is used called as Linear SVM
classifier.
• Non-linear SVM: Non-Linear SVM is used for non-linearly separated data, which
means if a dataset cannot be classified by using a straight line, then such data is
termed as non-linear data and classifier used is called as Non-linear SVM classifier.
The dimensions of the hyperplane depend on the features present in the dataset, which means
if there are 2 features (as shown in image), then hyperplane will be a straight line. And if
there are 3 features, then hyperplane will be a 2-dimension plane.
We always create a hyperplane that has a maximum margin, which means the maximum
distance between the data points.
Support Vectors:
The data points or vectors that are the closest to the hyperplane and which affect the position
of the hyperplane are termed as Support Vector. Since these vectors support the hyperplane,
hence called a Support vector.
Linear SVM:
The working of the SVM algorithm can be understood by using an example. Suppose we
have a dataset that has two tags (green and blue), and the dataset has two features x1 and x2.
We want a classifier that can classify the pair(x1, x2) of coordinates in either green or blue.
Consider the below image:
So as it is 2-d space so by just using a straight line, we can easily separate these two classes.
But there can be multiple lines that can separate these classes. Consider the below image:
Hence, the SVM algorithm helps to find the best line or decision boundary; this best
boundary or region is called as a hyperplane. SVM algorithm finds the closest point of the
lines from both the classes. These points are called support vectors. The distance between the
vectors and the hyperplane is called as margin. And the goal of SVM is to maximize this
margin. The hyperplane with maximum margin is called the optimal hyperplane.
Non-Linear SVM:
If data is linearly arranged, then we can separate it by using a straight line, but for non-linear
data, we cannot draw a single straight line. Consider the below image:
So to separate these data points, we need to add one more dimension. For linear data, we have
used two dimensions x and y, so for non-linear data, we will add a third dimension z. It can
be calculated as:
z=x2 +y2
By adding the third dimension, the sample space will become as below image:
Since we are in 3-d Space, hence it is looking like a plane parallel to the x-axis. If we convert
it in 2d space with z=1, then it will become as:
# Scatter plot
plt.scatter(X[:, 0], X[:, 1],
c=y,
s=20, edgecolors="k")
plt.show()
OUTPUT:
Aim: implementation of Hebbian learning rules to train a neural network to perform the
logical AND operation.
Introduction:
Theory :
Methodology:
1]. Neural Network Architecture: We design a simple neural network consisting of two input
neurons and one output neuron. The input neurons represent the two binary inputs of the AND
gate, and the output neuron produces the result of the AND operation.
2]. Activation Function: We use a bipolar sigmoid function as the activation function for the
output neuron. The bipolar sigmoid function maps the output of the neuron to the range [-1, 1],
introducing non-linearity into the network.
3]. Hebbian Learning: We implement Hebbian learning rules to update the weights of the
connections between neurons. Hebbian learning is a local learning rule that strengthens the
connections between neurons when they are simultaneously active.
4]. Training: We train the neural network using Hebbian learning with input patterns
corresponding to all possible combinations of binary inputs for the AND gate.
5]. Testing: After training, we evaluate the performance of the neural network by feeding it
with different input patterns and observing the corresponding output.
There are 4 training samples, so there will be 4 iterations. Also, the activation function used
here is Bipolar Sigmoidal Function so the range is [-1,1].
Step 1 :
Set weight and bias to zero, w = [ 0 0 0 ]T and b = 0.
Step 2 :
Set input vector Xi = Si for i = 1 to 4.
X1 = [ -1 -1 1 ]T
X2 = [ -1 1 1 ]T
X3 = [ 1 -1 1 ]T
X 4 = [ 1 1 1 ]T
Step 3 :
Output value is set to y = t.
Step 4 :
Modifying weights using Hebbian Rule:
First iteration –
w(new) = w(old) + x 1y1 = [ 0 0 0 ]T + [ -1 -1 1 ]T . [ -1 ] = [ 1 1 -1 ]T
For the second iteration, the final weight of the first one will be used and so on.
Second iteration –
w(new) = [ 1 1 -1 ]T + [ -1 1 1 ]T . [ -1 ] = [ 2 0 -2 ]T
Third iteration –
w(new) = [ 2 0 -2]T + [ 1 -1 1 ]T . [ -1 ] = [ 1 1 -3 ]T
Fourth iteration –
w(new) = [ 1 1 -3]T + [ 1 1 1 ]T . [ 1 ] = [ 2 2 -2 ]T
So, the final weight matrix is [ 2 2 -2 ]T
The experiment demonstrates the successful implementation of Hebbian learning rules for
training a neural network to perform the logical AND operation .The weights of the neural
network are adjusted during the training process to effectively perform the AND operation.
After training, the neural network successfully produces the correct output for all possible input
patterns of the AND gate.
import numpy as np
def bipolar_sigmoid(x):
return 2 / (1 + np.exp(-x)) - 1
weights += delta_w
def AND_gate(input_data):
learning_rate = 0.1
# Training
for _ in range(1000):
for i in range(len(input_patterns)):
input_pattern = np.array(input_patterns[i])
# Testing
print("\nTesting:")
for i in range(len(input_patterns)):
input_pattern = np.array(input_patterns[i])
AND_gate([])
OUTPUT:
Testing:
Input: [-1, -1] Output: [-1.]
Input: [-1, 1] Output: [0.12670444]
Input: [1, -1] Output: [-0.12670444]
Input: [1, 1] Output: [1.]
Introduction:
The McCulloch-Pitts model, proposed by Warren McCulloch and Walter Pitts in 1943, is one
of the earliest neural network models. It describes the behavior of a simplified neuron, which
receives input signals, applies weights to them, sums them up, and produces an output based
on a threshold. In this experiment, we apply the McCulloch-Pitts model to implement an AND
gate, a fundamental logical operation.
THEORY:
Logic gates are elementary building blocks of digital circuits, performing logical operations
on binary inputs. By implementing logic gates using neural networks, we bridge the gap
between conventional digital computing and neural computation. The McCulloch-Pitts
neural network model, inspired by the biological neurons, offers a simple yet powerful
framework for modeling logical operations.
Methodology:
McCulloch-Pitts Neurons: We implement the McCulloch-Pitts neuron model, which
receives binary input signals and produces binary outputs based on predefined thresholds.
NOT Gate Implementation: For the NOT gate, we employ a single McCulloch-Pitts neuron
that inversely mirrors its input signal.
Experiment Execution: We test each logic gate implementation by providing different input
combinations and observing the corresponding outputs. We verify that the neural networks
accurately compute the truth tables of their respective logic gates.
Architecture of OR Gate
Results:
The implemented McCulloch-Pitts neural networks successfully emulate the behavior of
logic gates. For each gate (AND, OR, and NOT), the neural networks produce the expected
outputs based on their truth tables, demonstrating the ability to perform logical operations
using neural computation.
Conclusion:
This experiment highlights the versatility of the McCulloch-Pitts neural network model in
simulating logical operations. By configuring the connections and thresholds of McCulloch-
Pitts neurons, we can construct neural networks that mimic the behavior of conventional
logic gates. This integration of neural and digital computing paradigms opens avenues for
novel approaches to information processing and computational tasks.
x2 = [0, 1, 0, 1]
w1 = [1, 1, 1, 1]
w2 = [1, 1, 1, 1]
t = 2
#output
print("x1 x2 w1 w2 t O")
for i in range(len(x1)):
if ( x1[i]*w1[i] + x2[i]*w2[i] ) >= t:
print(x1[i],' ',x2[i],' ',w1[i],' ',w2[i],' ',t,' ', 1)
else:
print(x1[i],' ',x2[i],' ',w1[i],' ',w2[i],' ',t,' ', 0)
FOR OR GATE¶
#inputs
x1 = [0, 0, 1, 1]
x2 = [0, 1, 0, 1]
w1 = [1, 1, 1, 1]
w2 = [1, 1, 1, 1]
t = 1
#output
print("x1 x2 w1 w2 t O")
for i in range(len(x1)):
if ( x1[i]*w1[i] + x2[i]*w2[i] ) >= t:
print(x1[i],' ',x2[i],' ',w1[i],' ',w2[i],' ',t,' ', 1)
else:
print(x1[i],' ',x2[i],' ',w1[i],' ',w2[i],' ',t,' ', 0)
OUTPUT OR GATE:
x1 x2 w1 w2 t O
0 0 1 1 1 0
0 1 1 1 1 1
1 0 1 1 1 1
1 1 1 1 1 1
#inputs
x = [0, 1]
w = [-1, -1]
t = 0
#output
print("x w t O")
for i in range(len(x)):
if ( x[i]*w[i]) >= t:
print(x[i],' ',w[i],' ',t,' ', 1)
else:
print(x[i],' ',w[i],' ',t,' ', 0)
x w t O
0 -1 0 1
1 -1 0 0
#inputs
x1 = [0, 0, 1, 1]
x2 = [0, 1, 0, 1]
w1 = [-1, -1, -1, -1]
w2 = [-1, -1, -1, -1]
t = -2
#output
print("x1 x2 w1 w2 t O")
for i in range(len(x1)):
if ( x1[i]*w1[i] + x2[i]*w2[i] ) > t:
print(x1[i],' ',x2[i],' ',w1[i],' ',w2[i],' ',t,' ', 1)
else:
print(x1[i],' ',x2[i],' ',w1[i],' ',w2[i],' ',t,' ', 0)
x1 x2 w1 w2 t O
0 0 -1 -1 -2 1
0 1 -1 -1 -2 1
1 0 -1 -1 -2 1
1 1 -1 -1 -2 0
#inputs
x1 = [0, 0, 1, 1]
x2 = [0, 1, 0, 1]
w1 = [1, 1, 1, 1]
w2 = [1, 1, 1, 1]
t = 0
#output
print("x1 x2 w1 w2 t O")
for i in range(len(x1)):
if ( x1[i]*w1[i] + x2[i]*w2[i] ) <= t:
print(x1[i],' ',x2[i],' ',w1[i],' ',w2[i],' ',t,' ', 1)
else:
print(x1[i],' ',x2[i],' ',w1[i],' ',w2[i],' ',t,' ', 0)
x1 x2 w1 w2 t O
0 0 1 1 0 1
0 1 1 1 0 0
1 0 1 1 0 0
1 1 1 1 0 0
#inputs
x1 = [0, 0, 1, 1]
x2 = [0, 1, 0, 1]
w1 = [1, 1, 1, 1]
w2 = [1, 1, 1, 1]
w3 = [1, 1, 1, 1]
w4 = [-1, -1, -1, -1]
w5 = [-1, -1, -1, -1]
w6 = [1, 1, 1, 1]
t1 = [0.5,0.5,0.5,0.5]
t2 = [-1.5,-1.5,-1.5,-1.5]
t3 = [1.5,1.5,1.5,1.5]
def XOR (a, b):
if a != b:
return 1
else:
return 0
#output
print('x1 x2 w1 w2 w3 w4 w5 w6 t1 t2 t3 O')
for i in range(len(x1)):
print(x1[i],' ',x2[i],' ',w1[i],' ',w2[i],' ',w3[i],' ',w4[i],' ',
w5[i],' ',w6[i],' ',t1[i],' ',t2[i],' ',t3[i],' ',XOR(x1[i],x2[i]))
x1 x2 w1 w2 w3 w4 w5 w6 t1 t2 t3 O
0 0 1 1 1 -1 -1 1 0.5 -1.5 1.5 0
0 1 1 1 1 -1 -1 1 0.5 -1.5 1.5 1
1 0 1 1 1 -1 -1 1 0.5 -1.5 1.5 1
1 1 1 1 1 -1 -1 1 0.5 -1.5 1.5 0
Title: implementation of PCA on the Iris dataset using Python and scikit-learn library.
Aim: transform high-dimensional data into a lower-dimensional space while preserving the
most important information in the dataset.
Introduction:
PCA, which stands for Principal Component Analysis, is a dimensionality reduction technique
used in data analysis and machine learning. It aims to transform high-dimensional data into a
lower-dimensional space while preserving the most important information in the dataset.
The main idea behind PCA is to identify the directions (principal components) in which the
data varies the most. These directions represent the axes along which the data points have the
highest variance. By projecting the data onto these principal components, PCA allows for a
more compact representation of the dataset while minimizing information loss.
1. Standardization: PCA typically starts with standardizing the features of the dataset to
have a mean of 0 and a standard deviation of 1. This step is crucial because PCA is
sensitive to the scale of features.
2. Covariance Matrix Calculation: PCA calculates the covariance matrix of the
standardized data. The covariance matrix summarizes the relationships between all
pairs of features in the dataset. It provides information about how each feature varies
with respect to others.
3. Eigendecomposition: PCA performs eigendecomposition on the covariance matrix to
obtain the eigenvectors and eigenvalues. Eigenvectors represent the directions of
maximum variance in the data, while eigenvalues indicate the magnitude of variance
along these directions.
4. Selection of Principal Components: PCA sorts the eigenvalues in descending order
and selects the top k eigenvectors corresponding to the largest eigenvalues, where k is
the desired number of principal components. These principal components capture most
of the variance in the data.
5. Projection: PCA projects the original data onto the selected principal components to
obtain the new feature space. Each data point is represented by its coordinates along
the principal components.
• Dimensionality Reduction: PCA reduces the number of features in the dataset while
retaining most of the important information. This can lead to simpler and more
interpretable models and faster computation.
• Visualization: PCA can be used to visualize high-dimensional data in lower
dimensions (e.g., 2D or 3D) for easier interpretation and exploration.
• Noise Reduction: PCA can help in reducing noise and redundancy in the data by
focusing on the most informative features.
• Feature Engineering: PCA can be used as a preprocessing step for feature engineering
in machine learning tasks to improve model performance.
Overall, PCA is a powerful technique for data analysis and preprocessing, particularly when
dealing with high-dimensional datasets.
Results : The PCA implementation successfully reduced the dimensionality of the Iris dataset
from four dimensions to two dimensions. The scatter plot of the data points in the new 2-
dimensional space showed a clear separation between the three species of iris flowers. The first
principal component (PC1) and the second principal component (PC2) captured a significant
amount of variance in the dataset, as evidenced by their high explained variance ratios.
PCA
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
feature_names = iris.feature_names
# Perform PCA
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X_std)
OUTPUT:
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(x):
return x * (1 - x)
class NeuralNetwork:
def __init__(self, input_size, hidden_size, output_size):
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
return self.output
# Backward pass
self.backward_pass(X, y, output)
# Example usage:
# Input data (4 features), target output (1 output)
X = np.array([[0, 0, 1, 1],
[0, 1, 1, 0],
[1, 0, 1, 0],
[1, 1, 1, 1]])
y = np.array([[0, 1, 1, 0]]).T # Target output
OUTPUT:
Epoch 1/100, Mean Squared Error: 0.3909198935257375
Epoch 2/100, Mean Squared Error: 0.36761319023216843
Epoch 3/100, Mean Squared Error: 0.3407214073553024
Epoch 4/100, Mean Squared Error: 0.3128286116839329
Epoch 5/100, Mean Squared Error: 0.2881041414280933
Epoch 6/100, Mean Squared Error: 0.27002608479593215
Epoch 7/100, Mean Squared Error: 0.25907428765598484
Epoch 8/100, Mean Squared Error: 0.2533005830016185
Epoch 9/100, Mean Squared Error: 0.25045070634549754
Epoch 10/100, Mean Squared Error: 0.2490243571166713
Epoch 11/100, Mean Squared Error: 0.24823559789439453
Epoch 12/100, Mean Squared Error: 0.24771525949253997
Epoch 13/100, Mean Squared Error: 0.24729744852157265
Epoch 14/100, Mean Squared Error: 0.2469081741342835
Epoch 15/100, Mean Squared Error: 0.24651359625068736
Epoch 16/100, Mean Squared Error: 0.24609708181511797
Epoch 17/100, Mean Squared Error: 0.24564922657424218
Epoch 18/100, Mean Squared Error: 0.24516354434357218
Epoch 19/100, Mean Squared Error: 0.24463460268932458
Epoch 20/100, Mean Squared Error: 0.2440572086387934
Epoch 21/100, Mean Squared Error: 0.24342604391988595
Epoch 22/100, Mean Squared Error: 0.24273549260704744
Epoch 23/100, Mean Squared Error: 0.24197955150030104
Epoch 24/100, Mean Squared Error: 0.2411517767755278
Epoch 25/100, Mean Squared Error: 0.2402452475666762
Epoch 26/100, Mean Squared Error: 0.2392525388390253
Epoch 27/100, Mean Squared Error: 0.2381657010349174
Epoch 28/100, Mean Squared Error: 0.2369762463018299
Epoch 29/100, Mean Squared Error: 0.23567514226139646
Epoch 30/100, Mean Squared Error: 0.23425281493398856
Epoch 31/100, Mean Squared Error: 0.23269916288778134
Epoch 32/100, Mean Squared Error: 0.23100358505675164
Epoch 33/100, Mean Squared Error: 0.2291550250220682
Epoch 34/100, Mean Squared Error: 0.2271420349034005
Epoch 35/100, Mean Squared Error: 0.22495286238338627
Epoch 36/100, Mean Squared Error: 0.22257556481787466
Epoch 37/100, Mean Squared Error: 0.21999815490373723
Epoch 38/100, Mean Squared Error: 0.21720878302685684
Epoch 39/100, Mean Squared Error: 0.21419596222428522
Epoch 40/100, Mean Squared Error: 0.21094884264329808