from hulearn.classification import *

FunctionClassifier

This class allows you to pass a function to make the predictions you're interested in.

Parameters

Name Type Description Default
func the function that can make predictions required
**kwargs extra keyword arguments will be pass to the function, can be grid-search-able {}

The functions that are passed need to be pickle-able. That means no lambda functions!

Usage:

import numpy as np
import pandas as pd
from sklearn.model_selection import GridSearchCV

from hulearn.datasets import load_titanic
from hulearn.classification import FunctionClassifier

df = load_titanic(as_frame=True)
X, y = df.drop(columns=['survived']), df['survived']

def class_based(dataf, sex='male', pclass=1):
    predicate = (dataf['sex'] == sex) & (dataf['pclass'] == pclass)
    return np.array(predicate).astype(int)

mod = FunctionClassifier(class_based, pclass=10)
params = {'pclass': [1, 2, 3], 'sex': ['male', 'female']}
grid = GridSearchCV(mod, cv=3, param_grid=params).fit(X, y)
pd.DataFrame(grid.cv_results_)

fit(self, X, y)

Show source code in classification/functionclassifier.py
43
44
45
46
47
48
49
50
    def fit(self, X, y):
        """
        Fit the classifier. No-Op.
        """
        # Run it to confirm no error happened.
        self.fitted_ = True
        _ = self.func(X, **self.kwargs)
        return self

Fit the classifier. No-Op.

partial_fit(self, X, y, classes=None, sample_weight=None)

Show source code in classification/functionclassifier.py
52
53
54
55
56
57
58
59
    def partial_fit(self, X, y, classes=None, sample_weight=None):
        """
        Fit the classifier partially. No-Op.
        """
        # Run it to confirm no error happened.
        self.fitted_ = True
        _ = self.func(X, **self.kwargs)
        return self

Fit the classifier partially. No-Op.

predict(self, X)

Show source code in classification/functionclassifier.py
61
62
63
64
65
66
    def predict(self, X):
        """
        Make predictions using the passed function.
        """
        check_is_fitted(self, ["fitted_"])
        return self.func(X, **self.kwargs)

Make predictions using the passed function.

InteractiveClassifier

This tool allows you to take a drawn model and use it as a classifier.

Parameters

Name Type Description Default
json_desc chart data in dictionary form required
smoothing smoothing to apply to poly-counts 0.001
refit if True, you no longer need to call .fit(X, y) in order to .predict(X) True

Usage:

from sklego.datasets import load_penguins
from hulearn.experimental.interactive import InteractiveCharts

df = load_penguins(as_frame=True)
charts = InteractiveCharts(df, labels="species")

# Next notebook cell
charts.add_chart(x="bill_length_mm", y="bill_depth_mm")
# Next notebook cell
charts.add_chart(x="flipper_length_mm", y="body_mass_g")

# After drawing a model, export the data
json_data = charts.data()

# You can now use your drawn intuition as a model!
from hulearn.classification.interactive import InteractiveClassifier
clf = InteractiveClassifier(clf_data)
X, y = df.drop(columns=['species']), df['species']

# This doesn't do anything. But scikit-learn demands it.
clf.fit(X, y)

# This makes predictions, based on your drawn model.
# It can also be used in `GridSearchCV` for benchmarking!
clf.predict(X)

fit(self, X, y)

Show source code in classification/interactiveclassifier.py
114
115
116
117
118
119
120
    def fit(self, X, y):
        """
        Fit the classifier. Bit of a formality, it's not doing anything specifically.
        """
        self.classes_ = list(self.json_desc[0]["polygons"].keys())
        self.fitted_ = True
        return self

Fit the classifier. Bit of a formality, it's not doing anything specifically.

from_json(path, smoothing=0.001, refit=True) (classmethod)

Show source code in classification/interactiveclassifier.py
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    @classmethod
    def from_json(cls, path, smoothing=0.001, refit=True):
        """
        Load the classifier from json stored on disk.

        Arguments:
            path: path of the json file
            smoothing: smoothing to apply to poly-counts
            refit: if `True`, you no longer need to call `.fit(X, y)` in order to `.predict(X)`

        Usage:

        ```python
        from hulearn.classification import InteractiveClassifier

        InteractiveClassifier.from_json("path/to/file.json")
        ```
        """
        json_desc = json.loads(pathlib.Path(path).read_text())
        return InteractiveClassifier(
            json_desc=json_desc, smoothing=smoothing, refit=refit
        )

Load the classifier from json stored on disk.

Parameters

Name Type Description Default
path path of the json file required
smoothing smoothing to apply to poly-counts 0.001
refit if True, you no longer need to call .fit(X, y) in order to .predict(X) True

Usage:

from hulearn.classification import InteractiveClassifier

InteractiveClassifier.from_json("path/to/file.json")

predict(self, X)

Show source code in classification/interactiveclassifier.py
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
    def predict(self, X):
        """
        Predicts the class for each item in `X`.

        Usage:

        ```python
        from hulearn.classification import InteractiveClassifier
        clf = InteractiveClassifier(clf_data)
        X, y = load_data(...)

        # This doesn't do anything. But scikit-learn demands it.
        clf.fit(X, y)

        # This makes predictions, based on your drawn model.
        clf.predict(X)
        ```
        """
        check_is_fitted(self, ["classes_", "fitted_"])
        return np.array(
            [self.classes_[i] for i in self.predict_proba(X).argmax(axis=1)]
        )

Predicts the class for each item in X.

Usage:

from hulearn.classification import InteractiveClassifier
clf = InteractiveClassifier(clf_data)
X, y = load_data(...)

# This doesn't do anything. But scikit-learn demands it.
clf.fit(X, y)

# This makes predictions, based on your drawn model.
clf.predict(X)

predict_proba(self, X)

Show source code in classification/interactiveclassifier.py
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
    def predict_proba(self, X):
        """
        Predicts the associated probabilities for each class.

        Usage:

        ```python
        from hulearn.classification import InteractiveClassifier
        clf = InteractiveClassifier(clf_data)
        X, y = load_data(...)

        # This doesn't do anything. But scikit-learn demands it.
        clf.fit(X, y)

        # This makes predictions, based on your drawn model.
        clf.predict_proba(X)
        ```
        """
        # Because we're not doing anything during training, for convenience this
        # method can formally "fit" during the predict call. This is a scikit-learn
        # anti-pattern so we allow you to turn this off.
        if self.refit:
            if not self.fitted_:
                self.fit(X)
        check_is_fitted(self, ["classes_", "fitted_"])
        if isinstance(X, pd.DataFrame):
            hits = [
                self._count_hits(self.poly_data, x[1].to_dict()) for x in X.iterrows()
            ]
        else:
            hits = [
                self._count_hits(self.poly_data, {k: v for k, v in enumerate(x)})
                for x in X
            ]
        count_arr = (
            np.array([[h[c] for c in self.classes_] for h in hits]) + self.smoothing
        )
        return count_arr / count_arr.sum(axis=1).reshape(-1, 1)

Predicts the associated probabilities for each class.

Usage:

from hulearn.classification import InteractiveClassifier
clf = InteractiveClassifier(clf_data)
X, y = load_data(...)

# This doesn't do anything. But scikit-learn demands it.
clf.fit(X, y)

# This makes predictions, based on your drawn model.
clf.predict_proba(X)