Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example for preprocessing.dictmapper.DictMapper and meta.outlier_classifier.OutlierClassifier #646

Merged
merged 7 commits into from
Mar 29, 2024
51 changes: 24 additions & 27 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 Expand Up @@ -52,33 +76,6 @@ def fit(self, X, y=None):
ValueError
- If the underlying model is not an outlier detection model.
- If the underlying model does not have a `decision_function` method.

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()
isolation_forest.fit(X)
detector_preds = isolation_forest.predict(X)
# array[ 1 1 1 -1]

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]]
```
"""
X, y = check_X_y(X, y, estimator=self)
if not self._is_outlier_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
104 changes: 28 additions & 76 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,
}
FBruzzesi marked this conversation as resolved.
Show resolved Hide resolved

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 All @@ -43,41 +71,6 @@ def fit(self, X, y=None):
-------
self : DictMapper
The fitted transformer.

Example
-------
```py
import pandas as pd
from sklego.preprocessing.dictmapper import DictMapper

X = pd.DataFrame({
"city": ["Amsterdam", "Leiden", "Utrecht", "Amsterdam", "Haarlem"],
"university": ["uva", "lei", "uu", "vu", "none"]
})

mapper = {

#population

"Amsterdam": 1_181_817,
"Leiden": 130_181,
"Utrecht": 367_984,
"Haarlem": 165_396,

#ranking

"uva": 64,
"lei": 214,
"uu": 117,
"vu": 105
}

dict_mapper = DictMapper(mapper, 0)
_ = dict_mapper.fit(X)

dict_mapper.n_features_in_
# 2
```
"""
X = check_array(
X,
Expand Down Expand Up @@ -107,47 +100,6 @@ def transform(self, X):
------
ValueError
If the number of columns from `X` differs from the number of columns when fitting.

Example
-------
```py
import pandas as pd
from sklego.preprocessing.dictmapper import DictMapper

X = pd.DataFrame({
"city": ["Amsterdam", "Leiden", "Utrecht", "Amsterdam", "Haarlem"],
"university": ["uva", "lei", "uu", "vu", "none"]
})

mapper = {

#population

"Amsterdam": 1_181_817,
"Leiden": 130_181,
"Utrecht": 367_984,
"Haarlem": 165_396,

#ranking

"uva": 64,
"lei": 214,
"uu": 117,
"vu": 105
}

dict_mapper = DictMapper(mapper, 0)
_ = dict_mapper.fit(X)

X_trans = dict_mapper.transform(X)
X_trans
# array([[1181817, 64],
# [ 130181, 214],
# [ 367984, 117],
# [1181817, 105],
# [ 165396, 0]])

```
"""
check_is_fitted(self, ["n_features_in_"])
X = check_array(
Expand Down