forked from ciortanmadalina/modality_tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpolator1d.py
51 lines (37 loc) · 1.78 KB
/
interpolator1d.py
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
from sklearn.base import BaseEstimator
from pykrige import OrdinaryKriging
import scipy.interpolate
from collections import Counter
import matplotlib.mlab as mlab
class Interpolator1D(BaseEstimator):
def __init__(self, interpolation_type):
self.interpolation_type = interpolation_type
def fit_density(self, X):
if frequency_interpolation_type =='linear_density':
self.model = scipy.interpolate.interp1d(
list(Counter(X).keys()), list(Counter(X).values()),fill_value="extrapolate")
if self.interpolation_type == 'kde':
self.model = mlab.GaussianKDE(X, 'scott')
def fit(self, X, y):
if self.interpolation_type == 'kriging':
# pykrige doesn't support 1D data for now, only 2D or 3D
# adapting the 1D input to 2D
self.model = OrdinaryKriging(X, np.zeros(X.shape), y, variogram_model='gaussian')
if self.interpolation_type == 'linear':
self.model = scipy.interpolate.interp1d(X, y,fill_value="extrapolate")
if self.interpolation_type =='gmm':
self.model= cluster_utils.gmr(X, y)
def predict(self, X_pred):
if self.interpolation_type == 'kriging':
y_pred, y_std = self.model.execute('grid', X_pred, np.array([0.]))
return np.squeeze(y_pred)
if self.interpolation_type in ['linear', 'linear_density']:
return self.model(X_pred)
if self.interpolation_type == 'kde':
return self.model.evaluate(X_pred)
if self.interpolation_type == 'gmm':
return self.model.predict(np.array([0]), X_pred[:, np.newaxis]).ravel()
model = Interpolator1D('linear')
model.fit(X, y)
pred = model.predict(X_pred)
plt.plot(X_pred, pred)