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
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Jun 15, 2025
commit 046f63f1b500ceacf4e90cea34ea94a73a58023d
12 changes: 8 additions & 4 deletions neural_network/rbfnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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)
Expand All @@ -31,8 +32,8 @@ def __init__(self, num_centers, gamma):

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
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
Expand All @@ -51,6 +52,7 @@ def predict(self, x):
rbf_activations = self._rbf(x, self.centers)
return rbf_activations.dot(self.weights)


if __name__ == "__main__":
# Load and preprocess Iris dataset
iris = load_iris()
Expand All @@ -66,7 +68,9 @@ def predict(self, x):
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
)

# Initialize and train the RBF Neural Network
rbfnn = RBFNN(num_centers=10, gamma=1.0)
Expand All @@ -79,4 +83,4 @@ def predict(self, x):

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