Feature Attribution¶
- confetti.attribution.cam.cam(model, X_data: ndarray)¶
Compute Class Activation Maps (CAMs) [1]. for 1D convolutional classifiers.
This function computes class activation maps for a trained Keras model by combining the activations of the final
Conv1Dlayer with the weights of the finalDenseclassifier layer. It highlights the temporal regions most influential for the model’s predicted class.- Parameters:
model (keras.Model) – A trained Keras model containing at least one
Conv1Dlayer followed by aDenseclassification layer.X_data (ndarray of shape (n_samples, timesteps, channels)) – Input multivariate time series for which CAMs will be computed.
- Returns:
The class activation map for each input instance.
- Return type:
ndarray of shape (n_samples, timesteps)
- Raises:
CONFETTIConfigurationError – If no
Conv1Dlayer is found, if noDenseoutput layer is present, or if the number of filters in the final convolutional layer does not match the input dimensionality of the classifier layer.
Note
The CAM for a given instance is defined as:
CAM(t) = Σ_k w[k, c] * A[k, t]
- where:
A[k, t]is the activation of filterkat timesteptfrom the final convolutional layer.w[:, c]are the weights connecting each filter to the predicted classcin the final dense layer.
References
- confetti.attribution.cam_visualization.visualize_cam(weights: ndarray, instance_index: int) None¶
Visualize the Class Activation Map (CAM) for a single instance.
This function produces a simple matplotlib plot showing the normalized class activation map for a chosen instance. The weights for all instances are first min–max normalized across the dataset, and the selected CAM is displayed over time with a clean white-background design.
- Parameters:
weights (ndarray of shape (n_instances, timesteps)) – The CAM weights computed for each instance.
instance_index (int) – Index of the instance whose CAM should be visualized.
- Raises:
IndexError – If
instance_indexis out of range forweights.ValueError – If
weightsis not a 2D array.
Note
The CAM values are normalized using
MinMaxScalerprior to visualization to enable clearer comparison across instances.