Skip to content

Add RBFNN implementation with Iris classification #12793

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
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
86 changes: 86 additions & 0 deletions neural_network/rbfnn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# RBF Neural Network (RBFNN) on Iris Dataset
# ------------------------------------------------------
# This script implements a Radial Basis Function Neural Network (RBFNN)
# for classification tasks, using the Iris dataset as an example.
#
# Features:
# - Uses KMeans to determine RBF centers.
# - Applies Gaussian radial basis function as hidden layer activation.
# - Trains output weights using least-squares fitting.
#
# Includes:
# - Full training and prediction pipeline.
# - Evaluation using classification accuracy.
# ------------------------------------------------------

import numpy as np
from scipy.spatial.distance import cdist
from sklearn.cluster import KMeans
from sklearn.datasets import load_iris
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder, StandardScaler


class RBFNN:
def __init__(self, num_centers, gamma):
# Initialize with number of RBF centers and spread parameter (gamma)
self.num_centers = num_centers
self.gamma = gamma
self.centers = None
self.weights = None

def _rbf(self, x, centers):
# Compute Gaussian RBF activations for inputs x given the centers
dist = cdist(x, centers, "euclidean") # Compute Euclidean distance to centers
return np.exp(-self.gamma * (dist**2)) # Apply Gaussian function

def train(self, x_data, y_data):
# Train the RBFNN
# Step 1: Use KMeans to find cluster centers for RBFs
kmeans = KMeans(n_clusters=self.num_centers, random_state=0).fit(x_data)
self.centers = kmeans.cluster_centers_

# Step 2: Compute RBF activations
rbf_activations = self._rbf(x_data, self.centers)

# Step 3: Solve output weights using least squares
self.weights = np.linalg.pinv(rbf_activations).dot(y_data)

def predict(self, x):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file neural_network/rbfnn.py, please provide doctest for the function predict

Please provide return type hint for the function: predict. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide descriptive name for the parameter: x

Please provide type hint for the parameter: x

# Predict using learned weights and RBF activations
rbf_activations = self._rbf(x, self.centers)
return rbf_activations.dot(self.weights)


if __name__ == "__main__":
# Load and preprocess Iris dataset
iris = load_iris()
x = iris.data # Feature matrix
y = iris.target.reshape(-1, 1) # Labels

# Standardize features
scaler = StandardScaler()
x_scaled = scaler.fit_transform(x)

# One-hot encode target labels for multi-class classification
encoder = OneHotEncoder(sparse_output=False)
y_encoded = encoder.fit_transform(y)

# Split data into training and testing sets
x_train, x_test, y_train, y_test = train_test_split(
x_scaled, y_encoded, test_size=0.2, random_state=42
)

# Initialize and train the RBF Neural Network
rbfnn = RBFNN(num_centers=10, gamma=1.0)
rbfnn.train(x_train, y_train)

# Predict on test set
y_pred_probs = rbfnn.predict(x_test)
y_pred = np.argmax(y_pred_probs, axis=1)
y_true = np.argmax(y_test, axis=1)

# Evaluate accuracy
accuracy = accuracy_score(y_true, y_pred)
print(f"Classification Accuracy: {accuracy:.4f}")