Skip to content

Conversation

@ruman23
Copy link

@ruman23 ruman23 commented Oct 20, 2025

Describe your change:

This pull request adds a new implementation of the Gaussian Mixture Model (GMM) algorithm under the machine_learning/ directory.

The GMM is an unsupervised learning algorithm that represents data as a mixture of multiple Gaussian distributions.
It uses the Expectation–Maximization (EM) algorithm to iteratively estimate each component’s mean, covariance, and mixing probability.

Key Highlights:

  • Implemented entirely from scratch using NumPy (no external ML frameworks).
  • Includes type hints, doctests, and a main() demonstration.
  • Fully compliant with TheAlgorithms’ code style and structure.
  • Educational and mathematically clear implementation of the EM procedure.

References:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All function and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

@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.

Gaussian Mixture Model implemented using the Expectation-Maximization algorithm.
"""

def __init__(self, n_components=2, max_iter=100, tol=1e-4, seed=None):

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:

Please provide type hint for the parameter: n_components

Please provide type hint for the parameter: max_iter

Please provide type hint for the parameter: tol

Please provide type hint for the parameter: seed

self.covariances_ = None
self.log_likelihoods_ = []

def _initialize_parameters(self, X):

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/gaussian_mixture_model.py, please provide doctest for the function _initialize_parameters

Please provide return type hint for the function: _initialize_parameters. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: X

Please provide descriptive name for the parameter: X

)
self.weights_ = np.ones(self.n_components) / self.n_components

def _e_step(self, X):

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/gaussian_mixture_model.py, please provide doctest for the function _e_step

Please provide return type hint for the function: _e_step. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: X

Please provide descriptive name for the parameter: X

responsibilities /= responsibilities.sum(axis=1, keepdims=True)
return responsibilities

def _m_step(self, X, responsibilities):

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/gaussian_mixture_model.py, please provide doctest for the function _m_step

Please provide return type hint for the function: _m_step. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: X

Please provide descriptive name for the parameter: X

Please provide type hint for the parameter: responsibilities

def _m_step(self, X, responsibilities):
"""Update weights, means, and covariances"""
n_samples, n_features = X.shape
Nk = responsibilities.sum(axis=0)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: Nk

# Add small regularization term for numerical stability
self.covariances_[k] += np.eye(n_features) * 1e-6

def _compute_log_likelihood(self, X):

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/gaussian_mixture_model.py, please provide doctest for the function _compute_log_likelihood

Please provide return type hint for the function: _compute_log_likelihood. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: X

Please provide descriptive name for the parameter: X

log_likelihood = np.sum(np.log(np.sum(total_pdf, axis=1) + 1e-12))
return log_likelihood

def fit(self, X):

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/gaussian_mixture_model.py, please provide doctest for the function fit

Please provide return type hint for the function: fit. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: X

Please provide descriptive name for the parameter: X


print(f"{TAG}Training complete. Final log-likelihood: {log_likelihood:.4f}")

def predict(self, X):

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/gaussian_mixture_model.py, please provide doctest for the function predict

Please provide return type hint for the function: predict. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: X

Please provide descriptive name for the parameter: X

responsibilities = self._e_step(X)
return np.argmax(responsibilities, axis=1)

def plot_results(self, X):

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/gaussian_mixture_model.py, please provide doctest for the function plot_results

Please provide return type hint for the function: plot_results. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: X

Please provide descriptive name for the parameter: X

@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Oct 20, 2025
@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Oct 20, 2025
@algorithms-keeper algorithms-keeper bot removed 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.

warnings.filterwarnings("ignore")

TAG = "GAUSSIAN-MIXTURE/ "
FloatArray: TypeAlias = NDArray[np.float64]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: FloatArray

@algorithms-keeper algorithms-keeper bot added 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant