diff --git a/CHANGELOG.md b/CHANGELOG.md index 1db26ee..0da1755 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/ai/linear_model/__init__.py b/ai/linear_model/__init__.py index d5ebf3b..3310908 100644 --- a/ai/linear_model/__init__.py +++ b/ai/linear_model/__init__.py @@ -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 diff --git a/ai/linear_model/perceptron.py b/ai/linear_model/perceptron.py index cc94df3..aff77bf 100644 --- a/ai/linear_model/perceptron.py +++ b/ai/linear_model/perceptron.py @@ -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 @@ -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): @@ -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. diff --git a/examples/linear_model/perceptron.py b/examples/linear_model/perceptron.py new file mode 100644 index 0000000..afe838a --- /dev/null +++ b/examples/linear_model/perceptron.py @@ -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()