23AD1401 Machine Learning Unit 5
23AD1401 Machine Learning Unit 5
In machine learning, each task or problem is divided into classification and Regression. Not all
metrics can be used for all types of problems; hence, it is important to know and understand
which metrics should be used. Different evaluation metrics are used for both Regression and
Classification tasks. In this topic, we will discuss metrics used for classification and regression
tasks.
o Accuracy
o Confusion Matrix
o Precision
o Recall
o F-Score
o AUC(Area Under the Curve)-ROC
I. Accuracy
The accuracy metric is one of the simplest Classification metrics to implement, and it can
be determined as the number of correct predictions to the total number of predictions.
To implement an accuracy metric, we can compare ground truth and predicted values in
a loop, or we can also use the scikit-learn module for this.
Then we need to pass the ground truth and predicted values in the function to calcul
ate the accuracy.
1.
2. print(f'Accuracy Score is {accuracy_score(y_test,y_hat)}')
Although it is simple to use and implement, it is suitable only for cases where an equal
number of samples belong to each class.
It is good to use the Accuracy metric when the target variable classes in data are
approximately balanced. For example, if 60% of classes in a fruit image dataset are of
Apple, 40% are Mango. In this case, if the model is asked to predict whether the image
is of Apple or Mango, it will give a prediction with 97% of accuracy.
It is recommended not to use the Accuracy measure when the target variable majorly
belongs to one class. For example, Suppose there is a model for a disease prediction in
which, out of 100 people, only five people have a disease, and 95 people don't have
one. In this case, if our model predicts every person with no disease (which means a
bad prediction), the Accuracy measure will be 95%, which is not correct.
II. Confusion Matrix
A confusion matrix is a tabular representation of prediction outcomes of any binary
classifier, which is used to describe the performance of the classification model on a set
of test data when true values are known.
The confusion matrix is simple to implement, but the terminologies used in this matrix
might be confusing for beginners.
A typical confusion matrix for a binary classifier looks like the below image(However, it
can be extended to use for classifiers with more than two classes).
o In the matrix, columns are for the prediction values, and rows specify the
Actual values. Here Actual and prediction give two possible classes, Yes or
No. So, if we are predicting the presence of a disease in a patient, the
Prediction column with Yes means, Patient has the disease, and for NO, the
Patient doesn't have the disease.
o In this example, the total number of predictions are 165, out of which 110 time
predicted yes, whereas 55 times predicted No.
o However, in reality, 60 cases in which patients don't have the disease,
whereas 105 cases in which patients have the disease.
In general, the table is divided into four terminologies, which are as follows:
1. True Positive(TP): In this case, the prediction outcome is true, and it is true in
reality, also.
2. True Negative(TN): in this case, the prediction outcome is false, and it is false in
reality, also.
3. False Positive(FP): In this case, prediction outcomes are true, but they are false
in actuality.
4. False Negative(FN): In this case, predictions are false, and they are true in
actuality.
III. Precision
The precision metric is used to overcome the limitation of Accuracy. The precision
determines the proportion of positive prediction that was actually correct. It can be
calculated as the True Positive or predictions that are actually true to the total positive
predictions (True Positive and False Positive).
From the above definitions of Precision and Recall, we can say that recall determines
the performance of a classifier with respect to a false negative, whereas precision gives
information about the performance of a classifier with respect to a false positive.
So, if we want to minimize the false negative, then, Recall should be as near to 100%,
and if we want to minimize the false positive, then precision should be close to 100% as
possible.
V. F-Scores
F-score or F1 Score is a metric to evaluate a binary classification model on the basis of
predictions that are made for the positive class. It is calculated with the help of Precision
and Recall. It is a type of single score that represents both Precision and Recall. So, the
F1 Score can be calculated as the harmonic mean of both precision and Recall,
assigning equal weight to each of them.
As F-score make use of both precision and recall, so it should be used if both of them
are important for evaluation, but one (precision or recall) is slightly more important to
consider than the other. For example, when False negatives are comparatively more
important than false positives, or vice versa.
VI. AUC-ROC
Sometimes we need to visualize the performance of the classification model on charts;
then, we can use the AUC-ROC curve. It is one of the popular and important metrics for
evaluating the performance of the classification model.
Firstly, let's understand ROC (Receiver Operating Characteristic curve) curve. ROC
represents a graph to show the performance of a classification model at different
threshold levels. The curve is plotted between two parameters, which are:
TPR or true Positive rate is a synonym for Recall, hence can be calculated as:
To calculate value at any point in a ROC curve, we can evaluate a logistic regression
model multiple times with different classification thresholds, but this would not be much
efficient. So, for this, one efficient method is used, which is known as AUC.
AUC should be used to measure how well the predictions are ranked rather than their
absolute values. Moreover, it measures the quality of predictions of the model without
considering the classification threshold.
Further, AUC is not a useful metric when there are wide disparities in the cost of false
negatives vs. false positives, and it is difficult to minimize one type of classification error.
To understand MAE, let's take an example of Linear Regression, where the model draws
a best fit line between dependent and independent variables. To measure the MAE or
error in prediction, we need to calculate the difference between actual values and
predicted values. But in order to find the absolute error for the complete dataset, we
need to find the mean absolute of the complete dataset.
Here,
Y is the Actual outcome, Y' is the predicted outcome, and N is the total number of data
points.
MAE is much more robust for the outliers. One of the limitations of MAE is that it is not
differentiable, so for this, we need to apply different optimizers such as Gradient
Descent. However, to overcome this limitation, another metric can be used, which is
Mean Squared Error or MSE.
Since in MSE, errors are squared, therefore it only assumes non-negative values, and it
is usually positive and non-zero.
Moreover, due to squared differences, it penalizes small errors also, and hence it leads
to over-estimation of how bad the model is.
Y is the Actual outcome, Y' is the predicted outcome, and N is the total number of data
points.
The R squared score will always be less than or equal to 1 without concerning if the
values are too large or small.
To overcome the issue of R square, adjusted R squared is used, which will always show
a lower value than R². It is because it adjusts the values of increasing predictors and
only shows improvement if there is a real improvement.
Here,
The Azure Machine Learning Studio (Azure ML Studio) facilitates quick model
creation and deployment, with an easy-to-use graphical user interface (GUI). The
automated Machine Learning (AutoML) feature in Azure ML speeds up the repetitive
and time-consuming process of feature and algorithm selection.
Note: There are a lot of terms and concepts, but throughout the series, it should
become clearer as I make user of these concepts to build out examples.
When the compute target used to host production deployment of an ML model for
performing inference is fully managed by Azure Machine learning, it’s called an
“inference cluster”. This currently includes “Azure Machine Learning endpoints”
and “Azure Machine Learning Kubernetes”. Both can be used for Real-time and
Batch inference.
The typical high-level design of an Azure Machine Learning Workflow involves the
following steps:
1. Acquire Data – This is typically the first step for any Machine Learning
workflow and involves making the raw data available to the Azure Machine
experiment. Azure ML offers several options to gather data including manual
data entry, Azure Blob storage, Azure SQL Database, Web URLs,
compressed files, etc.
2. Data Preparation – Azure ML offers numerous modules to prepare and
transform data. It includes tools for filtering, cleaning missing values, adding
rows and columns, changing data types, splitting the data set for training and
testing, etc.
3. Feature Engineering – Azure ML provides various methods for Feature
engineering like Filter-based feature selection, Fisher Linear Discriminant
Analysis, Permutation Feature Importance, etc.
4. Select and implement Machine Learning Algorithms – Azure ML comes
with a wide array of built-in Machine Learning Algorithms and options to tune
their parameters.
5. Train Machine Learning Model – Azure ML provides modules to quickly train
and score Machine Learning Models
6. Evaluate Machine Learning Models – Azure ML provides modules to easily
evaluate the performance of a trained model using industry-standard metrics.
In the rest of this article, we are going to start the process of building an Azure ML
example.
Azure Machine Learning workspace setup
Azure Machine Learning workspace is the central hub for all Azure ML artifacts and
the repository for storing experiments, logs, etc. This section walks you through the
steps of setting up their Azure Machine Learning workspace and upload a demo file
in their storage account.
Note: An Azure account with access to the Azure Portal is needed to perform these
steps. Please follow this link to learn more about creating your free account with
Microsoft Azure.
This first step is designed to help you understand the process of setting up your
workspace and upload data to be processed.
1. Log into the Azure Portal and click on “Create a resource” > select the “AI +
Machine Learning” category > Type “Azure Machine Learning” in the
search bar as shown in Figure 5, then select “Azure Machine Learning” from
the list of results:
This brings up the “Azure Machine Learning” product page with a product
overview and tabs for additional details about Plans, Usage Information,
Support, and product reviews. Click the ‘Create’ button to launch the machine
learning creation wizard, as shown in Figure 5.
Figure 5. Azure Machine Learning product page
1. On the “Basics” tab for the machine learning workspace creation wizard. You
can see my choices in Figure 6, but your values will likely vary some. Names
are not important if they work. Many of the names will give you a default that
will work for the example, especially if you plan to delete everything after
following along with the article.
Select your “Subscription” from the dropdown list.
Select your “Resource group” from the dropdown list (You can also opt to
create a new resource group using the “create new” option under this field)
Enter the “Workspace name” you like to create (Please follow the prompts
for naming rules)
Select “Region” for your workspace from the dropdown list.
Enter the name for your “Storage account” (or create a new one. Please
follow prompts for naming rules)
Enter the name for your “Key vault” (or create a new one. Please follow the
prompts for naming rules)
Enter the name for your “Application insights” (or create a new one. Please
follow the prompts for naming rules)
Enter the name for your “Container registry” (or create a new one. Please
follow the prompts for naming rules)
Then click the ‘Review + create’ button, which runs validation on your entries.
Figure 6. Create Machine Learning workspace
In Figure 7, after validation has passed, you will be presented with a summary
of your entries for review. Click the ‘Create’ button.
Figure 7. Review entries and create machine learning workspace
Please review this page to confirm the green check mark against all
Resources and the Status “OK”
Figure 8. Azure Machine Learning workspace deployment status
Click on the “Go to resource” button to navigate to your newly created machine
learning workspace. This section will familiarize readers with the newly created
machine-learning workspace. Figure 9 shows the screen that should follow, with the
values that match your choices.
Let’s upload a test data file into the storage account (I will use it to create a
Datastore). Navigate to the Azure Portal’s left side Main menu > Storage
accounts > mhatreredgatedemostgacct > Containers, to review the list of existing
blob storage containers.
Figure 10. Create a new Blob storage container
As shown in Figure 10, click the ‘+ Container’ button on the top left > Enter a unique
name in the Name text box for the new container > click the ‘Create’ button. Once
the new container is created and shows up under the container list, click to open it.
Figure 11. Upload text file to the Azure blob storage container
After the file upload completes, navigate back to the storage account (I will use this
file in a subsequent step to create the data asset).
Navigate the left side menu of the storage account, to “Access keys” under the
“Security +” networking section as shown in Figure 12. Access keys can be used to
authenticate your application’s requests to this storage account. Copy one of the
keys, to use in the subsequent step to create the Datastore.
Figure 12. Copy the Storage account access key
Azure ML Studio (the successor of Machine Learning Studio classic) is the pivotal
platform for most machine learning tasks in Azure. This powerful platform enables a
wide gamut of machine learning professionals with varying levels of skills and
preferences, to work with a range of no-code to extensive-coding options.
Figure 13. Launch Azure Machine Learning Studio
Click on the ‘Launch Studio’ button to open the Azure ML Studio as shown in
Figure 13 (Note: you may be prompted to log in again as you are going to a different
tool in a different window.) This opens the tool as shown in Figure 14.
Figure 14. Azure Machine Learning Studio
The left side menu consists of three key sections: Author, Assets, and Manage
The “Author” section is geared towards creators of Machine learning pipelines and
currently offers three authoring experiences.
Data Asset and Datastore are the two key components in Azure ML workspace, to
connect the actual sources of data with the machine learning authoring tools. Figure
15 shows how Data Asset encompasses a logical representation of the actual
sources of data, while the Datastore safeguards the connection details to the actual
sources of data by keeping the credentials in a separate secure location
(represented by the lock icon)
In Azure Machine Learning Studio, navigate to Data > Datastores as seen in Figure
16.
The Azure Machine Learning workspace has a default Azure Blob Storage Datastore
“woekspaceblobstorage(Default)” for temporarily saving various files generated
during the authoring process. Three other Datastores “workspacefilestore”,
“workspaceworkingdirectory” and “workspaceartifactstore” found in the Storage
Account are set up as part of creating the workspace. Click on each one of them to
reveal details such as storage account name, Storage URI and creation date.
To create a new datastore and link it to the blob storage container created
previously, click on the ‘+ Create’ button as shown in Figure 17.
Figure 17. Create Azure Machine Learning Datastore
Once the new datastore is created, it shows up under the list of datastores. Click to
open the “Overview” page > navigate to the “Browse preview” section to confirm
the “datastore_test.txt” file is listed as shown in Figure 18.
Figure 18. Datastore with test file from a blob container
To create the data asset from this datastore, click the ‘create data asset’ button and
in the “Data type” section.
enter a name for the new Data asset into the “Name” field.
enter a description for the Data asset into the “Description” field.
select “File (uri_file)” from the drop-down list in the “Type” field.
Click ‘Next’ to navigate to the “Storage path” section as seen in Figure 19.
Figure 19. Create data asset
After setting the values, click Next to choose the storage path.
Click Next to navigate to the “Review” section > Review the data asset settings >
Click the ‘Create’ button.
This creates the data asset for the test_dataasset.txt file and makes it available for
the machine learning authoring processes and tools in Azure Machine Learning.
5.3 Image Recognition
Image recognition is one of the most common applications of machine learning. It is used to
identify objects, persons, places, digital images, etc. The popular use case of image
recognition and face detection is, Automatic friend tagging suggestion:
It is based on the Facebook project named "Deep Face," which is responsible for face
recognition and person identification in the picture.
Image recognition is a mechanism used to identify an object within an image and to classify it
in a specific category, based on the way human people recognize objects within different sets
of images.
Machines only recognize categories of objects that we have programmed into them. They are
not naturally able to know and identify everything that they see. If a machine is programmed
to recognize one category of images, it will not be able to recognize anything else outside of
the program. The machine will only be able to specify whether the objects present in a set of
images correspond to the category or not. Whether the machine will try to fit the object in the
category, or it will ignore it completely.
For a machine, an image is only composed of data, an array of pixel values. Each pixel
contains information about red, green, and blue color values (from 0 to 255 for each of them).
For black and white images, the pixel will have information about darkness and whiteness
values (from 0 to 255 for both of them).
Machines don’t have a look at the whole image; they are only interested in pixel values and
patterns in these values. They simply take pixel patterns of an item and compare them with
other patterns. If two patterns are close enough, the machine will associate them and
recognize the second pattern as something it has already encountered in the past. In that
sense, what is happening is the machine will look for groups of similar pixel values across
images and will try to place them in specific image categories.
It is very rare that a program recognizes an image at 100%. Pixel patterns are very rarely
100% the same when comparing them. Solving these problems and finding improvements is
the job of IT researchers, the goal being to propose the best experience possible to users.
The goal of image recognition is to identify, label and classify objects which are detected into
different categories. Object or image recognition is a whole process that involves various
traditional computer vision tasks:
Object Detection: determining the presence of objects with the help of bounding boxes and
categorizing it within the class it belongs
Object Segmentation: distinguishing the various elements. Identify and locate each and every
item of the picture. Segmentation doesn’t use bounding boxes but highlights the contour of
the object in the image.
Set up, Training and Testing
First of all, the machine has to know exactly what it has to look for. Thus, it is necessary to
give it the parameters you decide to work on. Defining the dimensions of bounding boxes and
what elements are inside is crucial. To do so, the machine has to be provided with some
references, which can be pictures, videos or photographs, etc. These elements will allow it to
be more efficient when analyzing future data. This will create a sort of data library that will
then be used by the Neural Network to distinguish the various objects. A Neural Network is
composed of multiple artificial neurons. These neurons are meant to imitate the human brain.
It works with a set of various algorithms also inspired by the way the brain functions. If we
want the image recognition model to analyze and categorize different races of dogs, the
model will need to have a database of the various races in order to recognize them.
Second, the model needs to go on a training phase. The dataset needs to be entered within a
program in order to function properly. And this phase is only meant to train the
Convolutional Neural Network (CNN) to identify specific objects and organize them
accurately in the correspondent classes.
PROGRAM
Rudimentary speech recognition software has a limited vocabulary and may only identify
words and phrases when spoken clearly. More sophisticated software can handle natural
speech, different accents and various languages.
Speech recognition uses a broad array of research in computer science, linguistics and
computer engineering. Many modern devices and text-focused programs have speech
recognition functions in them to allow for easier or hands-free use of a device.
Speech recognition and voice recognition are two different technologies and should not be
confused:
Speech recognition systems use computer algorithms to process and interpret spoken words
and convert them into text. A software program turns the sound a microphone records into
written language that computers and humans can understand, following these four steps:
Speech recognition software must adapt to the highly variable and context-specific nature of
human speech. The software algorithms that process and organize audio into text are trained
on different speech patterns, speaking styles, languages, dialects, accents and phrasings. The
software also separates spoken audio from background noise that often accompanies the
signal.
To meet these requirements, speech recognition systems use two types of models:
Acoustic models. These represent the relationship between linguistic units of speech
and audio signals.
Language models. Here, sounds are matched with word sequences to distinguish
between words that sound similar.
Speech recognition systems have quite a few applications. Here is a sampling of them.
Mobile devices. Smartphones use voice commands for call routing, speech-to-text
processing, voice dialing and voice search. Users can respond to a text without looking at
their devices. On Apple iPhones, speech recognition powers the keyboard and Siri, the virtual
assistant. Functionality is available in secondary languages, too. Speech recognition can also
be found in word processing applications like Microsoft Word, where users can dictate words
to be turned into text.
Education. Speech recognition software is used in language instruction. The software hears
the user's speech and offers help with pronunciation.
Customer service. Automated voice assistants listen to customer queries and provides
helpful resources.
Healthcare applications. Doctors can use speech recognition software to transcribe notes in
real time into healthcare records.
Disability assistance. Speech recognition software can translate spoken words into text
using closed captions to enable a person with hearing loss to understand what others are
saying. Speech recognition can also enable those with limited use of their hands to work with
computers, using voice commands instead of typing.
Court reporting. Software can be used to transcribe courtroom proceedings, precluding the
need for human transcribers.
Emotion recognition. This technology can analyze certain vocal characteristics to determine
what emotion the speaker is feeling. Paired with sentiment analysis, this can reveal how
someone feels about a product or service.
The power behind speech recognition features comes from a set of algorithms and
technologies. They include the following:
N-grams: This is the simplest type of language model (LM), which assigns
probabilities to sentences or phrases. An N-gram is sequence of N-words. For
example, “order the pizza” is a trigram or 3-gram and “please order the pizza” is a 4-
gram. Grammar and the probability of certain word sequences are used to improve
recognition and accuracy.
Neural networks: Primarily leveraged for deep learning algorithms, neural networks
process training data by mimicking the interconnectivity of the human brain through
layers of nodes. Each node is made up of inputs, weights, a bias (or threshold) and an
output. If that output value exceeds a given threshold, it “fires” or activates the node,
passing data to the next layer in the network. Neural networks learn this mapping
function through supervised learning, adjusting based on the loss function through the
process of gradient descent. While neural networks tend to be more accurate and can
accept more data, this comes at a performance efficiency cost as they tend to be
slower to train compared to traditional language models.
There are several advantages to using speech recognition software, including the following:
While convenient, speech recognition technology still has a few issues to work through.
Limitations include:
Inconsistent performance. The systems may be unable to capture words accurately
because of variations in pronunciation, lack of support for some languages and inability
to sort through background noise. Ambient noise can be especially challenging. Acoustic
training can help filter it out, but these programs aren't perfect. Sometimes it's impossible
to isolate the human voice.
Speed. Some speech recognition programs take time to deploy and master. The
speech processing may feel relatively slow.
Source file issues. Speech recognition success depends on the recording equipment
used, not just the software.
PROGRAM
import speech_recognition as sr
import pyttsx3
r=[Link]()
def speakText(command):
engine=pyttsx3,init()
[Link](command)
[Link]()
r.adjust_for_ambient_noise(source2,duiration=0.2)
audio2=[Link](source2)
MyText=[Link](source2)
MyText=[Link]()
speakText(MyText)
Spam email detection: Classifying emails as genuine or spam. This is one of the crucial
components of most email application and services these days. If an application can
successfully separate the spams emails from genuine emails, it can quickly become
application of choice by millions of people. The spam can further be divided into categories
like deal alerts, newsletters, fishing emails, etc. making it a multiclass problem.
o Content Filter
o Header filter
o General blacklists filter
o Rules-based filters
o Permission filters
Some machine learning algorithms such as Multi-Layer Perceptron, Decision tree, and Naïve
Bayes classifier are used for email spam filtering and malware detection.
PROGRAM:
# Importing Libraries
import numpy as np
import pandas as pd
import [Link] as plt
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import GridSearchCV
from sklearn import svm
# Loading Dataset
data = pd.read_csv(Specific location Ex:
'[Link]
# Checking the information of the dataset
[Link]()
# Checking the information of the dataset
X = data['EmailText'].values
y = data['Label'].values
# Splitting our data into training and testing
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=0)
# Converting text into integer using CountVectorizer()
# Converting String to Integer
cv = CountVectorizer()
X_train = cv.fit_transform(X_train)
X_test = [Link](X_test)
# SVM algorithm
from [Link] import SVC
classifier = SVC(kernel = 'rbf', random_state = 0)
[Link](X_train, y_train)
# Accuracy
print([Link](X_test,y_test))
5.4 Online fraud detection
Machine learning is making our online transaction safe and secure by detecting fraud
transaction. Whenever we perform some online transaction, there may be various ways that a
fraudulent transaction can take place such as fake accounts, fake ids, and steal money in the
middle of a transaction. So to detect this, Feed Forward Neural network helps us by checking
whether it is a genuine transaction or a fraud transaction.
For each genuine transaction, the output is converted into some hash values, and these values
become the input for the next round. For each genuine transaction, there is a specific pattern
which gets change for the fraud transaction hence, it detects it and makes our online
transactions more secure.
In medical science, machine learning is used for diseases diagnoses. With this, medical
technology is growing very fast and able to build 3D models that can predict the exact
position of lesions in the brain. It helps in finding brain tumors and other brain-related
diseases easily.
There are different types of sensing technologies available in medical diagnosis, like
ultrasound, X-ray, magnetic resonance imaging, etc. This diagnostic sensory data can then be
given to a machine learning system which can then analyze the signals and classify the
medical conditions into different predetermined types.
PROGRAM:
import numpy as np
import pandas as pd
import csv
from [Link] import MaximumLikelihoodEstimator
from [Link] import BayesianNetwork
from [Link] import VariableElimination
heartDisease = pd.read_csv('/content/[Link]')
heartDisease = [Link]('?',[Link])
print('Sample instances from the dataset are given below')
print([Link]())
model= BayesianNetwork([('age','heartdisease'),('gender','heartdisease'),
('exang','heartdisease'),('cp','heartdisease'),('heartdisease','restecg'),('heartdisease','chol')])
print('\nLearning CPD using Maximum likelihood estimators')
[Link](heartDisease, estimator=MaximumLikelihoodEstimator)
print('\n Inferencing with Bayesian Network:')
HeartDiseasetest_infer = VariableElimination(model)
print('\n 1. Probability of HeartDisease given evidence= restecg')
q1=HeartDiseasetest_infer.query(variables=['heartdisease'],evidence={'restecg':1})
print(q1)
print('\n 2. Probability of HeartDisease given evidence= cp')
q2=HeartDiseasetest_infer.query(variables=['heartdisease'],evidence={'cp':2})
print(q2)