-
-
Notifications
You must be signed in to change notification settings - Fork 49.5k
feat: add Radial Basis Function Neural Network (Implements #12322) #13636
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
base: master
Are you sure you want to change the base?
feat: add Radial Basis Function Neural Network (Implements #12322) #13636
Conversation
Implements TheAlgorithms#12322 - Add RadialBasisFunctionNetwork class with train() and predict() methods - Use KMeans clustering for RBF center initialization - Implement Gaussian RBF activation functions - Use least-squares fitting for output weight calculation - Include comprehensive doctests and error handling - Add detailed docstrings with mathematical formulas
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
| weights: Output layer weights | ||
| """ | ||
|
|
||
| def __init__(self, num_centers: int = 10, gamma: float = 1.0): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:
| self.centers = None | ||
| self.weights = None | ||
|
|
||
| def _gaussian_rbf(self, x: np.ndarray, center: np.ndarray) -> float: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file machine_learning/rbf_network.py, please provide doctest for the function _gaussian_rbf
Please provide descriptive name for the parameter: x
| distance_squared = np.sum((x - center) ** 2) | ||
| return np.exp(-self.gamma * distance_squared) | ||
|
|
||
| def _compute_rbf_activations(self, X: np.ndarray) -> np.ndarray: # noqa: N803 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file machine_learning/rbf_network.py, please provide doctest for the function _compute_rbf_activations
Please provide descriptive name for the parameter: X
machine_learning/rbf_network.py
Outdated
|
|
||
| return activations | ||
|
|
||
| def train(self, X: np.ndarray, y: np.ndarray) -> None: # noqa: N803 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide descriptive name for the parameter: X
Please provide descriptive name for the parameter: y
machine_learning/rbf_network.py
Outdated
| # weights = (A^T A)^-1 A^T y, where A is the activation matrix | ||
| self.weights = np.linalg.lstsq(activations, y, rcond=None)[0] | ||
|
|
||
| def predict(self, X: np.ndarray) -> np.ndarray: # noqa: N803 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide descriptive name for the parameter: X
for more information, see https://pre-commit.ci
Description
Implements #12322
This PR adds a complete implementation of Radial Basis Function Neural Network (RBFNN) to the machine learning algorithms collection.
Implementation Details
Architecture:
Features:
__init__(num_centers, gamma): Initialize network parameterstrain(X, y): Train using KMeans clustering and least-squarespredict(X): Make predictions on new dataTraining Process:
Mathematical Foundation
The Gaussian RBF is defined as:
φ(x) = exp(-γ * ||x - c||²)
Where:
Output is computed as weighted sum:
y = Σ(w_i * φ_i(x))Testing
All doctests pass successfully:
python -m doctest machine_learning/rbf_network.py -v
Test coverage:
Requirements Met
✅ RBFNN class with
num_centersandgammainitialization✅
train(X, y)method with KMeans and least-squares✅
predict(X)method for making predictions✅ Gaussian RBF activation functions
✅ Comprehensive documentation and doctests
Example Usage
import numpy as np
from machine_learning.rbf_network import RadialBasisFunctionNetwork
Generate training data
X_train = np.random.randn(100, 2)
y_train = np.sum(X_train ** 2, axis=1)
Create and train RBFNN
rbfnn = RadialBasisFunctionNetwork(num_centers=10, gamma=1.0)
rbfnn.train(X_train, y_train)
Make predictions
X_test = np.random.randn(20, 2)
predictions = rbfnn.predict(X_test)
References