Advertisement
Korotkodul

sem_10.06

Oct 4th, 2024
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. from typing import Sequence, Union
  2. from numbers import Real
  3.  
  4. from regressor_abc import RegressorABC
  5.  
  6. #regressors
  7. class NonparametricRegressor(RegressorABC):
  8.     _k: int
  9.     train: list
  10.  
  11.     def __init__(self, k: int):
  12.         self._k = k
  13.  
  14.     def fit(self, abscissa: Sequence[Real], ordinates: Sequence[Real]) -> None:
  15.         train = zip(abscissa, ordinates)
  16.  
  17.     def predict(self, abscissa: Union[Real, Sequence[Real]]) -> list:
  18.        
  19.         return abscissa
  20.    
  21.     def _predict(self, x: Real):
  22.         ro = [abs(x - xi) for xi, yi in self.train]
  23.         ro.sort()
  24.        
  25.         h = ro[self._k][0]
  26.         K = [self._K(roi / h) for roi, _ in ro]
  27.         y = sum(ro[i][1] * K[i] for i in range(len(ro)))
  28.         y /= sum(K)
  29.         return y
  30.    
  31.     @staticmethod
  32.     def _K(self, x):
  33.         if abs(x) <= 1:
  34.             return 3 / 4 * (1 - x ** 2)
  35.         return 0
  36.        
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement