Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added "Perceptron" API - "single layer neural network" #21

Merged
merged 2 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# Changelog

## ai — 2024-12-28

### Added

- Added examples on "Perceptron" - single layer neural network ([#659fd15](https://www.github.com/joshiayush/ai/commit/659fd15))
- Added implementation of "Perceptron" - single layer neural network ([#c6afcc5](https://www.github.com/joshiayush/ai/commit/c6afcc5))
- Added `statistic` calculating functions to `ai.stats` module (#20) ([#ae05cf1](https://www.github.com/joshiayush/ai/commit/ae05cf1))

## docs — 2024-12-28

### Added

- Added a __Support Vector Machine__ model trained for face recognition ([#912c4c2](https://www.github.com/joshiayush/ai/commit/912c4c2))


## ai — 2024-01-04

### Added
Expand Down
2 changes: 2 additions & 0 deletions ai/linear_model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@

* `ai.linear_model.linear.LinearRegression`
* `ai.linear_model.logistic.LogisticRegression`
* `ai.linear_model.perceptron.Perceptron`
"""

from .linear import LinearRegression
from .logistic import LogisticRegression
from .perceptron import Perceptron
12 changes: 6 additions & 6 deletions ai/linear_model/perceptron.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __init__(self,
order to reach to a effecient prediction that minimizes the loss.
random_state: Seed to generate random weights and bias.
"""
self.aplha = alpha
self.alpha = alpha
self.n_iters = n_iters
self.random_state = random_state

Expand All @@ -65,16 +65,16 @@ def fit(self, X: np.ndarray, y: np.ndarray) -> 'Perceptron':
"""
rgen = np.random.RandomState(self.random_state)
self.weights = rgen.normal(loc=0.0, scale=0.01, size=X.shape[1])
self.b = np.float(0.)
self.errors = []
self.b_ = np.float64(0.)
self.errors_ = []
for _ in range(self.n_iters):
errors = 0
for xi, target in zip(X, y):
update = self.alpha * (target - self.predict(xi))
self.weights += update * xi
self.b += update
self.b_ += update
errors += int(update != 0.0)
self.errors.append(errors)
self.errors_.append(errors)
return self

def net_input(self, X):
Expand All @@ -87,7 +87,7 @@ def net_input(self, X):
Returns:
The dot product of `X` and `weights` plus `b`.
"""
return np.dot(X, self.weights) + self.b
return np.dot(X, self.weights) + self.b_

def predict(self, X):
"""Return class label after unit step.
Expand Down
51 changes: 51 additions & 0 deletions examples/linear_model/perceptron.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright 2023 The AI Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# pylint: disable=too-many-function-args, invalid-name, missing-module-docstring
# pylint: disable=missing-class-docstring

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

from ai.linear_model import Perceptron

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

# select setosa and versicolor
y = df.iloc[0:100, 4].values
y = np.where(y == "Iris-setosa", 0, 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(alpha=0.1, n_iters=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()
Loading