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)
| 1346 | |
| 1347 | |
| 1348 | def polyfit(x, y, deg, rcond=None, full=False, w=None): |
| 1349 | """ |
| 1350 | Least-squares fit of a polynomial to data. |
| 1351 | |
| 1352 | Return the coefficients of a polynomial of degree `deg` that is the |
| 1353 | least squares fit to the data values `y` given at points `x`. If `y` is |
| 1354 | 1-D the returned coefficients will also be 1-D. If `y` is 2-D multiple |
| 1355 | fits are done, one for each column of `y`, and the resulting |
| 1356 | coefficients are stored in the corresponding columns of a 2-D return. |
| 1357 | The fitted polynomial(s) are in the form |
| 1358 | |
| 1359 | .. math:: p(x) = c_0 + c_1 * x + ... + c_n * x^n, |
| 1360 | |
| 1361 | where `n` is `deg`. |
| 1362 | |
| 1363 | Parameters |
| 1364 | ---------- |
| 1365 | x : array_like, shape (`M`,) |
| 1366 | x-coordinates of the `M` sample (data) points ``(x[i], y[i])``. |
| 1367 | y : array_like, shape (`M`,) or (`M`, `K`) |
| 1368 | y-coordinates of the sample points. Several sets of sample points |
| 1369 | sharing the same x-coordinates can be (independently) fit with one |
| 1370 | call to `polyfit` by passing in for `y` a 2-D array that contains |
| 1371 | one data set per column. |
| 1372 | deg : int or 1-D array_like |
| 1373 | Degree(s) of the fitting polynomials. If `deg` is a single integer |
| 1374 | all terms up to and including the `deg`'th term are included in the |
| 1375 | fit. For NumPy versions >= 1.11.0 a list of integers specifying the |
| 1376 | degrees of the terms to include may be used instead. |
| 1377 | rcond : float, optional |
| 1378 | Relative condition number of the fit. Singular values smaller |
| 1379 | than `rcond`, relative to the largest singular value, will be |
| 1380 | ignored. The default value is ``len(x)*eps``, where `eps` is the |
| 1381 | relative precision of the platform's float type, about 2e-16 in |
| 1382 | most cases. |
| 1383 | full : bool, optional |
| 1384 | Switch determining the nature of the return value. When ``False`` |
| 1385 | (the default) just the coefficients are returned; when ``True``, |
| 1386 | diagnostic information from the singular value decomposition (used |
| 1387 | to solve the fit's matrix equation) is also returned. |
| 1388 | w : array_like, shape (`M`,), optional |
| 1389 | Weights. If not None, the weight ``w[i]`` applies to the unsquared |
| 1390 | residual ``y[i] - y_hat[i]`` at ``x[i]``. Ideally the weights are |
| 1391 | chosen so that the errors of the products ``w[i]*y[i]`` all have the |
| 1392 | same variance. When using inverse-variance weighting, use |
| 1393 | ``w[i] = 1/sigma(y[i])``. The default value is None. |
| 1394 | |
| 1395 | Returns |
| 1396 | ------- |
| 1397 | coef : ndarray, shape (`deg` + 1,) or (`deg` + 1, `K`) |
| 1398 | Polynomial coefficients ordered from low to high. If `y` was 2-D, |
| 1399 | the coefficients in column `k` of `coef` represent the polynomial |
| 1400 | fit to the data in `y`'s `k`-th column. |
| 1401 | |
| 1402 | [residuals, rank, singular_values, rcond] : list |
| 1403 | These values are only returned if ``full == True`` |
| 1404 | |
| 1405 | - residuals -- sum of squared residuals of the least squares fit |
nothing calls this directly
no test coverage detected
searching dependent graphs…