> For the complete documentation index, see [llms.txt](https://docs.sov.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sov.ai/api-reference/extensions/shapley-global-importance.md).

# Shapley Global Importance

**Module:** `sovai.extensions.shapley_global_importance`

## Classes

### `ClusteringExplainer`

```python
class ClusteringExplainer
```

Trains a classifier to predict cluster labels and provides SHAP explanations.

**Attributes**

* `random_state`
* `model`
* `explainer`
* `scaler`

**Methods**

### `__init__()`

```python
def __init__(self, random_state = 42)
```

Initializes the explainer and scaler.

**Parameters**

| Parameter      | Type | Description   |
| -------------- | ---- | ------------- |
| `random_state` | —    | Default: `42` |

***

### `fit()`

```python
def fit(self, X, y)
```

Fits the LGBM classifier and creates the SHAP explainer.

**Parameters**

| Parameter | Type | Description |
| --------- | ---- | ----------- |
| `X`       | —    | —           |
| `y`       | —    | —           |

***

### `get_shap_values()`

```python
def get_shap_values(self, X)
```

Gets SHAP values using the trained explainer.

**Parameters**

| Parameter | Type | Description |
| --------- | ---- | ----------- |
| `X`       | —    | —           |

***

***

## Functions

### `hash_of_df()`

```python
def hash_of_df(df, sample_size = 100)
```

Calculates a SHA256 hash of a sampled portion of a DataFrame.

**Parameters**

| Parameter     | Type | Description    |
| ------------- | ---- | -------------- |
| `df`          | —    | —              |
| `sample_size` | —    | Default: `100` |

***

### `get_shap_values_for_dataset()`

```python
def get_shap_values_for_dataset(
    df,
    clustering_method = 'KMEANS',
    n_clusters = 10,
    random_state = 42,
    sample_size = 5000,
)
```

Performs clustering, trains a model, and calculates mean absolute SHAP values.

**Parameters**

| Parameter           | Type | Description         |
| ------------------- | ---- | ------------------- |
| `df`                | —    | —                   |
| `clustering_method` | —    | Default: `'KMEANS'` |
| `n_clusters`        | —    | Default: `10`       |
| `random_state`      | —    | Default: `42`       |
| `sample_size`       | —    | Default: `5000`     |

***

### `run_simulations_frame_global()`

```python
def run_simulations_frame_global(df, num_simulations = 4, clustering_method = 'KMEANS')
```

Runs multiple simulations of SHAP value calculation in parallel and averages.

**Parameters**

| Parameter           | Type | Description         |
| ------------------- | ---- | ------------------- |
| `df`                | —    | —                   |
| `num_simulations`   | —    | Default: `4`        |
| `clustering_method` | —    | Default: `'KMEANS'` |

***

### `run_simulations_global_importance()`

```python
def run_simulations_global_importance(
    df,
    num_simulations = 4,
    clustering_method = 'KMEANS',
)
```

Calculates overall feature importance based on averaged SHAP values.

**Parameters**

| Parameter           | Type | Description         |
| ------------------- | ---- | ------------------- |
| `df`                | —    | —                   |
| `num_simulations`   | —    | Default: `4`        |
| `clustering_method` | —    | Default: `'KMEANS'` |

***
