.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/03_variogram/01_find_best_model.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_03_variogram_01_find_best_model.py: Finding the best fitting variogram model ---------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 5-11 .. code-block:: Python import numpy as np from matplotlib import pyplot as plt import gstools as gs .. GENERATED FROM PYTHON SOURCE LINES 12-13 Generate a synthetic field with an exponential model. .. GENERATED FROM PYTHON SOURCE LINES 13-20 .. code-block:: Python x = np.random.RandomState(19970221).rand(1000) * 100.0 y = np.random.RandomState(20011012).rand(1000) * 100.0 model = gs.Exponential(dim=2, var=2, len_scale=8) srf = gs.SRF(model, mean=0, seed=19970221) field = srf((x, y)) .. GENERATED FROM PYTHON SOURCE LINES 21-22 Estimate the variogram of the field with 40 bins and plot the result. .. GENERATED FROM PYTHON SOURCE LINES 22-26 .. code-block:: Python bins = np.arange(40) bin_center, gamma = gs.vario_estimate((x, y), field, bins) .. GENERATED FROM PYTHON SOURCE LINES 27-28 Define a set of models to test. .. GENERATED FROM PYTHON SOURCE LINES 28-42 .. code-block:: Python models = { "Gaussian": gs.Gaussian, "Exponential": gs.Exponential, "Matern": gs.Matern, "Stable": gs.Stable, "Rational": gs.Rational, "Circular": gs.Circular, "Spherical": gs.Spherical, "SuperSpherical": gs.SuperSpherical, "JBessel": gs.JBessel, } scores = {} .. GENERATED FROM PYTHON SOURCE LINES 43-44 Iterate over all models, fit their variogram and calculate the r2 score. .. GENERATED FROM PYTHON SOURCE LINES 44-56 .. code-block:: Python # plot the estimated variogram plt.scatter(bin_center, gamma, color="k", label="data") ax = plt.gca() # fit all models to the estimated variogram for model in models: fit_model = models[model](dim=2) para, pcov, r2 = fit_model.fit_variogram(bin_center, gamma, return_r2=True) fit_model.plot(x_max=40, ax=ax) scores[model] = r2 .. image-sg:: /examples/03_variogram/images/sphx_glr_01_find_best_model_001.png :alt: 01 find best model :srcset: /examples/03_variogram/images/sphx_glr_01_find_best_model_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 57-58 Create a ranking based on the score and determine the best models .. GENERATED FROM PYTHON SOURCE LINES 58-65 .. code-block:: Python ranking = sorted(scores.items(), key=lambda item: item[1], reverse=True) print("RANKING by Pseudo-r2 score") for i, (model, score) in enumerate(ranking, 1): print(f"{i:>6}. {model:>15}: {score:.5}") plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none RANKING by Pseudo-r2 score 1. Stable: 0.98218 2. Matern: 0.98176 3. SuperSpherical: 0.98141 4. Exponential: 0.98041 5. Rational: 0.97711 6. Spherical: 0.97334 7. Circular: 0.96725 8. Gaussian: 0.95928 9. JBessel: 0.95831 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.529 seconds) .. _sphx_glr_download_examples_03_variogram_01_find_best_model.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 01_find_best_model.ipynb <01_find_best_model.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 01_find_best_model.py <01_find_best_model.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 01_find_best_model.zip <01_find_best_model.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_