简体   繁体   中英

numpy.polyfit giving error for covariance matrix

This is the code:

p=[1,2,3,4]

q=[4,5,6,7]

z,n=numpy.polyfit(p,q,1,cov=True)

It gives me this message:

4 z,n=np.polyfit(p,q,1,cov=True)

C:\\Users\\Rekha\\Anaconda3\\lib\\site-packages\\numpy\\lib\\polynomial.py in polyfit(x, y, deg, rcond, full, w, cov)

601 # Plus, it gives a slightly more conservative estimate of uncertainty.

602 if len(x) <= order + 2:

--> 603 raise ValueError("the number of data points must exceed order + 2 " 604 "for Bayesian estimate the covariance matrix")

605 fac = resids / (len(x) - order - 2.0)

ValueError: the number of data points must exceed order + 2 for Bayesian estimate the covariance matrix

I can't figure out why this is happening? I have four data points, which is enough to fit a line

The error is saying precisely what the problem is: you do not have enough data point to estimate the covariance matrix (and not to perform the fit).

import numpy as np

p = [1, 2, 3, 4]
q = [4, 5, 6, 7]

z = np.polyfit(p, q, 1, cov=False)
print(z)

Or you can have the estimate of the covariance matrix if you meet the suggested condition, ie one more data point.

import numpy as np

p = [1, 2, 3, 4, 5]
q = [4, 5, 6, 7, 8]

z, n = np.polyfit(p, q, 1, cov=True)

print(z, n)

I don't know what you need the covariance matrix for, but if all you need is the error on the fit parameter, you could consider calculating this manually, eg by taking all possible C(n, k) combinations of your n data points for all valid values of k and then calculating the standard deviation of the obtained coefficients.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM