0% found this document useful (0 votes)
30 views

ML Lab Manual

Uploaded by

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

ML Lab Manual

Uploaded by

Keshav Bagaade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 36

AURORA’STECHNOLOGICAL AND RESEARCH

INSTITUTE
(ApprovedbyAICTEandAffiliatedtoJNTUH) (AccreditedbyNAACwith‘A’Grade)
Parvathapur,Uppal,Medipally(M),Medchal(D),Telangana,Hyderabad-500098

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


Academic Year:2023-2024 Semester: III YEAR - II Sem

MACHINE LEARNING LAB MANUAL


MACHINE LEARNING LAB MANUAL
B.Tech. III Year II Sem.

Course Objective: The objective of this lab is to get an overview of the various machine
learning techniques and can able to demonstrate them using python.

Course Outcomes: After the completion of the course the student can able to:

 understand complexity of Machine Learning algorithms and their limitations;


 understand modern notions in data analysis-oriented computing;
 be capable of confidently applying common Machine Learning algorithms in
practice and implementing their own;
 Be capable of performing experiments in Machine Learning using real-world data.

List of Experiments
1. The probability that it is Friday and that a student is absent is 3 %. Since there are 5
school days in a week, the probability that it is Friday is 20 %. What is
theprobability that a student is absent given that today is Friday? Apply Baye’s rule
in python to get the result. (Ans: 15%)
2. Extract the data from database using python
3. Implement k-nearest neighbours classification using python
4. Given the following data, which specify classifications for nine combinations of
VAR1 and VAR2 predict a classification for a case where VAR1=0.906 and
VAR2=0.606, using the result of k- means clustering with 3 means (i.e., 3 centroids)

VAR VAR2 CLAS


1 S
1.713 1.586 0
0.180 1.786 1
0.353 1.240 1
0.940 1.566 0
1.486 0.759 1
1.266 1.106 0
1.540 0.419 1
0.459 1.799 1
0.773 0.186 1
5. The following training examples map descriptions of individuals onto high,
medium and low credit-worthiness.

medium skiing design single twenties no- highRisk


high golf trading married forties yes - low Risk
low speedway transport married thirties yes-medRisk
medium football banking single thirties yes- lowRisk
high flying media married fifties yes - highRisk
low football security single twenties no- medRisk
medium golf media single thirties yes-medRisk
medium golf transport married forties yes-lowRisk
high skiing banking single thirties yes-highRisk
low golf unemployed married forties yes -highRisk

Input attributes are (from left to right) income, recreation, job, status, age-group, home-
owner. Find the unconditional probability of `golf' and the conditional probability of
`single' given `medRisk' in the dataset?
6. Implement linear regression using python.
7. Implement Naïve Bayes theorem to classify the English text
8. Implement an algorithm to demonstrate the significance of genetic algorithm
9. Implement the finite words classification system using Back-propagation algorithm
EXPERIMENT : 1

The probability that it is Friday and that a student is absent is 3%. Since there are 5 school
days in a week, the probability that it is Friday is 20%. What is theprobability that a
student is absent given that today is Friday? Apply Baye’s rule in python to get the result.
(Ans: 15%)

Aim : The probability that it is Friday and that a student is absent is 3 %. Since there are 5 school
days in a week, the probability that it is Friday is 20 %. What is the probability that a student is
absent given that today is Friday? Apply Baye’s rule in python to get the result.

Explanation:

F : Friday
A : Absent

Based on the given problem statement,

The probability that it is Friday and that a student is absent is 3%


i.e
P(A ∩ F)= 3% = 3 / 100 = 0.03

and

The probability that it is Friday is 20%


i.e

P(F)=20% = 20/100 = 0.2

Then,

P(A ∣ F)
The probability that a student is absent given that today is Friday

By the definition of Baye's rule( conditional probability ), we have


P(A ∣ F) = P(A ∩ F) / P(F)

PROGRAM :

# The probability that it is Friday and that a student is absent is 3%


pAF=0.03
print("The probability that it is Friday and that a student is absent :",pAF)
# The probability that it is Friday is 20%
pF=0.2
print("The probability that it is Friday : ",pF)
# The probability that a student is absent given that today is Friday
pResult=(pAF/pF)
# Display the Result
print("The probability that a student is absent given that today is Friday : ",pResult * 100,"%")

OUTPUT:

The probability that it is Friday and that a student is absent :

The probability that it is Friday :

The probability that a student is absent given that today is Friday :


EXPERIMENT NO: 2

Extract the data from database using python


Aim: Extract the data from database using python

Explanation:

===> First You need to Create a Table (students) in Mysql Database (SampleDB)
---> Open Command prompt and then execute the following command to enter into MySQL
prompt.
--> mysql -u root -p
And then, you need to execute the following commands at MySQL prompt to create table in the
database.
--> create database SampleDB;
--> use SampleDB;
--> CREATE TABLE students (sid VARCHAR(10),sname VARCHAR(10),age int);
--> INSERT INTO students VALUES('s521','Jhon Bob',23);
--> INSERT INTO students VALUES('s522','Dilly',22);
--> INSERT INTO students VALUES('s523','Kenney',25);
--> INSERT INTO students VALUES('s524','Herny',26);
===> Next,Open Command propmt and then execute the following command to install
mysql.connector
package to connect with mysql database through python.

--> pip install mysql.connector (Windows)


--> sudo apt-get install mysql.connector (linux)

PROGRAM:

import mysql.connector
# Create the connection object
myconn = mysql.connector.connect(host = "localhost", user = "root",passwd =
"",database="SampleDB")
# Creating the cursor object
cur = myconn.cursor()
# Executing the query
cur.execute("select * from students")
# Fetching the rows from the cursor object
result = cur.fetchall()
print("Student Details are :")
# Printing the result
for x in result:
print(x);
# Commit the transaction
myconn.commit()
# Close the connection
myconn.close()

OUTPUT:

C:\xampp\mysql\bin>mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.
Your MariaDB connection id is 9
Commands end with; or \g.
Server version: 10.4.18-MariaDB mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> create database SampleDB;
Query OK, 1 row affected (0.046 sec)
MariaDB [(none)]> use SampleDB;
Database changed
MariaDB [SampleDB]> CREATE TABLE students (sid VARCHAR(10), sname VARCHAR(10),age int); Query
OK, & rows affected (0.285 sec)
MariaDB [SampleDB]> INSERT INTO students VALUES ('s521', 'Jhon Bob', 23);
Query OK, 1 row affected (0.066 sec)
MariaDB [SampleDB]> INSERT INTO students VALUES ('s522', 'Dilly',22); Query OK, 1 row affected (0.061
sec)
MariaDB [SampleDB]> INSERT INTO students VALUES ('s523', 'Kenney',25); Query OK, 1 row affected
(0.029 sec)
MariaDB [SampleDB]> INSERT INTO students VALUES ('s524', 'Herny',26); Query OK, 1 row affected (0.040
sec)
MariaDB [SampleDB]>

D:\Machine Learning\Lab>python Week2.py


Student Details are :
('s521', 'Jhon Bob', 23)
('s522', 'Dilly', 22)
('s523', 'Kenney', 25) ('s524', 'Herny', 26)
EXPERIMENTNO: 3

Implement k-nearest neighbours classification using python


Aim : Implement k-nearest neighbours classification using python.

Explanation:

===> To run this program you need to install the sklearn Module
===> Open Command propmt and then execute the following command to install sklearn
Module
---> pip install scikit-learn

In this program, we are going to use iris dataset.And this dataset Split into training(70%) and test
set(30%).

The iris dataset conatins the following features

---> sepal length (cm)


---> sepal width (cm)
---> petal length (cm)
---> petal width (cm)
The Sample data in iris dataset format is [5.4 3.4 1.7 0.2]

Where 5.4 ---> sepal length (cm)


3.4 ---> sepal width (cm)
1.7 ---> petal length (cm)
0.2 ---> petal width (cm)

PROGRAM:

# Import necessary modules


from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
import random

# Loading data
data_iris = load_iris()
# To get list of target names
label_target = data_iris.target_names
print()
print("Sample Data from Iris Dataset")
print("*"*30)
# to display the sample data from the iris dataset
for i in range(10):
rn = random.randint(0,120)
print(data_iris.data[rn],"===>",label_target[data_iris.target[rn]])

# Create feature and target arrays


X = data_iris.data
y = data_iris.target

# Split into training and test set


X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size = 0.3, random_state=1)

print("The Training dataset length: ",len(X_train))


print("The Testing dataset length: ",len(X_test))
try:
nn = int(input("Enter number of neighbors :"))
knn = KNeighborsClassifier(nn)

knn.fit(X_train, y_train)
# to display the score
print("The Score is :",knn.score(X_test, y_test))
# To get test data from the user
test_data = input("Enter Test Data :").split(",")
for i in range(len(test_data)):
test_data[i] = float(test_data[i])
print()
v = knn.predict([test_data])
print("Predicted output is :",label_target[v])
except:
print("Please supply valid input......")

OUTPUT:
D:\Machine Learning\Lab>python Week3.py
Sample Data from Iris Dataset
******************************✶
[6.8 2.8 4.8 1.4] ===> versicolor [5.8 2.7 3.9 1.2] ===> versicolor
[6.6 2.9 4.6 1.3] [5.1 3.8 1.6 0.2] [4.9 2.5 4.5 1.7] [7.3 2.9 6.3 1.8] [4.9 3.1 1.5 0.1] [4.5 2.3 1.3 0.3] [7.6
3. [6.6 3.=> versicolor
> setosa ===> virginica ===> virginica setosa
setosa
6.6 2.1] ===> virginica 4.4 1.4] ===> versicolor
105
The Training dataset length: The Testing dataset length: 45 Enter number of neighbors :10
The Score is : 0.9777777777777777 Enter Test Data :6.2,2.6,3.4,0.6
Predicted output is : ['versicolor']

D:\Machine Learning\Lab>python Week3.py


Sample Data from Iris Dataset
******************************
[6.1 2.8 4.7 1.2] ===> versicolor [4.9 3.1 1.5 0.2] ===> setosa
[5.4 3.9 1.3 0.4] [4.9 2.4 3.3 1. ] [6.4 3.2 5.3 2.3] [6.3 3.3 6. 2.5] [6.5 3. 5.8 2.2] [5.6 2.7 4.2 1.3] [5.1 2.5
[4.4 3.
===> setosa
===> versicolor ===> virginica ===> virginica ===> virginica
===> ===>
versicolor versicolor
3. 1.1] 1.3 0.2] ===> setosa
The Training dataset length:
105
The Testing dataset length: 45 Enter number of neighbors The Score is :
Enter Test Data:10
0.9777777777777777 :5.0,3.3,1.4,0.3
Predicted output is : ['setosa']
EXPERIMENT NO: 4

Given the following data, which specify classifications for nine ombinationsof VAR1 and
VAR2 predict a classification for a case where VAR1=0.906and VAR2=0.606, using the
result of k-means clustering with 3 means (i.e., 3centroids)
'''Aim: Given the following data, which specify classifications for nine ombinationsof VAR1 and
VAR2 predict a classification for a case where VAR1=0.906and VAR2=0.606, using the result
of k-means clustering with 3 means (i.e., 3centroids)

Explanation:

===> To run this program you need to install the sklearn Module

===> Open Command propmt and then execute the following command to install sklearn
Module
---> pip install scikit-learn

In this program, we are going to use the following data

VAR1 VAR2 CLASS


1.713 1.586 0
0.180 1.786 1
0.353 1.240 1
0.940 1.566 0
1.486 0.759 1
1.266 1.106 0
1.540 0.419 1
0.459 1.799 1
0.773 0.186 1
And, we need apply k-means clustering with 3 means (i.e., 3 centroids)

Finally, you need to predict the class for the VAR1=0.906 and VAR2=0.606

PROGRAM:

from sklearn.cluster import KMeans


import numpy as np
X = np.array([[1.713,1.586], [0.180,1.786], [0.353,1.240],
[0.940,1.566], [1.486,0.759], [1.266,1.106],[1.540,0.419],[0.459,1.799],[0.773,0.186]])
y=np.array([0,1,1,0,1,0,1,1,1])
kmeans = KMeans(n_clusters=3, random_state=0).fit(X,y)
print("The input data is ")
print("VAR1 \t VAR2 \t CLASS")
i=0
for val in X:
print(val[0],"\t",val[1],"\t",y[i])
i+=1
print("="*20)
# To get test data from the user
print("The Test data to predict ")
test_data = []
VAR1 = float(input("Enter Value for VAR1 :"))
VAR2 = float(input("Enter Value for VAR2 :"))
test_data.append(VAR1)
test_data.append(VAR2)
print("="*20)
print("The predicted Class is : ",kmeans.predict([test_data]))

OUTPUT:

D:\Machine Learning\Lab>python Week4.py The input data is


VAR1 VAR2 CLASS
1.713 1.586 0
0.18 1.786 1
0.353 1.24 1
0.94 1.566 0
1.486 0.759 1
1.266 1.106 0
1.54 0.419 1
0.459 1.799 1
0.773 0.186 1
|====================
The Test data to predict
Enter Value for VAR1:0.906
Enter Valuefor VAR2 :0.606
|====================
The predicted Class is : [0]
EXPERIMENT NO: 5

The following training examples map descriptions of individuals onto high, medium and
low credit-worthiness.Input attributes are (from left to right) income, recreation, job,
status, age-group, home-owner. Find the unconditional probability of 'golf' and the
conditional probability of 'single' given 'medRisk' in the dataset
'''Aim:The following training examples map descriptions of individuals onto high, medium and
low credit-worthiness.

medium skiing design single twenties no -> highRisk


high golf trading married forties yes -> lowRisk
low speedway transport married thirties yes -> medRisk
medium football banking single thirties yes -> lowRisk
high flying media married fifties yes -> highRisk
low football security single twenties no -> medRisk
medium golf media single thirties yes -> medRisk
medium golf transport married forties yes -> lowRisk
high skiing banking single thirties yes -> highRisk
low golf unemployed married forties yes -> highRisk

Input attributes are (from left to right) income, recreation, job, status, age-group, home-owner.
Find the unconditional probability of 'golf' and the conditional probability of 'single' given
'medRisk' in the dataset

Explanation:

In the given data set,

----> The total number of records are 10.

----> The number of records which contains 'golf' are 4.


----> Then, the Unconditional probability of golf :

= The number of records which contains 'golf' / total number of records


= 4 / 10
= 0.4

To find the Conditional probability of single given medRisk,

---> S : single
---> MR : medRisk

---> By the definition of Baye's rule( conditional probability ), we have


P(S ∣ MR) = P(S ∩ MR) / P(MR)

Based on the given problem statement,

P(S ∩ MR) = The number of MedRisk with Single records / total number of Records
= 2 / 10 = 0.2
and

P(MR) = The number of records with MedRisk /total number of Records


= 3 / 10 = 0.3

P(S ∣ MR) = 0.2 / 0.3


Then, the Conditional probability of single given medRisk

= 0.66666

PROGRAM:

total Records=10
numGolfRecords=4
unConditionalprobGolf=numGolfRecords / total_Records
print("Unconditional probability of golf: ={}".format(unConditionalprobGolf))
#conditional probability of 'single' given 'medRisk'
numMedRiskSingle=2
numMedRisk=3
probMedRiskSingle=numMedRiskSingle/total_Records
probMedRisk=numMedRisk/total_Records
conditionalProb=(probMedRiskSingle/probMedRisk)
print("Conditional probability of single given medRisk: = {}".format(conditionalProb))

OUTPUT:

D:\Machine Learning\Lab>python Week5.py


Unconditional probability of golf: =0.4
Conditional probability of single given medRisk: = 0.6666666666666667
EXPERIMENT NO: 6

Implement naive baye's theorem to classify the English text


AIM: Implement linear regression using python

Explanation:

===> To run this program you need to install the pandas Module

---> pandas Module is used to read csv files

===> To install, Open Command propmt and then execute the following command

---> pip install pandas

And, then you need to install the sklearn Module

===> Open Command propmt and then execute the following command to install sklearn
Module

---> pip install scikit-learn

Finally, you need to create dataset called "Statements_data.csv" file.

PROGRAM:

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score, confusion_matrix, precision_score, recall_score

msglbl_data = pd.read_csv('Statements_data.csv', names=['Message', 'Label'])


print("The Total instances in the Dataset: ", msglbl_data.shape[0])
msglbl_data['labelnum'] = msglbl_data.Label.map({'pos': 1, 'neg': 0})
# place the data in X and Y Vectors
X = msglbl_data["Message"]
Y = msglbl_data.labelnum
# to split the data into train se and test set
Xtrain, Xtest, Ytrain, Ytest = train_test_split(X, Y)

count_vect = CountVectorizer()
Xtrain_dims = count_vect.fit_transform(Xtrain)
Xtest_dims = count_vect.transform(Xtest)
df = pd.DataFrame(Xtrain_dims.toarray(),columns=count_vect.get_feature_names_out())
clf = MultinomialNB()
# to fit the train data into model
clf.fit(Xtrain_dims, Ytrain)
# to predict the test data
prediction = clf.predict(Xtest_dims)
print('******** Accuracy Metrics *********')
print('Accuracy : ', accuracy_score(Ytest, prediction))
print('Recall : ', recall_score(Ytest, prediction))
print('Precision : ',precision_score(Ytest, prediction))
print('Confusion Matrix : \n', confusion_matrix(Ytest, prediction))
print(10*"-")
# to predict the input statement
test_stmt = [input("Enter any statement to predict :")]
test_dims = count_vect.transform(test_stmt)
pred = clf.predict(test_dims)
for stmt,lbl in zip(test_stmt,pred):
if lbl == 1:
print("Statement is Positive")
else:
print("Statement is Negative")

Statements_data.csv

This is very good place,pos


I like this biryani,pos
I feel very happy,pos
This is my best work,pos
I do not like this restaurant,neg
I am tired of this stuff,neg
I can't deal with this,neg
What an idea it is,pos
My place is horrible,neg
This is an awesome place,pos
I do not like the taste of this juice,neg
I love to sing,pos
I am sick and tired,neg
I love to dance,pos
What a great holiday,pos
That is a bad locality to stay,neg
We will have good fun tomorrow,pos
I hate this food,neg
OUTPUT:

D:\Machine Learning\Lab>python Week7.py The Total instances in the Dataset: 18


******** Accuracy Metrics **** *** Accuracy: 0.6 Recall : 1.0
Precision : 0.6
Confusion Matrix :
[[02] [03]]
Enter any statement to predict :I hate juice Statement is Negative

D:\Machine Learning\Lab>python Week7.py The Total instances in the Dataset: ********


Accuracy Metrics **** Accuracy: 0.6
Recall : 1.0 Precision : 0.5
Confusion Matrix :
[[12]
[02]]
Enter any statement to predict :I love this banana Statement is Positive

D:\Machine Learning\Lab>python Week7.py The Total instances in the Dataset: ********


Accuracy Metrics **** Accuracy: 0.6
Recall : 1.0 Precision : 0.5
Confusion Matrix :
[[12]
[02]]
Enter any statement to predict :I love this banana Statement is Positive
EXPERIMENT NO: 7

Implement naive baye's theorem to classify the English text


AIM: Implement linear regression using python

Explanation:

===> To run this program you need to install the pandas Module
---> pandas Module is used to read csv files
===> To install, Open Command propmt and then execute the following command
---> pip install pandas
And, then you need to install the sklearn Module

==> Open Command propmt and then execute the following command to install sk learn Module

---> pip install scikit-learn

Finally, you need to create dataset called "Statements_data.csv" file.

PROGRAM:

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score, confusion_matrix, precision_score, recall_score

msglbl_data = pd.read_csv('Statements_data.csv', names=['Message', 'Label'])


print("The Total instances in the Dataset: ", msglbl_data.shape[0])
msglbl_data['labelnum'] = msglbl_data.Label.map({'pos': 1, 'neg': 0})
# place the data in X and Y Vectors
X = msglbl_data["Message"]
Y = msglbl_data.labelnum
# to split the data into train se and test set
Xtrain, Xtest, Ytrain, Ytest = train_test_split(X, Y)

count_vect = CountVectorizer()
Xtrain_dims = count_vect.fit_transform(Xtrain)
Xtest_dims = count_vect.transform(Xtest)
df = pd.DataFrame(Xtrain_dims.toarray(),columns=count_vect.get_feature_names_out())
clf = MultinomialNB()
# to fit the train data into model
clf.fit(Xtrain_dims, Ytrain)
# to predict the test data
prediction = clf.predict(Xtest_dims)
print('******** Accuracy Metrics *********')
print('Accuracy : ', accuracy_score(Ytest, prediction))
print('Recall : ', recall_score(Ytest, prediction))
print('Precision : ',precision_score(Ytest, prediction))
print('Confusion Matrix : \n', confusion_matrix(Ytest, prediction))
print(10*"-")
# to predict the input statement
test_stmt = [input("Enter any statement to predict :")]
test_dims = count_vect.transform(test_stmt)
pred = clf.predict(test_dims)
for stmt,lbl in zip(test_stmt,pred):
if lbl == 1:
print("Statement is Positive")
else:
print("Statement is Negative")

Statements_data.csv

This is very good place,pos


I like this biryani,pos
I feel very happy,pos
This is my best work,pos
I do not like this restaurant,neg
I am tired of this stuff,neg
I can't deal with this,neg
What an idea it is,pos
My place is horrible,neg
This is an awesome place,pos
I do not like the taste of this juice,neg
I love to sing,pos
I am sick and tired,neg
I love to dance,pos
What a great holiday,pos
That is a bad locality to stay,neg
We will have good fun tomorrow,pos
I hate this food,neg
OUTPUT:

D:\Machine Learning\Lab>python Week7.py The Total instances in the Dataset: ******** Accuracy
Metrics *********
Accuracy: 0.6
Recall : 1.0
Precision
: 0.6
Confusion Matrix :
[[0 2]
[03]]
Enter any statement to predict :I hate juice Statement is Negative

D:\Machine Learning\Lab>python Week7.py The Total instances in the Dataset: 18 ********


Accuracy Metrics ****
Accuracy: 0.6
Recall : 1.0
Precision
: 0.5
Confusion Matrix :
[[12]
[02]]
Enter any statement to predict :I love this banana Statement is Positive

D:\Machine Learning\Lab>python Week7.py The Total instances in the Dataset: 18 ********


Accuracy Metrics ********* Accuracy: 0.8
Recall : 1.0 Precision: 0.75
Confusion Matrix :
[[11] [e 3]]
Enter any statement to predict :I do not like this place
Statement is Negative
EXPERIMENT NO: 8

IMPLEMENT AN ALGORITHM TO DEMONSTRATE THE SIGNIFICANCE


OF GENETIC ALGORITHM

AIM: Implement An Algorithm To Demonstrate The Significance Of Genetic Algorithm

DESCRIPTION:

Genetic Algorithms (GAs) are adaptive heuristic search algorithms that


belong to the larger part of evolutionary algorithms. Genetic algorithms are based
on the ideas of natural selection and genetics. These are intelligent exploitation of
random search provided with historicaldata to direct the search into the region of
better performance in solution space. They are commonly used to generate high-
quality solutions for optimization problems and search problems.Genetic
algorithms simulate the process of natural selection which means those specieswho
canadapt tochanges intheir environment areableto surviveandreproduceandgo to
next generation. In simple words, they simulate “survival of the fittest” among
individual of consecutive generation for solving a problem. Each generation consist
of a population of individuals and each individual represents a point in search
space and possible solution. Each individual is represented as a string of
character/integer/float/bits. This stringis analogous to the Chromosome.Genetic
algorithms are based on an analogy with genetic structure and behavior of
chromosome of the population. Following is the foundation of GAs based on this
analogy –

 Individual in population compete for resources and mate


 Those individuals whoare successful (fittest) then mate tocreate more
offspring than others
 Genesfrom“fittest”parent propagatethroughoutthegeneration,that
issometimes parents create offspring which is better than either
parent.
 Thuseachsuccessivegeneration ismoresuitedfortheir environment.

The population of individuals are maintained within search space. Each


individualrepresent a solution in search space for given problem. Each
individual is coded as a finite length
vector(analogoustochromosome)ofcomponents.Thesevariablecomponentsare
analogousto Genes.Thusachromosome(individual) iscomposedofseveralgenes(variable components)

A Fitness Score is given to each individual which shows the ability ofanindividual to “compete”.
The individual having optimal fitness score (or near optimal) are sought.The GAs maintains the population of n
individuals (chromosome/solutions) along with their fitness scores.The individuals having better fitness scores are
given more chance to reproduce than others. The individuals with better fitness scores are selected who mate and
produce better offspring by combining chromosomes of parents. The population size is static so the room has to
be created for new arrivals. So, someindividuals die and get replacedby new arrivals eventually creating new
generation when all the mating opportunity of the old population is exhausted. It is hoped that over successive
generations better solutions willarrive while least fit die.Each new generation has on average more “better genes”
than the individual (solution)of previousgenerations.Thuseachnewgenerationshavebetter“partial solutions” than
previous generations. Once the offsprings produced having no significant difference than offspring produced by
previous populations, the population is converged. The algorithm is said to be converged to a set ofsolutions for
the problem.

OPERATORSOFGENETICALGORITHM:
Once the initialgenerationiscreated,thealgorithmevolvethegenerationusing following operators –
1) SelectionOperator: The idea istogivepreferencetothe individualswithgoodfitness
scores and allow themto pass there genes to the successive generations.
2) Crossover Operator: This represents mating between individuals. Two individuals are
selected using selection operator and crossover sites are chosen randomly. Then the genes at
thesecrossoversitesareexchangedthuscreatingacompletelynew individual(offspring).For
example –
MutationOperator:Thekeyidea istoinsert randomgenesinoffspringtomaintainthe diversity in population to avoid
the premature convergence. For example –

ALGORITHM:

1) Randomlyinitializepopulationsp
2) Determinefitnessofpopulation
3) Untillconvergencerepeat:
a) Selectparentsfrompopulation
b) Crossoverandgeneratenewpopulation
c) Performmutationonnewpopulation
d) Calculatefitnessfornewpopulation
USESOFGENETIC ALGORITHM:

 TheyareRobust

 Provideoptimisationoverlargespacestate.

 UnliketraditionalAI,theydonotbreakonslightchangeininputorpresenceofnoise
PROGRAM:
importnumpy
defcal_pop_fitness(equation_inputs,pop):

#Calculatingthefitnessvalueofeachsolutioninthecurrentpopulation.

#Thefitness functioncalulatesthesumofproductsbetweeneachinput and its corresponding weight.

fitness=numpy.sum(pop*equation_inputs, axis=1)

return fitness

defselect_mating_pool(pop,fitness,num_parents):

#Selectingthebest individuals inthecurrent generationasparentsforproducingthe offspring of the next


generation.

parents=numpy.empty((num_parents,

pop.shape[1])) for parent_num in

range(num_parents):

max_fitness_idx=numpy.where(fitness==

numpy.max(fitness)) max_fitness_idx =

max_fitness_idx[0][0]parents[parent_num, :] =

pop[max_fitness_idx, :] fitness[max_fitness_idx] =

-99999999999

returnparents
def crossover(parents, offspring_size):

offspring = numpy.empty(offspring_size
#Thepoint at whichcrossovertakesplacebetweentwo parents.Usually, it isatthecenter. crossover_point =

numpy.uint8(offspring_size[1]/2)

for kinrange(offspring_size[0]):

#Indexofthefirst parentto mate.

parent1_idx = k%parents.shape[0]

# Index of the second parent to mate.

parent2_idx=(k+1)%parents.shape[0]

#Thenewoffspringwillhave itsfirst halfofitsgenestakenfromthe first parent. offspring[k,

0:crossover_point] = parents[parent1_idx, 0:crossover_point]

#Thenewoffspringwillhave itssecondhalfofits genestakenfromthesecondparent. offspring[k, crossover_point:] =

parents[parent2_idx, crossover_point:]

returnoffspring

defmutation(offspring_crossover,num_mutations=1):

mutations_counter=numpy.uint8(offspring_crossover.shape[1]/num_mutations)

#Mutationchangesanumberofgenesasdefined bythe num_mutations argument. The changes


are random.

foridx in range(offspring_crossover.shape[0]): gene_idx =

mutations_counter - 1

formutation_numinrange(num_mutations): # The

randomvaluetobeaddedtothegene.
random_value=numpy.random.uniform(-1.0,1.0, 1)

offspring_crossover[idx, gene_idx] = offspring_crossover[idx, gene_idx] +


random_value

gene_idx = gene_idx +

mutations_counterreturn

offspring_crossover

importnumpy

"""

The y=target is to maximize this equation

ASAP: y =

w1x1+w2x2+w3x3+w4x4+w5x5+6wx6

where(x1,x2,x3,x4,x5,x6)=(4,-2,3.5,5,- 11,-4.7)

Whatarethebestvaluesforthe6weightsw1tow6?

Wearegoingto usethegeneticalgorithmforthebest possiblevaluesaftera number of generations.

"""

#Inputsoftheequation. equation_inputs =

[4,-

2,3.5,5,-11,-4.7]

# Number of the weights we are looking to

optimize.num_weights=len(equation_inputs)
"""

Genetic algorithm

parameters:Mating pool

size Population size

"""

sol_per_pop=8

num_parents_mating= 4
#Definingthepopulationsize.

pop_size=(sol_per_pop,num_weights)#Thepopulationwillhavesol_per_pop chromosome where each


chromosome has num_weights genes.

#Creatingthe initialpopulation.

