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 |
|
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 |
|
Fit the classifier partially. No-Op.
predict(self, X)
¶
Show source code in classification/functionclassifier.py
61 62 63 64 65 66 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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)