Skip to content

BUG: Polynomial.fit() produces incorrect coefficients but plots correctly #24497

@ohunter

Description

@ohunter

Describe the issue:

Using Polynomial.fit() from numpy.polynomial.polynomial produces incorrect coefficients but correctly with matplotlib.

Reproduce the code example:

import numpy as np
from numpy.polynomial.polynomial import Polynomial
import matplotlib.pyplot as plt

x = np.array([0.43907794, 1.69045005, 3.27113063, 5.31284303, 12.03073546, 20.0 , 34.95060373, 40.0219539 , 50.29637761, 59.9780461 ,61.29527991, 66.76180022, 72.55762898, 78.74862788, 80.0, 82.37102086, 84.61031833, 87.83754116])
y = np.array([1.59255172, 1.552, 1.49986207, 1.45186207, 1.39972414, 1.35337931, 1.30124138, 1.28717241, 1.25241379, 1.20855172, 1.20027586, 1.15227586, 1.10013793, 1.05213793, 1.03806897, 1.0, 0.952, 0.90565517])

degree = 3
f = Polynomial.fit(x, y, degree)
g = Polynomial(np.polyfit(x, y, degree)[::-1])

print(f"f(x) = {f}")
print(f"g(x) = {g}")

plt.plot(x, y, 'o')
xx, yy = f.linspace()
plt.plot(xx, yy, lw=2, label='f(x)')
xxx, yyy = g.linspace()
plt.plot(xxx, yyy, lw=2, label='g(x)')
plt.legend()
plt.show()

Error message:

No response

Runtime information:

import sys, numpy; print(numpy.version); print(sys.version)
1.25.2
3.11.4 (tags/v3.11.4:d2340ef, Jun 7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)]
print(numpy.show_runtime())
[{'numpy_version': '1.25.2',
'python': '3.11.4 (tags/v3.11.4:d2340ef, Jun 7 2023, 05:45:37) [MSC v.1934 '
'64 bit (AMD64)]',
'uname': uname_result(system='Windows', node='KPC22026425', release='10', version='10.0.19045', machine='AMD64')},
{'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
'found': ['SSSE3',
'SSE41',
'POPCNT',
'SSE42',
'AVX',
'F16C',
'FMA3',
'AVX2'],
'not_found': ['AVX512F',
'AVX512CD',
'AVX512_SKX',
'AVX512_CLX',
'AVX512_CNL',
'AVX512_ICL']}},
{'architecture': 'Haswell',
'filepath': 'C:\Users\olejeh\testing\ultra_battery_tuning\venv\Lib\site-packages\numpy\.libs\libopenblas64__v0.3.23-246-g3d31191b-gcc_10_3_0.dll',
'internal_api': 'openblas',
'num_threads': 12,
'prefix': 'libopenblas',
'threading_layer': 'pthreads',
'user_api': 'blas',
'version': '0.3.23.dev'}]
None

Context for the issue:

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions