Least-squares fit of a polynomial to data. Return the coefficients of a polynomial of degree `deg` that is the least squares fit to the data values `y` given at points `x`. If `y` is 1-D the returned coefficients will also be 1-D. If `y` is 2-D multiple fits are done, one for e
(x, y, deg, rcond=None, full=False, w=None)
| 1212 | |
| 1213 | |
| 1214 | def polyfit(x, y, deg, rcond=None, full=False, w=None): |
| 1215 | """ |
| 1216 | Least-squares fit of a polynomial to data. |
| 1217 | |
| 1218 | Return the coefficients of a polynomial of degree `deg` that is the |
| 1219 | least squares fit to the data values `y` given at points `x`. If `y` is |
| 1220 | 1-D the returned coefficients will also be 1-D. If `y` is 2-D multiple |
| 1221 | fits are done, one for each column of `y`, and the resulting |
| 1222 | coefficients are stored in the corresponding columns of a 2-D return. |
| 1223 | The fitted polynomial(s) are in the form |
| 1224 | |
| 1225 | .. math:: p(x) = c_0 + c_1 * x + ... + c_n * x^n, |
| 1226 | |
| 1227 | where `n` is `deg`. |
| 1228 | |
| 1229 | Parameters |
| 1230 | ---------- |
| 1231 | x : array_like, shape (`M`,) |
| 1232 | x-coordinates of the `M` sample (data) points ``(x[i], y[i])``. |
| 1233 | y : array_like, shape (`M`,) or (`M`, `K`) |
| 1234 | y-coordinates of the sample points. Several sets of sample points |
| 1235 | sharing the same x-coordinates can be (independently) fit with one |
| 1236 | call to `polyfit` by passing in for `y` a 2-D array that contains |
| 1237 | one data set per column. |
| 1238 | deg : int or 1-D array_like |
| 1239 | Degree(s) of the fitting polynomials. If `deg` is a single integer |
| 1240 | all terms up to and including the `deg`'th term are included in the |
| 1241 | fit. For NumPy versions >= 1.11.0 a list of integers specifying the |
| 1242 | degrees of the terms to include may be used instead. |
| 1243 | rcond : float, optional |
| 1244 | Relative condition number of the fit. Singular values smaller |
| 1245 | than `rcond`, relative to the largest singular value, will be |
| 1246 | ignored. The default value is ``len(x)*eps``, where `eps` is the |
| 1247 | relative precision of the platform's float type, about 2e-16 in |
| 1248 | most cases. |
| 1249 | full : bool, optional |
| 1250 | Switch determining the nature of the return value. When ``False`` |
| 1251 | (the default) just the coefficients are returned; when ``True``, |
| 1252 | diagnostic information from the singular value decomposition (used |
| 1253 | to solve the fit's matrix equation) is also returned. |
| 1254 | w : array_like, shape (`M`,), optional |
| 1255 | Weights. If not None, the weight ``w[i]`` applies to the unsquared |
| 1256 | residual ``y[i] - y_hat[i]`` at ``x[i]``. Ideally the weights are |
| 1257 | chosen so that the errors of the products ``w[i]*y[i]`` all have the |
| 1258 | same variance. When using inverse-variance weighting, use |
| 1259 | ``w[i] = 1/sigma(y[i])``. The default value is None. |
| 1260 | |
| 1261 | .. versionadded:: 1.5.0 |
| 1262 | |
| 1263 | Returns |
| 1264 | ------- |
| 1265 | coef : ndarray, shape (`deg` + 1,) or (`deg` + 1, `K`) |
| 1266 | Polynomial coefficients ordered from low to high. If `y` was 2-D, |
| 1267 | the coefficients in column `k` of `coef` represent the polynomial |
| 1268 | fit to the data in `y`'s `k`-th column. |
| 1269 | |
| 1270 | [residuals, rank, singular_values, rcond] : list |
| 1271 | These values are only returned if ``full == True`` |