Skip to content

Commit e055137

Browse files
Add RBFNN implementation with Iris classification with updated code
1 parent 750e7da commit e055137

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

neural_network/Radial_Basis_Function_Neural_Networks.py renamed to neural_network/rbfnn.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
# ------------------------------------------------------
1515

1616
import numpy as np
17-
from sklearn.cluster import KMeans
1817
from scipy.spatial.distance import cdist
18+
from sklearn.cluster import KMeans
1919
from sklearn.datasets import load_iris
20-
from sklearn.preprocessing import StandardScaler, OneHotEncoder
21-
from sklearn.model_selection import train_test_split
2220
from sklearn.metrics import accuracy_score
21+
from sklearn.model_selection import train_test_split
22+
from sklearn.preprocessing import OneHotEncoder, StandardScaler
2323

2424
class RBFNN:
2525
def __init__(self, num_centers, gamma):
@@ -29,9 +29,9 @@ def __init__(self, num_centers, gamma):
2929
self.centers = None
3030
self.weights = None
3131

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
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
3535
return np.exp(-self.gamma * (dist ** 2)) # Apply Gaussian function
3636

3737
def train(self, x_data, y_data):
@@ -54,29 +54,29 @@ def predict(self, x):
5454
if __name__ == "__main__":
5555
# Load and preprocess Iris dataset
5656
iris = load_iris()
57-
X = iris.data # Feature matrix
57+
x = iris.data # Feature matrix
5858
y = iris.target.reshape(-1, 1) # Labels
5959

6060
# Standardize features
6161
scaler = StandardScaler()
62-
X_scaled = scaler.fit_transform(X)
62+
x_scaled = scaler.fit_transform(x)
6363

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

6868
# 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)
69+
x_train, x_test, y_train, y_test = train_test_split(x_scaled, y_encoded, test_size=0.2, random_state=42)
7070

7171
# Initialize and train the RBF Neural Network
7272
rbfnn = RBFNN(num_centers=10, gamma=1.0)
73-
rbfnn.train(X_train, y_train)
73+
rbfnn.train(x_train, y_train)
7474

7575
# Predict on test set
76-
y_pred_probs = rbfnn.predict(X_test)
76+
y_pred_probs = rbfnn.predict(x_test)
7777
y_pred = np.argmax(y_pred_probs, axis=1)
7878
y_true = np.argmax(y_test, axis=1)
7979

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

0 commit comments

Comments
 (0)