Class KernelSHAP

Kernel SHAP method to approximate Shapley attributions by solving s specially weighted linear regression problem.

Hierarchy

  • KernelSHAP

Constructors

  • Initialize a new KernelSHAP explainer.

    Parameters

    • model: ((x: number[][]) => Promise<number[]>)

      The trained model to explain

        • (x: number[][]): Promise<number[]>
        • Parameters

          • x: number[][]

          Returns Promise<number[]>

    • data: number[][]

      The background data

    • seed: null | number

      Optional random seed in the range [0, 1)

    Returns KernelSHAP

Properties

data: number[][]

Background data

expectedValue: number

Expected prediction value

kernelWeight: null | Matrix = null

Kernel weights for each coalition sample [nSamples, 1]

lastMask: null | Matrix = null

Mask used in the last run [nSamples]

lcg: (() => number)

Type declaration

    • (): number
    • Random seed

      Returns number

maskMat: null | Matrix = null

Matrix to store the feature masks [nSamples, nVaryFeatures]

model: ((x: number[][]) => Promise<number[]>)

Type declaration

    • (x: number[][]): Promise<number[]>
    • Prediction model

      Parameters

      • x: number[][]

      Returns Promise<number[]>

nFeatures: number

Number of features

nSamplesAdded: number

Number of coalition samples added

nTargets: number

Dimension of the prediction output

nVaryFeatures: null | number = null
predictions: number[]

Model's prediction on the background ata

rng: RandomUniform

Uniform random number generator

rngInt: RandomInt

Uniform random integer generator

sampledData: null | Matrix = null

Sampled data in a matrix form. It is initialized after the explain() call. [nSamples * nBackground, nFeatures]

varyingIndexes: null | number[] = null

Column indexes that the explaining x has different column value from at least ont instance in the background data.

yExpMat: null | Matrix = null

Expected model predictions on the sample data [nSamples, nTargets]

yMat: null | Matrix = null

Model prediction outputs on the sampled data [nSamples * nBackground, nTargets]

Methods

  • Parameters

    • x: number[]
    • mask: number[]
    • weight: number

    Returns void

  • Estimate SHAP values of the given sample x

    Parameters

    • x: number[]

      One data sample

    • nSamples: null | number = null

      Number of coalitions to samples (default to null which uses a heuristic to determine a large sample size)

    Returns Promise<number[][]>

  • Find varying indexes (if x has columns that are the same for every background instances, then the shap value is 0 for those columns)

    Parameters

    • x: number[]

      Explaining instance x

    Returns number[]

  • Initialize data structures to prepare for the feature coalition sampling

    Parameters

    • nSamples: number

      Number of coalitions to sample

    Returns void

  • Enumerate/sample feature coalitions to approximate the shapley values

    Returns

    Sample rate (fraction of sampled feature coalitions)

    Parameters

    • x: number[]

      Instance to explain

    • nSamples: null | number

      Number of coalitions to sample

    Returns number

  • Helper function to convert a mask array into a string

    Returns

    String version of the binary mask array

    Parameters

    • mask: number[]

      Binary mask array

    Returns string

Generated using TypeDoc