Skip to content

Reimplement polynomial_regression.py #8889

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

Merged
merged 10 commits into from
Jul 28, 2023

Conversation

tianyizheng02
Copy link
Contributor

Describe your change:

Contributes to #6216

  • Reimplemented machine_learning/polynomial_regression.py using numpy because the old original implementation was just a how-to on doing polynomial regression using sklearn
  • Added detailed function documentation, doctests, and algorithm explanation
  • Fixed typo in file name: polymonial_regression.py -> polynomial_regression.py
  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • 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 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".

tianyizheng02 and others added 3 commits July 24, 2023 03:24
Rename machine_learning/polymonial_regression.py to
machine_learning/polynomial_regression.py

Reimplement machine_learning/polynomial_regression.py using numpy
because the old original implementation was just a how-to on doing
polynomial regression using sklearn

Add detailed function documentation, doctests, and algorithm
explanation
@algorithms-keeper algorithms-keeper bot added enhancement This PR modified some existing files awaiting reviews This PR is ready to be reviewed labels Jul 24, 2023
@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Jul 24, 2023
@tianyizheng02
Copy link
Contributor Author

=================================== FAILURES ===================================
__ [doctest] machine_learning.polynomial_regression.PolynomialRegression.fit ___
133         Traceback (most recent call last):
134         ...
135         ArithmeticError: Design matrix is not full rank, can't compute coefficients
136 
137         Make sure errors don't grow too large:
138         >>> coefs = np.array([-250, 50, -2, 36, 20, -12, 10, 2, -1, -15, 1])
139         >>> y = PolynomialRegression._design_matrix(x, len(coefs) - 1) @ coefs
140         >>> poly_reg = PolynomialRegression(degree=len(coefs) - 1)
141         >>> poly_reg.fit(x, y)
142         >>> np.allclose(poly_reg.params, coefs, atol=10e-5)
Expected:
    True
Got:
    False

This doctest passes when I run it locally on my own machine, so I'm guessing the failure here is due to some variation in numpy's calculations on different systems

@tianyizheng02
Copy link
Contributor Author

=================================== FAILURES ===================================
__ [doctest] machine_learning.polynomial_regression.PolynomialRegression.fit ___
133         Traceback (most recent call last):
134         ...
135         ArithmeticError: Design matrix is not full rank, can't compute coefficients
136 
137         Make sure errors don't grow too large:
138         >>> coefs = np.array([-250, 50, -2, 36, 20, -12, 10, 2, -1, -15, 1])
139         >>> y = PolynomialRegression._design_matrix(x, len(coefs) - 1) @ coefs
140         >>> poly_reg = PolynomialRegression(degree=len(coefs) - 1)
141         >>> poly_reg.fit(x, y)
142         >>> poly_reg.params    # Output params for debugging
Expected nothing
Got:
    array([-250.0000132 ,   50.00017691,   -2.00040817,   36.00038528,
             19.99980068,  -11.99993801,    9.99998778,    2.00000153,
             -1.00000012,  -14.99999999,    1.        ])

The estimated parameters clearly are close to the true parameters, so I'll just set the tolerance to a larger value.

@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Jul 24, 2023
Copy link
Contributor

@CaedenPH CaedenPH left a comment

Choose a reason for hiding this comment

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

Very nice documentation about each function

@algorithms-keeper algorithms-keeper bot added the require tests Tests [doctest/unittest/pytest] are required label Jul 24, 2023
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.

@algorithms-keeper algorithms-keeper bot removed the require tests Tests [doctest/unittest/pytest] are required label Jul 24, 2023
@algorithms-keeper algorithms-keeper bot removed the awaiting reviews This PR is ready to be reviewed label Jul 28, 2023
@cclauss cclauss merged commit e406801 into TheAlgorithms:master Jul 28, 2023
@tianyizheng02 tianyizheng02 deleted the polynomial-regression branch July 28, 2023 19:07
sedatguzelsemme pushed a commit to sedatguzelsemme/Python that referenced this pull request Sep 15, 2024
* Reimplement polynomial_regression.py

Rename machine_learning/polymonial_regression.py to
machine_learning/polynomial_regression.py

Reimplement machine_learning/polynomial_regression.py using numpy
because the old original implementation was just a how-to on doing
polynomial regression using sklearn

Add detailed function documentation, doctests, and algorithm
explanation

* updating DIRECTORY.md

* Fix matrix formatting in docstrings

* Try to fix failing doctest

* Debugging failing doctest

* Fix failing doctest attempt 2

* Remove unnecessary return value descriptions in docstrings

* Readd placeholder doctest for main function

* Fix typo in algorithm description

---------

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
@isidroas isidroas mentioned this pull request Jan 25, 2025
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This PR modified some existing files
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants