• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mmschlk / shapiq / 17372021070

01 Sep 2025 08:26AM UTC coverage: 93.704% (-0.2%) from 93.906%
17372021070

Pull #431

github

web-flow
Merge 7d2c68dac into 68f21c1d6
Pull Request #431: Product kernel explainer

180 of 203 new or added lines in 14 files covered. (88.67%)

4 existing lines in 2 files now uncovered.

5090 of 5432 relevant lines covered (93.7%)

0.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

74.07
/src/shapiq/explainer/product_kernel/conversion.py
1
"""Functions for converting scikit-learn models to a format used by shapiq."""
2

3
from __future__ import annotations
1✔
4

5
from typing import TYPE_CHECKING
1✔
6

7
from shapiq.explainer.product_kernel.base import ProductKernelModel
1✔
8

9
if TYPE_CHECKING:
1✔
NEW
10
    from shapiq.typing import Model
×
11

12

13
def convert_svm(model: Model) -> ProductKernelModel:
1✔
14
    """Converts a scikit-learn SVM model to the product kernel format used by shapiq.
15

16
    Args:
17
        model: The scikit-learn SVM model to convert. Can be either a binary support vector classifier (SVC) or a support vector regressor (SVR).
18

19
    Returns:
20
        ProductKernelModel: The converted model in the product kernel format.
21

22
    """
23
    X_train = model.support_vectors_
1✔
24
    n, d = X_train.shape
1✔
25

26
    if hasattr(model, "kernel"):
1✔
27
        kernel_type = model.kernel
1✔
28
        if kernel_type != "rbf":
1✔
29
            msg = "Currently only RBF kernel is supported for SVM models."
1✔
30
            raise ValueError(msg)
1✔
31
    else:
NEW
32
        msg = "Kernel type not found in the model. Ensure the model is a valid SVC or SVR."
×
NEW
33
        raise ValueError(msg)
×
34

35
    return ProductKernelModel(
1✔
36
        alpha=model.dual_coef_.flatten(),
37
        X_train=X_train,
38
        n=n,
39
        d=d,
40
        gamma=model._gamma,  # noqa: SLF001
41
        kernel_type=kernel_type,
42
        intercept=model.intercept_[0],
43
    )  # TODO (IsaH57): check if gamma is always needed or just when rbf is used (Issue #425)
44

45

46
def convert_gp_reg(model: Model) -> ProductKernelModel:
1✔
47
    """Converts a scikit-learn Gaussian Process Regression model to the product kernel format used by shapiq.
48

49
    Args:
50
        model: The scikit-learn Gaussian Process Regression model to convert.
51

52
    Returns:
53
        ProductKernelModel: The converted model in the product kernel format.
54

55
    """
56
    X_train = model.X_train_
1✔
57
    n, d = X_train.shape
1✔
58

59
    if hasattr(model, "kernel"):
1✔
60
        kernel_type = model.kernel_.__class__.__name__.lower()  # Get the kernel type as a string
1✔
61
        if kernel_type != "rbf":
1✔
NEW
62
            msg = "Currently only RBF kernel is supported for Gaussian Process Regression models."
×
NEW
63
            raise ValueError(msg)
×
64
    else:
NEW
65
        msg = "Kernel type not found in the model. Ensure the model is a valid Gaussian Process Regressor."
×
NEW
66
        raise ValueError(msg)
×
67

68
    return ProductKernelModel(
1✔
69
        alpha=model.alpha_.flatten(),
70
        X_train=X_train,
71
        n=n,
72
        d=d,
73
        gamma=(2 * (model.kernel_.length_scale**2)) ** -1,
74
        kernel_type=kernel_type,
75
    )
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc