Skip to content

Python: lack of validation of the predict_function's output shape may cause errors #325

Description

@pfraczek

Currently shape of the predict_function output is not validated and anything that does not raise an error while
computing residuals passes silently through instantianting Explainer object, with incorrect results:

import numpy as np
import dalex as dx
from sklearn.ensemble import RandomForestRegressor


data = dx.datasets.load_fifa()
X = data.drop(columns=['nationality', 'value_eur'])
y = data['value_eur']

model = RandomForestRegressor()
model.fit(X, y)

def predict_function_return_2d(model, data):
    n_rows = data.shape[0]
    prediction = model.predict(data)

    return prediction.reshape((n_rows, 1))

def predict_function_return_3d(model, data):
    n_rows = data.shape[0]
    prediction = model.predict(data)

    return prediction.reshape((n_rows, 1, 1))

def predict_function_return_one_element_array(model, data):
    return np.array(0.2)


exp_2d = dx.Explainer(model, X, y, verbose=True, model_type='regression', predict_function=predict_function_return_2d)
print(exp_2d.y_hat.shape)
print(exp_2d.residuals.shape)


exp_3d = dx.Explainer(model, X, y, verbose=True, model_type='regression', predict_function=predict_function_return_3d)
print(exp_3d.y_hat.shape)
print(exp_3d.residuals.shape)


exp_one = dx.Explainer(model, X, y, verbose=True, model_type='regression', predict_function=predict_function_return_one_element_array)
print(exp_one.y_hat.shape)
print(exp_one.residuals.shape)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Python 🐍Related to Pythoninvalid ❕This doesn't seem right, potential bug

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions