def f(array):
    return (np.sum(X, axis=1) > 0.1).astype(float)


from sklearn.dummy import DummyRegressor
c = DummyRegressor()
c.fit(X, y)
c.predict = f

pdp, axes = partial_dependence.partial_dependence(c, [0, 1], X = X, method='exact')

"""
Explanation: Plug in dummy classifier :
Works with a hack
End of explanation
"""


pdp, axes = partial_dependence.partial_dependence(logit_model, [0, 1], X = X, method='exact')

pdp, axes = partial_dependence.partial_dependence(logit_model, [0, 1], X = X, method='estimated')

"""
Explanation: Use on classification model with exact method:
Fails due to bad error handling
End of explanation
"""


pdp_exact, axes_exact = partial_dependence.partial_dependence(rf_r_model, [0], X = X, method='exact')
pdp_est, axes_est = partial_dependence.partial_dependence(rf_r_model, [0], X = X, method='estimated')
pdp_rec, axes_rec = partial_dependence.partial_dependence(rf_r_model, [0], X = X, method='recursion')

ax = pd.DataFrame(pdp_exact[0], index = axes_exact[0], columns=['exact']).plot()
pd.DataFrame(pdp_est[0], index = axes_est[0], columns=['estimated']).plot(ax=ax)
pd.DataFrame(pdp_rec[0], index = axes_rec[0], columns=['recursion']).plot(ax=ax)

"""
Explanation: Forest regressor model with estimated, exact, and recursive, using data
Works, though estimated is pretty bad
End of explanation
"""


grid = [i / 10. for i in range(-70, 90)]
pdp_exact, axes_exact = partial_dependence.partial_dependence(rf_r_model, [0], grid = grid, X=X, method='exact')
pdp_est, axes_est = partial_dependence.partial_dependence(rf_r_model, [0], grid = grid, X=X, method='estimated')
pdp_rec, axes_rec = partial_dependence.partial_dependence(rf_r_model, [0], grid = grid, X=X,  method='recursion')

ax = pd.DataFrame(pdp_exact[0], index = axes_exact[0], columns=['exact']).plot()
pd.DataFrame(pdp_est[0], index = axes_est[0], columns=['estimated']).plot(ax=ax)
pd.DataFrame(pdp_rec[0], index = axes_rec[0], columns=['recursion']).plot(ax=ax)

"""
Explanation: Forest regressor model with estimated, exact, and recursive, using grid and data:
Fails due requirement of either X or grid but not both
End of explanation
"""


def get_multiclass(x, Y):
    if x > np.percentile(Y, .66):
        return 1
    elif x < np.percentile(Y, .33):
        return -1
    else:
        return 0
y_multiclass = np.array(map(lambda x: get_multiclass(x, y), y))
rf_multi_model = RandomForestClassifier()
rf_multi_model.fit(X, y_multiclass)

pdp_exact, axes_exact = partial_dependence.partial_dependence(rf_multi_model, [0], X=X, method='exact')

"""
Explanation: Multiclass classification:
Fails due to bad error handling
End of explanation
"""


