Skip to content

Commit

Permalink
Example for preprocessing.dictmapper.DictMapper and `meta.outlier_c…
Browse files Browse the repository at this point in the history
…lassifier.OutlierClassifier` (koaning#646)

* Added an example to preprocessing.dictmapper.DictMapper

* Example added to preprocessing.dictmapper.DictMapper

* Example added to meta.outlierclassifier.OutlierClassifier

* Added example to meta.outlier_classifier.Outlier.Classifier and fixed little mistake I made in outlier_remover

* moved examples to class docstring, used compose.ColumnTransformer in dictmapper

* Update sklego/preprocessing/dictmapper.py

---------

Co-authored-by: Francesco Bruzzesi <[email protected]>
  • Loading branch information
anopsy and FBruzzesi committed Mar 29, 2024
1 parent d321198 commit 14ef241
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
24 changes: 24 additions & 0 deletions sklego/meta/outlier_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,30 @@ class OutlierClassifier(BaseEstimator, ClassifierMixin):
The fitted underlying outlier detection model.
classes_ : array-like of shape (2,)
Classes used for prediction (0 or 1)
Example
-------
```py
from sklearn.ensemble import IsolationForest
from sklego.meta.outlier_classifier import OutlierClassifier
X = [[0], [0.5], [-1], [99]]
y = [0, 0, 0, 1]
isolation_forest = IsolationForest()
outlier_clf = OutlierClassifier(isolation_forest)
_ = outlier_clf.fit(X, y)
preds = outlier_clf.predict([[100], [-0.5], [0.5], [1]])
# array[1. 0. 0. 0.]
proba_preds = outlier_clf.predict_proba([[100], [-0.5], [0.5], [1]])
# [[0.34946567 0.65053433]
# [0.79707913 0.20292087]
# [0.80275406 0.19724594]
# [0.80275406 0.19724594]]
```
"""

def __init__(self, model):
Expand Down
2 changes: 1 addition & 1 deletion sklego/model_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def KlusterFoldValidation(**kwargs):
class ClusterFoldValidation:
"""Cross validator that creates folds based on provided cluster method.
This ensures that data points in the same cluster are not split across different folds.
!!! info "New in version 0.9.0"
Parameters
Expand Down
28 changes: 28 additions & 0 deletions sklego/preprocessing/dictmapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,34 @@ class DictMapper(TransformerMixin, BaseEstimator):
Number of features seen during `fit`.
dim_ : int
Deprecated, please use `n_features_in_` instead.
Example
-------
```py
import pandas as pd
from sklego.preprocessing.dictmapper import DictMapper
from sklearn.compose import ColumnTransformer
X = pd.DataFrame({
"city_pop": ["Amsterdam", "Leiden", "Utrecht", "None", "Haarlem"]
})
mapper = {
"Amsterdam": 1_181_817,
"Leiden": 130_181,
"Utrecht": 367_984,
"Haarlem": 165_396,
}
ct = ColumnTransformer([("dictmapper", DictMapper(mapper, 0), ["city_pop"])])
X_trans = ct.fit_transform(X)
X_trans
# array([[1181817],
# [ 130181],
# [ 367984],
# [ 0],
# [ 165396]])
```
"""

def __init__(self, mapper, default):
Expand Down
2 changes: 1 addition & 1 deletion sklego/preprocessing/outlier_remover.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class OutlierRemover(TrainOnlyTransformerMixin, BaseEstimator):
isolation_forest = IsolationForest()
isolation_forest.fit(X)
detector_preds = isolator_forest.predict(X)
detector_preds = isolation_forest.predict(X)
outlier_remover = OutlierRemover(isolation_forest, refit=True)
outlier_remover.fit(X)
Expand Down

0 comments on commit 14ef241

Please sign in to comment.