PDPbox Documentation

repository·master·Indexed 21 days ago

https://github.com/sosuneko/pdpbox

PDPbox is a Python toolbox for visualizing the influence of specific features on supervised machine learning model predictions using Partial Dependence Plots (PDP). It provides tools for analyzing model behavior through info plots (TargetPlot, PredictPlot) and interaction plots (InteractTargetPlot, InteractPredictPlot), and supports both scikit-learn models and custom prediction functions via pred_func.

Tokens
4.1K
Snippets
17
Records
29
Agent score
74%

What's inside PDPbox

  1. Manage ICE plot visibility and overcrowding

    master

    Individual Conditional Expectation (ICE) plots can suffer from two main visual issues:

    1. Difficulty comparing curves: Because curves start at different $\hat{f}(x)$ values, it can be hard to see if they differ in shape. Use the center parameter in pdp_plot or pdp_interact_plot to align them.
    2. Overcrowding: Drawing too many ICE curves makes the plot unreadable. Use the frac_to_plot or cluster parameters in pdp_plot or pdp_interact_plot to limit the number of curves displayed.
  2. Visualize feature distribution in PDP plots

    master

    Omitting the feature distribution in Partial Dependence visualizations can be misleading, as it may lead to over-interpreting the plot in regions where there are almost no actual feature values.

    To include the feature distribution in your plots, use the plot_pts_dist parameter in the pdp_plot method.

  3. How PDPbox is structured

    master

    PDPbox follows a layered architecture designed to separate the user interface from the underlying plotting logic and visual styling. The architecture consists of three main layers:

    1. Plot API: The primary entry point for users to interact with the library.
    2. Plot Engine: A layer that contains the specific logic for generating different types of plots.
    3. Plot Style: A layer dedicated to managing the visual appearance and styling of the plots.

    Users interact with the Plot API, which delegates the heavy lifting to the appropriate Plot Engine, which in turn uses a Plot Style to ensure consistent and correct visual representation.

  4. Use the plot method in pdpbox info plots

    master
    The pdpbox.info_plots.TargetPlot and pdpbox.info_plots.PredictPlot classes both inherit from the internal _InfoPlot base class. They share a common interface via the .plot() method, which is used to generate information plots for analyzing model behavior.
  5. Configure code coverage with Coverage.py and Codecov

    master

    The project uses two distinct tools for coverage management:

    1. Coverage.py: Controlled by .coveragerc. This file defines how coverage data is collected during execution.
    2. Codecov: Controlled by codecov.yml. This file defines how the collected data is interpreted and presented in the Codecov dashboard.
  6. Handle feature correlation in PDP and Interaction plots

    master

    Partial Dependence Plots (PDP) assume that the features being analyzed ($X_C$) are uncorrelated with the remaining features ($X_S$). If features are highly correlated (e.g., height and weight), the averaging process in PDP may include unrealistic data points, making the results untrustworthy.

    To mitigate this, use the data_transformer parameter in the following methods to transform or filter the data used for calculation:

    • pdp_isolate
    • pdp_interact
  7. How PDPbox handles model predictions

    master

    PDPbox is designed to visualize how specific features influence model predictions using Partial Dependence Plots (PDP).

    Scikit-learn Models

    If you are using standard scikit-learn models, PDPbox automatically detects the prediction interface. It typically looks for:

    • model.predict
    • model.predict_proba

    Custom Models

    For models that do not follow the standard scikit-learn interface, you can provide a custom prediction function using the pred_func parameter available in PDPbox methods. This allows you to use PDPbox with any model by explicitly defining how predictions should be generated.

  8. Structure of the centralized parameters.json file

    master

    The project uses a central JSON file (assets/docs_helper/parameters.json) to store documentation for parameters, return values, and attributes. This prevents redundancy and ensures consistency across different functions and classes using the same parameters.

    Each entry in the JSON list is a dictionary with the following keys:

    • param: The name of the parameter.
    • type: The data type.
    • desp: The description text.
    • target: A mapping dictionary containing:
      • params: A list of full names (strings) of functions that take this parameter.
      • returns: A list of full names of functions that return this parameter.
      • attrs: A list of full names of classes that have this attribute.

    Full names follow the format module_name.class_name or module_name.function_name.

    {
        "param": "show_percentile",
        "type": "bool",
        "desp": "If True, percentiles are shown in the plot.",
        "target": {
            "params": [
                "info_plots._InfoPlot.plot",
                "info_plots._InteractInfoPlot.plot"
            ],
            "returns": [],
            "attrs": [
                "styles.InfoPlotStyle"
            ]
        }
    }
  9. Use InteractTargetPlot and InteractPredictPlot for interaction information plots

    master

    The pdpbox.info_plots module provides two main classes for visualizing interaction information: InteractTargetPlot and InteractPredictPlot. Both classes inherit from the internal _InteractInfoPlot base class and share a common .plot() method.

    • Use InteractTargetPlot to visualize interaction information related to the target variable.
    • Use InteractPredictPlot to visualize interaction information related to the model predictions.
    # Example usage pattern (exact parameters depend on specific class implementation)
    from pdpbox.info_plots import InteractTargetPlot, InteractPredictPlot
    
    # For target-based interaction plots
    plot_target = InteractTargetPlot(target_data, feature_data, ...)
    plot_target.plot()
    
    # For prediction-based interaction plots
    plot_predict = InteractPredictPlot(model, feature_data, ...)
    plot_predict.plot()
  10. Install PDPbox from source (latest develop version)

    master

    To install the latest development version from the GitHub repository, clone the repository and run the setup script.

    $ git clone https://github.com/SauceCat/PDPbox.git
    $ cd PDPbox
    $ python setup.py install
  11. Generate UML diagrams with Pyreverse

    master

    You can analyze the Python code and generate UML diagrams using pyreverse (part of the PyLint tool suite). This command generates diagrams in PlantUML format and saves them to the specified directory.

    pyreverse -o plantuml -d assets/diagrams pdpbox
  12. Run the docstring generation script

    master

    After recording parameters in parameters.json and inserting placeholders in your Python files, run the generate_docstring.py script to automate the process.

    Steps:

    1. Navigate to the helper directory.
    2. Execute the script providing the parameter file and the target Python files.
    3. Review the generated *_updated.py files using a diff tool.
    4. Replace the original files with the updated versions if the changes are correct.
    cd assets/docs_helper
    python generate_docstring.py --param_file parameters.json --py_files <py_file1> <py_file2> ...