|
| 1 | +# RBF Neural Network (RBFNN) on Iris Dataset |
| 2 | +# ------------------------------------------------------ |
| 3 | +# This script implements a Radial Basis Function Neural Network (RBFNN) |
| 4 | +# for classification tasks, using the Iris dataset as an example. |
| 5 | +# |
| 6 | +# Features: |
| 7 | +# - Uses KMeans to determine RBF centers. |
| 8 | +# - Applies Gaussian radial basis function as hidden layer activation. |
| 9 | +# - Trains output weights using least-squares fitting. |
| 10 | +# |
| 11 | +# Includes: |
| 12 | +# - Full training and prediction pipeline. |
| 13 | +# - Evaluation using classification accuracy. |
| 14 | +# ------------------------------------------------------ |
| 15 | + |
| 16 | +import numpy as np |
| 17 | +from sklearn.cluster import KMeans |
| 18 | +from scipy.spatial.distance import cdist |
| 19 | +from sklearn.datasets import load_iris |
| 20 | +from sklearn.preprocessing import StandardScaler, OneHotEncoder |
| 21 | +from sklearn.model_selection import train_test_split |
| 22 | +from sklearn.metrics import accuracy_score |
| 23 | + |
| 24 | +class RBFNN: |
| 25 | + def __init__(self, num_centers, gamma): |
| 26 | + # Initialize with number of RBF centers and spread parameter (gamma) |
| 27 | + self.num_centers = num_centers |
| 28 | + self.gamma = gamma |
| 29 | + self.centers = None |
| 30 | + self.weights = None |
| 31 | + |
| 32 | + def _rbf(self, X, centers): |
| 33 | + # Compute Gaussian RBF activations for inputs X given the centers |
| 34 | + dist = cdist(X, centers, 'euclidean') # Compute Euclidean distance to centers |
| 35 | + return np.exp(-self.gamma * (dist ** 2)) # Apply Gaussian function |
| 36 | + |
| 37 | + def train(self, x_data, y_data): |
| 38 | + # Train the RBFNN |
| 39 | + # Step 1: Use KMeans to find cluster centers for RBFs |
| 40 | + kmeans = KMeans(n_clusters=self.num_centers, random_state=0).fit(x_data) |
| 41 | + self.centers = kmeans.cluster_centers_ |
| 42 | + |
| 43 | + # Step 2: Compute RBF activations |
| 44 | + rbf_activations = self._rbf(x_data, self.centers) |
| 45 | + |
| 46 | + # Step 3: Solve output weights using least squares |
| 47 | + self.weights = np.linalg.pinv(rbf_activations).dot(y_data) |
| 48 | + |
| 49 | + def predict(self, x): |
| 50 | + # Predict using learned weights and RBF activations |
| 51 | + rbf_activations = self._rbf(x, self.centers) |
| 52 | + return rbf_activations.dot(self.weights) |
| 53 | + |
| 54 | +if __name__ == "__main__": |
| 55 | + # Load and preprocess Iris dataset |
| 56 | + iris = load_iris() |
| 57 | + X = iris.data # Feature matrix |
| 58 | + y = iris.target.reshape(-1, 1) # Labels |
| 59 | + |
| 60 | + # Standardize features |
| 61 | + scaler = StandardScaler() |
| 62 | + X_scaled = scaler.fit_transform(X) |
| 63 | + |
| 64 | + # One-hot encode target labels for multi-class classification |
| 65 | + encoder = OneHotEncoder(sparse_output=False) |
| 66 | + y_encoded = encoder.fit_transform(y) |
| 67 | + |
| 68 | + # Split data into training and testing sets |
| 69 | + X_train, X_test, y_train, y_test = train_test_split(X_scaled, y_encoded, test_size=0.2, random_state=42) |
| 70 | + |
| 71 | + # Initialize and train the RBF Neural Network |
| 72 | + rbfnn = RBFNN(num_centers=10, gamma=1.0) |
| 73 | + rbfnn.train(X_train, y_train) |
| 74 | + |
| 75 | + # Predict on test set |
| 76 | + y_pred_probs = rbfnn.predict(X_test) |
| 77 | + y_pred = np.argmax(y_pred_probs, axis=1) |
| 78 | + y_true = np.argmax(y_test, axis=1) |
| 79 | + |
| 80 | + # Evaluate accuracy |
| 81 | + accuracy = accuracy_score(y_true, y_pred) |
| 82 | + print(f"Classification Accuracy: {accuracy:.4f}") |
0 commit comments