Pattern - Recognigation - Lab 3 Sept 23 - Practical File
Pattern - Recognigation - Lab 3 Sept 23 - Practical File
Lab Manual
CS-455
Pattern Recognition Lab
NAME :
Session 2024-20245
INDEX
S No. Program
The continuously increasing volume of data created makes human interpretation impractical, boosting the need
for machines to be able to spot patterns quickly and effectively. The capacity to automatically recognize
patterns and regularities in data has many uses, ranging from facial recognition software to tumor diagnosis.
Pattern recognition is one of the most popular applications of machine learning. There are patterns everywhere.
It permeates all aspects of our everyday existence. Everything, from the style and color of our clothing to the
use of clever voice assistants, includes a pattern.
A pattern in literature or film would be a description of a genre. If a user consistently chooses to watch dark
comedy on Netflix, the streaming service is unlikely to suggest any sad melodramas.
In the context of machine learning, “pattern recognition” refers to the use of complex algorithms to identify
patterns in the input data. Computer vision, voice recognition, face identification, etc., are only a few of the
many contemporary technical applications of pattern recognition.
How is Pattern Recognition Implemented?
The notion of learning is used to produce pattern recognition. Learning allows the system to be taught and
become more adaptive, resulting in more accurate outcomes. A portion of the dataset is used to train the
system, while the remainder is used to test it.
Source: geeksforgeeks
The training set comprises pictures or data that will be used to train or develop the model. Training rules
provide the criterion for output decisions.
Training algorithms are used to match a set of input data with an output choice. The algorithms and rules are
then used to help in training. The system generates results based on the information gathered from the data.
The testing set is used to validate the system’s accuracy. After the system has been trained, the testing data is
used to determine whether the accurate output is obtained. This data accounts for around 20% of the total data
in the system.
The Pattern Recognition method is divided into five stages. These stages can be described as follows:
• Phase 1 Sensing: The system turns the incoming data into similar data during this step.
• Phase 2 Segmentation: The perceived objects are isolated during this step.
• Phase 3 Feature extraction: In this step, the features or qualities of the objects are computed and sent for
further classification.
• Phase 4 Classification: The detected objects are classified or arranged in groups or cases at this step.
• Phase 5 Post-processing: Here, further considerations are done before concluding.
Different Types of Pattern Recognition
Image:
This form of pattern recognition identifies certain things portrayed in photographs. Image recognition is a
fundamental element of computer vision, which is a machine’s capacity to recognize pictures and execute
appropriate actions (e.g., a self-driving car slowing down after identifying a pedestrian ahead).
Image recognition is often used in operations like Face detection, visual search, and OCR (optical character
recognition).
Sound:
This pattern recognition approach is used to detect distinct sounds. After evaluating them, the system identifies
audio signals as belonging to a certain category. Here are some applications for sound pattern recognition:
This kind of pattern recognition examines human speech sounds to identify the speaker. Unlike voice
recognition, it does not include language processing and only detects personal features in a speaking pattern. It
is usually used for security purposes (personal identification).
Common applications involve- the Internet of things and mobile or web apps.
Speech: Speech recognition catches aspects of a language in the sound of a person speaking, similar to how
optical character recognition detects letters and words on an image. Popular applications for this technology
include- Voice-to-text converters, video auto-captioning, and virtual assistants.
• Overfitting: occurs when a model is trained too well on the training data, resulting in poor generalization to
new, unseen data. This can be mitigated by using techniques like regularization and cross-validation.
• Underfitting: Underfitting occurs when a model is not complex enough to capture the underlying patterns
in the data. This can be mitigated by using more complex models or by increasing the amount of training
data.
• The Curse of Dimensionality: As the number of features in the data increases, the amount of data required
to model the patterns in the data accurately increases too. This can be mitigated by using dimensionality
reduction techniques.
• Data Bias: If the training data is not representative of the population, the model will not be able to generalize
well to new, unseen data. This can be mitigated by using techniques like oversampling or SMOTE.
• Noise and Outliers: Noise and outliers in the data can make it difficult for the model to identify the
underlying patterns. This can be mitigated by using robust estimation techniques or removing outliers from
the data.
• Nonlinearity: Some patterns in the data may not be linear, making it difficult for linear models to capture
the underlying patterns accurately. This can be mitigated using non-linear models like neural networks or
decision trees.
• Lack of Interpretability: Some models, like neural networks, can be difficult to interpret and understand,
making it hard to understand why a particular decision was made.
Experiment No: 1
AIM : Write a program to calculate distance between two places .
Explanation:
To calculate distance between two landmark/place we have to use python library geopy. geopy makes it
easy for Python developers to locate the coordinates of addresses, cities, countries, and landmarks across
the globe. Geocoding is the process of transforming a description of a location—such as a pair of
coordinates, an address, or a name of a place—to a location on the earth's surface.
From geopy we import Nominatim and distance to locate and calculate the distance of two places.
Nominatim uses OpenStreetMap data to find locations on Earth by name and address (geocoding). It can
also do the reverse, find an address for any location on the planet. Now with the help of distance function
we calculate the distance between the places.
CODE:
a=Nominatim(user_agent="Surya")
OUTPUT
Explanation:
Iris flower has three species; setosa, versicolor, and virginica, which differs according to their
measurements.
Now assume that you have the measurements of the iris flowers according to their species, and here
your task
is to train a machine learning model that can learn from the measurements of the iris species and
classify them.
Data Sample
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 Iris-setosa
4.9 3 1.4 0.2 Iris-setosa
4.7 3.2 1.3 0.2 Iris-setosa
4.6 3.1 1.5 0.2 Iris-setosa
5 3.6 1.4 0.2 Iris-setosa
5.4 3.9 1.7 0.4 Iris-setosa
4.6 3.4 1.4 0.3 Iris-setosa
5 3.4 1.5 0.2 Iris-setosa
4.4 2.9 1.4 0.2 Iris-setosa
CODE
# Iris Flower Classification
# Import Packages
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
#The target labels of this dataset are present in the species column,have a look at
the target labels:
print("Target Labels", iris["Species"].unique())
#plot the data using a scatter plot which will plot the iris species according to the
sepal length and sepal width:
import plotly.express as px
fig = px.scatter(iris, x="Sepal.Width", y="Sepal.Length", color="Species")
fig.show()
#iris classification
#split the data into training and test sets, and use the KNN classification algorithm
to train the iris classification model:
x = iris.drop("Species", axis=1)
y = iris["Species"]
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)
#Now input a set of measurements of the iris flower and use the model to predict
the iris species:
x_new = np.array([[5, 2.9, 1, 0.2,0.3]])
prediction = knn.predict(x_new)
print("Prediction: {}".format(prediction))
OUTPUT
Prediction: ['Iris-versicolor']
Experiment 3
Computers obtain data by reading from the standard input, files, and sensors, then store it in the memory
for processing. Data processing could be online or offline, depending on the preferences of the user.
Data processing is the conversion of raw data to meaningful information. The aim is to break down the
data and represent it to the end user graphically.
We have two schemes in which you can capture and process data. These schemes are to record sensor
data offline and record them online. At the same time, you can access the data and process it on the
local PC using Python or Matlab.
Reading data and processing it offline is easy as compared to online. This is because it needs to establish
a real-time communication link.
The image above shows the sensors that are available to android mobile phones. These
sensors could detect the position, motion and even the environment on request. This assists
when using many applications such as cameras, games, e.t.c. The user can also record them to
use in the implementation of various applications such as:
Reading data and processing it offline is easy as compared to online. This is because it needs
to establish a real-time communication link. The steps of reading mobile sensor and online
processing is as shownbelow:
The steps that are used to record and process data offline is shown below:
You can open this data in a notepad or MS excel software. This format arranges data in
columns and rows. An example of data opened in a notepad and MS excel is shown below:
The data in the first column shows the time. The second column represented by wx is the x-
axis data. The third column represented by wy is the y-axis data, and finally, wz represents the
z-axis data.
The first row in the data represents the next row. When importing this data, we should consider
this line. It should be separated from the data during importation and processing.
Let us look at the third party android App that can collect sensor data. To get this App,
The result here is more than one. Note that all these applications are good. We recommend
data collector or physics toolbox for this article. This is because their interfaces are easy to
use.
There are two categories of applications. The first category collects data and saves them as
a .csv file for offline processing, e.g. data collector. The second category collects data and
sends them to a given web for online processing.
Since we are to process our data offline, we will install the physics toolbox. Once
:
When you open physics toolbox, you get the interface shown below
You notice that when you shake your mobile phone, there is variation in the waveform as
shown below:
From the left menu, you can select available sensors on your phone.
For example, select the sensor if you want to record g-force data. You then click on
the + sign. Once you click it, there is a pop-up data is being recorded. You can do
this for the required time. Once you stop recording, a new window opens up that
allows one to save the data.
import numpy as np
import csv
# Extracting data
time = data1[:, 0]
ax = data1[:, 1]
ay = data1[:, 2]
az = data1[:, 3]
aT = data1[:, 4]
plt.figure(figsize=(20, 10))
plt.xlabel('Time', fontsize=20)
plt.ylabel('Acc.values', fontsize=20)
plt.show()
#separate plots using subplots
plt.figure(figsize=(20, 20))
plt.subplot(2, 2, 1)
plt.plot(ax, color='r')
plt.title('a[x]', fontsize=30)
plt.subplot(2, 2, 2)
plt.plot(ax, color='g')
plt.title('a[y]', fontsize=30)
plt.subplot(2, 2, 3)
plt.plot(ax, color='b')
plt.title('a[z]', fontsize=30)
plt.subplot(2, 2, 4)
plt.plot(ax, color='k')
plt.title('a[Total]', fontsize=30)
plt.show()
Output:-