question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error when fitting zero-degree polynomial

See original GitHub issue

Description

When trying to fit data with a zero-degree 1-dimensional polynomial, a ValueError is raised.

Expected behavior

I expected a model object to be returned that represents a constant at the average of the data points.

Actual behavior

A ValueError is raised. The message is dependent on the fitter used.

Steps to Reproduce

Simply run the following script:

#!/usr/bin/env python3

import numpy as np

from astropy.modeling.models import Polynomial1D
from astropy.modeling.fitting import TRFLSQFitter, LevMarLSQFitter, DogBoxLSQFitter

if __name__ == '__main__' :
    model = Polynomial1D(0, c0=0)
    fitter = TRFLSQFitter() # Also fails with LevMarLSQFitter or DogBoxLSQFitter
    
    # Should work
    # Actually produes an error
    # The exact error depends on the fitter used
    # TRFLSQFitter    : ValueError: The return value of `jac` has wrong shape: expected (10, 1), actual (10, 10).
    # LevMarLSQFitter : ValueError: The array returned by a function changed size between calls
    # DogBoxLSQFitter : ValueError: The return value of `jac` has wrong shape: expected (10, 1), actual (10, 10).
    fit = fitter(model, np.arange(10, dtype=float), np.ones((10,)), weights=np.ones(10,))
    print('fit :', fit)

It seems that the error is in _NonLinearLSQFitter._wrap_deriv method. There is a try/except block. The except section returns a value that gives the correct behavior. However, the try section gives incorrect behavior.

System Details

>>> import platform; print(platform.platform())
Windows-10-10.0.19044-SP0
>>> import sys; print("Python", sys.version)
Python 3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)]
>>> import numpy; print("Numpy", numpy.__version__)
Numpy 1.22.0
>>> import erfa; print("pyerfa", erfa.__version__)
pyerfa 2.0.0.1
>>> import astropy; print("astropy", astropy.__version__)
astropy 5.1
>>> import scipy; print("Scipy", scipy.__version__)
Scipy 1.9.1
>>> import matplotlib; print("Matplotlib", matplotlib.__version__)
Matplotlib 3.5.3

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
EdBehncommented, Sep 9, 2022

@pllim - That’s a good point. I didn’t know about Const1D. However, I still think this bug should be fixed.

0reactions
WilliamJamiesoncommented, Sep 9, 2022

I stumbled on this by accident.

Thanks, we are always looking for the corner cases.

I’m creating a tool where the user can compose their own model as the sum of simpler components. (ie several gaussians and a polynomial background). That’s why I wasn’t using LinearLSQFitter.

No worries, I often see users wanting to always use the non-linear fitters because they “always work”; however, whenever possible one should use the linear fitter because it will be faster, more stable, and more accurate.

For your application it makes sense to use a more general tool, as your problems will generally be non-linear.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is the relationship between degree of polynomial and ...
Overfitting/underfitting may happen to any model that you use, polynomials are closer to what is already known, that's all. The first step is...
Read more >
Homework 9 - Amazon AWS
Plot the polynomial fits for a range of different polynomial degrees (say, from 1 to 10), and report the associated residual sum of...
Read more >
Wrong coefficients in a polynomial fit - Cross Validated
The problem was that the coefficients given by the program were not displayed in full precision. This caused a round-off error which caused...
Read more >
Polynomial curve fitting - MATLAB polyfit - MathWorks
This MATLAB function returns the coefficients for a polynomial p(x) of degree n that is a best fit (in a least-squares sense) for...
Read more >
1.1. Example: Polynomial Curve Fitting
For M = 9, the training set error goes to zero, as we might expect because this polynomial contains 10 degrees of freedom...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found