ML Lab Programs..Manual 02072022
ML Lab Programs..Manual 02072022
B.Tech. in COMPUTER SCIENCE AND ENGINEERING COURSE STRUCTURE & SYLLABUS (R18)
Applicable From 2019-20 Admitted Batch
CS604PC: MACHINE LEARNING LAB
Syllabus
III Year B.Tech. CSE II-Sem L T P C
0 0 3 1.5
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:
1. understand complexity of Machine Learning algorithms and their limitations;
2. understand modern notions in data analysis-oriented computing;
3. be capable of confidently applying common Machine Learning algorithms in practice and implementing their own;
4. 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 the probability that a student is absent given that today is
Friday? Apply Bayes’ rule in python to get the result. (Ans: 15%)
2. Extract the data from database using python
3. Implement k-nearest neighbors 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)
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
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 -> 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?
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
***
# ML LAB PROGRAM #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 the probability that a student
is absent given that today is Friday? Apply Bayes’ rule in python to get the result. (Ans: 15%)
Short Notes on Conditional Probability & Bayes Theorem:
Conditional Probability:
The conditional probability of the event is the probability that the event will occur, provided the information that an
event A has already occurred. This probability can be written as P(B|A), notation signifies the probability of B given A.
Conditional probability is the probability that an event has occurred, considering some additional information about
the outcomes of an experiment.
Mathematically, if the events A and B are not independent events, then the probability of the interaction of A and B (the
probability of occurrence of both events) is then given by:
P( A B) = P( A)P(B | A) .
Bayes Theorem:
Bayes’ theorem is a way to figure out conditional probability. Conditional probability is the probability of an event
happening, given that it has some relationship to one or more other events
Bayes theorem is the cornerstone of Bayesian learning methods because it provides a way to calculate the posterior
probability P(hlD), from the prior probability P(h), together with P(D) and P(D(h).
P ( D | h) P ( h)
P(h | D) =
P( D)
"""
Solution:
A: Student is absent
F: It is Friday
P(A∩F) = 0.03 and P(F)=0.2. The Problem is to find P(A|F)
By the Bayes Conditional Probability P(A|F)=P(A∩F)/P(F)
P(A|F)=0.03/0.02=0.15
"""
#Source Code
# P(A∩F)
p_a_and_f = 0.03
# P(F)
p_f = 0.2
# calculate P(A|F)
result = bayes_theorem(p_f, p_a_and_f)
# summarize
print('Probability that a student is absent on Friday is: P(A|F)= %.2f%%' %
(result*100))
bayesTherm1.py
(select, copy and paste in destination folder and execute)
# ML LAB PROGRAM #2:
2. Extract the data from database using python
Procedure:
You’ll learn the following MySQL SELECT operations from Python using a ‘MySQL
Connector Python’ module.
Execute the SELECT query and process the result set returned by the query in Python.
Use Python variables in a where clause of a SELECT query to pass dynamic values.
Use fetchall(), fetchmany(), and fetchone() methods of a cursor class to fetch all or limited rows from a
table.
Python Select from MySQL Table
This article demonstrates how to select rows of a MySQL table in Python.
You’ll learn the following MySQL SELECT operations from Python using a ‘MySQL Connector
Python’ module
Execute the SELECT query and process the result set returned by the querying Python.
Use Python variables in a where clause of a SELECT query to pass dynamic values.
Use fetchall(),fetchmany(), and fetchone() methods of a cursor class to fetch all or limited rows from
atable.
Steps to fetch rows from a MySQL database table Follow these steps:
SOURCE CODE:
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 20 09:11:24 2022
@author: RKCSE
mysql db connectivity
"""
"""
Install MySQL Driver
Python needs a MySQL driver to access the MySQL database.
In this tutorial we will use the driver "MySQL Connector".
We recommend that you use PIP to install "MySQL Connector".
PIP is most likely already installed in your Python environment.
Navigate your command line to the location of PIP, and type the following:
Download and install "MySQL Connector":
# pip install mysql-connector-python
"""
pip install mysql-connector-python
db = mysql.connect(
host = "localhost",
charset ='utf8',
user = "root",
passwd = "2009"
)
# create a database named "mydatabase":
cursor = db.cursor()
cursor.execute("CREATE DATABASE vgnt;")
cursor.execute("SHOW DATABASES")
for x in cursor:
print(x)
cursor.execute("USE vgnt")
cursor.execute("CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))")
myresult = cursor.fetchall()
for x in myresult:
print(x)
for x in result:
print(x)
# Sort the result alphabetically by name: result:
sql = "SELECT * FROM customers ORDER BY name"
cursor.execute(sql)
for x in cursor:
print(x)
cursor.execute("show tables")
for x in cursor:
print(x)
cursor.execute("show tables")
for x in cursor:
print(x)
db = mysql.connect(
host = "localhost",
charset ='utf8',
user = "root",
passwd = "1234"
)
cursor = db.cursor()
#"CREATE DATABASE vgnt;”
#"SHOW DATABASES"
('information_schema',)
('mysql',)
('performance_schema',)
('sakila',)
('sys',)
('vgnt',)
('world',)
"""
"CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255)) "
sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
val = ("John", "Highway 21")
cursor.execute(sql, val)
db.commit()
print(cursor.rowcount, "record inserted.")
"""
1 record inserted.
"""
sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
val = [
('Peter', 'Lowstreet 4'),
('Amy', 'Apple st 652'),
('Hannah', 'Mountain 21'),
('Michael', 'Valley 345'),
('Sandy', 'Ocean blvd 2'),
('Betty', 'Green Grass 1'),
('Richard', 'Sky st 331'),
('Susan', 'One way 98'),
('Vicky', 'Yellow Garden 2'),
('Ben', 'Park Lane 38'),
('William', 'Central st 954'),
('Chuck', 'Main Road 989'),
('Viola', 'Sideway 1633')
]
"""
13 was inserted.
('John', 'Highway 21')
('Peter', 'Lowstreet 4')
('Amy', 'Apple st 652')
('Hannah', 'Mountain 21')
('Michael', 'Valley 345')
('Sandy', 'Ocean blvd 2')
('Betty', 'Green Grass 1')
('Richard', 'Sky st 331')
('Susan', 'One way 98')
('Vicky', 'Yellow Garden 2')
('Ben', 'Park Lane 38')
('William', 'Central st 954')
('Chuck', 'Main Road 989')
('Viola', 'Sideway 1633')
"""
sql = "SELECT * FROM customers WHERE address LIKE '%way%'"
('John', 'Highway 21')
('Susan', 'One way 98')
('Viola', 'Sideway 1633')
cursor.execute("show tables")
for x in cursor:
print(x)
"""
cursor.execute("select * from customers")
for x in cursor:
print(x)
"""
pymysql.py
# ML LAB PROGRAM #5:
"""
The following training examples map descriptions of individuals onto high, medium and
low credit-worthiness.
#headers/attributes
income, recreation, job, status, age-group, home-owner
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
Conditional probability is the probability that an event has occurred, considering some additional
information about the outcomes of an experiment.
Mathematically, if the events A and B are not independent events, then the probability of the interaction of
A and B (the probability of occurrence of both events) is then given by:
P( A B) = P( A)P(B | A) .
Bayes Theorem:
Bayes’ theorem is a way to figure out conditional probability. Conditional probability is the probability of an
event happening, given that it has some relationship to one or more other events
Bayes theorem is the cornerstone of Bayesian learning methods because it provides a way to calculate the
posterior probability P(hlD), from the prior probability P(h), together with P(D) and P(D(h).
P ( D | h) P ( h)
P(h | D) =
P( D)
#Source Code
# Task-1: Unconditional Probability of Golf
totalRecords=10
numberGolfRecreation=4
probGolf=numberGolfRecreation/totalRecords
print("Unconditio nal probability of golf: =",probGolf)
# P(medRisk ∩ single)
numberMedRiskandSingle=2 #n(medRisk ∩ single)
probMedRiskandSingle=numberMedRiskandSingle/totalRecords #2/10
print("P(medRisk ∩ single): ",probMedRiskandSingle)
# P(medRisk|single)
probMedRiskgivenSingle=probMedRiskandSingle/probSingle
print("P(medRisk|single): ",probMedRiskgivenSingle)
numberMedRisk=3
probMedRisk=numberMedRisk/totalRecords
print("P(MedRisk): ",probMedRisk)
probSingleGivenMedRisk=probMedRiskgivenSingle*probSingle/probMedRisk
print("P(Single|medRisk): ",probSingleGivenMedRisk)
golfCondiProb.py golfCondiProbData
.csv
# ML LAB PROGRAM #6:
6. Implement linear regression using python.
Liner Regression:
Linear regression is a statistical method for modeling relationships between a dependent
variable with a given set of independent variables.
simple linear regression we want to model our data as follows:
y = B0 + B1 * x
we know and B0 and B1 are coefficients that we need to estimate that move the line
around. It is used in estimating exactly how much of y will change, when x changes a
certain amount. The line of best fit is nothing but the line that best expresses the
relationship between the data points.
Simple regression is great, because rather than having to search for values by trial and
error or calculate them analytically using more advanced linear algebra, we can estimate
them directly from our data.
# SOURCE CODE
# import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=1/3,
random_state=0)
Bayes theorem is the cornerstone of Bayesian learning methods because it provides a way to calculate the
posterior probability P(hlD), from the prior probability P(h), together with P(D) and P(D(h).
P ( D | h) P ( h)
P(h | D) =
P( D)
fit_transform():
This method performs fit and transform on the input data at a single time and converts the data points. If
we use fit and transform separate when we need both then it will decrease the efficiency of the model so we
use fit_transform() which will do both the work.
sklearn.naive_bayes.MultinomialNB():
Naïve Bayes classifier. It assumes that the features are drawn from a simple Multinomial
distribution. The Scikit-learn provides sklearn.naive_bayes.MultinomialNB to implement the
Multinomial Naïve Bayes algorithm for classification.
#SOURCE CODE
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
print(Xtrain)
Xtrain.shape[0]
print(ytrain)
ytrain.shape[0]
cv = CountVectorizer()
Xtrain_dm = cv.fit_transform(Xtrain)
print("Xtrain_dm\n",Xtrain_dm)
cv.vocabulary_
Xtest_dm = cv.transform(Xtest)
df = pd.DataFrame(Xtrain_dm.toarray(),columns=cv.get_feature_names())
print(df)
clf = MultinomialNB()
clf.fit(Xtrain_dm, ytrain)
pred = clf.predict(Xtest_dm)
print('Accuracy Metrics:')
df = pd.DataFrame(Xtrain_dm.toarray(),columns=cv.get_feature_names())
print(df)
am amazingplace an and awesome bad ... we went what will with work
0 0 0 1 0 1 0 ... 0 0 0 0 0 0
1 0 0 0 0 0 1 ... 0 0 0 0 0 0
2 0 0 0 0 0 0 ... 0 0 0 0 1 0
3 0 1 1 0 0 0 ... 0 0 0 0 0 0
4 0 0 0 0 0 0 ... 0 0 0 0 0 1
5 0 0 0 0 0 0 ... 1 0 0 1 0 0
6 0 0 0 0 0 0 ... 0 0 0 0 0 0
7 0 0 0 0 0 0 ... 0 0 0 0 0 0
8 0 0 0 0 0 0 ... 0 1 0 0 0 0
9 0 0 1 0 1 0 ... 0 0 1 0 0 0
10 0 0 0 0 0 0 ... 0 0 0 0 0 0
11 1 0 0 1 0 0 ... 0 0 0 0 0 0
12 0 0 0 0 0 0 ... 0 0 0 0 0 0
clf = MultinomialNB()
clf.fit(Xtrain_dm, ytrain)
pred = clf.predict(Xtest_dm)
print('Accuracy Metrics:')
textclassify1.py textclassify1Data.cs
v
# textClassify1Data.csv
SOURCE CODE:
ML LAB PROGRAM #3
3. Implement k-nearest neighbors classification using python
"""
# Import necessarymodules
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
import numpy as np
"""
# Loading data
irisData = load_iris()
# Createfeature and target arrays
X = irisData.data
y = irisData.target
# print(X); print(y)
# Split into training and test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.35, random_state=40)
# X_train, X_test, y_train, y_test = train_test_split(X, y)
knn = KNeighborsClassifier(n_neighbors=8)
knn.fit(X_train, y_train)
# Predict on dataset X_test, y_test
y_pred=knn.predict(X_test)
# numpy. array_equiv(y_test,y_pred)
print("\n",y_pred)
print("\n",y_test)
y_test==y_pred
np.sum(y_test!=y_pred)
OUTPUT:
print("\n",y_pred)
[0 2 0 1 2 2 0 1 0 0 0 2 1 1 0 1 1 1 2 2 0 2 1 2 2 2 0 0 1 0 2 0 1 0 2 2 2
2 0 0 1 1 1 2 2 0 0 2 0 1 2 0 1]
print("\n",y_test)
[0 2 0 1 2 2 0 1 0 0 0 2 1 1 0 1 1 1 1 2 0 2 1 2 2 2 0 0 1 0 2 0 1 0 2 2 2
2 0 0 1 1 1 2 2 0 0 2 0 1 2 0 1]
y_test==y_pred
Out[22]:
array([ True, True, True, True, True, True, True, True, True,
True, True, True, True, True, True, True, True, True,
False, True, True, True, True, True, True, True, True,
True, True, True, True, True, True, True, True, True,
True, True, True, True, True, True, True, True, True,
True, True, True, True, True, True, True, True])
np.sum(y_test!=y_pred)
Out[23]: 1
# New Test Cases for prediction
newX=([[5.6, 4.6, 1.6, 0.6]])
knn.predict(newX)
Out[26]: array([0])
newX=([[6.6, 2.6, 4.6, 1.6]])
knn.predict(newX)
Out[28]: array([1])
newX=([[5.6, 3.6, 5.6, 2.6]])
knn.predict(newX)
Out[30]: array([2])
knn.py
@author: krish
"""
"""
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)
"""
from sklearn.cluster import KMeans
import numpy as np
kmeans
# Predict a classification for a case where VAR1=0.906 and VAR2=0.606
kmeans.predict([[0.906, 0.606]]) #check the 2nd var set Cluster #0
# Few More
kmeans.predict([[0.180, 1.786]]) #check the 2nd var set Cluster #1
kmeans.predict([[0.940, 1.566]]) #check the 2nd var set Cluster #2
kmeans.predict([[1.486, 0.759]]) #check the 2nd var set Cluster #0
"""
OUTPUT:
kmeans = KMeans(n_clusters=3, random_state=0).fit(X,y)
kmeans
Out[69]: KMeans(n_clusters=3, random_state=0)
# Predict a classification for a case where VAR1=0.906 and VAR2=0.606
kmeans.predict([[0.906, 0.606]]) #check the 2nd var set Cluster #0
Out[71]: array([0])
# Few More
kmeans.predict([[0.180, 1.786]]) #check the 2nd var set Cluster #1
Out[73]: array([1])
kmeans.predict([[0.940, 1.566]]) #check the 2nd var set Cluster #2
Out[74]: array([2])
kmeans.predict([[1.486, 0.759]]) #check the 2nd var set Cluster #0
Out[75]: array([0])
"""
VIVA Qns:
For Example, let’s have 2 variables, named age and salary where age is in the range of 20 to 60 and salary is in
the range of 100-150K, since scales of these variables are different so when these variables are substituted in the
euclidean distance formula, then the variable which is on the large scale suppresses the variable which is on the
smaller scale. So, the impact of age will not be captured very clearly. Hence, you have to scale the variables to the
same range using Standard Scaler, Min-Max Scaler, etc.
2. Which metrics can you use to find the accuracy of the K means Algorithm?
There does not exist a correct answer to this question as k means being an unsupervised learning technique does
not discuss anything about the output column. As a result, one can not get the accuracy number or values from the
algorithm directly.
4. What are the ways to avoid the problem of initialization sensitivity in the K means Algorithm?
There are two ways to avoid the problem of initialization sensitivity:
• Repeat K means: It basically repeats the algorithm again and again along with initializing thecentroids
followed by picking up the cluster which results in the small intra-cluster distance and large inter
cluster distance.
• K Means++: It is a smart centroid initialization technique.
• Amongst the above two techniques, K-Means++ is the best approach.
# ML LAB PROGRAM #9:
Implement the finite words classification system using Back-propagation
algorithm.
The Back propagation algorithm is a supervised learning method for multilayer feed- forward networks from
the field of Artificial Neural Networks.
The principle of the backpropagation approach is to model a given function by modifying internal weightings
of input signals to produce an expected output signal. The system is trained using a supervised learning
method, where the error between the system’s output and a known expected output is presented to the
system and used to modify its internal state.
The main three layers are:
1. Input layer
2. Hidden layer
3. Output layer
Each layer has its own way of working and its own way to take action such that we are able to get the desired
results and correlate these scenarios to our conditions.
Source Code:
"""
Back propagation algorithm
"""
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
count_v = CountVectorizer()
Xtrain_dm = count_v.fit_transform(Xtrain)
Xtrain_dm
Xtest_dm = count_v.transform(Xtest)
df = pd.DataFrame(Xtrain_dm.toarray(),columns=count_v.get_feature_names())
df
clf = MLPClassifier(solver='lbfgs', alpha=1e-5,hidden_layer_sizes=(5, 2), random_state=1)
clf.fit(Xtrain_dm, ytrain)
pred = clf.predict(Xtest_dm)
print('Accuracy Metrics:')
print('Accuracy: ', accuracy_score(ytest, pred))
print('Recall: ', recall_score(ytest, pred))
print('Precision: ', precision_score(ytest, pred))
print('Confusion Matrix: \n', confusion_matrix(ytest, pred))
"""
# document.csv:
I love this sandwich,pos
This is an amazingplace,pos
I feel very good about these beers,pos
This is my best work,pos
What an awesome view,pos
I do not like this restaurant,neg
I am tired of this stuff,neg
I can't deal with this,neg He ismy sworn enemy,neg
My boss is horrible,neg
This is an awesome place,pos
I do not like the taste of this juice,neg
I love to dance,pos
I am sick and tired of this place,neg
What a great holiday,pos
That is a bad locality to stay,neg
We will have good fun tomorrow,pos
I went to my enemy's house today,neg
"""
"""
Just for Ref only:
sklearn.neural_network.MLPClassifier:
clf = MLPClassifier(solver='lbfgs', alpha=1e-5,hidden_layer_sizes=(5, 2), random_state=1)
This model optimizes the log-loss function using LBFGS or stochastic gradient descent. (New in
version 0.18.)
Activation function for the hidden layer: activation{‘identity’, ‘logistic’, ‘tanh’, ‘relu’},
default=’relu’
sklearn.neural_network.CountVectorizer():
Convert a collection of text documents to a matrix of token counts.
Tolerance for the optimization. When the loss or score is not improving by at least tol for
n_iter_no_change consecutive iterations, unless learning_rate is set to ‘adaptive’, convergence
is considered to be reached and training stops.
tolfloat, default=1e-4:
Tolerance for the optimization. When the loss or score is not improving by at least tol for
n_iter_no_change consecutive iterations, unless learning_rate is set to ‘adaptive’, convergence
is considered to be reached and training stops.
"""
Output:
Total Instances of Dataset: 18
Accuracy Metrics:
Accuracy: 0.8
Recall: 0.6666666666666666
Precision: 1.0
Confusion Matrix:
[[2 0]
[1 2]]
backprop.py backpropDoc.csv
# ML LAB PROGRAM #8:
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. 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 species who can adapt to changes
in their environment are able to survive and reproduce and go to next generation.
Operators of Genetic Algorithm:
Once the initial generation is created, the algorithm evolve the generation using
following operators –
1. Selection Operator: The idea is to give preference to the individuals with
good fitness scores and allow them to 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 these crossover sites are exchanged
thus creating a completely new individual (offspring).
3. Mutation Operator: The key idea is to insert random genes in offspring to
maintain the diversity in population to avoid the premature convergence
Source Code:
import matplotlib.pyplot
import numpy
def cal_pop_fitness(equation_inputs, pop):
# Calculating the fitness value of each solution in the current population.
# The fitness function calulates the sum of products between each input and its
corresponding weight.
fitness = numpy.sum(pop*equation_inputs, axis=1)
return fitness
"""
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)
What are the best values for the 6 weights w1 to w6?
We are going to use the genetic algorithm for the best possible values after a number
of generations.
"""
# Inputs of the equation.
equation_inputs = [4,-2,3.5,5,-11,-4.7]
"""
Genetic algorithm parameters: Mating pool size Population size
"""
sol_per_pop = 8
num_parents_mating = 4
# Defining the population size.
# The population will have sol_per_pop chromosome where each chromosome has
num_weights genes.
pop_size = (sol_per_pop,num_weights)
#Creating the initial population.
Output:
Generation : 528
Generation : 529
.
.
.
Generation : 997
Generation : 998
Generation : 999
Fitness
[ 23.77600938 -38.72547264 -3.8947447 37.10753127 36.47088852 -45.89452384 -
23.09632211 -39.97632958]
Best result : 37.107531271257386
Parents
[[ 1.08870518 3.53573945 2.059628 -2.23014052 -2.93675387 -2.43870245]
[ 2.73488817 -0.27533366 0.43937282 1.77062771 -2.97782039 3.86517002]
[-1.44232977 -1.85501121 -1.7915992 0.5713773 -2.70042871 0.09695724]
[ 1.14702539 -2.02199422 -0.58553948 2.15263606 2.60080023 -1.56769697]]
Crossover
[[1.08870518 3.53573945 2.059628 2.23014052 2.93675387 2.43870245]
[2.73488817 0.27533366 0.43937282 1.77062771 2.97782039 3.86517002]
[1.44232977 1.85501121 1.7915992 0.5713773 2.70042871 0.09695724]
[1.14702539 2.02199422 0.58553948 2.15263606 2.60080023 1.56769697]]
Mutation
[[1.08870518 3.53573945 2.059628 2.23014052 2.93675387 2.43870245]
[2.73488817 0.27533366 0.43937282 1.77062771 2.97782039 3.86517002]
[1.44232977 1.85501121 1.7915992 0.5713773 2.70042871 0.09695724]
[1.14702539 2.02199422 1.43349451 2.15263606 2.60080023 1.56769697]]
Best solution : [[[ 3.50731352 1.70379643 -1.13092437 -3.82269723 1.51092617
-1.27104807]]]
Best solution fitness : [-23.09632211]
ga.py
# Additional Programs
linearRegression2.p
y
bayesTherm2.py