Skip to content

Commit 393504f

Browse files
committed
Added the RBFNN as mentioned in the issue TheAlgorithms#12322
1 parent 74b540a commit 393504f

File tree

5 files changed

+105
-0
lines changed

5 files changed

+105
-0
lines changed

Diff for: RBFNN/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# RBF Neural Network (RBFNN)
2+
3+
A simple and efficient implementation of Radial Basis Function Neural Network using NumPy and scikit-learn.
4+
5+
## 📁 Structure
6+
- `rbfnn/`: RBFNN model implementation
7+
- `examples/`: Regression and classification demos
8+
- `requirements.txt`: Install dependencies
9+
- `README.md`: Project overview
10+
11+
## 🚀 Usage
12+
13+
### Install dependencies
14+
```bash
15+
pip install -r requirements.txt
16+
```
17+
### Run regression example
18+
```bash
19+
python examples/regression_example.py
20+
```
21+
### Run classification example
22+
```bash
23+
python examples/classification_example.py
24+
```

Diff for: RBFNN/model.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import numpy as np
2+
from sklearn.cluster import KMeans
3+
from numpy.linalg import pinv
4+
5+
class RBFNN:
6+
def __init__(self, num_centers=10, gamma=1.0):
7+
self.num_centers = num_centers
8+
self.gamma = gamma
9+
self.centers = None
10+
self.weights = None
11+
12+
def _rbf(self, x, center):
13+
return np.exp(-self.gamma * np.linalg.norm(x - center) ** 2)
14+
15+
def _compute_activations(self, X):
16+
G = np.zeros((X.shape[0], self.num_centers))
17+
for i, x in enumerate(X):
18+
for j, c in enumerate(self.centers):
19+
G[i, j] = self._rbf(x, c)
20+
return G
21+
22+
def train(self, X, y):
23+
kmeans = KMeans(n_clusters=self.num_centers, random_state=0).fit(X)
24+
self.centers = kmeans.cluster_centers_
25+
G = self._compute_activations(X)
26+
self.weights = pinv(G).dot(y)
27+
28+
def predict(self, X):
29+
G = self._compute_activations(X)
30+
return G.dot(self.weights)

Diff for: RBFNN/requirements.txt

Whitespace-only changes.

Diff for: RBFNN/tests/classification_example.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from sklearn.datasets import load_iris
2+
from sklearn.model_selection import train_test_split
3+
from sklearn.preprocessing import StandardScaler, OneHotEncoder
4+
from sklearn.metrics import accuracy_score
5+
from rbfnn.model import RBFNN
6+
import numpy as np
7+
8+
data = load_iris()
9+
X = data.data
10+
y = data.target.reshape(-1, 1)
11+
12+
encoder = OneHotEncoder(sparse_output=False)
13+
y_encoded = encoder.fit_transform(y)
14+
15+
scaler = StandardScaler()
16+
X_scaled = scaler.fit_transform(X)
17+
18+
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y_encoded, test_size=0.3)
19+
20+
model = RBFNN(num_centers=10, gamma=1.0)
21+
model.train(X_train, y_train)
22+
23+
y_pred_probs = model.predict(X_test)
24+
y_pred = np.argmax(y_pred_probs, axis=1)
25+
y_true = np.argmax(y_test, axis=1)
26+
27+
print("Classification Accuracy:", accuracy_score(y_true, y_pred))

Diff for: RBFNN/tests/regression_example.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
from sklearn.model_selection import train_test_split
4+
from rbfnn.model import RBFNN
5+
6+
# Generate sine wave data
7+
X = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)
8+
y = np.sin(X).ravel()
9+
10+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
11+
12+
# Train model
13+
model = RBFNN(num_centers=10, gamma=1.0)
14+
model.train(X_train, y_train)
15+
16+
# Predict
17+
y_pred = model.predict(X_test)
18+
19+
# Plot
20+
plt.scatter(X_test, y_test, label='True')
21+
plt.scatter(X_test, y_pred, label='Predicted', color='red', marker='x')
22+
plt.title("RBFNN Regression - Sine Function")
23+
plt.legend()
24+
plt.show()

0 commit comments

Comments
 (0)