0% found this document useful (0 votes)
3 views5 pages

DL Lab PRGS

Uploaded by

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

DL Lab PRGS

Uploaded by

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

Task1:

import numpy as np
frommatplotlib import pyplot as plt
import pandas as pd
frommatplotlib.colors import ListedColormap

# Creation of the main perceptron object.


class Perceptron(object):
#Initiating the learning rate and number of iterations.
def __init__(self, Learn_Rate=0.5, Iterations=10):
self.learn_rate = Learn_Rate
self.Iterations = Iterations
self.errors = []
self.weights = np.zeros(1 + x.shape[1])

# Defining fit method for model training.

def fit(self, x, y):


self.weights = np.zeros(1 + x.shape[1])
fori in range(self.Iterations):
error = 0
for xi, target in zip(x, y):
update = self.learn_rate * (target - self.predict(xi))
self.weights[1:] += update*xi
self.weights[0] += update
error += int(update != 0)

1
self.errors.append(error)
return self

# Net Input method for summing the given matrix inputs and their corresponding weights.
defnet_input(self, x):
return np.dot(x, self.weights[1:]) + self.weights[0]

# Predict method for predicting the classification of data inputs.


def predict(self, x):
returnnp.where(self.net_input(x) >= 0.0, 1, -1)

# Data retrieval and preperation.


y = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data",
header=None)
x = y.iloc[0:100, [0, 2]].values
plt.scatter(x[:50, 0], x[:50, 1], color='red')
plt.scatter(x[50:100, 0], x[50:100, 1], color='blue')
plt.scatter(x[100:150, 0], x[100:150, 1], color='yellow')
plt.show()
y = y.iloc[0:100, 4].values
y = np.where(y == 'Iris-setosa', -1, 1)

# Model training and evaluation.


Classifier = Perceptron (Learn_Rate=0.01, Iterations=50)
Classifier.fit(x, y)
plt.plot(range(1, len(Classifier.errors) + 1), Classifier.errors, marker='o')
plt.xlabel('Epochs')
plt.ylabel('Number of misclassifications')
plt.show()
defplot_decision_regions(X, y, classifier, resolution=0.02):
# setup marker generator and color map

2
markers = ('s', 'x', 'o', '^', 'v')
colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan')
cmap = ListedColormap(colors[:len(np.unique(y))])
# plot the decision surface
x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1
x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution),
np.arange(x2_min, x2_max, resolution))
Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T)
Z = Z.reshape(xx1.shape)
plt.contourf(xx1, xx2, Z, alpha=0.4, cmap=cmap)
plt.xlim(xx1.min(), xx1.max())
plt.ylim(xx2.min(), xx2.max())
# plot class samples
foridx, cl in enumerate(np.unique(y)):
plt.scatter(x=X[y == cl, 0], y=X[y == cl, 1],
alpha=0.8, c=cmap(idx),
marker=markers[idx], label=cl)

# Showing the final results of the perceptron model.


plot_decision_regions(x, y, classifier=Classifier)
plt.show()

Task2
# sigmoid function
import numpy as np
frommatplotlib import pyplot
def sig(x):
return 1/(1 + np.exp(-x))
ip=[x for x in range(-100,100)]
op=[sig(x) for x in ip]

3
pyplot.plot(ip,op)
pyplot.show()

#RELU FUNCTION
frommatplotlib import pyplot
def rectified(x):
return max(0.0,x)
ip=[x for x in range(-100,100)]
op=[rectified(x) for x in ip]
pyplot.plot(ip,op)
pyplot.show()
#TANH FUNCTION
from math import exp
frommatplotlib import pyplot
defmytanh(x):
return ( exp(x)-exp(-x)/exp(x)+exp(-x) )
ip=[x for x in range(-100,100)]
op=[mytanh(x) for x in ip]
pyplot.plot(ip,op)
pyplot.show()
#SOFTMAX FUNCTION
import numpy as np
frommatplotlib import pyplot
defsoftmax(x):
return (np.exp(x)/np.sum(np.exp(x)))
ip=[12.0,13.0,14.0]
op=softmax(ip)
print(op)

4
pyplot.plot(ip,op)
pyplot.show()
print(np.sum(op))

You might also like