Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions sysidentpy/basis_function/_polynomial.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class Polynomial(BaseBasisFunction):
----------
degree : int (max_degree), default=2
The maximum degree of the polynomial features.
include_bias : bool, default=True
Whether to include the bias (constant) term in the output feature matrix.

Notes
-----
Expand All @@ -37,8 +39,10 @@ class Polynomial(BaseBasisFunction):
def __init__(
self,
degree: int = 2,
include_bias: bool = True,
):
self.degree = degree
self.include_bias = include_bias

def fit(
self,
Expand Down Expand Up @@ -94,6 +98,11 @@ def fit(
]
)
psi = psi[max_lag:, :]

if self.include_bias:
bias_column = np.ones((psi.shape[0], 1))
psi = np.hstack((bias_column, psi))

return psi

def transform(
Expand Down
32 changes: 27 additions & 5 deletions sysidentpy/basis_function/tests/test_basis_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,29 @@


def test_fit_polynomial():
basis_function = Polynomial(degree=2)
basis_function = Polynomial(degree=2, include_bias=True)
data = np.array(([1, 1, 1], [2, 3, 4], [3, 3, 3]))
max_lag = 1
output = np.array([[1, 4, 6, 8, 9, 12, 16], [1, 9, 9, 9, 9, 9, 9]])

r = basis_function.fit(data=data, max_lag=max_lag)

assert_array_equal(output, r)


def test_fit_polynomial_with_bias():
basis_function = Polynomial(degree=2, include_bias=True)
data = np.array(([1, 1, 1], [2, 3, 4], [3, 3, 3]))
max_lag = 1
output = np.array([[1, 4, 6, 8, 9, 12, 16], [1, 9, 9, 9, 9, 9, 9]])

r = basis_function.fit(data=data, max_lag=max_lag)

assert_array_equal(output, r)


def test_fit_polynomial_without_bias():
basis_function = Polynomial(degree=2, include_bias=False)
data = np.array(([1, 1, 1], [2, 3, 4], [3, 3, 3]))
max_lag = 1
output = np.array([[4, 6, 8, 9, 12, 16], [9, 9, 9, 9, 9, 9]])
Expand All @@ -16,11 +38,11 @@ def test_fit_polynomial():


def test_fit_polynomial_predefined():
basis_function = Polynomial(degree=2)
basis_function = Polynomial(degree=2, include_bias=True)
data = np.array(([1, 1, 1], [2, 3, 4], [3, 3, 3]))
max_lag = 1
predefined_regressors = np.array([0, 2, 4])
output = np.array([[4, 8, 12], [9, 9, 9]])
output = np.array([[1, 4, 8, 12], [1, 9, 9, 9]])

r = basis_function.fit(
data=data, max_lag=max_lag, predefined_regressors=predefined_regressors
Expand All @@ -30,10 +52,10 @@ def test_fit_polynomial_predefined():


def test_transform_polynomial():
basis_function = Polynomial(degree=2)
basis_function = Polynomial(degree=2, include_bias=True)
data = np.array(([1, 1, 1], [2, 3, 4], [3, 3, 3]))
max_lag = 1
output = np.array([[4, 6, 8, 9, 12, 16], [9, 9, 9, 9, 9, 9]])
output = np.array([[1, 4, 6, 8, 9, 12, 16], [1, 9, 9, 9, 9, 9, 9]])

r = basis_function.transform(data=data, max_lag=max_lag)

Expand Down