# 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'` |

***


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.sov.ai/api-reference/extensions/shapley-global-importance.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
