14
14
# ------------------------------------------------------
15
15
16
16
import numpy as np
17
- from sklearn .cluster import KMeans
18
17
from scipy .spatial .distance import cdist
18
+ from sklearn .cluster import KMeans
19
19
from sklearn .datasets import load_iris
20
- from sklearn .preprocessing import StandardScaler , OneHotEncoder
21
- from sklearn .model_selection import train_test_split
22
20
from sklearn .metrics import accuracy_score
21
+ from sklearn .model_selection import train_test_split
22
+ from sklearn .preprocessing import OneHotEncoder , StandardScaler
23
23
24
24
class RBFNN :
25
25
def __init__ (self , num_centers , gamma ):
@@ -29,9 +29,9 @@ def __init__(self, num_centers, gamma):
29
29
self .centers = None
30
30
self .weights = None
31
31
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
35
35
return np .exp (- self .gamma * (dist ** 2 )) # Apply Gaussian function
36
36
37
37
def train (self , x_data , y_data ):
@@ -54,29 +54,29 @@ def predict(self, x):
54
54
if __name__ == "__main__" :
55
55
# Load and preprocess Iris dataset
56
56
iris = load_iris ()
57
- X = iris .data # Feature matrix
57
+ x = iris .data # Feature matrix
58
58
y = iris .target .reshape (- 1 , 1 ) # Labels
59
59
60
60
# Standardize features
61
61
scaler = StandardScaler ()
62
- X_scaled = scaler .fit_transform (X )
62
+ x_scaled = scaler .fit_transform (x )
63
63
64
64
# One-hot encode target labels for multi-class classification
65
65
encoder = OneHotEncoder (sparse_output = False )
66
66
y_encoded = encoder .fit_transform (y )
67
67
68
68
# 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 )
70
70
71
71
# Initialize and train the RBF Neural Network
72
72
rbfnn = RBFNN (num_centers = 10 , gamma = 1.0 )
73
- rbfnn .train (X_train , y_train )
73
+ rbfnn .train (x_train , y_train )
74
74
75
75
# Predict on test set
76
- y_pred_probs = rbfnn .predict (X_test )
76
+ y_pred_probs = rbfnn .predict (x_test )
77
77
y_pred = np .argmax (y_pred_probs , axis = 1 )
78
78
y_true = np .argmax (y_test , axis = 1 )
79
79
80
80
# Evaluate accuracy
81
81
accuracy = accuracy_score (y_true , y_pred )
82
- print (f"Classification Accuracy: { accuracy :.4f} " )
82
+ print (f"Classification Accuracy: { accuracy :.4f} " )
0 commit comments