Class Visualisation
def get_PILImage(self, instance, reason, image=None, time_series=None, contrastive=False): Highlight
Return PIL images of the PIL library for an instance given in the instance parameter an explanation given in the reason parameter.
Displays for an instance given in the instance parameter an explanation given in the reason parameter. The given instance must be without the label. The given reason must be in Boolean variable form (without using the to_features() method). The explanation can be shown differently for images and time series, using the parameters provided.
Parameters
instance : list[float]
The instance to be explained.
reason : list[int]
A set of (signed) binary variables. Reason (explanation) to display.
image : dict (optional, default=None)
Python dictionary containing some information specific to images.
See the page of the ImageDict class for more information.
time_series : dict (optional, default=None)
To display time series.
Python dictionary where a key is the name of a time serie and each value of a key is a list containing time series feature names.
contrastive : Boolean (optional, default=False)
True if the explanation is a contrastive one.
When this parameter is set to True, the elimination of redundant features must be reversed.
width : int (optional, default=250)
The width parameter specifies the width in pixels of the resulting image.
The width value causes the image height to be scaled proportionally to the requested width (the aspect ratio is preserved).
Examples
from pyxai import Learning, Explaining
# Machine learning part
learner = Learning.Scikitlearn("tests/datasets/australian_0.csv", problem_type=Learning.CLASSIFICATION) # Dataset deleted
model = learner.evaluate(splitting_method=Learning.HOLD_OUT, model_type=Learning.RF)
instance, prediction = learner.get_instances(model, n=1, seed=11200, is_correct=True)
australian_types = {
"numerical": Learning.DEFAULT,
"categorical": {"A4*": (1, 2, 3),
"A5*": tuple(range(1, 15)),
"A6*": (1, 2, 3, 4, 5, 7, 8, 9),
"A12*": tuple(range(1, 4))},
"binary": ["A1", "A8", "A9", "A11"],
}
# Explainer part
explainer = Explaining.initialize(model, instance, features_type=australian_types)
majoritary_reason = explainer.majoritary_reason(time_limit=10)
explainer.visualisation.get_PILImage(instance, majoritary_reason)
def gui(self, image=None, time_series=None): Highlight
Open the PyXAI’s Graphical User Interface.
Parameters
image : dict (optional, default=None)
Python dictionary containing some information specific to images.
See the page of the ImageDict class for more information.
time_series : dict (optional, default=None)
To display time series.
Python dictionary where a key is the name of a time serie and each value of a key is a list containing time series feature names.
Examples
...
import numpy
def get_pixel_value(instance, x, y, shape):
index = x * shape[0] + y
return instance[index]
def instance_index_to_pixel_position(i, shape):
return i // shape[0], i % shape[0]
explainer.visualisation.gui(image={"shape": (28,28),
"dtype": numpy.uint8,
"get_pixel_value": get_pixel_value,
"instance_index_to_pixel_position": instance_index_to_pixel_position})
def notebook(self, instance, reason, image=None, time_series=None, contrastive=False, width=250): Highlight
Use the IPython.display() method to display images in a Jupyter notebook.
Displays for an instance given in the instance parameter an explanation given in the reason parameter. The given instance must be without the label. The given reason must be in Boolean variable form (without using the to_features() method). The explanation can be shown differently for images and time series, using the parameters provided.
Parameters
instance : list[float]
The instance to be explained.
reason : list[int]
A set of (signed) binary variables. Reason (explanation) to display.
image : dict (optional, default=None)
Python dictionary containing some information specific to images.
See the page of the ImageDict class for more information.
time_series : dict (optional, default=None)
To display time series.
Python dictionary where a key is the name of a time serie and each value of a key is a list containing time series feature names.
contrastive : Boolean (optional, default=False)
True if the explanation is a contrastive one.
When this parameter is set to True, the elimination of redundant features must be reversed.
width : int (optional, default=250)
The width parameter specifies the width in pixels of the resulting image.
The width value causes the image height to be scaled proportionally to the requested width (the aspect ratio is preserved).
Examples
from pyxai import Learning, Explaining
# Machine learning part
learner = Learning.Scikitlearn("tests/datasets/australian_0.csv", problem_type=Learning.CLASSIFICATION) # Dataset deleted
model = learner.evaluate(splitting_method=Learning.HOLD_OUT, model_type=Learning.RF)
instance, prediction = learner.get_instances(model, n=1, seed=11200, is_correct=True)
australian_types = {
"numerical": Learning.DEFAULT,
"categorical": {"A4*": (1, 2, 3),
"A5*": tuple(range(1, 15)),
"A6*": (1, 2, 3, 4, 5, 7, 8, 9),
"A12*": tuple(range(1, 4))},
"binary": ["A1", "A8", "A9", "A11"],
}
# Explainer part
explainer = Explaining.initialize(model, instance, features_type=australian_types)
majoritary_reason = explainer.majoritary_reason(time_limit=10)
explainer.visualisation.notebook(instance, majoritary_reason)
def resize_PILimage(self, image, width=250): Highlight
Resize a PIL image.
Parameters
image : PILimage
Image from the PIL library.
width : int (optional, default=250)
The width parameter specifies the width in pixels of the resulting image.
The width value causes the image height to be scaled proportionally to the requested width (the aspect ratio is preserved).
def save_png(self, file, instance, reason, image=None, time_series=None, contrastive=False, width=250): Highlight
Save PNG image files in the current directory (given via the file parameter) with the PIL library for an instance given in the instance parameter an explanation given in the reason parameter.
Displays for an instance given in the instance parameter an explanation given in the reason parameter. The given instance must be without the label. The given reason must be in Boolean variable form (without using the to_features() method). The explanation can be shown differently for images and time series, using the parameters provided.
Parameters
file : str
The filename (possibly with a path) where the image is saved.
The file path must exist.
instance : list[float]
The instance to be explained.
reason : list[int]
A set of (signed) binary variables. Reason (explanation) to display.
image : dict (optional, default=None)
Python dictionary containing some information specific to images.
See the page of the ImageDict class for more information.
time_series : dict (optional, default=None)
To display time series.
Python dictionary where a key is the name of a time serie and each value of a key is a list containing time series feature names.
contrastive : Boolean (optional, default=False)
True if the explanation is a contrastive one.
When this parameter is set to True, the elimination of redundant features must be reversed.
width : int (optional, default=250)
The width parameter specifies the width in pixels of the resulting image.
The width value causes the image height to be scaled proportionally to the requested width (the aspect ratio is preserved).
Examples
from pyxai import Learning, Explaining
# Machine learning part
learner = Learning.Scikitlearn("tests/datasets/australian_0.csv", problem_type=Learning.CLASSIFICATION) # Dataset deleted
model = learner.evaluate(splitting_method=Learning.HOLD_OUT, model_type=Learning.RF)
instance, prediction = learner.get_instances(model, n=1, seed=11200, is_correct=True)
australian_types = {
"numerical": Learning.DEFAULT,
"categorical": {"A4*": (1, 2, 3),
"A5*": tuple(range(1, 15)),
"A6*": (1, 2, 3, 4, 5, 7, 8, 9),
"A12*": tuple(range(1, 4))},
"binary": ["A1", "A8", "A9", "A11"],
}
# Explainer part
explainer = Explaining.initialize(model, instance, features_type=australian_types)
majoritary_reason = explainer.majoritary_reason(time_limit=10)
explainer.visualisation.save_png("save/explanation1.png", instance, majoritary_reason)
def screen(self, instance, reason, image=None, time_series=None, contrastive=False, width=250): Highlight
Use the Image.show() method to display images on screen with the PIL library.
Displays for an instance given in the instance parameter an explanation given in the reason parameter. The given instance must be without the label. The given reason must be in Boolean variable form (without using the to_features() method). The explanation can be shown differently for images and time series, using the parameters provided.
Parameters
instance : list[float]
The instance to be explained.
reason : list[int]
A set of (signed) binary variables. Reason (explanation) to display.
image : dict (optional, default=None)
Python dictionary containing some information specific to images.
See the page of the ImageDict class for more information.
time_series : dict (optional, default=None)
To display time series.
Python dictionary where a key is the name of a time serie and each value of a key is a list containing time series feature names.
contrastive : Boolean (optional, default=False)
True if the explanation is a contrastive one.
When this parameter is set to True, the elimination of redundant features must be reversed.
width : int (optional, default=250)
The width parameter specifies the width in pixels of the resulting image.
The width value causes the image height to be scaled proportionally to the requested width (the aspect ratio is preserved).
Examples
from pyxai import Learning, Explaining
# Machine learning part
learner = Learning.Scikitlearn("tests/datasets/australian_0.csv", problem_type=Learning.CLASSIFICATION) # Dataset deleted
model = learner.evaluate(splitting_method=Learning.HOLD_OUT, model_type=Learning.RF)
instance, prediction = learner.get_instances(model, n=1, seed=11200, is_correct=True)
australian_types = {
"numerical": Learning.DEFAULT,
"categorical": {"A4*": (1, 2, 3),
"A5*": tuple(range(1, 15)),
"A6*": (1, 2, 3, 4, 5, 7, 8, 9),
"A12*": tuple(range(1, 4))},
"binary": ["A1", "A8", "A9", "A11"],
}
# Explainer part
explainer = Explaining.initialize(model, instance, features_type=australian_types)
majoritary_reason = explainer.majoritary_reason(time_limit=10)
explainer.visualisation.screen(instance, majoritary_reason)