From 393504fdcbe3133f35d56d0acc8741eae844850a Mon Sep 17 00:00:00 2001 From: oxBinaryBrain Date: Mon, 7 Apr 2025 19:05:08 +0530 Subject: [PATCH 1/3] Added the RBFNN as mentioned in the issue #12322 --- RBFNN/README.md | 24 +++++++++++++++++++++ RBFNN/model.py | 30 +++++++++++++++++++++++++++ RBFNN/requirements.txt | 0 RBFNN/tests/classification_example.py | 27 ++++++++++++++++++++++++ RBFNN/tests/regression_example.py | 24 +++++++++++++++++++++ 5 files changed, 105 insertions(+) create mode 100644 RBFNN/README.md create mode 100644 RBFNN/model.py create mode 100644 RBFNN/requirements.txt create mode 100644 RBFNN/tests/classification_example.py create mode 100644 RBFNN/tests/regression_example.py diff --git a/RBFNN/README.md b/RBFNN/README.md new file mode 100644 index 000000000000..d0e9dccde041 --- /dev/null +++ b/RBFNN/README.md @@ -0,0 +1,24 @@ +# RBF Neural Network (RBFNN) + +A simple and efficient implementation of Radial Basis Function Neural Network using NumPy and scikit-learn. + +## 📁 Structure +- `rbfnn/`: RBFNN model implementation +- `examples/`: Regression and classification demos +- `requirements.txt`: Install dependencies +- `README.md`: Project overview + +## 🚀 Usage + +### Install dependencies +```bash +pip install -r requirements.txt +``` +### Run regression example +```bash +python examples/regression_example.py +``` +### Run classification example +```bash +python examples/classification_example.py +``` \ No newline at end of file diff --git a/RBFNN/model.py b/RBFNN/model.py new file mode 100644 index 000000000000..3430afcc7ece --- /dev/null +++ b/RBFNN/model.py @@ -0,0 +1,30 @@ +import numpy as np +from sklearn.cluster import KMeans +from numpy.linalg import pinv + +class RBFNN: + def __init__(self, num_centers=10, gamma=1.0): + self.num_centers = num_centers + self.gamma = gamma + self.centers = None + self.weights = None + + def _rbf(self, x, center): + return np.exp(-self.gamma * np.linalg.norm(x - center) ** 2) + + def _compute_activations(self, X): + G = np.zeros((X.shape[0], self.num_centers)) + for i, x in enumerate(X): + for j, c in enumerate(self.centers): + G[i, j] = self._rbf(x, c) + return G + + def train(self, X, y): + kmeans = KMeans(n_clusters=self.num_centers, random_state=0).fit(X) + self.centers = kmeans.cluster_centers_ + G = self._compute_activations(X) + self.weights = pinv(G).dot(y) + + def predict(self, X): + G = self._compute_activations(X) + return G.dot(self.weights) diff --git a/RBFNN/requirements.txt b/RBFNN/requirements.txt new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/RBFNN/tests/classification_example.py b/RBFNN/tests/classification_example.py new file mode 100644 index 000000000000..8e42cb75be95 --- /dev/null +++ b/RBFNN/tests/classification_example.py @@ -0,0 +1,27 @@ +from sklearn.datasets import load_iris +from sklearn.model_selection import train_test_split +from sklearn.preprocessing import StandardScaler, OneHotEncoder +from sklearn.metrics import accuracy_score +from rbfnn.model import RBFNN +import numpy as np + +data = load_iris() +X = data.data +y = data.target.reshape(-1, 1) + +encoder = OneHotEncoder(sparse_output=False) +y_encoded = encoder.fit_transform(y) + +scaler = StandardScaler() +X_scaled = scaler.fit_transform(X) + +X_train, X_test, y_train, y_test = train_test_split(X_scaled, y_encoded, test_size=0.3) + +model = RBFNN(num_centers=10, gamma=1.0) +model.train(X_train, y_train) + +y_pred_probs = model.predict(X_test) +y_pred = np.argmax(y_pred_probs, axis=1) +y_true = np.argmax(y_test, axis=1) + +print("Classification Accuracy:", accuracy_score(y_true, y_pred)) diff --git a/RBFNN/tests/regression_example.py b/RBFNN/tests/regression_example.py new file mode 100644 index 000000000000..352902e15bf6 --- /dev/null +++ b/RBFNN/tests/regression_example.py @@ -0,0 +1,24 @@ +import numpy as np +import matplotlib.pyplot as plt +from sklearn.model_selection import train_test_split +from rbfnn.model import RBFNN + +# Generate sine wave data +X = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1) +y = np.sin(X).ravel() + +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) + +# Train model +model = RBFNN(num_centers=10, gamma=1.0) +model.train(X_train, y_train) + +# Predict +y_pred = model.predict(X_test) + +# Plot +plt.scatter(X_test, y_test, label='True') +plt.scatter(X_test, y_pred, label='Predicted', color='red', marker='x') +plt.title("RBFNN Regression - Sine Function") +plt.legend() +plt.show() From 7f2ddd8e6bac378047181436af73fe943d5d6ef3 Mon Sep 17 00:00:00 2001 From: oxBinaryBrain Date: Mon, 7 Apr 2025 19:08:12 +0530 Subject: [PATCH 2/3] model.py renamed to radial_basis_function_network.py --- RBFNN/{model.py => radial_basis_function_network.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename RBFNN/{model.py => radial_basis_function_network.py} (100%) diff --git a/RBFNN/model.py b/RBFNN/radial_basis_function_network.py similarity index 100% rename from RBFNN/model.py rename to RBFNN/radial_basis_function_network.py From ec3ddb2cdf668ca32fb0e976929a5e78d2e3591f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 7 Apr 2025 13:43:11 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- RBFNN/radial_basis_function_network.py | 1 + RBFNN/tests/regression_example.py | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/RBFNN/radial_basis_function_network.py b/RBFNN/radial_basis_function_network.py index 3430afcc7ece..0226bdfa7dd1 100644 --- a/RBFNN/radial_basis_function_network.py +++ b/RBFNN/radial_basis_function_network.py @@ -2,6 +2,7 @@ from sklearn.cluster import KMeans from numpy.linalg import pinv + class RBFNN: def __init__(self, num_centers=10, gamma=1.0): self.num_centers = num_centers diff --git a/RBFNN/tests/regression_example.py b/RBFNN/tests/regression_example.py index 352902e15bf6..235c54ec4205 100644 --- a/RBFNN/tests/regression_example.py +++ b/RBFNN/tests/regression_example.py @@ -17,8 +17,8 @@ y_pred = model.predict(X_test) # Plot -plt.scatter(X_test, y_test, label='True') -plt.scatter(X_test, y_pred, label='Predicted', color='red', marker='x') +plt.scatter(X_test, y_test, label="True") +plt.scatter(X_test, y_pred, label="Predicted", color="red", marker="x") plt.title("RBFNN Regression - Sine Function") plt.legend() plt.show()