pyreal.RealApp#
- class pyreal.RealApp(models, X_train_orig=None, y_train=None, transformers=None, feature_descriptions=None, active_model_id=None, classes=None, class_descriptions=None, pred_format_func=None, fit_transformers=False, id_column=None, openai_api_key=None, llm=None, context_description='')[source]#
Maintains all information about a Pyreal application to generate explanations
- __init__(models, X_train_orig=None, y_train=None, transformers=None, feature_descriptions=None, active_model_id=None, classes=None, class_descriptions=None, pred_format_func=None, fit_transformers=False, id_column=None, openai_api_key=None, llm=None, context_description='')[source]#
Initialize a RealApp object
- Parameters:
object (models (model) – model): Model(s) for this application
models (list of) – model): Model(s) for this application
model_id (or dict of) – model): Model(s) for this application
X_train_orig (DataFrame of shape (n_instances,n_features) – Training data for models. If None, must be provided when preparing explainers.
y_train (DataFrame of shape (n_instances,)) – The y values for the dataset
transformers (Transformer object or list of Transformer objects) – Transformers for this application
feature_name (feature_descriptions (dictionary of) – feature_description): Mapping of default feature names to readable names
active_model_id (string or int) – ID of model to store as active model, if None, this is set to the first model
classes (array) – List of class names returned by the model, in the order that the internal model considers them if applicable. Can be automatically extracted if model is an sklearn classifier None if model is not a classifier
class_descriptions (dict) – Interpretable descriptions of each class None if model is not a classifier
pred_format_func (function) – Function to format model prediction outputs
fit_transformers (Boolean) – If True, fit the transformers to X_train_orig on initialization
id_column (string or int) – Name of column that contains item ids in input data
openai_api_key (string) – OpenAI API key. Required for GPT narrative explanations, unless openai client is provided
llm (LLM model object) – Local LLM object or LLM client object to use to generate narratives.
context_description (string) – Description of the model’s prediction task, in sentence format. This is used by LLM model for narrative explanations. For example: “The model predicts the price of houses.”
Methods
__init__
(models[, X_train_orig, y_train, ...])Initialize a RealApp object
add_model
(model[, model_id])Add a model
from_sklearn
([pipeline, model, ...])Create a RealApp from a sklearn pipeline or model and transformers. Must provide one of: - just pipeline - just model - model and transformers.
Return the active model
predict
(x[, model_id, as_dict, format])Predict on x using the active model or model specified by model_id
predict_proba
(x[, model_id, as_dict, format])Return the predicted probabilities of x using the active model or model specified by model_id, only if the model has a predict_proba method
Initialize and fit a local feature contribution explainer
prepare_feature_importance
([x_train_orig, ...])Initialize and fit a global feature importance explainer
prepare_similar_examples
([x_train_orig, ...])Initialize and fit a nearest neighbor explainer
produce_feature_contributions
(x_orig[, ...])Produce a feature contribution explanation
produce_feature_importance
([model_id, ...])Produce a GlobalFeatureImportance explainer
Produce a feature contribution explanation, formatted in natural language sentence format using LLMs.
produce_similar_examples
(x_orig[, model_id, ...])Produce a SimilarExamples explainer
set_active_model_id
(active_model_id)Set a new active model
set_openai_client
([openai_client, ...])Set the openai client for this RealApp.
Run the training process for the LLM model used to generate narrative feature contribution explanations.