ML Lab Manual
ML Lab Manual
Maisammaguda,Dhulapally(V),Medchal(M),
Hyderabad -500100, Telangana.
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 theprobability that a
student is
absent given that today is Friday? Apply Baye’s rule in python to get the result. (Ans: 15%)
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
kmeans
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
MRCE
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?
MRCE
EXPERIMENT NO: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 Baye’s rule in python to get the result. (Ans: 15%)
Aim:
To Find the the probability that a student is absent given that today is Friday from
given data with Baye’s rule in python.
Theory:
P (Today is Friday)=0.2
P(B)=0.2
it is required to find
P(student is absent |today is Friday) P(A|B)
The formula for obtaining the conditional probability of event A, given the event B has
occurred is as follows:
P(A|B)=P(A∩B)/P(B)
Thus the required probability is as follows:
P(student is absent| today is Friday)=P(It is Friday∩student is absent)/P(Today is Friday)
=0.03/0.2=0.15
PROCEDURE / PROGRAMME
# calculate P(A|B) given P(A and B) and P(B)
return p_a_given_b
# P(A and B)
p_a_b = 0.03
# P(B)
p_b = 0.20
# calculate P(A|B)
# summarize
MRCE
EXPERIMENT NO:2
Extract the data from database using python
Aim:
To extract the data from database using python
Theory:
Refer to Python MySQL database connection to connect to MySQL database from Python
using MySQL Connector module
Next, prepare a SQL SELECT query to fetch rows from a table. You can select all or
limited rows based on your requirement. If the where condition is used, then it decides
the number of rows to fetch.
For example, SELECT col1, col2,…colnN FROM MySQL_table WHERE id = 10;. This will
return row number 10.
Next, use a connection.cursor() method to create a cursor object. This method creates a
new MySQLCursor object.
After successfully executing a Select operation, Use the fetchall() method of a cursor
object to get all rows from a query result. it returns a list of rows.
Iterate a row list using a for loop and access each row individually (Access each row’s
column data using a column name or index number.)
use cursor.clsoe() and connection.clsoe() method to close open connections after your
work completes.
MRCE
PROCEDURE / PROGRAMME
import mysql.connector
try:
connection=mysql.connector.connect(host='localhost',database='employeeDB',charset='
utf8',user='root',password='root')
print("connected")
cursor = connection.cursor()
cursor.execute(sql_select_Query)
records = cursor.fetchall()
except Error as e:
connection.close()
cursor.close()
import mysql.connector
try:
mydb =
mysql.connector.connect(host='localhost',database='employeeDB',charset='utf8',user='r
oot',password='root')
mycursor = mydb.cursor()
=[
MRCE
(2111,'rubesh','Lowstreet 4','2019-09-12'),
(2121,'siva','Apple st 652','2019-09-12'),
mycursor.executemany(sql, val)
mydb.commit()
except Error as e:
finally:
if mydb.is_connected():
mydb.close()
#cursor.close()
Output
connected
Id = 111
Name = siva
Address = madurai
Id = 112
Name = Ram
Address = Theni
Id = 2111
Name = rubesh
Address = Lowstreet 4
Id = 2121
Name = siva
MRCE
EXPERIMENT NO:3
Implement k-nearest neighbours classification using python
Aim:
To implement k-nearest neighbours classification using python
Theory:
• K-Nearest Neighbors is one of the most basic yet essential classification algorithms in
Machine Learning. It belongs to the supervised learning domain and finds intense
application in pattern recognition, data mining and intrusion detection.
• It is widely disposable in real-life scenarios since it is non-parametric, meaning, it does not
make any underlying assumptions about the distribution of data.
• Algorithm
Input: Let m be the number of training data samples. Let p be an unknown point.
Method:
1. Store the training samples in an array of data points arr[]. This means each element
of this array represents a tuple (x, y).
2. for i=0 to m
Calculate Euclidean distance d(arr[i], p).
3. Make set S of K smallest distances obtained. Each of these distances correspond to
an already classified data point.
4. Return the majority label among S.
PROCEDURE / PROGRAMME :
# import the required packages
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn import datasets
# Load dataset
iris=datasets.load_iris()
print("Iris Data set loaded...")
# Split the data into train and test samples
x_train, x_test, y_train, y_test = train_test_split(iris.data,iris.target,test_size=0.1)
print("Dataset is split into training and testing...")
print("Size of trainng data and its label",x_train.shape,y_train.shape)
print("Size of trainng data and its label",x_test.shape, y_test.shape) #
Prints Label no. and their names
for i in range(len(iris.target_names)):
print("Label", i , "-",str(iris.target_names[i]))
MRCE
#print(classification_report(y_test,y_pred))
Output
Result-1
Iris Data set loaded...
Dataset is split into training and testing samples...
Size of trainng data and its label (135, 4) (135,)
Size of trainng data and its label (15, 4) (15,)
Label 0 - setosa
Label 1 - versicolor
Label 2 - virginica
Results of Classification using K-nn with K=1
Sample: [4.4 3. 1.3 0.2] Actual-label: 0 Predicted-label: 0
Sample: [5.1 2.5 3. 1.1] Actual-label: 1 Predicted-label: 1
Sample: [6.1 2.8 4. 1.3] Actual-label: 1 Predicted-label: 1
Sample: [6. 2.7 5.1 1.6] Actual-label: 1 Predicted-label: 2
Sample: [6.7 2.5 5.8 1.8] Actual-label: 2 Predicted-label: 2
Sample: [5.1 3.8 1.5 0.3] Actual-label: 0 Predicted-label: 0
Sample: [6.7 3.1 4.4 1.4] Actual-label: 1 Predicted-label: 1
Sample: [4.8 3.4 1.6 0.2] Actual-label: 0 Predicted-label: 0
Sample: [5.1 3.5 1.4 0.3] Actual-label: 0 Predicted-label: 0
Sample: [5.4 3.7 1.5 0.2] Actual-label: 0 Predicted-label: 0
Sample: [5.7 2.8 4.1 1.3] Actual-label: 1 Predicted-label: 1
Sample: [4.5 2.3 1.3 0.3] Actual-label: 0 Predicted-label: 0
Sample: [4.4 2.9 1.4 0.2] Actual-label: 0 Predicted-label: 0
Sample: [5.1 3.5 1.4 0.2] Actual-label: 0 Predicted-label: 0
Sample: [6.2 3.4 5.4 2.3] Actual-label: 2 Predicted-label: 2
Classification Accuracy : 0.93
MRCE
EXPERIMENT NO: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 kmeans 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
Aim:
To predict a classification for a case where VAR1=0.906 and VAR2=0.606, using the result of
kmeans clustering with 3 means and given data.
Theory:
Step 1: Python 3 code snippet demonstrates the implementation of a simple K-Means
clustering to automatically divide input data into groups based on given features.
Step 2: “ , “ separated CSV file is loaded first, which contains three corresponding input
columns.
Step 3: K-Means clustering model is created from this input data. Afterwards, new data can
be classified using the predict() method based on the learned model.
Step 4: The Scikit-learn and the Pandas library to be installed (pip install sklearn, pip install
pandas).
Step 5
input_data.txt
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
MRCE
Step 6
PROCEDURE / PROGRAMME
import pandas as pd
import numpy as np
import pickle
print(input_data.to_string())
kmeans = KMeans(n_clusters=3)
kmeans.fit(input_data.values)
print(kmeans.labels_)
predicted_class = kmeans.predict([[0.906,0.606,1]])
print(predicted_class)
Output
0 1.713 1.586 0
1 0.180 1.786 1
2 0.353 1.240 1
3 0.940 1.566 0
4 1.486 0.759 1
5 1.266 1.106 0
6 1.540 0.419 1
7 0.459 1.799 1
8 0.773 0.186 1
[1 0 0 1 2 1 2 0 2]
[2]
MRCE
EXPERIMENT NO:5
The following training examples map descriptions of individuals onto high, medium and low
credit-worthiness.
Aim:
To unconditional probability of `golf' and the conditional probability of `single' given
`medRisk' in the dataset
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?
Theory:
Calculations of parts:
P(A) = (2+1) / (4+2+ 3+1) =0 .3
P(B) = (3+1) / (4+2+ 3+1) = 0.4
P(A∩B) = (.1) / (4+2+ 3+1) =0 .1
And per the formula, P(A|B) = P(A ∩ B) / P(B), put it together.
PROCEDURE / PROGRAMME
import pandas as pd
import numpy as np
df = pd.read_csv('pd.csv')
df.head(10)
print(len(df))
print(df.to_string())
df['Arecreation'] = np.where(df['recreation']=='golf', 1, 0)
df['Arisk'] = np.where(df['risk']=='medRisk', 1, 0)
MRCE
df['count'] = 1
df = df[['Arecreation','Arisk','count']]
df.head()
print(df.to_string())
table=pd.pivot_table(
df,
values='count',
index=['Arecreation'],
columns=['Arisk'],
aggfunc=np.size,
fill_value=0
print(table)
a0=table.at[0,0]
a1=table.at[0,1]
a2=table.at[1,0]
a3=table.at[1,1]
pa=(a1+a3)/(a0+a1+a2+a3)
pb=(a2+a3)/(a0+a1+a2+a3)
p_a_and_b=(a3/(a0+a1+a2+a3))
p_a_gives_b=p_a_and_b/pb
print(p_a_gives_b)
MRCE
output
10
0 0 0 1
1 1 0 1
2 0 1 1
3 0 0 1
4 0 0 1
5 0 1 1
6 1 1 1
7 1 0 1
8 0 0 1
9 1 0 1
Arisk 01
Arecreation
0 4 2
1 3 1
0.25
P(A|B) = 25%
MRCE
EXPERIMENT NO:6
Implement linear regression using python.
Aim:
To implement linear regression using python
Theory:
In order to provide a basic understanding of linear regression, we start with the most basic
version of linear regression, i.e. Simple linear regression.
MRCE
Now, the task is to find a line which fits best in above scatter plot so that we can predict
the response for any new feature values. (i.e a value of x not present in dataset)
This line is called regression line.
The equation of regression line is represented as:
Here,
and our task is to find the value of b_0 and b_1 for which J(b_0,b_1) is minimum!
Without going into the mathematical details, we present the result here:
PROCEDURE / PROGRAMME
import numpy as np
# number of observations/points
n = np.size(x)
m_x = np.mean(x)
m_y = np.mean(y)
MRCE
# plotting the actual points as scatter plot
# putting labels
plt.xlabel('x')
plt.ylabel('y')
plt.show()
def main():
# observations / data
x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# estimating
coefficients b =
estimate_coef(x, y)
print("Estimated coefficients:\nb_0 = {} \
plot_regression_line(x, y, b)
main()
Output
Estimated coefficients:
b_0 = 1.2363636363636363
b_1 = 1.1696969696969697
MRCE
EXPERIMENT NO:7
Implement Naïve Bayes theorem to classify the English text
Aim:
To implement Naïve Bayes theorem to classify the English text
Theory:
LEARN_NAIVE_BAYES_TEXT (Examples, V)
Examples is a set of text documents along with their target values. V is the set of all
possibletarget values. This function learns the probability terms P(w k |vj,), describing
the probabilitythat a randomly drawn word from a document in class v j will be the
English word wk. It also learns the class prior probabilities P(vj).
1. collect all words, punctuation, and other tokens that occur in Examples
Vocabulary ← c the set of all distinct words and other tokens occurring in any
textdocument from Examples
CLASSIFY_NAIVE_BAYES_TEXT (Doc)
Return the estimated target value for the document Doc. ai denotes the word found
in the ithposition within Doc.
positions ← all word positions in Doc that contain tokens found in Vocabulary
Return VNB, where
MRCE
Data set:
PROCEDURE / PROGRAMME
import pandas as pd
msg=pd.read_csv('naivetext.csv',names=['message','label'])
msg['labelnum']=msg.label.map({'pos':1,'neg':0})
X=msg.message
y=msg.labelnu
m print(X)
print(y)
MRCE
from sklearn.model_selection import train_test_split
xtrain,xtest,ytrain,ytest=train_test_split(X,y)
count_vect = CountVectorizer()
xtrain_dtm = count_vect.fit_transform(xtrain)
xtest_dtm=count_vect.transform(xtest)
print(count_vect.get_feature_names())
df=pd.DataFrame(xtrain_dtm.toarray(),columns=count_vect.get_feature_names()) #
clf = MultinomialNB().fit(xtrain_dtm,ytrain)
predicted = clf.predict(xtest_dtm)
print(metrics.confusion_matrix(ytest,predicted))
Output
MRCE
7 I can't deal with this
8 He is my sworn enemy
9 My boss is horrible
12 I love to dance
0 1
1 1
2 1
3 1
4 1
5 0
6 0
7 0
8 0
9 0
10 1
11 0
12 1
13 0
14 1
15 0
16 1
17 0
MRCE
The words or Tokens in the text documents
['am', 'amazing', 'an', 'awesome', 'best', 'boss', 'can', 'dance', 'deal', 'do', 'enemy', 'great',
'he', 'holiday', 'horrible', 'is', 'juice', 'like', 'love', 'my', 'not', 'of', 'place', 'restaurant',
'sandwich', 'stuff', 'sworn', 'taste', 'the', 'this', 'tired', 'to', 'view', 'what', 'with', 'work']
Confusion matrix
[[2 1]
[0 2]]
Basic knowledge
Confusion Matrix
MRCE
Example:
MRCE
Accuracy: how often is the classifier correct?
4 poor acting -
Unique word
Doc I loved the movie hate a grea good poor actin Clas
d t g s
1 1 1 1 1 +
2 1 1 1 1 -
3 2 1 1 1 +
4 1 1 -
5 1 1 1 1 +
MRCE
Doc I loved the movie hated a grea good poor actin Clas
t g s
1 1 1 1 1 +
3 2 1 1 1 +
5 1 1 1 1 +
𝑃(+) =
3
= 0.6
5
𝑃(𝐼 |+) =
1+1 1+1
𝑃(𝑎 |+) =
= 0.0833
14 + 10 = 0.0833
14 + 10
1+1 2+1
𝑃(𝑙𝑜𝑣𝑒𝑑 |+) = = 0.0833 𝑃(𝑔𝑟𝑒𝑎𝑡 |+) = = 0.125
14 + 10 14 + 10
1+1 2+1
𝑃(𝑡ℎ𝑒 |+) = = 0.0833 𝑃(𝑔𝑜𝑜𝑑 |+) = = 0.125
14 + 10 14 + 10
4+1 0+1
𝑃(𝑚𝑜𝑣𝑖𝑒 |+) = = 0.2083 𝑃(𝑝𝑜𝑜𝑟 |+) = = 0.0416
14 + 10 14 + 10
0+1 1+1
𝑃(ℎ𝑎𝑡𝑒𝑑 |+) = = 0.0416 𝑃(𝑎𝑐𝑡𝑖𝑛𝑔 |+) = = 0.0833
14 + 10 14 + 10
MRCE
Doc I loved the movie hate a great good poor actin Clas
d g s
2 1 1 1 1 -
4 1 1 -
MRC
EXPERIMENT NO:8
Implement an algorithm to demonstrate the significance of genetic algorithm
Aim:
Theory:
Genetic algorithm
MRC
This figure is copyrighted
material in this book and should not be used without permission.
The high-quality (high-fitness) solutions survive longer than the ones with low
fitness. The higher the fitness, the higher probability of selecting the solution as a
parent to produce new offspring. To produce the offspring, pairs of parents mate
using the crossover operation, where a new solution is generated that carries genes
from its parents.
After crossover, mutation is applied to add some random changes over the solution.
The evolution continues through a number of generations to reach the highest-
quality solution.
For more information about the genetic algorithm, read this article: Introduction to
Optimization with Genetic Algorithm.
Even though the same steps are applied to all types of problems, you still need to
select appropriate parameters to fit different problems. Some of these parameters
include:
MRC
Crossover operator type,
Crossover probability,
Mutation probability,
Fitness function.
For example, there are different types of parent selection, like rank and roulette
wheel, and you should know which one to use when designing the algorithm for a
specific problem.
The parameter we’ll be focusing on is mutation probability. So, let’s review the
mutation operation, and whether high or low mutation probability is better.
Given two parents to mate, the first operation in the mating process is the crossover.
The produced child just transfers some genes from its two parents. There’s nothing
new in the child, as all of its genes are already existing in its parents. The next figure
shows how crossover works.
This figure is
copyrighted material in this book and should not be used without permission.
If there are some bad genes within the parents, they will definitely be transferred to
their children after crossover. The mutation operation plays a crucial role in fixing
this issue.
During mutation, some genes are randomly selected from each child where some
random changes are applied. Genes are selected based on a random probability for
each gene. If the probability of mutating a gene is smaller than or equal to a
predefined threshold, then this gene will be selected for mutation. Otherwise, it will
be skipped. We’ll discuss mutation probability later on.
MRC
Let’s assume there are 4 genes in the solution, as in the next figure, where only the
last gene is selected for mutation. A random change is applied to change its old
value 2 and the new value is 4.
After briefly reviewing how random mutation works, next we’ll solve a problem using
the genetic algorithm with random mutation.
PROCEDURE / PROGRAMME
# objective
function # fitness
function def
objective(x):
"""
# the function to optimize
>> x**2 - y * y**0.5 + 14
"""
return (x[0] ** 2.0) - (x[1] * (x[1] ** 0.5)) + 14
MRC
substring = bitstring[start:end]
# convert bitstring to a string of chars
chars = "".join([str(s) for s in substring])
# convert string to integer
integer = int(chars, 2)
# scale integer to desired range
value = bounds[i][0] + (integer / largest) * (bounds[i][1] - bounds[i][0]) #
store
decoded.append(value)
# will return array of length same as length of bounds
# ie: decoded values for all variables
return decoded
# tournament selection
def selection(pop, scores, k=3):
"""
pick k tournaments and pick the best parent in each call
best is determined by scores array (fitness scores (based on objective function))
"""
# first random selection
selection_ix = randint(len(pop))
for ix in randint(0, len(pop), k - 1):
# check if better (e.g. perform a tournament)
if scores[ix] > scores[selection_ix]:
selection_ix = ix
return pop[selection_ix]
MRC
# mutation operator
def mutation(bitstring, r_mut):
"""
mutate bitstring itself, NOT the copy
"""
for i in range(len(bitstring)):
# check for a mutation
if rand() < r_mut:
# flip the bit
"""
bit-flip mutation
1 - (1) => 0
1 - (0) => 1
"""
bitstring[i] = 1 - bitstring[i]
# genetic algorithm
def genetic_algorithm(objective, bounds, n_bits, n_iter, n_pop, r_cross, r_mut): #
initial population of random bitstring
pop = [randint(0, 2, n_bits * len(bounds)).tolist() for _ in range(n_pop)]
"""
pop =>
[
[1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0,
1, 0, 0]
...
...
...
(n_pop)th array
]
"""
# enumerate
generations for gen in
range(n_iter): # decode
population
decoded = [decode(bounds, n_bits, p) for p in pop]
MRC
# select parents
selected = [selection(pop, scores) for _ in range(n_pop)]
# `selected` is a length 100 array of each element being 32 length arrays
# replace population
pop = children
Output
MRC
>0, new best f([14.81231689453125, 12.377120971679688]) = 189.860618
>1, new best f([14.81964111328125, 12.377960205078125]) = 190.073220
>1, new best f([14.81048583984375, 12.104522705078125]) = 191.236975
>3, new best f([14.81231689453125, 12.104827880859375]) = 191.289623
>3, new best f([14.811630249023438, 12.054473876953125]) = 191.531796
>4, new best f([14.93499755859375, 12.525588989257812]) = 192.724203
>5, new best f([14.99542236328125, 12.737686157226562]) = 193.402024
>5, new best f([14.925155639648438, 12.127792358398438]) = 194.525258
>6, new best f([14.925155639648438, 12.127639770507812]) = 194.526055
>6, new best f([14.994735717773438, 12.018768310546875]) = 197.175319
>11, new best f([14.996109008789062, 12.0262451171875]) = 197.177618
>12, new best f([14.99908447265625, 12.0360107421875]) = 197.216058
>13, new best f([14.9981689453125, 12.0274658203125]) = 197.233054
>13, new best f([14.998397827148438, 12.0262451171875]) = 197.246270
>14, new best f([14.995651245117188, 12.009384155273438]) = 197.251566
>15, new best f([14.998397827148438, 12.0164794921875]) = 197.297059
>16, new best f([14.9981689453125, 12.0079345703125]) = 197.334616
>17, new best f([14.999771118164062, 12.008544921875]) = 197.379506
>19, new best f([14.999771118164062, 12.003662109375]) = 197.404884
>23, new best f([14.999771118164062, 12.00244140625]) = 197.411228
>25, new best f([14.999771118164062, 12.001220703125]) = 197.417571
>26, new best f([14.999771118164062, 12.0]) = 197.423914
Done!
f([14.999771118164062, 12.0]) = 197.423914
EXPERIMENT NO:9
Aim:
Theory:
• Artificial neural networks (ANNs) provide a general, practical method for learning
realvalued,
discrete-valued, and vector-valued functions from examples.
• Algorithms such as BACKPROPAGATION gradient descent to tune network
parameters to
best fit a training set of input-output pairs.
• ANN learning is robust to errors in the training data and has been successfully
applied to
problems such as interpreting visual scenes, speech recognition, and learning robot
control
strategies.
Backpropogation
algorithm
MRC
1. Create a feed-forward network with ni inputs, nhidden hidden units, and nout
output units.
2. Initialize each wi to some small random value (e.g., between -.05 and .05).
3. Until the termination condition is met, do
For each training example <(x1,…xn),t>, do
// Propagate the input forward through the network:
a. Input the instance (x1, ..,xn) to the n/w & compute the n/w outputs ok for every
unit
// Propagate the errors backward through the network:
b. For each output unit k, calculate its error term k; k = ok(1-ok)(tk-ok)
c. For each hidden unit h, calculate its error term h; h=oh(1-oh) k wh,k k
d. For each network weight wi,j do; wi,j=wi,j+ wi,j where wi,j= j xi,j
PROCEDURE / PROGRAMME
X = np.array(([2, 9], [1, 5], [3, 6]), dtype=float) # Features ( Hrs Slept, Hrs Studied) y
X = X/np.amax(X,axis=0) # Normalize
y = y/100
def sigmoid(x):
return 1/(1+np.exp(-x))
def sigmoid_grad(x):
return x * (1 - x)
# Variable initialization
rate (eta)
wh=np.random.uniform(size=(input_neurons,hidden_neurons)) # 2x3
bh=np.random.uniform(size=(1,hidden_neurons)) # 1x3
wout=np.random.uniform(size=(hidden_neurons,output_neurons)) # 1x1
MRC
bout=np.random.uniform(size=(1,output_neurons))
for i in range(epoch):
#Forward Propogation
o_ip=np.dot(h_act,wout) + bout
output = sigmoid(o_ip)
#Backpropagation
outgrad = sigmoid_grad(output)
d_hidden = Eh * hiddengrad
wh += X.T.dot(d_hidden) *eta
Output
Normalized Input:
[[0.66666667 1. ]
[0.33333333 0.55555556]
[1. 0.66666667]]
MRC
Actual Output:
[[0.92]
[0.86]
[0.89]]
Predicted Output:
[[0.77295493]
[0.75973893]
[0.78167013]]
MRC