-
Notifications
You must be signed in to change notification settings - Fork 3
/
usage.py
114 lines (101 loc) · 2.42 KB
/
usage.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
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
# imports
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from GPy.kern import Matern32
from polire import (
Random,
Trend,
Spline,
IDW,
Kriging,
SpatialAverage,
NaturalNeighbor,
GP,
)
# sample data
X = [[0, 0], [0, 3], [3, 0], [3, 3]]
y = [0, 1.5, 1.5, 3]
X = np.array(X)
y = np.array(y)
regressors = [
Random(),
SpatialAverage(),
Spline(kx=1, ky=1),
Trend(),
IDW(coordinate_type="Geographic"),
Kriging(),
GP(Matern32(input_dim=2)),
]
def test_grid():
# Gridded interpolation testing
print("\nTesting on small dataset")
for r in regressors:
r.fit(X, y)
y_pred = r.predict_grid()
Z = y_pred
sns.heatmap(Z)
plt.title(r)
plt.show()
plt.close()
print("\nTesting completed on a small dataset\n")
print("\nTesting on a reasonable dataset")
df = pd.read_csv("tests/data/30-03-18.csv")
X1 = np.array(df[["longitude", "latitude"]])
y1 = np.array(df["value"])
for r in regressors:
r.fit(X1, y1)
y_pred = r.predict_grid()
Z = y_pred
sns.heatmap(Z)
plt.title(r)
plt.show()
plt.close()
def test_point():
# Pointwise interpolation testing
for r in regressors:
r.fit(X, y)
test_data = [
[0, 0],
[0, 3],
[3, 0],
[3, 3],
[1, 1],
[1.5, 1.5],
[2, 2],
[2.5, 2.5],
[4, 4],
]
y_pred = r.predict(np.array(test_data))
print(r)
print(y_pred)
def test_nn():
print("\nNatural Neighbors - Point Wise")
nn = NaturalNeighbor()
df = pd.read_csv("tests/data/30-03-18.csv")
X = np.array(df[["longitude", "latitude"]])
y = np.array(df["value"])
nn.fit(X, y)
test_data = [[77.16, 28.70], X[0]]
y_pred = nn.predict(np.array(test_data))
print(y_pred)
del nn
print("\nNatural Neighbors - Entire Grid")
# Suggested by Apoorv as a temporary fix
# Patience pays
nn = NaturalNeighbor()
nn.fit(X, y)
y_pred = nn.predict_grid()
print(y_pred)
sns.heatmap(y_pred)
plt.title(nn)
plt.show()
plt.close()
if __name__ == "__main__":
print("Testing Gridded Interpolation")
test_grid()
print("\nTesting Pointwise Interpolation")
test_point()
print("\nTesting Natural Neighbors")
test_nn()