Skip to content

Commit

Permalink
Add ClassificationKriging
Browse files Browse the repository at this point in the history
  • Loading branch information
mralbu committed Aug 31, 2020
1 parent 48bc433 commit daa1d3d
Show file tree
Hide file tree
Showing 4 changed files with 474 additions and 12 deletions.
12 changes: 12 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Kriging algorithms
* ``OrdinaryKriging3D``: 3D ordinary kriging
* ``UniversalKriging3D``: 3D universal kriging
* ``RegressionKriging``: An implementation of Regression-Kriging
* ``ClassificationKriging``: An implementation of Simplicial Indicator Kriging


Wrappers
Expand Down Expand Up @@ -109,6 +110,17 @@ the ``OrdinaryKriging`` or the ``UniversalKriging`` class, and performs a correc
A demonstration of the regression kriging is provided in the
`corresponding example <http://pykrige.readthedocs.io/en/latest/examples/07_regression_kriging2d.html#sphx-glr-examples-07-regression-kriging2d-py>`_.

Classification Kriging
------------------

`Simplifical Indicator kriging <https://www.sciencedirect.com/science/article/abs/pii/S1002070508600254>`_ can be performed
with `pykrige.rk.ClassificationKriging <http://pykrige.readthedocs.io/en/latest/examples/10_classification_kriging2d.html>`_.
This class takes as parameters a scikit-learn classification model, and details of either either
the ``OrdinaryKriging`` or the ``UniversalKriging`` class, and performs a correction steps on the ML classification prediction.

A demonstration of the classification kriging is provided in the
`corresponding example <http://pykrige.readthedocs.io/en/latest/examples/10_classification_kriging2d.html#sphx-glr-examples-10-classification-kriging2d-py>`_.


License
^^^^^^^
Expand Down
49 changes: 49 additions & 0 deletions examples/10_classification_kriging2d.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""
Classification kriging
------------------
An example of classification kriging
"""

import sys

from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import fetch_california_housing
from sklearn.preprocessing import KBinsDiscretizer

from pykrige.rk import ClassificationKriging
from pykrige.compat import train_test_split

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("regression 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))
Loading

0 comments on commit daa1d3d

Please sign in to comment.