new_population=numpy.random.uniform(low=-4.0,high=4.0, size=pop_size)

print(new_population)

"""

new_population[0,:]=[2.4,0.7,8,-2,5,1.1]

new_population[1,:]=[-0.4,2.7,5,-1,7,0.1]

new_population[2,:]=[-1,2,2,-3,2,0.9]

new_population[3,:]=[4, 7,12,6.1,1.4,-4]

new_population[4,:]=[3.1,4,0,2.4, 4.8,0]

new_population[5,:]=[-2,3,-

7,6,3, 3]"""
best_outputs = []

num_generations= 1000

for generation in range(num_generations):

print("Generation:",

generation)

# Measuring the fitness of each chromosome in the

population.fitness=cal_pop_fitness(equation_inputs, new_population)

print("Fitness")

print(fitness) best_outputs.append(numpy.max(numpy.sum(new_population*equation_inputs,

axis=1)))#Thebestresultinthecurrentiteration.

print("Bestresult:",numpy.max(numpy.sum(new_population*equation_inputs,axis=1)))

#Selectingthebest parentsinthepopulationfor mating.

parents = select_mating_pool(new_population, fitness,

num_parents_mating)

print("Parents ")

print(parents)

#Generatingnextgenerationusing
crossover.offspring_crossover= crossover(parents,

offspring_size=(pop_size[0]-parents.shape[0],num_weights))

print("Crossover")

print(offspring_cr

ossover)

#Addingsomevariationstotheoffspringusingmutation.

offspring_mutation = mutation(offspring_crossover,

num_mutations=2) print("Mutation") print(offspring_mutation)

#Creatingthenewpopulationbasedontheparentsandoffspring.

new_population[0:parents.shape[0], :] = parents new_population[parents.shape[0]:, :] =

offspring_mutation

#Gettingthebestsolutionafteriteratingfinishingallgenerations. #At first, the

fitness is calculated for each solution in the final generation. fitness =

cal_pop_fitness(equation_inputs, new_population)

#Thenreturnthe indexofthat solutioncorrespondingtothe best fitness.

best_match_idx = numpy.where(fitness == numpy.max(fitness))

