Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from typing import Sequence, Union
- from numbers import Real
- from regressor_abc import RegressorABC
- #regressors
- class NonparametricRegressor(RegressorABC):
- _k: int
- train: list
- def __init__(self, k: int):
- self._k = k
- def fit(self, abscissa: Sequence[Real], ordinates: Sequence[Real]) -> None:
- train = zip(abscissa, ordinates)
- def predict(self, abscissa: Union[Real, Sequence[Real]]) -> list:
- return abscissa
- def _predict(self, x: Real):
- ro = [abs(x - xi) for xi, yi in self.train]
- ro.sort()
- h = ro[self._k][0]
- K = [self._K(roi / h) for roi, _ in ro]
- y = sum(ro[i][1] * K[i] for i in range(len(ro)))
- y /= sum(K)
- return y
- @staticmethod
- def _K(self, x):
- if abs(x) <= 1:
- return 3 / 4 * (1 - x ** 2)
- return 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement