|
| 1 | +from pykrige.ok import OrdinaryKriging |
| 2 | +import matplotlib.pyplot as plt |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +plt.style.use("ggplot") |
| 6 | + |
| 7 | +np.random.seed(42) |
| 8 | + |
| 9 | +x = np.linspace(0, 12.5, 50) |
| 10 | +xpred = np.linspace(0, 12.5, 393) |
| 11 | +y = np.sin(x) * np.exp(-0.25 * x) + np.random.normal(-0.25, 0.25, 50) |
| 12 | + |
| 13 | +# compare OrdinaryKriging as an exact and non exact interpolator |
| 14 | +uk = OrdinaryKriging( |
| 15 | + x, np.zeros(x.shape), y, variogram_model="linear", exact_values=False |
| 16 | +) |
| 17 | +uk_exact = OrdinaryKriging(x, np.zeros(x.shape), y, variogram_model="linear") |
| 18 | + |
| 19 | +y_pred, y_std = uk.execute("grid", xpred, np.array([0.0]), backend="loop") |
| 20 | +y_pred_exact, y_std_exact = uk_exact.execute( |
| 21 | + "grid", xpred, np.array([0.0]), backend="loop" |
| 22 | +) |
| 23 | + |
| 24 | + |
| 25 | +y_pred = np.squeeze(y_pred) |
| 26 | +y_std = np.squeeze(y_std) |
| 27 | + |
| 28 | +y_pred_exact = np.squeeze(y_pred_exact) |
| 29 | +y_std_exact = np.squeeze(y_std_exact) |
| 30 | + |
| 31 | + |
| 32 | +fig, ax = plt.subplots(1, 1, figsize=(10, 4)) |
| 33 | + |
| 34 | +ax.scatter(x, y, label="Input Data") |
| 35 | +ax.plot(xpred, y_pred_exact, label="Exact Prediction") |
| 36 | +ax.plot(xpred, y_pred, label="Non Exact Prediction") |
| 37 | + |
| 38 | +ax.fill_between( |
| 39 | + xpred, |
| 40 | + y_pred - 3 * y_std, |
| 41 | + y_pred + 3 * y_std, |
| 42 | + alpha=0.3, |
| 43 | + label="Confidence interval", |
| 44 | +) |
| 45 | +ax.legend(loc=9) |
| 46 | +ax.set_ylim(-1.8, 1.3) |
| 47 | +ax.legend(loc=9) |
| 48 | +plt.xlabel("X") |
| 49 | +plt.ylabel("Field") |
| 50 | +plt.show() |
0 commit comments