Skip to content

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

Closed
rituuu001 wants to merge 3 commits into
TheAlgorithms:masterfrom
rituuu001:refactor-logistic-regression
Closed

rituuu001 wants to merge 3 commits 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
@cclauss

cclauss commented Sep 24, 2026 •

Copy link
Copy Markdown
Member

@priya-sundaram-dev, please review.

It seems like we are losing a lot of tests in these changes. Is there solid value added in the proposed changes?

@priya-sundaram-dev

Copy link
Copy Markdown
Contributor

Thanks @cclauss — I share the concern; I'd hold this one. The value added is real, but it doesn't offset the verifiable test coverage we'd lose.

What's lost: the PR replaces three small, heavily-doctested functions with one large OOP class. Concretely it drops 5 of the 7 numeric sigmoid_function doctests, and deletes cost_function and log_likelihood entirely — along with all of their numeric doctests (cross-entropy values, log-likelihood). The replacement LogisticRegression class ships a single smoke test, len(clf.predict(...)) == 4, with no numeric assertion on weights, loss, or probabilities.

For a teaching repo, the reproducible, asserted numeric behavior is the most valuable part of the file. Random-init OOP makes deterministic doctests harder, which is likely why they were dropped — but that's exactly the regression we don't want.

Scope: OVR + softmax multi-class also pushes well beyond the original single-example scope. That's not automatically bad, but it raises the bar for how well-tested it needs to be.

Suggestion for the author: either (a) keep the original functions and their doctests and add softmax as a separate, deterministically-seeded addition with its own numeric doctests; or (b) if going full OOP, seed the RNG and add doctests asserting on the learned probabilities/loss, and restore the sigmoid range tests plus a log-loss doctest.

As submitted I'd not merge — the tradeoff reduces tested behavior on an educational file. Happy to re-review once coverage is restored.

@cclauss cclauss closed this Sep 24, 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.

3 participants