numpy / numpy

The fundamental package for scientific computing with Python.
https://numpy.org
Other
27.56k stars 9.86k forks source link

Issue with running polyfit in a for loop? #17202

Open blee3014 opened 4 years ago

blee3014 commented 4 years ago

So polyfit seems weird in a for loop. I get a SVD convergence error on the second loop. If I run polyfit individually (e.g. by putting if i == 1, then run polyfit, etc in the for loop) then it works! In other words, I know it isn't NaN or inf problem because it outputs correctly with the if statement. Any ideas?

Reproducing code example:

import numpy as np
import glob

file_name = 'tt_64_mod*.npz'
file_list = glob.glob(file_name)

for i, f in enumerate(file_list):
    # if i == 1:    # for example, if I select the data manually and run it, it fits perfectly, but in a consecutive run, it raises SVD convergence error. E.g., it works okay for i == 0, then error on i == 1 run. But if I let it fit only on i == 1, then it works.

    data = np.load(f)
    power = data['power']
    bias = data['bias']
    data.close()

    pfit = np.polyfit(bias, power, deg=10)

Error message:

Traceback (most recent call last): File "D:/My Documents/Google Drive/LAB/Projects/high_yield/Project/Figures/Figure4_yield_vs_data_rate/dirac_data/new_folder/plot_data.py", line 14, in pfit = np.polyfit(b, power, deg=10) File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\lib\polynomial.py", line 580, in polyfit c, resids, rank, s = lstsq(lhs, rhs, rcond) File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\linalg\linalg.py", line 2156, in lstsq x, resids, rank, s = gufunc(a, b, rcond, signature=signature, extobj=extobj) File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\linalg\linalg.py", line 101, in _raise_linalgerror_lstsq raise LinAlgError("SVD did not converge in Linear Least Squares") numpy.linalg.linalg.LinAlgError: SVD did not converge in Linear Least Squares

Numpy/Python version information:

numpy: 1.15.1 sys: 3.6.4

charris commented 4 years ago

There is a problem with the latest Windows update (2004). Are you using that by any chance?

blee3014 commented 4 years ago

Yes indeed its update 2004. What's the cause/is there a fix?

charris commented 4 years ago

It seems to be a linking problem that is fixed in the latest Microsoft pre-release, but we have yet to hear from anyone who has tried it. I'll close this as a duplicate, you can follow events at #16744. If the problem persists after that is settled please open another issue.

blee3014 commented 4 years ago

i'll keep it open for now so that people can related this to the duplicate one. Thanks!

charris commented 4 years ago

There is a report that build 17763.1397 (Aug 11) fixes the problem. Not sure if you get that with a regular update.

blee3014 commented 4 years ago

It seems that build is for version 1809 of windows (https://support.microsoft.com/en-us/help/4565349/windows-10-update-kb4565349). I'll try reverting to that version (pre 2004) and see if it persists.

bashtage commented 4 years ago

Or use Conda provided Python with MKL for linear algebra.