skgrad
Fast analytic input gradients for fitted scikit-learn models.
skgrad differentiates a fitted model's prediction with respect to its input
features. It provides one NumPy-based interface for supported linear models,
classifiers, and multilayer perceptrons without finite differences, model
conversion, or an automatic-differentiation framework. Unsupported estimators raise TypeError; there is no numerical fallback.
gradient = skgrad.input_gradient(model, X)
For scalar-output models, gradient[i, j] is the derivative of prediction i
with respect to feature j. Multi-output models expose one gradient per target
or the complete input Jacobian.
Read the skgrad documentation for worked examples, the API contract, model coverage, and numerical conventions.
Installation
pip install skgrad
skgrad requires Python 3.9 or later and installs NumPy, scikit-learn, and its
small runtime dependencies automatically.
Quick start
import numpy as np
from sklearn.neural_network import MLPRegressor
import skgrad
rng = np.random.default_rng(0)
X_train = rng.normal(size=(200, 4))
y_train = np.sin(X_train[:, 0]) + X_train[:, 1] * X_train[:, 2]
model = MLPRegressor(
hidden_layer_sizes=(32, 32),
activation="tanh",
max_iter=1000,
random_state=0,
).fit(X_train, y_train)
X_eval = X_train[:5]
values = skgrad.model_output(model, X_eval) # (5, 1)
gradient = skgrad.input_gradient(model, X_eval) # (5, 4)
values, jacobian = skgrad.value_and_jacobian(model, X_eval)
# values: (samples, outputs)
# jacobian: (samples, outputs, features)
For a multi-output model, select one scalar output explicitly:
gradient = skgrad.input_gradient(model, X_eval, target=1)
Use skgrad.supports(model) to check model coverage before calculation.
For a ReLU MLP, a forward pass identifies the active hidden units at each input. Their known slopes and fitted weights then combine in a batched reverse pass. With multiple features, the scalar slopes pictured above become input gradient vectors computed by the same matrix operations.
Supported models
| Family | Models | Differentiated output |
|---|---|---|
| Linear regression | LinearRegression, Ridge, Lasso, ElasticNet |
Prediction |
| Linear classification | LogisticRegression, RidgeClassifier, LinearSVC |
Decision score |
| Linear support-vector regression | LinearSVR |
Prediction |
| Kernel support-vector regression | SVR, NuSVR |
Prediction |
| Binary kernel classification | SVC, NuSVC |
Decision score |
| Neural-network regression | MLPRegressor with squared-error or Poisson loss |
Prediction, including the Poisson exponential output link |
| Neural-network classification | MLPClassifier |
Binary or multiclass logits before logistic/softmax |
| Continuous pipelines | Supported scalers, polynomial expansion, PCA, and fitted feature selectors, then any supported estimator | Final estimator output, differentiated with respect to pipeline input features |
MLP hidden activations may be identity, logistic, tanh, or ReLU. At ReLU's
nondifferentiable origin, skgrad uses a zero derivative, matching
scikit-learn's backpropagation convention. Scalar and multi-output regression,
binary classification, and multiclass classification are supported.
Kernel SVMs support scikit-learn's linear, poly, rbf, and sigmoid
kernels. Multiclass kernel classifiers, callable kernels, and precomputed
kernels are not currently supported.
Tree models are intentionally excluded. Their predictions are piecewise constant, so ordinary gradients are zero almost everywhere and undefined at split boundaries. Use TreeIG, which computes exact Integrated Gradients from the prediction jumps at tree split crossings.
skgrad expects finite dense numeric inputs. Sequential and nested sklearn
pipelines may combine StandardScaler, RobustScaler, MaxAbsScaler,
MinMaxScaler, PolynomialFeatures, PCA, and supported fitted feature
selectors before a supported estimator. Gradients refer to the inputs of the
supplied pipeline, including every supported preprocessing chain rule.
Unknown transformers reject the entire analytic route; preprocessing is never
silently removed. See pipeline conventions
for clipping, whitening, feature selection, and attribution coordinates.
Output semantics
skgrad differentiates prediction functions with respect to input features,
not training losses with respect to fitted parameters.
- Regressors return their prediction output.
- Binary classifiers expose one score or logit for the positive class.
- Multiclass classifiers expose one score or logit per class in
model.classes_order. - Classification probabilities are deliberately not differentiated. Scores and logits compose cleanly with downstream attribution methods and avoid the redundant common direction of multiclass probabilities.
- Values and derivatives preserve normalized input precision across all backends: float32 stays float32, float16 promotes to float32, and float64 stays float64. Parameters are cast for evaluation; sklearn outputs may use a different dtype.
See the shape and output semantics for the complete contract.
Performance
Fast input gradients are the reason skgrad exists. Central finite differences
require two model evaluations per feature. skgrad instead reuses fitted
coefficients for affine models and computes MLP gradients with a forward and
reverse pass, obtaining all feature derivatives together. This matters especially
for Integrated Gradients, which evaluates gradients repeatedly along paths.
In the documented CPU benchmarks, logistic regression and MLP gradients were roughly 14× faster at 10 features, 100–170× at 100 features, and 950–1,300× at 1,000 features than central differences. The tested MLP also achieved speeds comparable to PyTorch CPU autodiff, directly from the fitted scikit-learn model. Results depend on the model, batch size, and runtime environment.
Use input_gradient when you need one output: it avoids constructing the full
multi-output Jacobian. See the performance guide
for accuracy checks, full timing tables, benchmark methodology, and reproducible
scripts.
API
skgrad.supports(model)
skgrad.gradient_properties(model)
skgrad.model_output(model, X)
skgrad.input_gradient(model, X, target=None)
skgrad.input_jacobian(model, X)
skgrad.value_and_jacobian(model, X)
value_and_jacobian is the general composition primitive. Its result contains
values with shape (samples, outputs) and jacobian with shape
(samples, outputs, features). input_gradient is the faster convenience API
when one scalar output is required.
gradient_properties(model) reports useful computational metadata. In
particular, downstream consumers can detect constant affine Jacobians and
avoid redundant evaluations. exact_quadrature_steps also reports when a
supported polynomial pipeline with an affine downstream estimator has a known
finite Gauss–Legendre order for exact straight-path gradient integration.
Scope
skgrad deliberately provides input derivatives, not an explanation method.
It does not choose baselines, perform numerical differentiation, integrate
gradients, calculate parameter gradients, or produce attribution plots. This
narrow scope keeps it useful as a small computational backend that other
packages can compose.
The project is licensed under the BSD 3-Clause License.
Unified IG
Unified IG is an important
consumer of skgrad. It uses skgrad for analytic gradients of supported
smooth scikit-learn models, TreeIG for tree paths, and automatic or numerical
backends for other model families, presenting them through one Integrated
Gradients interface. Use Unified IG when the goal is feature attribution rather
than direct access to model input gradients.
CBaseline constructs reference
baseline distributions; skgrad supplies analytic input derivatives; TreeIG
handles tree paths; UnifiedIG composes these components into attributions.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file skgrad-0.1.6.tar.gz.
File metadata
- Download URL: skgrad-0.1.6.tar.gz
- Upload date:
- Size: 230.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
826663ef4efb5e290e59f8572ade2dcfd3da9fb9e8296c416ee89e97991bc779
|
|
| MD5 |
468e77cbc8949751148205cb6ce267a7
|
|
| BLAKE2b-256 |
7fe0f83066091298fd6f1766c823e9f9e8978e1a25043b9378e942365b56c25b
|
Provenance
The following attestation bundles were made for skgrad-0.1.6.tar.gz:
Publisher:
release.yml on LudgerHentschel/skgrad
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
skgrad-0.1.6.tar.gz -
Subject digest:
826663ef4efb5e290e59f8572ade2dcfd3da9fb9e8296c416ee89e97991bc779 - Sigstore transparency entry: 2753581156
- Sigstore integration time:
-
Permalink:
LudgerHentschel/skgrad@041080f43458b331946a989aee91e5d1d65be0dc -
Branch / Tag:
refs/tags/v0.1.6 - Owner: https://github.com/LudgerHentschel
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@041080f43458b331946a989aee91e5d1d65be0dc -
Trigger Event:
push
-
Statement type:
File details
Details for the file skgrad-0.1.6-py3-none-any.whl.
File metadata
- Download URL: skgrad-0.1.6-py3-none-any.whl
- Upload date:
- Size: 19.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
86d4945c7ec17fd291f6ada203021855f27da50399199f8e83333f4c3e922c63
|
|
| MD5 |
06da7094fb5878aa1cd626f2211a5b37
|
|
| BLAKE2b-256 |
14c3a569152af7c8871fea3f46197e937f684d89d32ae457c02323e43cc1480c
|
Provenance
The following attestation bundles were made for skgrad-0.1.6-py3-none-any.whl:
Publisher:
release.yml on LudgerHentschel/skgrad
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
skgrad-0.1.6-py3-none-any.whl -
Subject digest:
86d4945c7ec17fd291f6ada203021855f27da50399199f8e83333f4c3e922c63 - Sigstore transparency entry: 2753581172
- Sigstore integration time:
-
Permalink:
LudgerHentschel/skgrad@041080f43458b331946a989aee91e5d1d65be0dc -
Branch / Tag:
refs/tags/v0.1.6 - Owner: https://github.com/LudgerHentschel
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@041080f43458b331946a989aee91e5d1d65be0dc -
Trigger Event:
push
-
Statement type: