21
21
from sklearn .model_selection import train_test_split
22
22
from sklearn .metrics import accuracy_score
23
23
24
+
24
25
class RBFNN :
25
26
def __init__ (self , num_centers , gamma ):
26
27
# Initialize with number of RBF centers and spread parameter (gamma)
@@ -31,8 +32,8 @@ def __init__(self, num_centers, gamma):
31
32
32
33
def _rbf (self , X , centers ):
33
34
# 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
35
+ dist = cdist (X , centers , " euclidean" ) # Compute Euclidean distance to centers
36
+ return np .exp (- self .gamma * (dist ** 2 )) # Apply Gaussian function
36
37
37
38
def train (self , x_data , y_data ):
38
39
# Train the RBFNN
@@ -51,6 +52,7 @@ def predict(self, x):
51
52
rbf_activations = self ._rbf (x , self .centers )
52
53
return rbf_activations .dot (self .weights )
53
54
55
+
54
56
if __name__ == "__main__" :
55
57
# Load and preprocess Iris dataset
56
58
iris = load_iris ()
@@ -66,7 +68,9 @@ def predict(self, x):
66
68
y_encoded = encoder .fit_transform (y )
67
69
68
70
# 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 )
71
+ X_train , X_test , y_train , y_test = train_test_split (
72
+ X_scaled , y_encoded , test_size = 0.2 , random_state = 42
73
+ )
70
74
71
75
# Initialize and train the RBF Neural Network
72
76
rbfnn = RBFNN (num_centers = 10 , gamma = 1.0 )
0 commit comments