Repository navigation
Expand file tree
/
Copy pathbase_estimator.py
More file actions
172 lines (140 loc) · 4.78 KB
/
Copy pathbase_estimator.py
File metadata and controls
172 lines (140 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
"""
The following file contains base classes for all estimators.
Class design and part of the code is taken and/or influenced
by the Python scikit-learn package, and specifically the
BaseEstimator.py file
# Author: Gilad Green <iml@mail.huji.ac.il>
# License: BSD 3 clause
"""
from __future__ import annotations
from abc import ABC, abstractmethod
from typing import NoReturn
import numpy as np
class BaseEstimator(ABC):
"""
Base class of supervised estimators (classifiers and regressors)
"""
def __init__(self) -> BaseEstimator:
"""
Initialize a supervised estimator instance
Attributes
----------
fitted_ : bool
Indicates if estimator has been fitted. Set by ``self.fit`` function
"""
self.fitted_ = False
def fit(self, X: np.ndarray, y: np.ndarray) -> BaseEstimator:
"""
Fit estimator for given input samples and responses
Parameters
----------
X : ndarray of shape (n_samples, n_features)
Input data to fit an estimator for
y : ndarray of shape (n_samples, )
Responses of input data to fit to
Returns
-------
self : returns an instance of self.
Notes
-----
After fitting sets ``self.fitted_`` attribute to `True`
"""
self._fit(X, y)
self.fitted_ = True
return self
def predict(self, X: np.ndarray) -> np.ndarray:
"""
Predict responses for given samples using fitted estimator
Parameters
----------
X : ndarray of shape (n_samples, n_features)
Input data to predict responses for
Returns
-------
responses : ndarray of shape (n_samples, )
Predicted responses of given samples
Raises
------
ValueError is raised if ``self.predict`` was called before calling ``self.fit``
"""
if not self.fitted_:
raise ValueError("Estimator must first be fitted before calling ``predict``")
return self._predict(X)
def loss(self, X: np.ndarray, y: np.ndarray) -> float:
"""
Evaluate performance under loss function specified for estimator
Parameters
----------
X : ndarray of shape (n_samples, n_features)
Test samples
y : ndarray of shape (n_samples, )
True labels of test samples
Returns
-------
loss : float
Performance under loss function specified for estimator
Raises
------
ValueError is raised if ``self.loss`` was called before calling ``self.fit``
"""
if not self.fitted_:
raise ValueError("Estimator must first be fitted before calling ``loss``")
return self._loss(X, y)
@abstractmethod
def _fit(self, X: np.ndarray, y: np.ndarray) -> NoReturn:
"""
Fit estimator for given input samples and responses
Parameters
----------
X : ndarray of shape (n_samples, n_features)
Input data to fit an estimator for
y : ndarray of shape (n_samples, )
Responses of input data to fit to
"""
raise NotImplementedError()
@abstractmethod
def _predict(self, X: np.ndarray) -> np.ndarray:
"""
Predict responses for given samples using fitted estimator
Parameters
----------
X : ndarray of shape (n_samples, n_features)
Input data to predict responses for
Returns
-------
responses : ndarray of shape (n_samples, )
Predicted responses of given samples
"""
raise NotImplementedError()
@abstractmethod
def _loss(self, X: np.ndarray, y: np.ndarray) -> float:
"""
Evaluate performance under loss function specified for estimator
Parameters
----------
X : ndarray of shape (n_samples, n_features)
Test samples
y : ndarray of shape (n_samples, )
True labels of test samples
Returns
-------
loss : float
Performance under loss function specified for estimator
"""
raise NotImplementedError()
def fit_predict(self, X: np.ndarray, y: np.ndarray) -> np.ndarray:
"""
Fit an estimator over given input data and predict responses for given samples
Parameters
----------
X : ndarray of shape (n_samples, n_features)
Input data to fit an estimator for
y : ndarray of shape (n_samples, )
Responses of input data to fit to
Returns
-------
responses : ndarray of shape (n_samples, )
Predicted responses of given samples
"""
self.fit(X, y)
return self.predict(X)