Skip to content

Refactor logistic regression to OOP class with multi-class support#14923

Open
rituuu001 wants to merge 1 commit into
TheAlgorithms:masterfrom
rituuu001:refactor-logistic-regression
Open

Refactor logistic regression to OOP class with multi-class support#14923
rituuu001 wants to merge 1 commit into
TheAlgorithms:masterfrom
rituuu001:refactor-logistic-regression

Conversation

@rituuu001

Copy link
Copy Markdown

Describe your change:

  • Add an algorithm? (Significantly expanded and enhanced the existing algorithm file into a robust, scalable multi-class classifier)
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests?
  • 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.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions 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". (Submitted directly without an open issue, as per Contributing guidelines for algorithm additions)

Summary of Enhancements

Refactored the core layout of machine_learning/logistic_regression.py. The original implementation was limited to Batch Gradient Descent and binary classification. This upgrade introduces a robust, object-oriented LogisticRegression class capable of handling both binary and advanced multi-class datasets entirely from scratch using NumPy.

  • Mini-batch Gradient Descent: Added efficient mini-batch sampling configurations to significantly optimize convergence and scalability on larger datasets.
  • True Multiclass Support: Implemented standalone mathematical variations for both One-vs-Rest (OVR) and Softmax (Multinomial) regression frameworks.
  • Early Stopping: Programmed vector log-loss tracking limits to break calculation epochs early once structural threshold targets stabilize (< 1e-6).
  • NumPy Random Generator Modernization: Upgraded all legacy execution calls (np.random.seed, np.random.randn, np.random.permutation) to utilize modern np.random.default_rng() constraints as recommended by NumPy documentation.
  • Type-Hinted & Lint Verified: Validated and confirmed 100% clean passes locally using the project's strict verification tools (black, ruff check, and mypy).

Verification Results

All required verification suites were executed locally and passed with zero errors:

  • Doctest Validation: Clean pass (7 tests passed in 11 items).
    py -m doctest -v machine_learning/logistic_regression.py

@algorithms-keeper algorithms-keeper Bot added enhancement This PR modified some existing files awaiting reviews This PR is ready to be reviewed labels Jul 12, 2026
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 enhancement This PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant