Skip to content

Conversation

@Monasri29-hub
Copy link

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:

  • Input layer accepts n-dimensional vectors
  • Hidden layer uses Gaussian RBF activation functions
  • Output layer performs linear combination of hidden activations

Features:

  • __init__(num_centers, gamma): Initialize network parameters
  • train(X, y): Train using KMeans clustering and least-squares
  • predict(X): Make predictions on new data
  • Comprehensive error handling and input validation
  • Detailed doctests for all methods

Training Process:

  1. Center Selection: Uses KMeans clustering to find optimal RBF centers
  2. Activation Computation: Calculates Gaussian RBF activations for all samples
  3. Weight Optimization: Solves for output weights using least-squares regression

Mathematical Foundation

The Gaussian RBF is defined as:
φ(x) = exp(-γ * ||x - c||²)

Where:

  • x: input vector
  • c: RBF center
  • γ: spread parameter

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:

  • Initialization with valid parameters
  • Training with various data shapes
  • Prediction after training
  • Error handling for invalid inputs

Requirements Met

✅ RBFNN class with num_centers and gamma initialization
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

Monasri29-hub and others added 4 commits October 20, 2025 21:00
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
@algorithms-keeper algorithms-keeper bot added require descriptive names This PR needs descriptive function and/or variable names require tests Tests [doctest/unittest/pytest] are required require type hints https://docs.python.org/3/library/typing.html labels Oct 20, 2025
Copy link

@algorithms-keeper algorithms-keeper bot left a 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 review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to 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):

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:

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

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


return activations

def train(self, X: np.ndarray, y: np.ndarray) -> None: # noqa: N803

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

# 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

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

@algorithms-keeper algorithms-keeper bot added awaiting reviews This PR is ready to be reviewed tests are failing Do not merge until tests pass and removed tests are failing Do not merge until tests pass labels Oct 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed require descriptive names This PR needs descriptive function and/or variable names require tests Tests [doctest/unittest/pytest] are required require type hints https://docs.python.org/3/library/typing.html

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants