0% found this document useful (0 votes)
11 views4 pages

Ipynb - Colab01

The document provides an implementation of a Perceptron and Adaline classifiers using Python and NumPy, detailing their parameters, methods for fitting training data, and predicting class labels. It also includes data visualization for the Iris dataset, demonstrating the classifiers' performance with different learning rates. The code is structured for use in a Jupyter Notebook environment, specifically Google Colab.

Uploaded by

androdragon0311
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)
11 views4 pages

Ipynb - Colab01

The document provides an implementation of a Perceptron and Adaline classifiers using Python and NumPy, detailing their parameters, methods for fitting training data, and predicting class labels. It also includes data visualization for the Iris dataset, demonstrating the classifiers' performance with different learning rates. The code is structured for use in a Jupyter Notebook environment, specifically Google Colab.

Uploaded by

androdragon0311
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/ 4

12/28/24, 2:11 PM Untitled0.

ipynb - Colab

#Perceptron

import numpy as np
class Perceptron(object):
"""Perceptron classifier.

Parameters

eta : float
Learning rate (between 0.0 and 1.0)
n_iter : int
Passes over the training dataset.
random_state : int
Random number generator seed for random weight
initialization.

Attributes

w_ : 1d-array
Weights after fitting.
errors_ : list
Number of misclassifications (updates) in each epoch.

"""
def init (self, eta=0.01, n_iter=50, random_state=1):
self.eta = eta
self.n_iter = n_iter
self.random_state = random_state

def fit(self, X, y):


"""Fit training data.

Parameters

X : {array-like}, shape = [n_examples, n_features]


Training vectors, where n_examples is the number of
examples and n_features is the number of features.
y : array-like, shape = [n_examples]
Target values.

Returns

self : object

"""
rgen = np.random.RandomState(self.random_state)
self.w_ = rgen.normal(loc=0.0, scale=0.01,size=1 + X.shape[1])
self.errors_ = []

for _ in range(self.n_iter):
errors = 0
for xi, target in zip(X, y):
update = self.eta * (target - self.predict(xi))
self.w_[1:] += update * xi
self.w_[0] += update
errors += int(update != 0.0)
self.errors_.append(errors)
return self

def net_input(self, X):


"""Calculate net input"""
return np.dot(X, self.w_[1:]) + self.w_[0]

def predict(self, X):


"""Return class label after unit step"""
return np.where(self.net_input(X) >= 0.0, 1, -1)

v1 = np.array([1, 2, 3])
v2 = 0.5 * v1
np.arccos(v1.dot(v2) / (np.linalg.norm(v1) * np.linalg.norm(v2)))

0.0

https://colab.research.google.com/drive/16gZKY_Jp46_P6ZLc-5N97lCut7QODAag#scrollTo=5TORI1ioZi7x&printMode=true 1/4
12/28/24, 2:11 PM Untitled0.ipynb - Colab
import os
import pandas as pd
s = os.path.join('https://archive.ics.uci.edu', 'ml','machine-learning-databases','iris','iris.data')
print('URL:', s)
URL:"https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
df = pd.read_csv(s,header=None,encoding='utf-8')
df.tail()

URL: https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data
0 1 2 3 4

145 6.7 3.0 5.2 2.3 Iris-virginica

146 6.3 2.5 5.0 1.9 Iris-virginica

147 6.5 3.0 5.2 2.0 Iris-virginica

148 6.2 3.4 5.4 2.3 Iris-virginica

149 5 9 3 0 5 1 1 8 Iris-virginica

df = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data',header=None, encoding='utf-8')

import matplotlib.pyplot as plt


import numpy as np
# select setosa and versicolor
y = df.iloc[0:100, 4].values
y = np.where(y == 'Iris-setosa', -1, 1)
# extract sepal length and petal length
X = df.iloc[0:100, [0, 2]].values
# plot data
plt.scatter(X[:50, 0], X[:50, 1], color='red', marker='o', label='setosa')
plt.scatter(X[50:100, 0], X[50:100, 1], color='blue', marker='x', label='versicolor')
plt.xlabel('sepal length [cm]')
plt.ylabel('petal length [cm]')
plt.legend(loc='upper left')
plt.show()

ppn = Perceptron(eta=0.1, n_iter=10)


ppn.fit(X, y)
plt.plot(range(1, len(ppn.errors_) + 1), ppn.errors_, marker='o')
plt.xlabel('Epochs')
plt.ylabel('Number of updates')
plt.show()

https://colab.research.google.com/drive/16gZKY_Jp46_P6ZLc-5N97lCut7QODAag#scrollTo=5TORI1ioZi7x&printMode=true 2/4
12/28/24, 2:11 PM Untitled0.ipynb - Colab

#Adaline

class AdalineGD(object):
"""ADAptive LInear NEuron classifier.

Parameters

eta : float
Learning rate (between 0.0 and 1.0)
n_iter : int
Passes over the training dataset.
random_state : int
Random number generator seed for random weight initialization.

Attributes

w_ : 1d-array
Weights after fitting.
cost_ : list
Sum-of-squares cost function value in each epoch.

"""
def init (self, eta=0.01, n_iter=50, random_state=1):
self.eta = eta
self.n_iter = n_iter
self.random_state = random_state
def fit(self, X, y):
""" Fit training data.

Parameters

X : {array-like}, shape = [n_examples, n_features]


Training vectors, where n_examples
is the number of examples and
n_features is the number of features.
y : array-like, shape = [n_examples]
Target values.

Returns
self : object

"""
rgen = np.random.RandomState(self.random_state)
self.w_ = rgen.normal(loc=0.0, scale=0.01, size=1 + X.shape[1])
self.cost_ = []

for i in range(self.n_iter):
net_input = self.net_input(X)
output = self.activation(net_input)
errors = (y - output)
self.w_[1:] += self.eta * X.T.dot(errors)
self.w_[0] += self.eta * errors.sum()
cost = (errors**2).sum() / 2.0
self.cost_.append(cost)
return self

def net_input(self, X):


"""Calculate net input"""

https://colab.research.google.com/drive/16gZKY_Jp46_P6ZLc-5N97lCut7QODAag#scrollTo=5TORI1ioZi7x&printMode=true 3/4
12/28/24, 2:11 PM Untitled0.ipynb - Colab
return np.dot(X, self.w_[1:]) + self.w_[0]

def activation(self, X):


"""Compute linear activation"""
return X

def predict(self, X):


"""Return class label after unit step"""
return np.where(self.activation(self.net_input(X)) >= 0.0, 1, -1)

import matplotlib.pyplot as plt


import numpy as np
fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(10, 4))
ada1 = AdalineGD(n_iter=10, eta=0.01).fit(X, y)
ax[0].plot(range(1, len(ada1.cost_) + 1), np.log10(ada1.cost_), marker='o')
ax[0].set_xlabel('Epochs')
ax[0].set_ylabel('log(Sum-squared-error)')
ax[0].set_title('Adaline - Learning rate 0.01')
ada2 = AdalineGD(n_iter=10, eta=0.0001).fit(X, y)
ax[1].plot(range(1, len(ada2.cost_) + 1), ada2.cost_, marker='o')
ax[1].set_xlabel('Epochs')
ax[1].set_ylabel('Sum-squared-error')
ax[1].set_title('Adaline - Learning rate 0.0001')
plt.show()

https://colab.research.google.com/drive/16gZKY_Jp46_P6ZLc-5N97lCut7QODAag#scrollTo=5TORI1ioZi7x&printMode=true 4/4

You might also like