@@ -62,34 +62,35 @@ First we will create a 2D dataset together with the associated x, y grids,
62
62
import numpy as np
63
63
import pykrige.kriging_tools as kt
64
64
from pykrige.ok import OrdinaryKriging
65
-
65
+
66
66
data = np.array([[0.3 , 1.2 , 0.47 ],
67
67
[1.9 , 0.6 , 0.56 ],
68
68
[1.1 , 3.2 , 0.74 ],
69
69
[3.3 , 4.4 , 1.47 ],
70
70
[4.7 , 3.8 , 1.74 ]])
71
-
71
+
72
72
gridx = np.arange(0.0 , 5.5 , 0.5 )
73
73
gridy = np.arange(0.0 , 5.5 , 0.5 )
74
-
74
+
75
75
# Create the ordinary kriging object. Required inputs are the X-coordinates of
76
76
# the data points, the Y-coordinates of the data points, and the Z-values of the
77
77
# data points. If no variogram model is specified, defaults to a linear variogram
78
78
# model. If no variogram model parameters are specified, then the code automatically
79
- # calculates the parameters by fitting the variogram model to the binned
79
+ # calculates the parameters by fitting the variogram model to the binned
80
80
# experimental semivariogram. The verbose kwarg controls code talk-back, and
81
81
# the enable_plotting kwarg controls the display of the semivariogram.
82
82
OK = OrdinaryKriging(data[:, 0 ], data[:, 1 ], data[:, 2 ], variogram_model = ' linear' ,
83
83
verbose = False , enable_plotting = False )
84
-
84
+
85
85
# Creates the kriged grid and the variance grid. Allows for kriging on a rectangular
86
86
# grid of points, on a masked rectangular grid of points, or with arbitrary points.
87
87
# (See OrdinaryKriging.__doc__ for more information.)
88
88
z, ss = OK .execute(' grid' , gridx, gridy)
89
-
89
+
90
90
# Writes the kriged grid to an ASCII grid file.
91
91
kt.write_asc_grid(gridx, gridy, z, filename = " output.asc" )
92
92
93
+
93
94
Universal Kriging Example
94
95
^^^^^^^^^^^^^^^^^^^^^^^^^
95
96
@@ -111,16 +112,17 @@ Universal Kriging Example
111
112
# the data points, the Y-coordinates of the data points, and the Z-values of the
112
113
# data points. Variogram is handled as in the ordinary kriging case.
113
114
# drift_terms is a list of the drift terms to include; currently supported terms
114
- # are 'regional_linear', 'point_log', and 'external_Z'. Refer to
115
+ # are 'regional_linear', 'point_log', and 'external_Z'. Refer to
115
116
# UniversalKriging.__doc__ for more information.
116
117
UK = UniversalKriging(data[:, 0 ], data[:, 1 ], data[:, 2 ], variogram_model = ' linear' ,
117
118
drift_terms = [' regional_linear' ])
118
-
119
+
119
120
# Creates the kriged grid and the variance grid. Allows for kriging on a rectangular
120
121
# grid of points, on a masked rectangular grid of points, or with arbitrary points.
121
122
# (See UniversalKriging.__doc__ for more information.)
122
123
z, ss = UK .execute(' grid' , gridx, gridy)
123
124
125
+
124
126
Three-Dimensional Kriging Example
125
127
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
126
128
@@ -140,44 +142,84 @@ Three-Dimensional Kriging Example
140
142
gridy = np.arange(0.0 , 0.6 , 0.01 )
141
143
gridz = np.arange(0.0 , 0.6 , 0.1 )
142
144
143
- # Create the 3D ordinary kriging object and solves for the three-dimension kriged
145
+ # Create the 3D ordinary kriging object and solves for the three-dimension kriged
144
146
# volume and variance. Refer to OrdinaryKriging3D.__doc__ for more information.
145
147
ok3d = OrdinaryKriging3D(data[:, 0 ], data[:, 1 ], data[:, 2 ], data[:, 3 ],
146
148
variogram_model = ' linear' )
147
149
k3d, ss3d = ok3d.execute(' grid' , gridx, gridy, gridz)
148
150
149
- # Create the 3D universal kriging object and solves for the three-dimension kriged
151
+ # Create the 3D universal kriging object and solves for the three-dimension kriged
150
152
# volume and variance. Refer to UniversalKriging3D.__doc__ for more information.
151
- uk3d = UniversalKriging3D(data[:, 0 ], data[:, 1 ], data[:, 2 ], data[:, 3 ],
153
+ uk3d = UniversalKriging3D(data[:, 0 ], data[:, 1 ], data[:, 2 ], data[:, 3 ],
152
154
variogram_model = ' linear' , drift_terms = [' regional_linear' ])
153
155
k3d, ss3d = uk3d.execute(' grid' , gridx, gridy, gridz)
154
156
155
- # To use the generic 'specified' drift term, the user must provide the drift values
156
- # at each data point and at every grid point. The following example is equivalent to
157
+ # To use the generic 'specified' drift term, the user must provide the drift values
158
+ # at each data point and at every grid point. The following example is equivalent to
157
159
# using a linear drift in all three spatial dimensions. Refer to
158
160
# UniversalKriging3D.__doc__ for more information.
159
161
zg, yg, xg = np.meshgrid(gridz, gridy, gridx, indexing = ' ij' )
160
- uk3d = UniversalKriging3D(data[:, 0 ], data[:, 1 ], data[:, 2 ], data[:, 3 ],
162
+ uk3d = UniversalKriging3D(data[:, 0 ], data[:, 1 ], data[:, 2 ], data[:, 3 ],
161
163
variogram_model = ' linear' , drift_terms = [' specified' ],
162
164
specified_drift = [data[:, 0 ], data[:, 1 ]])
163
165
k3d, ss3d = uk3d.execute(' grid' , gridx, gridy, gridz, specified_drift_arrays = [xg, yg, zg])
164
166
165
- # To use the generic 'functional' drift term, the user must provide a callable
166
- # function that takes only the spatial dimensions as arguments. The following example
167
- # is equivalent to using a linear drift only in the x-direction. Refer to
167
+ # To use the generic 'functional' drift term, the user must provide a callable
168
+ # function that takes only the spatial dimensions as arguments. The following example
169
+ # is equivalent to using a linear drift only in the x-direction. Refer to
168
170
# UniversalKriging3D.__doc__ for more information.
169
171
func = lambda x , y , z : x
170
- uk3d = UniversalKriging3D(data[:, 0 ], data[:, 1 ], data[:, 2 ], data[:, 3 ],
172
+ uk3d = UniversalKriging3D(data[:, 0 ], data[:, 1 ], data[:, 2 ], data[:, 3 ],
171
173
variogram_model = ' linear' , drift_terms = [' functional' ],
172
174
functional_drift = [func])
173
175
k3d, ss3d = uk3d.execute(' grid' , gridx, gridy, gridz)
174
176
175
- # Note that the use of the 'specified' and 'functional' generic drift capabilities is
176
- # essentially identical in the two-dimensional universal kriging class (except for a
177
- # difference in the number of spatial coordinates for the passed drift functions).
177
+ # Note that the use of the 'specified' and 'functional' generic drift capabilities is
178
+ # essentially identical in the two-dimensional universal kriging class (except for a
179
+ # difference in the number of spatial coordinates for the passed drift functions).
178
180
# See UniversalKriging.__doc__ for more information.
179
181
180
182
183
+ GSTools covariance models
184
+ ^^^^^^^^^^^^^^^^^^^^^^^^^
185
+
186
+ You can also use `GSTools <https://github.com/GeoStat-Framework/GSTools >`_
187
+ covariance models as input for the ``variogram_model `` in the
188
+ PyKrige routines:
189
+
190
+ .. code :: python
191
+
192
+ import numpy as np
193
+ from gstools import Gaussian
194
+ from pykrige.ok import OrdinaryKriging
195
+ from matplotlib import pyplot as plt
196
+
197
+ # conditioning data
198
+ data = np.array([[0.3 , 1.2 , 0.47 ],
199
+ [1.9 , 0.6 , 0.56 ],
200
+ [1.1 , 3.2 , 0.74 ],
201
+ [3.3 , 4.4 , 1.47 ],
202
+ [4.7 , 3.8 , 1.74 ]])
203
+ # grid definition for output field
204
+ gridx = np.arange(0.0 , 5.5 , 0.1 )
205
+ gridy = np.arange(0.0 , 6.5 , 0.1 )
206
+ # a GSTools based covariance model
207
+ cov_model = Gaussian(dim = 2 , len_scale = 1 , anis = 0.2 , angles = - 0.5 , var = 0.5 , nugget = 0.1 )
208
+ # ordinary kriging with pykrige
209
+ OK1 = OrdinaryKriging(data[:, 0 ], data[:, 1 ], data[:, 2 ], cov_model)
210
+ z1, ss1 = OK1 .execute(' grid' , gridx, gridy)
211
+ plt.imshow(z1, origin = " lower" )
212
+ plt.show()
213
+
214
+ Which gives:
215
+
216
+ .. image :: https://raw.githubusercontent.com/GeoStat-Framework/GSTools/master/docs/source/pics/20_pykrige.png
217
+ :width: 400px
218
+ :align: center
219
+
220
+ Have a look at the `documentation about the Covariance Model of GSTools <https://geostat-framework.readthedocs.io/projects/gstools/en/latest/tutorial_02_cov.html >`_.
221
+
222
+
181
223
Kriging Parameters Tuning
182
224
^^^^^^^^^^^^^^^^^^^^^^^^^
183
225
@@ -194,8 +236,8 @@ Regression Kriging
194
236
with `pykrige.rk.RegressionKriging <http://pykrige.readthedocs.io/en/latest/examples/regression_kriging2d.html >`_.
195
237
This class takes as parameters a scikit-learn regression model, and details of either either
196
238
the ``OrdinaryKriging `` or the ``UniversalKriging `` class, and performs a correction steps on the ML regression prediction.
197
-
198
- A demonstration of the regression kriging is provided in the
239
+
240
+ A demonstration of the regression kriging is provided in the
199
241
`corresponding example <http://pykrige.readthedocs.io/en/latest/examples/regression_kriging2d.html#sphx-glr-examples-regression-kriging2d-py >`_.
200
242
201
243
License
0 commit comments