MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / PolynomialRegression

Class PolynomialRegression

machine_learning/polynomial_regression.py:41–180  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

39
40
41class PolynomialRegression:
42 __slots__ = "degree", "params"
43
44 def __init__(self, degree: int) -> None:
45 """
46 @raises ValueError: if the polynomial degree is negative
47 """
48 if degree < 0:
49 raise ValueError("Polynomial degree must be non-negative")
50
51 self.degree = degree
52 self.params = None
53
54 @staticmethod
55 def _design_matrix(data: np.ndarray, degree: int) -> np.ndarray:
56 """
57 Constructs a polynomial regression design matrix for the given input data. For
58 input data x = (x₁, x₂, ..., xₙ) and polynomial degree m, the design matrix is
59 the Vandermonde matrix
60
61 |1 x₁ x₁² ⋯ x₁ᵐ|
62 X = |1 x₂ x₂² ⋯ x₂ᵐ|
63 |⋮ ⋮ ⋮ ⋱ ⋮ |
64 |1 xₙ xₙ² ⋯ xₙᵐ|
65
66 Reference: https://en.wikipedia.org/wiki/Vandermonde_matrix
67
68 @param data: the input predictor values x, either for model fitting or for
69 prediction
70 @param degree: the polynomial degree m
71 @returns: the Vandermonde matrix X (see above)
72 @raises ValueError: if input data is not N x 1
73
74 >>> x = np.array([0, 1, 2])
75 >>> PolynomialRegression._design_matrix(x, degree=0)
76 array([[1],
77 [1],
78 [1]])
79 >>> PolynomialRegression._design_matrix(x, degree=1)
80 array([[1, 0],
81 [1, 1],
82 [1, 2]])
83 >>> PolynomialRegression._design_matrix(x, degree=2)
84 array([[1, 0, 0],
85 [1, 1, 1],
86 [1, 2, 4]])
87 >>> PolynomialRegression._design_matrix(x, degree=3)
88 array([[1, 0, 0, 0],
89 [1, 1, 1, 1],
90 [1, 2, 4, 8]])
91 >>> PolynomialRegression._design_matrix(np.array([[0, 0], [0 , 0]]), degree=3)
92 Traceback (most recent call last):
93 ...
94 ValueError: Data must have dimensions N x 1
95 """
96 _rows, *remaining = data.shape
97 if remaining:
98 raise ValueError("Data must have dimensions N x 1")

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected