adstk 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- adstk/__init__.py +10 -0
- adstk/inspect/__init__.py +8 -0
- adstk/inspect/ice.py +164 -0
- adstk/py.typed +0 -0
- adstk-0.0.1.dist-info/METADATA +107 -0
- adstk-0.0.1.dist-info/RECORD +8 -0
- adstk-0.0.1.dist-info/WHEEL +4 -0
- adstk-0.0.1.dist-info/licenses/LICENSE +18 -0
adstk/__init__.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"""adstk - Actuarial Data Science Toolkit (for Python)
|
|
2
|
+
|
|
3
|
+
ADSTK aims to bundle together relevant actuarial models, tools and code simplifications to enrich the insurance Data Science experience in the Python ecosystem.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
__version__ = "0.1.0"
|
|
9
|
+
|
|
10
|
+
__all__ = ["__version__"]
|
adstk/inspect/ice.py
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
"""Individual Conditional Expectation (ICE) curves.
|
|
2
|
+
|
|
3
|
+
Individual Conditional Expectation (ICE) curves show how a fitted model's
|
|
4
|
+
prediction for a single instance changes as one feature is varied while
|
|
5
|
+
all other features are held fixed. Averaging ICE curves across instances
|
|
6
|
+
recovers the Partial Dependence Plot (PDP).
|
|
7
|
+
|
|
8
|
+
Part of ``adstk.inspect``. Public API:
|
|
9
|
+
|
|
10
|
+
- :func:`create_ice_curves` computes the raw ICE curve data.
|
|
11
|
+
- :func:`plot_ice_curves` computes ICE curves for one or more features
|
|
12
|
+
and renders them, overlaying the PDP.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
from typing import Optional, Sequence, Tuple
|
|
18
|
+
|
|
19
|
+
import numpy as np
|
|
20
|
+
import pandas as pd
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def create_ice_curves(
|
|
24
|
+
model,
|
|
25
|
+
X: pd.DataFrame,
|
|
26
|
+
feature_name: str,
|
|
27
|
+
n_points: int = 50,
|
|
28
|
+
feature_range: Optional[Tuple[float, float]] = None,
|
|
29
|
+
) -> Tuple[np.ndarray, np.ndarray]:
|
|
30
|
+
"""Compute Individual Conditional Expectation (ICE) curves.
|
|
31
|
+
|
|
32
|
+
For every row in ``X``, ``feature_name`` is swept across ``n_points``
|
|
33
|
+
values spanning ``feature_range`` (or the observed min/max) while all
|
|
34
|
+
other feature values are held at their originally observed values,
|
|
35
|
+
and the model's prediction is recorded at each point.
|
|
36
|
+
|
|
37
|
+
Parameters
|
|
38
|
+
----------
|
|
39
|
+
model : object
|
|
40
|
+
A fitted estimator exposing ``predict(X: np.ndarray) -> np.ndarray``.
|
|
41
|
+
X : pandas.DataFrame
|
|
42
|
+
Instances to compute curves for, with columns in the same order
|
|
43
|
+
the model expects.
|
|
44
|
+
feature_name : str
|
|
45
|
+
Column of ``X`` to vary.
|
|
46
|
+
n_points : int, default=50
|
|
47
|
+
Number of grid points to evaluate the feature at.
|
|
48
|
+
feature_range : tuple(float, float), optional
|
|
49
|
+
``(min, max)`` to sweep over. Defaults to the observed range of
|
|
50
|
+
``feature_name`` in ``X``.
|
|
51
|
+
|
|
52
|
+
Returns
|
|
53
|
+
-------
|
|
54
|
+
feature_values : numpy.ndarray, shape (n_points,)
|
|
55
|
+
The grid ``feature_name`` was evaluated at.
|
|
56
|
+
ice_curves : numpy.ndarray, shape (len(X), n_points)
|
|
57
|
+
``ice_curves[i, j]`` is the prediction for row ``i`` of ``X``
|
|
58
|
+
with ``feature_name`` fixed at ``feature_values[j]``.
|
|
59
|
+
|
|
60
|
+
Raises
|
|
61
|
+
------
|
|
62
|
+
KeyError
|
|
63
|
+
If ``feature_name`` is not a column of ``X``.
|
|
64
|
+
ValueError
|
|
65
|
+
If ``X`` has no rows, or ``feature_range`` is decreasing.
|
|
66
|
+
"""
|
|
67
|
+
if feature_name not in X.columns:
|
|
68
|
+
raise KeyError(
|
|
69
|
+
f"'{feature_name}' is not a column of X. "
|
|
70
|
+
f"Available columns: {list(X.columns)}"
|
|
71
|
+
)
|
|
72
|
+
if len(X) == 0:
|
|
73
|
+
raise ValueError("X must contain at least one row.")
|
|
74
|
+
|
|
75
|
+
if feature_range is None:
|
|
76
|
+
feature_min = X[feature_name].min()
|
|
77
|
+
feature_max = X[feature_name].max()
|
|
78
|
+
else:
|
|
79
|
+
feature_min, feature_max = feature_range
|
|
80
|
+
if feature_min > feature_max:
|
|
81
|
+
raise ValueError(
|
|
82
|
+
f"feature_range must not be decreasing, got ({feature_min}, {feature_max})."
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
feature_values = np.linspace(feature_min, feature_max, n_points)
|
|
86
|
+
|
|
87
|
+
n_samples = len(X)
|
|
88
|
+
feature_idx = X.columns.get_loc(feature_name)
|
|
89
|
+
|
|
90
|
+
# Build a single batch of (n_samples * n_points) modified rows so
|
|
91
|
+
# predict() is called once instead of n_samples * n_points times.
|
|
92
|
+
# This is the main cost driver for ICE curves at any real n_points.
|
|
93
|
+
batch = np.repeat(X.to_numpy(), n_points, axis=0)
|
|
94
|
+
batch[:, feature_idx] = np.tile(feature_values, n_samples)
|
|
95
|
+
|
|
96
|
+
predictions = np.asarray(model.predict(batch)).reshape(n_samples, n_points)
|
|
97
|
+
|
|
98
|
+
return feature_values, predictions
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def plot_ice_curves(
|
|
102
|
+
model,
|
|
103
|
+
X: pd.DataFrame,
|
|
104
|
+
feature_list: Sequence[str],
|
|
105
|
+
n_points: int = 50,
|
|
106
|
+
n_ice_lines: int = 25,
|
|
107
|
+
random_state: Optional[int] = None,
|
|
108
|
+
):
|
|
109
|
+
"""Plot ICE curves (with overlaid PDP) for one or more features.
|
|
110
|
+
|
|
111
|
+
Parameters
|
|
112
|
+
----------
|
|
113
|
+
model : object
|
|
114
|
+
A fitted estimator exposing ``predict``.
|
|
115
|
+
X : pandas.DataFrame
|
|
116
|
+
Instances to compute curves for. Pass a sample rather than the
|
|
117
|
+
full dataset -- cost scales with ``len(X) * n_points``.
|
|
118
|
+
feature_list : sequence of str
|
|
119
|
+
Feature columns to plot, one subplot each.
|
|
120
|
+
n_points : int, default=50
|
|
121
|
+
Grid resolution per feature.
|
|
122
|
+
n_ice_lines : int, default=25
|
|
123
|
+
Number of individual ICE curves to draw per subplot (drawing
|
|
124
|
+
all of ``len(X)`` gets unreadable fast).
|
|
125
|
+
random_state : int, optional
|
|
126
|
+
Seed controlling which instances are sampled for display.
|
|
127
|
+
|
|
128
|
+
Returns
|
|
129
|
+
-------
|
|
130
|
+
fig : matplotlib.figure.Figure
|
|
131
|
+
axes : numpy.ndarray of matplotlib.axes.Axes
|
|
132
|
+
"""
|
|
133
|
+
try:
|
|
134
|
+
import matplotlib.pyplot as plt
|
|
135
|
+
except ImportError as exc: # pragma: no cover
|
|
136
|
+
raise ImportError(
|
|
137
|
+
"plot_ice_curves requires matplotlib. Install it with "
|
|
138
|
+
"`pip install matplotlib`."
|
|
139
|
+
) from exc
|
|
140
|
+
|
|
141
|
+
rng = np.random.default_rng(random_state)
|
|
142
|
+
|
|
143
|
+
fig, axes = plt.subplots(1, len(feature_list), figsize=(5 * len(feature_list), 4))
|
|
144
|
+
axes = np.atleast_1d(axes)
|
|
145
|
+
|
|
146
|
+
for ax, feature in zip(axes, feature_list):
|
|
147
|
+
feature_vals, ice_data = create_ice_curves(model, X, feature, n_points)
|
|
148
|
+
|
|
149
|
+
n_lines = min(n_ice_lines, len(ice_data))
|
|
150
|
+
sample_indices = rng.choice(len(ice_data), n_lines, replace=False)
|
|
151
|
+
for idx in sample_indices:
|
|
152
|
+
ax.plot(feature_vals, ice_data[idx], alpha=0.4, color="tab:blue", linewidth=1)
|
|
153
|
+
|
|
154
|
+
pdp = np.mean(ice_data, axis=0)
|
|
155
|
+
ax.plot(feature_vals, pdp, color="tab:red", linewidth=2, label="PDP")
|
|
156
|
+
|
|
157
|
+
ax.set_xlabel(feature)
|
|
158
|
+
ax.set_ylabel("Prediction")
|
|
159
|
+
ax.set_title(f"ICE: {feature}")
|
|
160
|
+
ax.grid(True, alpha=0.3)
|
|
161
|
+
ax.legend()
|
|
162
|
+
|
|
163
|
+
fig.tight_layout()
|
|
164
|
+
return fig, axes
|
adstk/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: adstk
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: ADSTK aims to bundle together relevant actuarial models, tools and code simplifications to enrich the Actuarial / Insurance Data Science experience in the Python ecosystem.
|
|
5
|
+
Project-URL: Homepage, https://codeberg.org/ftranschel/adstk
|
|
6
|
+
Project-URL: Repository, https://codeberg.org/ftranschel/adstk
|
|
7
|
+
Project-URL: Issues, https://codeberg.org/ftranschel/adstk/issues
|
|
8
|
+
Project-URL: Documentation, https://ftranschel.codeberg.page/adstk/
|
|
9
|
+
Author-email: Fabian Transchel <ftranschel@hs-harz.de>
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: actuarial,aktuariat,data science,inspection,insurance,pricing,telematics
|
|
13
|
+
Classifier: Development Status :: 2 - Pre-Alpha
|
|
14
|
+
Classifier: Intended Audience :: Science/Research
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Topic :: Office/Business :: Financial
|
|
21
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
22
|
+
Classifier: Typing :: Typed
|
|
23
|
+
Requires-Python: >=3.10
|
|
24
|
+
Requires-Dist: numpy>=1.24
|
|
25
|
+
Requires-Dist: pandas>=1.5
|
|
26
|
+
Requires-Dist: scipy>=1.10
|
|
27
|
+
Provides-Extra: docs
|
|
28
|
+
Requires-Dist: mkdocs-material>=9.5; extra == 'docs'
|
|
29
|
+
Requires-Dist: mkdocstrings[python]>=0.25; extra == 'docs'
|
|
30
|
+
Provides-Extra: test
|
|
31
|
+
Requires-Dist: matplotlib; extra == 'test'
|
|
32
|
+
Requires-Dist: pytest; extra == 'test'
|
|
33
|
+
Requires-Dist: pytest-cov; extra == 'test'
|
|
34
|
+
Requires-Dist: scikit-learn; extra == 'test'
|
|
35
|
+
Description-Content-Type: text/markdown
|
|
36
|
+
|
|
37
|
+
# adstk
|
|
38
|
+
|
|
39
|
+
ADSTK aims to bundle together relevant actuarial models, tools and code simplifications to enrich the Actuarial / Insurance Data Science experience in the Python ecosystem.
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
## Features
|
|
43
|
+
|
|
44
|
+
ADSTK currently ships with a limited number of methods and use cases:
|
|
45
|
+
|
|
46
|
+
* Partial dependence display with support for individual conditional expectation curves (ICE)
|
|
47
|
+
|
|
48
|
+
### Dependencies
|
|
49
|
+
|
|
50
|
+
adstk requires:
|
|
51
|
+
|
|
52
|
+
- Python (>= 3.10)
|
|
53
|
+
- NumPy (>= 1.24)
|
|
54
|
+
- SciPy (>= 1.10)
|
|
55
|
+
|
|
56
|
+
Optional requirements for plotting etc.
|
|
57
|
+
|
|
58
|
+
- matplotlib ()
|
|
59
|
+
- plotly ()
|
|
60
|
+
|
|
61
|
+
### User installation
|
|
62
|
+
|
|
63
|
+
The easiest way to install adstk will using `pip` once we publish on PyPi:
|
|
64
|
+
|
|
65
|
+
pip install -U adstk
|
|
66
|
+
|
|
67
|
+
## Changelog
|
|
68
|
+
|
|
69
|
+
See the [changelog](CHANGELOG.md) for a history of notable changes to adstk.
|
|
70
|
+
|
|
71
|
+
## Development
|
|
72
|
+
|
|
73
|
+
We welcome human contributors of all experience levels. The adstk community
|
|
74
|
+
goals are to be helpful, welcoming, and effective. The
|
|
75
|
+
[Contributing guide](CONTRIBUTING.md) has detailed information about
|
|
76
|
+
contributing code, documentation, tests, and more. Please also note our
|
|
77
|
+
[AI contribution policy](AGENTS.md) before submitting anything.
|
|
78
|
+
|
|
79
|
+
### Important links
|
|
80
|
+
|
|
81
|
+
- Official source code repo: https://codeberg.org/ftranschel/adstk
|
|
82
|
+
- Download releases: https://pypi.org/project/adstk/
|
|
83
|
+
- Issue tracker: https://codeberg.org/ftranschel/adstk/issues
|
|
84
|
+
|
|
85
|
+
### Source code
|
|
86
|
+
|
|
87
|
+
You can check the latest sources with the command:
|
|
88
|
+
|
|
89
|
+
git clone https://codeberg.org/ftranschel/adstk.git
|
|
90
|
+
|
|
91
|
+
### Contributing
|
|
92
|
+
|
|
93
|
+
To learn more about making a contribution to adstk, please see our
|
|
94
|
+
[Contributing guide](CONTRIBUTING.md).
|
|
95
|
+
|
|
96
|
+
### Testing
|
|
97
|
+
|
|
98
|
+
After installation, you can launch the test suite from the source
|
|
99
|
+
directory (you will need `pytest` installed):
|
|
100
|
+
|
|
101
|
+
pytest
|
|
102
|
+
|
|
103
|
+
### Submitting a Pull Request
|
|
104
|
+
|
|
105
|
+
Before opening a pull request, please read the
|
|
106
|
+
[Contributing guide](CONTRIBUTING.md) and our [AI policy](AGENTS.md) to
|
|
107
|
+
make sure your submission complies with our guidelines.
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
adstk/__init__.py,sha256=i1s3uKNCDPNGDrwjRwbGcTRwbVbf3Oe7LTXIUVjNRT8,311
|
|
2
|
+
adstk/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
+
adstk/inspect/__init__.py,sha256=nr4ahX1CCr-dHBCBFhJxXz1RpBCI60ZS5ziC6mGu7C4,164
|
|
4
|
+
adstk/inspect/ice.py,sha256=Hv0Nf-AAnDjZSGY2ms_cLj5THUxoufQinDF716lZfgg,5597
|
|
5
|
+
adstk-0.0.1.dist-info/METADATA,sha256=MtNTh4hwcmV-4Dtzwa9JCF2ltWHy7hQtq7S3Sc5KTeg,3526
|
|
6
|
+
adstk-0.0.1.dist-info/WHEEL,sha256=THafob7ofN-NsuMN7Mg4qZyHaQI7KkD-QlcQatYhXPo,87
|
|
7
|
+
adstk-0.0.1.dist-info/licenses/LICENSE,sha256=5aeYhO-7OQsJ9KjaxWdN8JYQ8YarQr700N15fdtejp4,1067
|
|
8
|
+
adstk-0.0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ftranschel
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
|
6
|
+
associated documentation files (the "Software"), to deal in the Software without restriction, including
|
|
7
|
+
without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
|
|
9
|
+
following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
12
|
+
portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
|
15
|
+
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
|
|
16
|
+
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
17
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
18
|
+
USE OR OTHER DEALINGS IN THE SOFTWARE.
|