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 1 commit
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
Prev Previous commit
Next Next commit
Add RBFNN implementation with Iris classification with updated code
  • Loading branch information
shubhamkatyaan committed Jun 15, 2025
commit e05513769573fb43e8dec975e3c60b4d5903cdf6
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
# - Evaluation using classification accuracy.
# ------------------------------------------------------

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

Check failure on line 22 in neural_network/rbfnn.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

neural_network/rbfnn.py:16:1: I001 Import block is un-sorted or un-formatted

class RBFNN:
def __init__(self, num_centers, gamma):
Expand All @@ -29,9 +29,9 @@
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
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):
Expand All @@ -54,29 +54,29 @@
if __name__ == "__main__":
# Load and preprocess Iris dataset
iris = load_iris()
X = iris.data # Feature matrix
x = iris.data # Feature matrix
y = iris.target.reshape(-1, 1) # Labels

# Standardize features
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
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)
x_train, x_test, y_train, y_test = train_test_split(x_scaled, y_encoded, test_size=0.2, random_state=42)

Check failure on line 69 in neural_network/rbfnn.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

neural_network/rbfnn.py:69:89: E501 Line too long (108 > 88)

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

# Predict on test set
y_pred_probs = rbfnn.predict(X_test)
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}")
print(f"Classification Accuracy: {accuracy:.4f}")

Check failure on line 82 in neural_network/rbfnn.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W292)

neural_network/rbfnn.py:82:54: W292 No newline at end of file
Loading