Polynomial regression: Difference between revisions
<code>
m (Taking "Example" off the TOC) |
(<code>) |
||
Line 14:
=={{header|Ada}}==
<code ada>
with Ada.Numerics.Real_Arrays; use Ada.Numerics.Real_Arrays;
Line 27:
return Solve (A * Transpose (A), A * Y);
end Fit;
</
The function Fit implements least squares approximation of a function defined in the points as specified by the arrays ''x''<sub>''i''</sub> and ''y''<sub>''i''</sub>. The basis φ<sub>''j''</sub> is ''x''<sup>''j''</sup>, ''j''=0,1,..,''N''. The implementation is straightforward. First the plane matrix A is created. A<sub>ji</sub>=φ<sub>''j''</sub>(''x''<sub>''i''</sub>). Then the linear problem AA<sup>''T''</sup>''c''=A''y'' is solved. The result ''c''<sub>''j''</sub> are the coefficients. Constraint_Error is propagated when dimensions of X and Y differ or else when the problem is ill-defined.
===Example===
<code ada>
with Fit;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
Line 46:
Put (C (2), Aft => 3, Exp => 0);
end Fitting;
</
Sample output:
<pre>
Line 155:
{{libheader|numpy}}
<code python>
>>> x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]▼
>>> coeffs = numpy.polyfit(x,y,deg=2)▼
>>> coeffs
</code>
Substitute back received coefficients.
<code python>
</code>
Find max absolute error.
<code python>
</code>
===Example===
For input arrays `x' and `y':
<code python>
>>> y = [2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0]
</code>
<code python>
1.085 N + 10.36 N - 0.6164
</code>
Thus we confirm once more that for already sorted sequences the considered quick sort implementation has quadratic dependence on sequence length (see [[Query Performance|'''Example''' section for Python language on ''Query Performance'' page]]).
|