shapley-value-regression 0.0.0__tar.gz
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.
- shapley_value_regression-0.0.0/LICENSE +21 -0
- shapley_value_regression-0.0.0/PKG-INFO +185 -0
- shapley_value_regression-0.0.0/README.md +168 -0
- shapley_value_regression-0.0.0/pyproject.toml +34 -0
- shapley_value_regression-0.0.0/setup.cfg +4 -0
- shapley_value_regression-0.0.0/src/docs/source/conf.py +51 -0
- shapley_value_regression-0.0.0/src/shapley/__init__.py +3 -0
- shapley_value_regression-0.0.0/src/shapley/data/Distance_Metro_KM.csv +31 -0
- shapley_value_regression-0.0.0/src/shapley/data/death.csv +3142 -0
- shapley_value_regression-0.0.0/src/shapley/data/load.py +119 -0
- shapley_value_regression-0.0.0/src/shapley/models/__init__.py +0 -0
- shapley_value_regression-0.0.0/src/shapley/models/shapley.py +188 -0
- shapley_value_regression-0.0.0/src/shapley_value_regression.egg-info/PKG-INFO +185 -0
- shapley_value_regression-0.0.0/src/shapley_value_regression.egg-info/SOURCES.txt +16 -0
- shapley_value_regression-0.0.0/src/shapley_value_regression.egg-info/dependency_links.txt +1 -0
- shapley_value_regression-0.0.0/src/shapley_value_regression.egg-info/requires.txt +3 -0
- shapley_value_regression-0.0.0/src/shapley_value_regression.egg-info/top_level.txt +2 -0
- shapley_value_regression-0.0.0/test/test.py +64 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sarbadal Pal
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: shapley-value-regression
|
|
3
|
+
Version: 0.0.0
|
|
4
|
+
Summary: A simple library to calculate Shapley values for regression models using pandas and scikit-learn.
|
|
5
|
+
Author: Sarbadal Pal
|
|
6
|
+
License: MIT
|
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
|
8
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Requires-Python: >=3.10
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Requires-Dist: pandas>=2.0
|
|
14
|
+
Requires-Dist: scikit-learn>=1.3
|
|
15
|
+
Requires-Dist: tabulate>=0.10
|
|
16
|
+
Dynamic: license-file
|
|
17
|
+
|
|
18
|
+
# Shapley Value Regression
|
|
19
|
+
|
|
20
|
+

|
|
21
|
+

|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
# Introduction:
|
|
25
|
+
|
|
26
|
+
In the econometric literature multicollinearity is defined as the incidence of
|
|
27
|
+
high degree of correlation among some or all regressor variables. Strong multicollinearity has
|
|
28
|
+
deleterious effects on the confidence intervals of linear regression coefficients (β in the linear
|
|
29
|
+
regression model y=Xβ+u). Although it does not affect the explanatory power ($R^2$) of the
|
|
30
|
+
regressors or unbiasedness of the estimated coefficients associated with them, it does inflate
|
|
31
|
+
their standard error of estimate rendering test of hypothesis misleading or paradoxical, often
|
|
32
|
+
such that although $R^2$ could be very high, individual coefficients may all have poor Student’s tvalues.
|
|
33
|
+
Thus, strong multicollinearity may lead to failure in rejecting a false null hypothesis of
|
|
34
|
+
ineffectiveness of the regressor variable to the regressand variable (type II error). Very
|
|
35
|
+
frequently, it also affects the sign of the regression coefficients. However, it has been pointed
|
|
36
|
+
out that the incidence of high degree of correlation (measured in terms of a large condition
|
|
37
|
+
number; Belsley et al., 1980) among some or all regressor variables alone (unsupported by large
|
|
38
|
+
variance of error in the regressand variable, y) has little effect on the precision of regression
|
|
39
|
+
coefficients. Large condition number coupled with a large variance of error in the regressand
|
|
40
|
+
variable destabilizes the regression estimator; either of the two in isolation cannot cause much
|
|
41
|
+
harm, although the condition number is relatively more potent in determining the stability of
|
|
42
|
+
estimated regression coefficients (Mishra, 2004-a).
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
### Shapley value regression:
|
|
46
|
+
> This is an entirely different strategy to assess the contribution of
|
|
47
|
+
regressor variables to the regressand variable. It owes its origin in the theory of cooperative
|
|
48
|
+
games (Shapley, 1953). The value of $R^2$ obtained by fitting a linear regression model y=Xβ+u is
|
|
49
|
+
considered as the value of a cooperative game played by X (whose members, xj ϵ X; j=1, m,
|
|
50
|
+
work in a coalition) against y (explaining it). The analyst does not have enough information to
|
|
51
|
+
disentangle the contributions made by the individual members xj ϵ X; j=1, m, but only their joint
|
|
52
|
+
1 contribution ($R^2$) is known. The Shapley value decomposition imputes the most likely
|
|
53
|
+
contribution of each individual xj ϵ X; j=1, m, to $R^2$.
|
|
54
|
+
|
|
55
|
+
### An algorithm to impute the contribution of individual variables to Shapley value:
|
|
56
|
+
>Let there be m number of regressor variables in the model y=Xβ+u. Let X(p, r) be the r-membered subset
|
|
57
|
+
of X in which the pth regressor appears and X(q, r) be the r-membered subset of X in which the
|
|
58
|
+
pth regressor does not appear. Further, let $R^2(p, r)$ be the $R^2$ obtained by regression of y on X(p,
|
|
59
|
+
r) and $R^2(q, r)$ be the $R^2$ obtained by regression of y on X(q, r). Then, the share of the regressor
|
|
60
|
+
variable p (that is xp ϵ X) is given by
|
|
61
|
+
|
|
62
|
+
$$
|
|
63
|
+
S(p) = \frac{1}{m}\frac{\sum_{i=1}^{m}\left[R^2(p,r) - R^2(q, r-1)\right]}{k}.
|
|
64
|
+
$$
|
|
65
|
+
|
|
66
|
+
Moreover, $R^2(q,0) = 0$. Here k is the number of cases in which the evaluation in [.] was carried
|
|
67
|
+
out. The sum of all $S(p)$ for p=1, m (that is, $\sum_{p=1}^{m}S(p)$) is the $R^2$ of y=Xβ+u : (all xj ϵ X) or the
|
|
68
|
+
total value of the game:
|
|
69
|
+
|
|
70
|
+
$$
|
|
71
|
+
R^2 = \sum_{p=1}^{m}S(p) = \sum_{p=1}^{m}\frac{1}{m}\sum_{r=1}^{k}\frac{\sum_{c=1}^{k}\left[R^2(p,r)-R^2(q, r-1)\right]}{k}.
|
|
72
|
+
$$
|
|
73
|
+
|
|
74
|
+
### Computational details of share of $X_j$ in $R^2$:
|
|
75
|
+
|
|
76
|
+
|r | r-1 |x1 |x2 |x3 |x4 |$R^2$ |K | operation | values | Sum/k | Grand value |
|
|
77
|
+
|---|:----|:--|:--|:--|:--|:-------|:---|:------------|:--------|:----------|:-----------:|
|
|
78
|
+
|4 | |1 |2 |3 |4 |0.98237 | |plus |+0.98237 | | |
|
|
79
|
+
| |3 | |2 |3 |4 |0.97282 | |minus |-0.97282 | | |
|
|
80
|
+
| | | | | | | |k=1 |Sum/k | |0.009556 | |
|
|
81
|
+
|3 | |1 |2 |3 | |0.98228 | |plus |+0.98228 | | |
|
|
82
|
+
|3 | |1 |2 | |4 |0.98233 | |plus |+0.98233 | | |
|
|
83
|
+
|3 | |1 | |3 |4 |0.98128 | |plus |+0.98128 | | |
|
|
84
|
+
| |2 | |2 |3 | |0.84702 | |minus |-0.84702 | | |
|
|
85
|
+
| |2 | |2 |3 | |0.68006 | |minus |-0.68006 | | |
|
|
86
|
+
| |2 | |2 |3 | |0.93529 | |minus |-0.93529 | | |
|
|
87
|
+
| | | | | | | |k=3 |Sum/k | |0.161175 | |
|
|
88
|
+
|2 | |1 |2 | | |0.97867 | |plus |+0.97867 | | |
|
|
89
|
+
|2 | |1 | |3 | |0.54816 | |plus |+0.54816 | | |
|
|
90
|
+
|2 | |1 | | |4 |0.97247 | |plus |+0.97247 | | |
|
|
91
|
+
| |1 | |2 | | |0.66626 | |minus |-0.66626 | | |
|
|
92
|
+
| |1 | | |3 | |0.28587 | |minus |-0.28587 | | |
|
|
93
|
+
| |1 | | | |4 |0.67454 | |minus |-0.67454 | | |
|
|
94
|
+
| | | | | | | |k=3 |Sum/k | |0.290878 | |
|
|
95
|
+
|1 | |1 | | | |0.53394 | |plus |+0.53394 | | |
|
|
96
|
+
| | | | | | | |k=1 |Sum/k | |0.533948 | |
|
|
97
|
+
| | | | | | | | |Sum(sum/k)/m | | |**0.248889** |
|
|
98
|
+
|
|
99
|
+
## Time Complexity
|
|
100
|
+
|
|
101
|
+
For estimated execution time and scalability details by number of regressors, see [TIMEComplexity.md](TIMEComplexity.md).
|
|
102
|
+
|
|
103
|
+
## Example: Use as a package
|
|
104
|
+
|
|
105
|
+
Install locally in editable mode from the project root:
|
|
106
|
+
|
|
107
|
+
```bash
|
|
108
|
+
pip install -e .
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Then import and use the package in Python:
|
|
112
|
+
|
|
113
|
+
```python
|
|
114
|
+
from shapley import ShapleyValue
|
|
115
|
+
from shapley.data.load import get_dataset_info, load_dataset
|
|
116
|
+
|
|
117
|
+
dataset_key = "death_rate"
|
|
118
|
+
|
|
119
|
+
info = get_dataset_info(dataset_key)
|
|
120
|
+
df = load_dataset(dataset_key)
|
|
121
|
+
|
|
122
|
+
sv = ShapleyValue(df, info["default_X"], info["target"])
|
|
123
|
+
contributions = sv.get_shapley_contribution(verbose=False)
|
|
124
|
+
|
|
125
|
+
print(contributions)
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Expected output format:
|
|
129
|
+
|
|
130
|
+
```text
|
|
131
|
+
Regressor Share
|
|
132
|
+
lower_95_confidence_interval_for_death_rate <float>
|
|
133
|
+
upper_95_confidence_interval_for_death_rate <float>
|
|
134
|
+
recent_5_year_trend_2_in_death_rates <float>
|
|
135
|
+
average_deaths_per_year <float>
|
|
136
|
+
Total <float>
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Get contribution for a single regressor:
|
|
140
|
+
|
|
141
|
+
```python
|
|
142
|
+
contribution, details = sv.get_shapley_contribution_of("average_deaths_per_year")
|
|
143
|
+
print(contribution)
|
|
144
|
+
print(details.head())
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
Available packaged datasets:
|
|
148
|
+
|
|
149
|
+
- `death_rate`
|
|
150
|
+
- `distance_metro`
|
|
151
|
+
|
|
152
|
+
## Example: Use your own pandas DataFrame
|
|
153
|
+
|
|
154
|
+
You can also pass your own DataFrame directly, as long as selected feature columns and target column are numeric.
|
|
155
|
+
|
|
156
|
+
```python
|
|
157
|
+
import pandas as pd
|
|
158
|
+
|
|
159
|
+
from shapley import ShapleyValue
|
|
160
|
+
|
|
161
|
+
# User-provided dataset
|
|
162
|
+
df = pd.DataFrame(
|
|
163
|
+
{
|
|
164
|
+
"sq_feet": [950, 1100, 1300, 1600, 1800, 2100],
|
|
165
|
+
"age_years": [20, 15, 12, 10, 8, 5],
|
|
166
|
+
"bedrooms": [2, 2, 3, 3, 4, 4],
|
|
167
|
+
"distance_km": [8.0, 7.2, 6.5, 5.0, 4.0, 3.0],
|
|
168
|
+
"price_usd": [220000, 250000, 290000, 340000, 390000, 450000],
|
|
169
|
+
}
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
X = ["sq_feet", "age_years", "bedrooms", "distance_km"]
|
|
173
|
+
y = "price_usd"
|
|
174
|
+
|
|
175
|
+
sv = ShapleyValue(df, X, y)
|
|
176
|
+
|
|
177
|
+
# All feature contributions
|
|
178
|
+
contributions = sv.get_shapley_contribution(verbose=False)
|
|
179
|
+
print(contributions)
|
|
180
|
+
|
|
181
|
+
# Single feature contribution and detailed intermediate table
|
|
182
|
+
sq_feet_share, sq_feet_details = sv.get_shapley_contribution_of("sq_feet")
|
|
183
|
+
print("sq_feet share:", sq_feet_share)
|
|
184
|
+
print(sq_feet_details.head())
|
|
185
|
+
```
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
# Shapley Value Regression
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
# Introduction:
|
|
8
|
+
|
|
9
|
+
In the econometric literature multicollinearity is defined as the incidence of
|
|
10
|
+
high degree of correlation among some or all regressor variables. Strong multicollinearity has
|
|
11
|
+
deleterious effects on the confidence intervals of linear regression coefficients (β in the linear
|
|
12
|
+
regression model y=Xβ+u). Although it does not affect the explanatory power ($R^2$) of the
|
|
13
|
+
regressors or unbiasedness of the estimated coefficients associated with them, it does inflate
|
|
14
|
+
their standard error of estimate rendering test of hypothesis misleading or paradoxical, often
|
|
15
|
+
such that although $R^2$ could be very high, individual coefficients may all have poor Student’s tvalues.
|
|
16
|
+
Thus, strong multicollinearity may lead to failure in rejecting a false null hypothesis of
|
|
17
|
+
ineffectiveness of the regressor variable to the regressand variable (type II error). Very
|
|
18
|
+
frequently, it also affects the sign of the regression coefficients. However, it has been pointed
|
|
19
|
+
out that the incidence of high degree of correlation (measured in terms of a large condition
|
|
20
|
+
number; Belsley et al., 1980) among some or all regressor variables alone (unsupported by large
|
|
21
|
+
variance of error in the regressand variable, y) has little effect on the precision of regression
|
|
22
|
+
coefficients. Large condition number coupled with a large variance of error in the regressand
|
|
23
|
+
variable destabilizes the regression estimator; either of the two in isolation cannot cause much
|
|
24
|
+
harm, although the condition number is relatively more potent in determining the stability of
|
|
25
|
+
estimated regression coefficients (Mishra, 2004-a).
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
### Shapley value regression:
|
|
29
|
+
> This is an entirely different strategy to assess the contribution of
|
|
30
|
+
regressor variables to the regressand variable. It owes its origin in the theory of cooperative
|
|
31
|
+
games (Shapley, 1953). The value of $R^2$ obtained by fitting a linear regression model y=Xβ+u is
|
|
32
|
+
considered as the value of a cooperative game played by X (whose members, xj ϵ X; j=1, m,
|
|
33
|
+
work in a coalition) against y (explaining it). The analyst does not have enough information to
|
|
34
|
+
disentangle the contributions made by the individual members xj ϵ X; j=1, m, but only their joint
|
|
35
|
+
1 contribution ($R^2$) is known. The Shapley value decomposition imputes the most likely
|
|
36
|
+
contribution of each individual xj ϵ X; j=1, m, to $R^2$.
|
|
37
|
+
|
|
38
|
+
### An algorithm to impute the contribution of individual variables to Shapley value:
|
|
39
|
+
>Let there be m number of regressor variables in the model y=Xβ+u. Let X(p, r) be the r-membered subset
|
|
40
|
+
of X in which the pth regressor appears and X(q, r) be the r-membered subset of X in which the
|
|
41
|
+
pth regressor does not appear. Further, let $R^2(p, r)$ be the $R^2$ obtained by regression of y on X(p,
|
|
42
|
+
r) and $R^2(q, r)$ be the $R^2$ obtained by regression of y on X(q, r). Then, the share of the regressor
|
|
43
|
+
variable p (that is xp ϵ X) is given by
|
|
44
|
+
|
|
45
|
+
$$
|
|
46
|
+
S(p) = \frac{1}{m}\frac{\sum_{i=1}^{m}\left[R^2(p,r) - R^2(q, r-1)\right]}{k}.
|
|
47
|
+
$$
|
|
48
|
+
|
|
49
|
+
Moreover, $R^2(q,0) = 0$. Here k is the number of cases in which the evaluation in [.] was carried
|
|
50
|
+
out. The sum of all $S(p)$ for p=1, m (that is, $\sum_{p=1}^{m}S(p)$) is the $R^2$ of y=Xβ+u : (all xj ϵ X) or the
|
|
51
|
+
total value of the game:
|
|
52
|
+
|
|
53
|
+
$$
|
|
54
|
+
R^2 = \sum_{p=1}^{m}S(p) = \sum_{p=1}^{m}\frac{1}{m}\sum_{r=1}^{k}\frac{\sum_{c=1}^{k}\left[R^2(p,r)-R^2(q, r-1)\right]}{k}.
|
|
55
|
+
$$
|
|
56
|
+
|
|
57
|
+
### Computational details of share of $X_j$ in $R^2$:
|
|
58
|
+
|
|
59
|
+
|r | r-1 |x1 |x2 |x3 |x4 |$R^2$ |K | operation | values | Sum/k | Grand value |
|
|
60
|
+
|---|:----|:--|:--|:--|:--|:-------|:---|:------------|:--------|:----------|:-----------:|
|
|
61
|
+
|4 | |1 |2 |3 |4 |0.98237 | |plus |+0.98237 | | |
|
|
62
|
+
| |3 | |2 |3 |4 |0.97282 | |minus |-0.97282 | | |
|
|
63
|
+
| | | | | | | |k=1 |Sum/k | |0.009556 | |
|
|
64
|
+
|3 | |1 |2 |3 | |0.98228 | |plus |+0.98228 | | |
|
|
65
|
+
|3 | |1 |2 | |4 |0.98233 | |plus |+0.98233 | | |
|
|
66
|
+
|3 | |1 | |3 |4 |0.98128 | |plus |+0.98128 | | |
|
|
67
|
+
| |2 | |2 |3 | |0.84702 | |minus |-0.84702 | | |
|
|
68
|
+
| |2 | |2 |3 | |0.68006 | |minus |-0.68006 | | |
|
|
69
|
+
| |2 | |2 |3 | |0.93529 | |minus |-0.93529 | | |
|
|
70
|
+
| | | | | | | |k=3 |Sum/k | |0.161175 | |
|
|
71
|
+
|2 | |1 |2 | | |0.97867 | |plus |+0.97867 | | |
|
|
72
|
+
|2 | |1 | |3 | |0.54816 | |plus |+0.54816 | | |
|
|
73
|
+
|2 | |1 | | |4 |0.97247 | |plus |+0.97247 | | |
|
|
74
|
+
| |1 | |2 | | |0.66626 | |minus |-0.66626 | | |
|
|
75
|
+
| |1 | | |3 | |0.28587 | |minus |-0.28587 | | |
|
|
76
|
+
| |1 | | | |4 |0.67454 | |minus |-0.67454 | | |
|
|
77
|
+
| | | | | | | |k=3 |Sum/k | |0.290878 | |
|
|
78
|
+
|1 | |1 | | | |0.53394 | |plus |+0.53394 | | |
|
|
79
|
+
| | | | | | | |k=1 |Sum/k | |0.533948 | |
|
|
80
|
+
| | | | | | | | |Sum(sum/k)/m | | |**0.248889** |
|
|
81
|
+
|
|
82
|
+
## Time Complexity
|
|
83
|
+
|
|
84
|
+
For estimated execution time and scalability details by number of regressors, see [TIMEComplexity.md](TIMEComplexity.md).
|
|
85
|
+
|
|
86
|
+
## Example: Use as a package
|
|
87
|
+
|
|
88
|
+
Install locally in editable mode from the project root:
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
pip install -e .
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Then import and use the package in Python:
|
|
95
|
+
|
|
96
|
+
```python
|
|
97
|
+
from shapley import ShapleyValue
|
|
98
|
+
from shapley.data.load import get_dataset_info, load_dataset
|
|
99
|
+
|
|
100
|
+
dataset_key = "death_rate"
|
|
101
|
+
|
|
102
|
+
info = get_dataset_info(dataset_key)
|
|
103
|
+
df = load_dataset(dataset_key)
|
|
104
|
+
|
|
105
|
+
sv = ShapleyValue(df, info["default_X"], info["target"])
|
|
106
|
+
contributions = sv.get_shapley_contribution(verbose=False)
|
|
107
|
+
|
|
108
|
+
print(contributions)
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Expected output format:
|
|
112
|
+
|
|
113
|
+
```text
|
|
114
|
+
Regressor Share
|
|
115
|
+
lower_95_confidence_interval_for_death_rate <float>
|
|
116
|
+
upper_95_confidence_interval_for_death_rate <float>
|
|
117
|
+
recent_5_year_trend_2_in_death_rates <float>
|
|
118
|
+
average_deaths_per_year <float>
|
|
119
|
+
Total <float>
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Get contribution for a single regressor:
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
contribution, details = sv.get_shapley_contribution_of("average_deaths_per_year")
|
|
126
|
+
print(contribution)
|
|
127
|
+
print(details.head())
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
Available packaged datasets:
|
|
131
|
+
|
|
132
|
+
- `death_rate`
|
|
133
|
+
- `distance_metro`
|
|
134
|
+
|
|
135
|
+
## Example: Use your own pandas DataFrame
|
|
136
|
+
|
|
137
|
+
You can also pass your own DataFrame directly, as long as selected feature columns and target column are numeric.
|
|
138
|
+
|
|
139
|
+
```python
|
|
140
|
+
import pandas as pd
|
|
141
|
+
|
|
142
|
+
from shapley import ShapleyValue
|
|
143
|
+
|
|
144
|
+
# User-provided dataset
|
|
145
|
+
df = pd.DataFrame(
|
|
146
|
+
{
|
|
147
|
+
"sq_feet": [950, 1100, 1300, 1600, 1800, 2100],
|
|
148
|
+
"age_years": [20, 15, 12, 10, 8, 5],
|
|
149
|
+
"bedrooms": [2, 2, 3, 3, 4, 4],
|
|
150
|
+
"distance_km": [8.0, 7.2, 6.5, 5.0, 4.0, 3.0],
|
|
151
|
+
"price_usd": [220000, 250000, 290000, 340000, 390000, 450000],
|
|
152
|
+
}
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
X = ["sq_feet", "age_years", "bedrooms", "distance_km"]
|
|
156
|
+
y = "price_usd"
|
|
157
|
+
|
|
158
|
+
sv = ShapleyValue(df, X, y)
|
|
159
|
+
|
|
160
|
+
# All feature contributions
|
|
161
|
+
contributions = sv.get_shapley_contribution(verbose=False)
|
|
162
|
+
print(contributions)
|
|
163
|
+
|
|
164
|
+
# Single feature contribution and detailed intermediate table
|
|
165
|
+
sq_feet_share, sq_feet_details = sv.get_shapley_contribution_of("sq_feet")
|
|
166
|
+
print("sq_feet share:", sq_feet_share)
|
|
167
|
+
print(sq_feet_details.head())
|
|
168
|
+
```
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "shapley-value-regression"
|
|
7
|
+
version = "0.0.0"
|
|
8
|
+
description = "A simple library to calculate Shapley values for regression models using pandas and scikit-learn."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = {text = "MIT"}
|
|
12
|
+
authors = [{name = "Sarbadal Pal"}]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Programming Language :: Python :: 3",
|
|
15
|
+
"License :: OSI Approved :: MIT License",
|
|
16
|
+
"Operating System :: OS Independent",
|
|
17
|
+
]
|
|
18
|
+
dependencies = [
|
|
19
|
+
"pandas>=2.0",
|
|
20
|
+
"scikit-learn>=1.3",
|
|
21
|
+
"tabulate>=0.10"
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
[tool.setuptools.packages.find]
|
|
25
|
+
where = ["src"]
|
|
26
|
+
|
|
27
|
+
[tool.setuptools]
|
|
28
|
+
include-package-data = true
|
|
29
|
+
|
|
30
|
+
[tool.setuptools.package-data]
|
|
31
|
+
shapley = [
|
|
32
|
+
"*.py",
|
|
33
|
+
"data/*.csv",
|
|
34
|
+
]
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import sys
|
|
3
|
+
import time
|
|
4
|
+
|
|
5
|
+
os.environ["TZ"] = "Asia/Kolkata"
|
|
6
|
+
time.tzset()
|
|
7
|
+
|
|
8
|
+
# Add your package root to PYTHONPATH
|
|
9
|
+
sys.path.insert(0, os.path.abspath('../..'))
|
|
10
|
+
|
|
11
|
+
def setup(app):
|
|
12
|
+
app.add_js_file('custom.js')
|
|
13
|
+
|
|
14
|
+
html_title = 'Shapley Value'
|
|
15
|
+
project = 'Shapley Value'
|
|
16
|
+
copyright = '2026, Sarbadal Pal'
|
|
17
|
+
author = 'Sarbadal Pal'
|
|
18
|
+
release = '0.0.1'
|
|
19
|
+
|
|
20
|
+
extensions = [
|
|
21
|
+
'sphinx.ext.autodoc',
|
|
22
|
+
'sphinx.ext.napoleon',
|
|
23
|
+
'sphinx.ext.viewcode',
|
|
24
|
+
'myst_parser',
|
|
25
|
+
'sphinx_togglebutton',
|
|
26
|
+
'sphinx_copybutton',
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
templates_path = ['_templates']
|
|
30
|
+
exclude_patterns = []
|
|
31
|
+
|
|
32
|
+
html_context = {
|
|
33
|
+
"READTHEDOCS": False,
|
|
34
|
+
"display_lower_left": False,
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
# html_theme = 'furo'
|
|
38
|
+
html_theme = 'sphinx_rtd_theme'
|
|
39
|
+
html_last_updated_use_utc = False
|
|
40
|
+
html_last_updated_fmt = "%Y-%m-%d %H:%M:%S IST"
|
|
41
|
+
|
|
42
|
+
html_static_path = ['_static']
|
|
43
|
+
html_css_files = [
|
|
44
|
+
'custom.css',
|
|
45
|
+
'font.css',
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
html_allow_unsafe = True
|
|
49
|
+
html_show_sphinx = False
|
|
50
|
+
html_copy_source = False
|
|
51
|
+
html_show_sourcelink = False
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
Property_ID,Sq_Feet,Age_Yrs,Bedrooms,Distance_Metro_KM,Floors,Price_USD
|
|
2
|
+
101,4174,11,2,7.2,2,504000
|
|
3
|
+
102,1860,16,3,7.4,1,199000
|
|
4
|
+
103,2294,9,2,7.8,3,300000
|
|
5
|
+
104,2130,15,3,1.2,3,330000
|
|
6
|
+
105,2095,14,3,3.9,1,283000
|
|
7
|
+
106,4092,14,2,1.6,3,543000
|
|
8
|
+
107,2638,18,5,8.7,3,327000
|
|
9
|
+
108,3169,11,3,6.4,1,395000
|
|
10
|
+
109,1466,19,4,3.6,1,205000
|
|
11
|
+
110,2238,2,4,1.1,3,382000
|
|
12
|
+
111,1330,4,3,3.5,3,242000
|
|
13
|
+
112,2482,18,4,3.6,3,361000
|
|
14
|
+
113,3135,6,1,7.4,2,412000
|
|
15
|
+
114,4444,8,2,6.6,2,559000
|
|
16
|
+
115,4171,6,1,8.9,2,500000
|
|
17
|
+
116,3919,17,2,5.0,1,468000
|
|
18
|
+
117,1130,3,2,1.6,2,185000
|
|
19
|
+
118,2685,13,2,7.3,1,314000
|
|
20
|
+
119,4380,17,4,7.7,1,534000
|
|
21
|
+
120,1769,8,2,5.8,2,279000
|
|
22
|
+
121,3391,1,2,7.8,2,439000
|
|
23
|
+
122,2515,19,3,5.2,2,323000
|
|
24
|
+
123,4485,14,2,5.5,3,562000
|
|
25
|
+
124,3853,6,4,4.6,2,520000
|
|
26
|
+
125,3433,11,1,0.7,3,486000
|
|
27
|
+
126,2215,7,5,1.5,1,379000
|
|
28
|
+
127,1955,14,3,0.8,3,323000
|
|
29
|
+
128,3324,2,2,6.5,2,427000
|
|
30
|
+
129,2184,13,1,3.5,1,291000
|
|
31
|
+
130,1459,16,4,5.3,1,184000
|