Adapters
Summary
effector engines take a plain numpy-in / numpy-out callable. The adapters are facilitation only: each one returns such a callable for a common model object — you call the adapter, look at what it gave you, and make the final pass into the constructor yourself. Nothing in effector auto-detects or silently wraps a model.
import effector
# sklearn regressor / pipeline
model = effector.adapters.from_sklearn(estimator)
# classifier: explain P(class = k), one explanation per class
model = effector.adapters.classifier_proba(clf, class_="yes")
# torch module (jacobian=True also builds the autograd jacobian for RHALE/DerPDP)
model = effector.adapters.from_torch(net)
model, model_jac = effector.adapters.from_torch(net, jacobian=True)
# the explicit handshake: probe the wrapper on two rows of your data
effector.adapters.check(model, X)
pdp = effector.PDP(X, model, schema=schema) # the final pass is yours
Every returned callable also validates its own output on each call (shape
(N,), numeric) and raises a named, actionable error instead of letting a
wrong shape travel into a kernel.
API
effector.adapters
Model adapters — wrappers that turn common model objects into the plain callable effector needs.
Every engine takes a numpy-in / numpy-out callable f(X: (N, D)) -> (N,).
Each adapter here returns one; check probes it on two rows of your data
before you build an engine.
model = effector.adapters.from_sklearn(est)
effector.adapters.check(model, X)
pdp = effector.PDP(X, model, schema=schema) # the final pass is yours
Facilitation only
Adapters only return callables — nothing in effector auto-detects or silently wraps a model. You call the adapter, look at what it gave you, and pass it to the constructor yourself.
Every returned callable validates its own output on each call (shape (N,),
numeric) and raises a named, actionable error instead of letting a wrong shape
travel into a kernel. sklearn / torch are never imported at module import
time — only inside the adapter that needs them.
Functions:
| Name | Description |
|---|---|
check |
Probe a model wrapper on two rows of your data — the explicit handshake. |
classifier_proba |
Wrap a classifier into a numpy->numpy callable for one class' probability. |
from_sklearn |
Wrap an sklearn-style regressor into a numpy->numpy callable. |
from_torch |
Wrap a torch module into numpy->numpy callable(s). |
check(model, X, model_jac=None)
Probe a model wrapper on two rows of your data — the explicit handshake.
model = effector.adapters.from_sklearn(est)
effector.adapters.check(model, X) # raises loudly, or is silent
pdp = effector.PDP(X, model, schema=schema)
Call it right before constructing an engine; it is the only place a model
call happens outside the engines, and you trigger it. Only X[:2] is
evaluated, so the probe is instant even for slow models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
the callable you are about to pass to an engine. |
required | |
X
|
your data (2-D numpy array); only |
required | |
model_jac
|
optional jacobian callable; probed for shape |
None
|
Returns:
| Type | Description |
|---|---|
None
|
None — silence means the wrapper is numpy-in / numpy-out with the |
None
|
shapes effector expects. |
Raises:
| Type | Description |
|---|---|
TypeError
|
|
ValueError
|
|
Source code in effector/adapters.py
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
classifier_proba(estimator, class_=1)
Wrap a classifier into a numpy->numpy callable for one class' probability.
model = effector.adapters.classifier_proba(clf, class_="yes")
pdp = effector.PDP(X, model, schema=schema)
The per-class probability is effector's classification story: the returned
callable computes predict_proba(X)[:, k] — the surface P(class = k) —
which every engine explains like any regression output. One explanation
per class; loop over classes yourself if you want them all.
How class_ is resolved
Against estimator.classes_ at wrap time: label match first,
positional column index as fallback for plain ints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
estimator
|
a fitted object with |
required | |
class_
|
the class to explain — a label from |
1
|
Returns:
| Type | Description |
|---|---|
Callable
|
a plain callable |
Raises:
| Type | Description |
|---|---|
TypeError
|
no |
ValueError
|
|
Source code in effector/adapters.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 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 | |
from_sklearn(estimator)
Wrap an sklearn-style regressor into a numpy->numpy callable.
model = effector.adapters.from_sklearn(est)
pdp = effector.PDP(X, model, schema=schema)
The returned function calls estimator.predict and validates the
output (shape (N,), numeric) on every call.
Classifiers are rejected
Anything with predict_proba raises — class labels are not a
regression surface. Explain a per-class probability instead:
effector.adapters.classifier_proba(clf, class_=1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
estimator
|
a fitted object with |
required |
Returns:
| Type | Description |
|---|---|
Callable
|
a plain callable |
Callable
|
yourself. |
Raises:
| Type | Description |
|---|---|
TypeError
|
the object has no |
ValueError
|
the object is a classifier (has |
Source code in effector/adapters.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | |
from_torch(module, device=None, jacobian=False)
Wrap a torch module into numpy->numpy callable(s).
model = effector.adapters.from_torch(net)
model, model_jac = effector.adapters.from_torch(net, jacobian=True)
rhale = effector.RHALE(X, model, model_jac, schema=schema)
The forward wrapper puts the module in eval mode, runs under no_grad,
and moves tensors to/from device. torch is imported lazily — effector
gains no torch dependency.
jacobian=True returns a tuple
You get (model, model_jac), not a single callable. The jacobian
is built on autograd via the sum-backward trick — valid because each
row's output depends only on that row's input — and is exactly what
RHALE / DerPDP want.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
a |
required | |
device
|
torch device for inference; default = the module's own. |
None
|
|
jacobian
|
bool
|
also build |
False
|
Returns:
| Type | Description |
|---|---|
|
|
|
|
|
Source code in effector/adapters.py
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | |