Skip to content

Add RBF Neural Network Algorithm (#12322) #12659

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions RBFNN/README.md
Original file line number Diff line number Diff line change
@@ -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
```
31 changes: 31 additions & 0 deletions RBFNN/radial_basis_function_network.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import numpy as np

Check failure on line 1 in RBFNN/radial_basis_function_network.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (INP001)

RBFNN/radial_basis_function_network.py:1:1: INP001 File `RBFNN/radial_basis_function_network.py` is part of an implicit namespace package. Add an `__init__.py`.
from sklearn.cluster import KMeans
from numpy.linalg import pinv

Check failure on line 3 in RBFNN/radial_basis_function_network.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

RBFNN/radial_basis_function_network.py:1:1: I001 Import block is un-sorted or un-formatted


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):

Check failure on line 16 in RBFNN/radial_basis_function_network.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N803)

RBFNN/radial_basis_function_network.py:16:36: N803 Argument name `X` should be lowercase
G = np.zeros((X.shape[0], self.num_centers))

Check failure on line 17 in RBFNN/radial_basis_function_network.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

RBFNN/radial_basis_function_network.py:17:9: N806 Variable `G` in function should be lowercase
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):

Check failure on line 23 in RBFNN/radial_basis_function_network.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N803)

RBFNN/radial_basis_function_network.py:23:21: N803 Argument name `X` should be lowercase
kmeans = KMeans(n_clusters=self.num_centers, random_state=0).fit(X)
self.centers = kmeans.cluster_centers_
G = self._compute_activations(X)

Check failure on line 26 in RBFNN/radial_basis_function_network.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

RBFNN/radial_basis_function_network.py:26:9: N806 Variable `G` in function should be lowercase
self.weights = pinv(G).dot(y)

def predict(self, X):

Check failure on line 29 in RBFNN/radial_basis_function_network.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N803)

RBFNN/radial_basis_function_network.py:29:23: N803 Argument name `X` should be lowercase
G = self._compute_activations(X)

Check failure on line 30 in RBFNN/radial_basis_function_network.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

RBFNN/radial_basis_function_network.py:30:9: N806 Variable `G` in function should be lowercase
return G.dot(self.weights)
Empty file added RBFNN/requirements.txt
Empty file.
27 changes: 27 additions & 0 deletions RBFNN/tests/classification_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from sklearn.datasets import load_iris

Check failure on line 1 in RBFNN/tests/classification_example.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (INP001)

RBFNN/tests/classification_example.py:1:1: INP001 File `RBFNN/tests/classification_example.py` is part of an implicit namespace package. Add an `__init__.py`.
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

Check failure on line 6 in RBFNN/tests/classification_example.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

RBFNN/tests/classification_example.py:1:1: I001 Import block is un-sorted or un-formatted

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))
24 changes: 24 additions & 0 deletions RBFNN/tests/regression_example.py
Original file line number Diff line number Diff line change
@@ -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()
Loading