lore_sa.neighgen.RandomGenerator
- class lore_sa.neighgen.RandomGenerator(bbox: AbstractBBox, dataset: Dataset, encoder: EncDec, ocr=0.1, mutpb=0.1)[source]
Random Generator creates neighbor instances by generating random values starting from the instance to explain
- __init__(bbox: AbstractBBox, dataset: Dataset, encoder: EncDec, ocr=0.1, mutpb=0.1)[source]
- Parameters:
bbox – the Black Box model to explain
dataset – the dataset with the descriptor of the original dataset
encoder – an encoder to transfrom the data from/to the black box model
ocr – acronym for One Class Ratio, it is the ratio of the number of instances of the minority class
Methods
__init__(bbox, dataset, encoder[, ocr, mutpb])- param bbox:
the Black Box model to explain
balance_neigh(z, Z, num_samples)check_generated([filter_function, check_fuction])It contains the logic to check the requirements for generated data
generate(x, num_instances, descriptor, encoder)random generation of new instances.
generate_synthetic_instance([from_z, mutpb])Generate a single synthetic instance.
- abstract check_generated(filter_function=None, check_fuction=None)
It contains the logic to check the requirements for generated data
- generate(x, num_instances, descriptor, encoder)[source]
random generation of new instances. The starting instance x is only used to detect the value type of each feature, in order to generate new values only for numeric features.
- Parameters:
x[dict] – the starting instance from the real dataset
num_instances[int] – the number of instances to generate
descriptor[dict] – data descriptor as generated from a Dataset object
encoder[EncDec] – the encoder eventually to encode the instance
The list (or range) associated to each key is used to randomly choice an element within the list.
- Return [instances]:
an array of instances with the new data generated
- generate_synthetic_instance(from_z=None, mutpb=1.0)
Generate a single synthetic instance.
This method creates one synthetic instance by randomly sampling or mutating feature values. For categorical features, it randomly selects from valid values. For numerical features, it samples from the feature’s range.
- Parameters:
from_z (np.array, optional) – Starting instance in encoded space to mutate. If None, generates a completely random instance. If provided, features are mutated with probability mutpb.
mutpb (float, optional) – Mutation probability for each feature (0 to 1). Only used when from_z is provided. Default is 1.0 (mutate all features).
- Returns:
A single synthetic instance in encoded space, shape (n_encoded_features,)
- Return type:
np.array
Note
The method respects feature types and valid ranges from the dataset descriptor. For categorical features, it ensures the one-hot encoding constraint (exactly one category is active).