예제 #1
0
def mock_ale_explainer(request):
    """
    Instantiates an ALE explainer with a mock predictor.
    """
    out_dim, out_type = request.param
    predictor = MockPredictor(out_dim=out_dim, out_type=out_type, seed=0)
    explainer = ALE(predictor)

    return explainer
예제 #2
0
def ale(model, frame: frame):
    """ Calcula los valores de Accumulated Local Effect (ALE) del modelo para los datos en frame
    Arg:
        model: modelo entrenado con el cual calcular el partial dependence
        frame: data de entrada del modelo
    Returns:
        Entrega un diccionario con los nombres de las variables como sus llaves y los valores una tupla de los valores que estas toman
        y su valor ALE calculado
    """
    exp_results = ALE(model.predict,
                      feature_names=frame.columns).explain(frame.values)
    ale_results = {}
    for i in range(len(exp_results['feature_names'])):
        ale_results[exp_results['feature_names'][i]] = (
            exp_results['feature_values'][i],
            exp_results['ale_values'][i].reshape(
                (exp_results['feature_values'][i].shape)))
    return ale_results
예제 #3
0
def make_ale(dirname: Optional[Path] = None) -> ALE:
    data = load_iris()
    feature_names = data.feature_names
    target_names = data.target_names
    X = data.data
    y = data.target
    X_train, X_test, y_train, y_test = train_test_split(X,
                                                        y,
                                                        test_size=0.25,
                                                        random_state=42)

    # train model
    lr = LogisticRegression(max_iter=200)
    lr.fit(X_train, y_train)

    # create explainer
    logit_fun_lr = lr.decision_function
    logit_ale_lr = ALE(logit_fun_lr,
                       feature_names=feature_names,
                       target_names=target_names)

    if dirname is not None:
        logit_ale_lr.save(dirname)
    return logit_ale_lr
예제 #4
0
def ale_explainer(iris_data, lr_classifier):
    ale = ALE(predictor=lr_classifier.predict_proba,
              feature_names=iris_data['metadata']['feature_names'])
    return ale