# alibi.confidence.trustscore module¶

class alibi.confidence.trustscore.TrustScore(k_filter=10, alpha=0.0, filter_type=None, leaf_size=40, metric='euclidean', dist_filter_type='point')[source]

Bases: object

__init__(k_filter=10, alpha=0.0, filter_type=None, leaf_size=40, metric='euclidean', dist_filter_type='point')[source]

Initialize trust scores.

Parameters
• k_filter (int) – Number of neighbors used during either kNN distance or probability filtering.

• alpha (float) – Fraction of instances to filter out to reduce impact of outliers.

• filter_type (Optional[str]) – Filter method; either ‘distance_knn’ or ‘probability_knn’

• leaf_size (int) – Number of points at which to switch to brute-force. Affects speed and memory required to build trees. Memory to store the tree scales with n_samples / leaf_size.

• metric (str) – Distance metric used for the tree. See sklearn’s DistanceMetric class for a list of available metrics.

• dist_filter_type (str) – Use either the distance to the k-nearest point (dist_filter_type = ‘point’) or the average distance from the first to the k-nearest point in the data (dist_filter_type = ‘mean’).

Return type

None

filter_by_distance_knn(X)[source]

Filter out instances with low kNN density. Calculate distance to k-nearest point in the data for each instance and remove instances above a cutoff distance.

Parameters

X (ndarray) – Data

Return type

ndarray

Returns

Filtered data.

filter_by_probability_knn(X, Y)[source]

Filter out instances with high label disagreement amongst its k nearest neighbors.

Parameters
• X (ndarray) – Data

• Y (ndarray) – Predicted class labels

Return type

Tuple[ndarray, ndarray]

Returns

Filtered data and labels.

fit(X, Y, classes=None)[source]

Build KDTrees for each prediction class.

Parameters
• X (ndarray) – Data

• Y (ndarray) – Target labels, either one-hot encoded or the actual class label.

• classes (Optional[int]) – Number of prediction classes, needs to be provided if Y equals the predicted class.

Return type

None

score(X, Y, k=2, dist_type='point')[source]

Calculate trust scores = ratio of distance to closest class other than the predicted class to distance to predicted class.

Parameters
• X (ndarray) – Instances to calculate trust score for.

• Y (ndarray) – Either prediction probabilities for each class or the predicted class.

• k (int) – Number of nearest neighbors used for distance calculation.

• dist_type (str) – Use either the distance to the k-nearest point (dist_type = ‘point’) or the average distance from the first to the k-nearest point in the data (dist_type = ‘mean’).

Return type

Tuple[ndarray, ndarray]

Returns

Batch with trust scores and the closest not predicted class.