|
1 | 1 | import numpy as np
|
2 | 2 |
|
| 3 | + |
3 | 4 | class RadialBasisFunctionNeuralNetwork:
|
4 | 5 | """
|
5 | 6 | A simple implementation of a Radial Basis Function Neural Network (RBFNN).
|
@@ -46,7 +47,9 @@ def _gaussian_rbf(self, input_vector: np.ndarray, center: np.ndarray) -> float:
|
46 | 47 | >>> rbf_nn._gaussian_rbf(np.array([0, 0]), center)
|
47 | 48 | 0.1353352832366127
|
48 | 49 | """
|
49 |
| - return np.exp(-(np.linalg.norm(input_vector - center) ** 2) / (2 * self.spread ** 2)) |
| 50 | + return np.exp( |
| 51 | + -(np.linalg.norm(input_vector - center) ** 2) / (2 * self.spread**2) |
| 52 | + ) |
50 | 53 |
|
51 | 54 | def _compute_rbf_outputs(self, input_data: np.ndarray) -> np.ndarray:
|
52 | 55 | """
|
@@ -91,10 +94,14 @@ def fit(self, input_data: np.ndarray, target_values: np.ndarray):
|
91 | 94 | True
|
92 | 95 | """
|
93 | 96 | if input_data.shape[0] != target_values.shape[0]:
|
94 |
| - raise ValueError("Number of samples in input_data and target_values must match.") |
| 97 | + raise ValueError( |
| 98 | + "Number of samples in input_data and target_values must match." |
| 99 | + ) |
95 | 100 |
|
96 | 101 | # Initialize centers using random samples from input_data
|
97 |
| - random_indices = np.random.choice(input_data.shape[0], self.num_centers, replace=False) |
| 102 | + random_indices = np.random.choice( |
| 103 | + input_data.shape[0], self.num_centers, replace=False |
| 104 | + ) |
98 | 105 | self.centers = input_data[random_indices]
|
99 | 106 |
|
100 | 107 | # Compute the RBF outputs for the training data
|
@@ -128,6 +135,7 @@ def predict(self, input_data: np.ndarray) -> np.ndarray:
|
128 | 135 | # Example Usage
|
129 | 136 | if __name__ == "__main__":
|
130 | 137 | import doctest
|
| 138 | + |
131 | 139 | doctest.testmod()
|
132 | 140 |
|
133 | 141 | # Sample dataset for XOR problem
|
|
0 commit comments