# Sensitivity analysis of a machine learning model

Let’s say I have a set of input variables (A, B, C and D) and I predict a target (y) using a machine learning model (XGBRegressor in my case) with a reasonable performance (5% relative error on test set).

from sklearn.datasets import make_regression
import pandas as pd
from xgboost import XGBRegressor

X, y = make_regression(n_samples=500, n_features=4, n_informative=2, noise=0.3)
X = pd.DataFrame(X, columns=['A', 'B', 'C', 'D'])

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


Now, I want to do some kind of sensitivity analysis on this model by answering two questions:

1. What is the impact of a 5% independent increase in variables A, B and C (not D) on the target variable?

2. From variables A, B, C and D; which combination of values of A, B and C (without touching D) increases the target y value by 10, minimizing the sum of A, B and C.

I have already answered question one (see this gist). However, how can question 2 be coded? I imagine that this implies an optimization problem.