print("Bestsolution:",
new_population[best_match_idx,:])print("Best solution

fitness : ", fitness[best_match_idx])

importmatplotlib.pyplotmat plotlib.pyplot.plot(best_outp

uts) matplotlib.pyplot.xlabel("Ite

ration")

matplotlib.pyplot.ylabel("Fit

ness") matplotlib.pyplot.show()

OUTPUT:

[[0.582041412.32880696-2.951302092.570569533.33055238-0.58167871] [-1.65052225
3.52263842-2.46577305-1.7005396-3.804802020.29677167] [ 2.6239874-2.01548549-
1.722922953.61090243-1.25604726-2.32647264][-3.451673932.857718253.74655682-
2.017906260.25750106-3.12923247][2.86026334-0.4306777-3.262979561.74863348-
1.93705571-3.18855672][-1.700120890.98685104-1.911920723.91873942-0.09354385
1.43038667] [0.31769009-0.872908093.752497852.576579930.588830822.83231871][
3.833149260.33838112-2.49509594-1.507631743.99440509-0.03037715]]

Gen

erati

on :

Fitn

ess

[-33.708344139.6777259451.30214363-4.6238336545.91897711-1.566046069.24418172-
45.41084308]
Best result:

51.302143629097614

Parents

[[2.6239874-2.01548549-1.722922953.61090243-1.25604726-2.32647264][2.86026334-
0.4306777-3.262979561.74863348-1.93705571-3.18855672][-1.650522253.52263842-
2.46577305-1.7005396-3.804802020.29677167][0.31769009-0.872908093.75249785
2.576579930.58883082 2.83231871]]

Crossover

[[2.6239874-2.01548549-1.722922951.74863348-1.93705571-3.18855672][2.86026334-
0.4306777-3.26297956-1.7005396-3.804802020.29677167] [-1.650522253.52263842-
2.46577305 2.576579930.588830822.83231871][0.31769009-0.872908093.75249785
3.61090243-1.25604726-2.32647264]]

Mutation

[[2.6239874-2.01548549-1.678966321.74863348-1.93705571-3.97789372][2.86026334-
0.4306777-3.12878279-1.7005396-3.80480202-0.15430324][-1.650522253.52263842-
3.37669601 2.576579930.588830822.25153466][0.31769009-0.872908092.93428907
3.61090243-1.25604726-2.71597954]]
.
.

Gener

ation:

999

Fitnes

s
[2554.39355622551.72360738 2549.405839542549.299316292552.24225166
2550.45506206
2547.12995122551.22467397]

Best result:

2554.3935561987346

Parents

[[3.17690088e-01-8.72908094e-012.67689952e+021.74863348e+00- 1.93705571e+00
-3.37108802e+02][3.17690088e-01 -8.72908094e-01
2.67638232e+021.74863348e+00-
1.93705571e+00-3.36689592e+02][3.17690088e-01-8.72908094e-012.67254110e+02
1.74863348e+00-1.93705571e+00-3.36865291e+02][3.17690088e-01-8.72908094e-01
2.67370854e+021.74863348e+00-1.93705571e+00-3.36672197e+02]]

Crossover

[[3.17690088e-01-8.72908094e-012.67689952e+021.74863348e+00- 1.93705571e+00
- 3.36689592e+02]

[3.17690088e-01-8.72908094e-012.67638232e+021.74863348e+00

-1.93705571e+00-3.36865291e+02]

[3.17690088e-01-8.72908094e-012.67254110e+021.74863348e+00

-1.93705571e+00-3.36672197e+02]

[3.17690088e-01-8.72908094e-012.67370854e+021.74863348e+00

-1.93705571e+00-3.37108802e+02]]

Mutation

[[3.17690088e-01-8.72908094e-01 2.68382875e+021.74863348e+00

-1.93705571e+00-3.36222272e+02]

[3.17690088e-01-8.72908094e-012.68456819e+021.74863348e+00
-1.93705571e+00-3.37417363e+02]

[3.17690088e-01-8.72908094e-012.67606746e+021.74863348e+00

-1.93705571e+00-3.36866918e+02]

[3.17690088e-01-8.72908094e-012.67051753e+021.74863348e+00

-1.93705571e+00-3.37331663e+02]]

Bestsolution:[[[3.17690088e-01-8.72908094e-012.68456819e+021.74863348e+00

-
1.9
370
557
1e+
00-
3.3
741
736
3e+
02]
]]

Bestsolutionfitness
:[2558.52782726]
EXPERIMENT NO: 9

Implement the finite words classification system using Back-propagation algorithm


AIM: Implement the finite words classification system using Back-propagation algorithm

Explanation:

===> To run this program you need to install the pandas Module

---> pandas Module is used to read csv files

===> To install, Open Command propmt and then execute the following command

---> pip install pandas

And, then you need to install the sklearn Module

==> Open Command propmt and then execute the following command to install sklearn Module

---> pip install scikit-learn

===> Open Command propmt and then execute the following command to install sklearn-
neuralnetwork Module

---> pip install scikit-neural network

Finally, you need to create dataset called "Statements_data.csv" file.

PROGRAM:

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score, confusion_matrix, precision_score, recall_score

msglbl_data = pd.read_csv('Statements_data.csv', names=['Message', 'Label'])


print("The Total instances in the Dataset: ", msglbl_data.shape[0])
msglbl_data['labelnum'] = msglbl_data.Label.map({'pos': 1, 'neg': 0})
# place the data in X and Y Vectors
X = msglbl_data["Message"]
Y = msglbl_data.labelnum
# to split the data into train se and test set
Xtrain, Xtest, Ytrain, Ytest = train_test_split(X, Y)
count_vect = CountVectorizer()
Xtrain_dims = count_vect.fit_transform(Xtrain)
Xtest_dims = count_vect.transform(Xtest)
df = pd.DataFrame(Xtrain_dims.toarray(),columns=count_vect.get_feature_names_out())
clf = MLPClassifier(solver='lbfgs', alpha=1e-5,hidden_layer_sizes=(5, 2), random_state=1)
# to fit the train data into model
clf.fit(Xtrain_dims, Ytrain)
# to predict the test data
prediction = clf.predict(Xtest_dims)
print('******** Accuracy Metrics *********')
print('Accuracy : ', accuracy_score(Ytest, prediction))
print('Recall : ', recall_score(Ytest, prediction))
print('Precision : ',precision_score(Ytest, prediction))
print('Confusion Matrix : \n', confusion_matrix(Ytest, prediction))
print(10*"-")
# to predict the input statement
test_stmt = [input("Enter any statement to predict :")]
test_dims = count_vect.transform(test_stmt)
pred = clf.predict(test_dims)
for stmt,lbl in zip(test_stmt,pred):
if lbl == 1:
print("Statement is Positive")
else:
print("Statement is Negative")

Statements_data.csv

This is very good place,pos


I like this biryani,pos
I feel very happy,pos
This is my best work,pos
I do not like this restaurant,neg
I am tired of this stuff,neg
I can't deal with this,neg
What an idea it is,pos
My place is horrible,neg
This is an awesome place,pos
I do not like the taste of this juice,neg
I love to sing,pos
I am sick and tired,neg
I love to dance,pos
What a great holiday,pos
That is a bad locality to stay,neg
We will have good fun tomorrow,pos
I hate this food,neg
OUTPUT:

D:\Machine Learning\Lab>python Week9.py The Total instances in the Dataset: 18 ******** Accuracy
Metrics ***** ***
Accuracy:
Recall :
0.4
0.6666666666666666
Precision : : 0.5
Confusion Matrix :
[[0 2]
[1 2]]
Enter any statement to predict :I love biryani
Statement is Positive

D:\Machine Learning\Lab>python Week9.py The Total instances in the Dataset: ******** Accuracy Metrics
********* Accuracy: 0.8
Recall: 0.6666666666666666
Precision : 1.0
Confusion Matrix :
[[20] [12]]
Enter any statement to predict :i do not like summer
Statement is Negative

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy