.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/10_classification_kriging2d.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_10_classification_kriging2d.py: Classification kriging ---------------------- An example of classification kriging .. GENERATED FROM PYTHON SOURCE LINES 7-51 .. rst-class:: sphx-glr-script-out .. code-block:: none /home/docs/checkouts/readthedocs.org/user_builds/pykrige/envs/latest/lib/python3.11/site-packages/sklearn/preprocessing/_discretization.py:296: FutureWarning: The current default behavior, quantile_method='linear', will be changed to quantile_method='averaged_inverted_cdf' in scikit-learn version 1.9 to naturally support sample weight equivalence properties by default. Pass quantile_method='averaged_inverted_cdf' explicitly to silence this warning. warnings.warn( ======================================== classification model: SVC Finished learning classification model Finished kriging residuals Classification Score: 0.212 CK score: 0.6566666666666666 ======================================== classification model: RandomForestClassifier Finished learning classification model Finished kriging residuals Classification Score: 0.5766666666666667 CK score: 0.5946666666666667 ======================================== classification model: LogisticRegression /home/docs/checkouts/readthedocs.org/user_builds/pykrige/envs/latest/lib/python3.11/site-packages/sklearn/linear_model/_logistic.py:473: ConvergenceWarning: lbfgs failed to converge after 10000 iteration(s) (status=1): STOP: TOTAL NO. OF ITERATIONS REACHED LIMIT Increase the number of iterations to improve the convergence (max_iter=10000). You might also want to scale the data as shown in: https://scikit-learn.org/stable/modules/preprocessing.html Please also refer to the documentation for alternative solver options: https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression n_iter_i = _check_optimize_result( Finished learning classification model Finished kriging residuals Classification Score: 0.524 CK score: 0.6553333333333333 | .. code-block:: Python import sys from sklearn.datasets import fetch_california_housing from sklearn.ensemble import RandomForestClassifier from sklearn.linear_model import LogisticRegression from sklearn.model_selection import train_test_split from sklearn.preprocessing import KBinsDiscretizer from sklearn.svm import SVC from pykrige.ck import ClassificationKriging svc_model = SVC(C=0.1, gamma="auto", probability=True) rf_model = RandomForestClassifier(n_estimators=100) lr_model = LogisticRegression(max_iter=10000) models = [svc_model, rf_model, lr_model] try: housing = fetch_california_housing() except PermissionError: # this dataset can occasionally fail to download on Windows sys.exit(0) # take the first 5000 as Kriging is memory intensive p = housing["data"][:5000, :-2] x = housing["data"][:5000, -2:] target = housing["target"][:5000] discretizer = KBinsDiscretizer(encode="ordinal") target = discretizer.fit_transform(target.reshape(-1, 1)) p_train, p_test, x_train, x_test, target_train, target_test = train_test_split( p, x, target, test_size=0.3, random_state=42 ) for m in models: print("=" * 40) print("classification model:", m.__class__.__name__) m_ck = ClassificationKriging(classification_model=m, n_closest_points=10) m_ck.fit(p_train, x_train, target_train) print( "Classification Score: ", m_ck.classification_model.score(p_test, target_test) ) print("CK score: ", m_ck.score(p_test, x_test, target_test)) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 12.715 seconds) .. _sphx_glr_download_examples_10_classification_kriging2d.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 10_classification_kriging2d.ipynb <10_classification_kriging2d.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 10_classification_kriging2d.py <10_classification_kriging2d.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 10_classification_kriging2d.zip <10_classification_kriging2d.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_