Exp - 4 - 5 (Prakash)
Exp - 4 - 5 (Prakash)
Exp - 4 - 5 (Prakash)
Multiple layer perceptron are effectively applied to handle tricky problems if trained with a vastly
accepted algorithm identified as the back-propagation algorithm (error) in a supervised manner.
It functions on learning law with error-correction. It is also a simplified version for the least mean
square (LMS) filtering algorithm which is equally popular to error back-propagation algorithm. In Error
back- propagation training there are two computational passes via several network layers:
In forward pass, vector input is applied to the nodes of the system propagating each layer„s outcome
to
the next layer via network. To get the accurate response of the network, these outputs pass on from
several layers and arrive at a set of outputs. In forward pass network weights are permanent. On other
hand in the backward pass, weights are adjusted according to rule for error correction. Error signal is
the actual response of the network minus the desired response. The propagation of this error signal
through the network is towards backward in direction opposite to the connections of synaptic. The
move
the real response of network closer to the favored response, tuning of weights is to be done. There are
three unique features of a multilayer perception:
1. For each neuron in any system, its illustration has an activation function that is non-linear. The
logistical function is used to define a function which is sigmoid.
2. There are layer(s) of hidden neurons not contained in the input or the output present in the
neural network. The study over complex tasks is facilitated by these hidden neurons.
3. Connectivity degree is high in network. Weight's population should be changed if there is a
requirement to alter the connectivity of the network.
The stochastic gradient descent version of the BACKPROPAGATION algorithm for feed forward
networks containing two layers of sigmoid units.
Step 1: begins by constructing a network with the desired number of hidden and output units and
initializing all network weights to small random values. . For each training example, it applies the
network to the example, calculates the error of the network output for this example, computes the
gradient with respect to the error on this example, then updates all weights in the network. This
gradient
descent step is iterated (often thousands of times, using the same training examples multiple times)
until
the network performs acceptably well.
Step 2: The gradient descent weight-update rule is similar to the delta training rule The only difference
is that the error (t - o) in the delta rule is replaced by a more complex error term aj.
Nikhil Shukla
21003215301112
Step 3: updates weights incrementally, following the Presentation of each training example. This
corresponds to a stochastic approxi- mation to gradient descent. To obtain the true gradient of E one
would sum the Sj, xji values over all training examples before altering weight values.
Step 4: The weight-update loop in BACKPROPAGATION may be iterated thousands of times in a
typical application. A variety of termination conditions can be used to halt the procedure.
One may choose to halt after a fixed number of iterations through the loop, or once the error on the
training examples falls below some threshold.
PROGRAM:
import random
network = list()
hidden_layer = [{'weights':[random.uniform(-0.5,0.5) for i in range(n_inputs + 1)]} for i in range(n_hidden)]
network.append(hidden_layer)
network.append(output_layer)
i= 1
j=j+1
i=i+1
return network
for i in range(len(weights)-1):
return activation
def transfer(activation):
Nikhil Shukla
2100321530112
return 1.0 / (1.0 + exp(-activation))
neuron['output'] = transfer(activation)
new_inputs.append(neuron['output'])
inputs = new_inputs
return inputs
def transfer_derivative(output):
return output * (1.0 - output)
for i in reversed(range(len(network))):
layer = network[i]
errors = list()
if i != len(network)-1:
for j in range(len(layer)):
error = 0.0
errors.append(error)
else:
for j in range(len(layer)):
neuron = layer[j]
errors.append(expected[j] - neuron['output'])
for j in range(len(layer)):
neuron = layer[j]
Nikhil Shukla
2100321530112
def update_weights(network, row, l_rate):
for i in range(len(network)):
inputs = row[:-1]
if i != 0:
sum_error = 0
expected[row[-1]] = 1
backward_propagate_error(network, expected)
update_weights(network, row, l_rate)
seed(2)
dataset = [[2.7810836,2.550537003,0],
[1.465489372,2.362125076,0],
[3.396561688,4.400293529,0],
[1.38807019,1.850220317,0],
[3.06407232,3.005305973,0],
[7.627531214,2.759262235,1],
[5.332441248,2.088626775,1],
[6.922596716,1.77106367,1],
[8.675418651,-0.242068655,1],
Nikhil Shukla
2100321530112
[7.673756466,3.508563011,1]]
n_inputs = len(dataset[0]) - 1
#Network Initialization
j=1
j=j+1
i=i+1
OUTPUT:
The input Data Set :
[[2.7810836, 2.550537003, 0], [1.465489372, 2.362125076, 0], [3.396561688, 4.400293529, 0], [1.38807019,
1.850220317, 0], [3.06407232, 3.005305973, 0], [7.627531214, 2.759262235, 1], [5.332441248, 2.088626775,
1], [6.922596716, 1.77106367, 1], [8.675418651, -0.242068655, 1], [7.673756466, 3.508563011, 1]]
Number of Inputs :
Number of Outputs :
2
The initialised Neural Network:
Layer[1] Node[1]:
Layer[1] Node[2]:
Layer[2] Node[1]:
Layer[2] Node[2]:
Nikhil Shukla
2100321530112
{'weights': [0.10680173364083789, 0.08120401711200309, -0.3416171297451944]}
Layer[1] Node[1]:
{'weights': [0.8642508164347665, -0.8497601716670763, -0.8668929014392035], 'output':
0.9295587965836384, 'delta': 0.005645382825629247}
Layer[1] Node[2]:
Nikhil Shukla
2100321530112
EXPERIMENT – 5
OBJECTIVE: Write a program to implement the naïve Bayesian classifier for a sample training data
set stored as a .CSV file. Compute the accuracy of the classifier, considering few test data sets.
PURPOSE: It is a classification technique based on Bayes? Theorem with an assumption of
independence among predictors. In simple terms, a Naive Bayes classifier assumes that the presence of
a particular feature in a class is unrelated to the presence of any other feature. For example, a fruit may
be considered to be an apple if it is red, round, and about 3 inches in diameter. Even if these features
depend on each other or upon the existence of the other features, all of these properties independently
contribute to the probability that this fruit is an apple and that is why it is known as „Naive?.
Bayes theorem provides a way of calculating posterior probability P(c|x) from P(c), P(x) and P(x|c).
Look at the equation below:
Handling Of Data:
load the data from the CSV file and split in to training and test data set.
Nikhil Shukla
210032153012
# 1.Data Handling
# 1.1 Loading the Data from csv file of Pima indians diabetes dataset.
def loadcsv(filename):
lines = csv.reader(open(filename, "r"))
dataset = list(lines)
for i in range(len(dataset)):
# converting the attributes from string to floating point numbers
dataset[i] = [float(x) for x in dataset[i]]
return dataset
#1.2 Splitting the Data set into Training Set
def splitDataset(dataset, splitRatio):
trainSize = int(len(dataset) * splitRatio)
trainSet = []
copy = list(dataset)
while len(trainSet) < trainSize:
index = random.randrange(len(copy)) # random index
trainSet.append(copy.pop(index))
return [trainSet, copy]
def separateByClass(dataset):
separated = {}
for i in range(len(dataset)):
vector = dataset[i]
if (vector[-1] not in separated):
separated[vector[-1]] = []
separated[vector[-1]].append(vector)
return separated
def mean(numbers):
return sum(numbers)/float(len(numbers))
def stdev(numbers):
avg = mean(numbers)
variance = sum([pow(x-avg,2) for x in numbers])/float(len(numbers)-1)
return math.sqrt(variance)
def summarize(dataset):
summaries = [(mean(attribute), stdev(attribute)) for attribute in zip(*dataset)]
del summaries[-1]
return summaries
def summarizeByClass(dataset):
separated = separateByClass(dataset)
summaries = {}
for classValue, instances in separated.items():
summaries[classValue] = summarize(instances)
return summaries
def calculateProbability(x, mean, stdev):
exponent = math.exp(-(math.pow(x-mean,2)/(2*math.pow(stdev,2))))
return (1 / (math.sqrt(2*math.pi) * stdev)) * exponent
#3.2 Calculate Class Probabilities
def calculateClassProbabilities(summaries, inputVector):
probabilities = {}
for classValue, classSummaries in summaries.items():
probabilities[classValue] = 1
for i in range(len(classSummaries)):
mean, stdev = classSummaries[i]
x = inputVector[i]
probabilities[classValue] *= calculateProbability(x, mean, stdev)
return probabilities
Nikhil Shukla
2100321530112
def predict(summaries, inputVector):
probabilities = calculateClassProbabilities(summaries, inputVector)
bestLabel, bestProb = None, -1
for classValue, probability in probabilities.items():
if bestLabel is None or probability > bestProb:
bestProb = probability
bestLabel = classValue
return bestLabel
def getPredictions(summaries, testSet):
predictions = []
for i in range(len(testSet)):
result = predict(summaries, testSet[i])
predictions.append(result)
return predictions
#5. Computing Accuracy
def getAccuracy(testSet, predictions):
correct = 0
for i in range(len(testSet)):
if testSet[i][-1] == predictions[i]:
correct += 1
return (correct/float(len(testSet))) * 100.0
#Main Function
def main():
filename = 'ConceptLearning.csv'
splitRatio = 0.67
dataset = loadcsv(filename)
print("\n The length of the Data Set : ",len(dataset))
print("\n The Data Set Splitting into Training and Testing \n")
trainingSet, testSet = splitDataset(dataset, splitRatio)
print('\n Number of Rows in Training Set:{0} rows'.format(len(trainingSet)))
print('\n Number of Rows in Testing Set:{0} rows'.format(len(testSet)))
print("\n First Five Rows of Training Set:\n")
for i in range(0,5):
print(trainingSet[i],"\n")
print("\n First Five Rows of Testing Set:\n")
for i in range(0,5):
print(testSet[i],"\n")
# prepare model
summaries = summarizeByClass(trainingSet)
print("\n Model Summaries:\n",summaries)
# test model
predictions = getPredictions(summaries, testSet)
print("\nPredictions:\n",predictions)
accuracy = getAccuracy(testSet, predictions)
print('\n Accuracy: {0}%'.format(accuracy))
main()
OUTPUT:
The length of the Data Set : 768
Nikhil Shukla
2100321530112
Number of Rows in Testing Set:254 rows
First Five Rows of Training Set:
Model Summaries:
{0.0: [(3.3474320241691844, 3.045635385378286), (111.54380664652568, 26.040069054720693),
(68.45921450151057, 18.15540652389224), (19.94561933534743, 14.709615608767137),
(71.50151057401813, 101.04863439385403), (30.863141993957708, 7.207208162103949),
(0.4341842900302116, 0.2960911906946818), (31.613293051359516, 12.100651311117689)], 1.0:
[(4.469945355191257, 3.7369440851983082), (139.3879781420765, 33.733070931373234),
(71.14754098360656, 20.694403393963842), (22.92896174863388, 18.151995092528765),
(107.97814207650273, 146.92526156736633), (35.28633879781422, 7.783342260348583),
(0.5569726775956286, 0.3942245334398509), (36.78688524590164, 11.174610282702282)]}
Predictions:
[0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0,
1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0,
1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0,
0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0,
1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0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