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/radial_basis_function_network.py b/RBFNN/radial_basis_function_network.py new file mode 100644 index 000000000000..0226bdfa7dd1 --- /dev/null +++ b/RBFNN/radial_basis_function_network.py @@ -0,0 +1,31 @@ +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..235c54ec4205 --- /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()