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

# alibi.confidence.model\_linearity

## Constants

### `logger`

```python
logger: logging.Logger = <Logger alibi.confidence.model_linearity (WARNING)>
```

Instances of the Logger class represent a single logging channel. A "logging channel" indicates an area of an application. Exactly how an "area" is defined is up to the application developer. Since an application can have any number of areas, logging channels are identified by a unique string. Application areas can be nested (e.g. an area of "input processing" might include sub-areas "read CSV files", "read XLS files" and "read Gnumeric files"). To cater for this natural nesting, channel names are organized into a namespace hierarchy where levels are separated by periods, much like the Java or Python package namespace. So in the instance given above, channel names might be "input" for the upper level, and "input.csv", "input.xls" and "input.gnu" for the sub-levels. There is no arbitrary limit to the depth of nesting.

## `LinearityMeasure`

### Constructor

```python
LinearityMeasure(self, method: str = 'grid', epsilon: float = 0.04, nb_samples: int = 10, res: int = 100, alphas: Optional[numpy.ndarray] = None, model_type: str = 'classifier', agg: str = 'pairwise', verbose: bool = False) -> None
```

| Name         | Type                      | Default        | Description                                                                                    |
| ------------ | ------------------------- | -------------- | ---------------------------------------------------------------------------------------------- |
| `method`     | `str`                     | `'grid'`       | Method for sampling. Supported methods: `'knn'`                                                |
| `epsilon`    | `float`                   | `0.04`         | Size of the sampling region around the central instance as a percentage of the features range. |
| `nb_samples` | `int`                     | `10`           | Number of samples to generate.                                                                 |
| `res`        | `int`                     | `100`          | Resolution of the grid. Number of intervals in which the feature range is discretized.         |
| `alphas`     | `Optional[numpy.ndarray]` | `None`         | Coefficients in the superposition.                                                             |
| `model_type` | `str`                     | `'classifier'` | Type of task. Supported values: `'regressor'`                                                  |
| `agg`        | `str`                     | `'pairwise'`   | Aggregation method. Supported values: `'global'`                                               |
| `verbose`    | `bool`                    | `False`        |                                                                                                |

### Methods

#### `fit`

```python
fit(X_train: numpy.ndarray) -> None
```

| Name      | Type            | Default | Description   |
| --------- | --------------- | ------- | ------------- |
| `X_train` | `numpy.ndarray` |         | Training set. |

**Returns**

* Type: `None`

#### `score`

```python
score(predict_fn: Callable, x: numpy.ndarray) -> numpy.ndarray
```

| Name         | Type            | Default | Description           |
| ------------ | --------------- | ------- | --------------------- |
| `predict_fn` | `Callable`      |         | Prediction function.  |
| `x`          | `numpy.ndarray` |         | Instance of interest. |

**Returns**

* Type: `numpy.ndarray`

## Functions

### `infer_feature_range`

```python
infer_feature_range(X_train: numpy.ndarray) -> numpy.ndarray
```

Infers the feature range from the training set.

| Name      | Type            | Default | Description   |
| --------- | --------------- | ------- | ------------- |
| `X_train` | `numpy.ndarray` |         | Training set. |

**Returns**

* Type: `numpy.ndarray`

### `linearity_measure`

```python
linearity_measure(predict_fn: Callable, x: numpy.ndarray, feature_range: Union[List[Any], numpy.ndarray, None] = None, method: str = 'grid', X_train: Optional[numpy.ndarray] = None, epsilon: float = 0.04, nb_samples: int = 10, res: int = 100, alphas: Optional[numpy.ndarray] = None, agg: str = 'global', model_type: str = 'classifier') -> numpy.ndarray
```

Calculate the linearity measure of the model around an instance of interest x.

| Name            | Type                                    | Default        | Description                                                                             |
| --------------- | --------------------------------------- | -------------- | --------------------------------------------------------------------------------------- |
| `predict_fn`    | `Callable`                              |                | Predict function.                                                                       |
| `x`             | `numpy.ndarray`                         |                | Instance of interest.                                                                   |
| `feature_range` | `Union[List[Any], numpy.ndarray, None]` | `None`         | Array with min and max values for each feature.                                         |
| `method`        | `str`                                   | `'grid'`       | Method for sampling. Supported values: `'knn'`                                          |
| `X_train`       | `Optional[numpy.ndarray]`               | `None`         | Training set.                                                                           |
| `epsilon`       | `float`                                 | `0.04`         | Size of the sampling region as a percentage of the feature range.                       |
| `nb_samples`    | `int`                                   | `10`           | Number of samples to generate.                                                          |
| `res`           | `int`                                   | `100`          | Resolution of the grid. Number of intervals in which the features range is discretized. |
| `alphas`        | `Optional[numpy.ndarray]`               | `None`         | Coefficients in the superposition.                                                      |
| `agg`           | `str`                                   | `'global'`     | Aggregation method. Supported values: `'global'`                                        |
| `model_type`    | `str`                                   | `'classifier'` | Type of task. Supported values: `'regressor'`                                           |

**Returns**

* Type: `numpy.ndarray`


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://docs.seldon.ai/alibi-explain/api-reference/confidence/model_linearity.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
