<jupyter_start><jupyter_text>Data processing<jupyter_code>#respond variable
data["y"] = data["y"].replace("yes",1)
data["y"] = data["y"].replace("no",0)
#default
data["default"] = data["default"].replace("yes",1)
data["default"] = data["default"].replace("no",0)
data["default"] = data["default"].replace("unknown",2)

#housing
data["housing"] = data["housing"].replace("yes",1)
data["housing"] = data["housing"].replace("no",0)
data["housing"] = data["housing"].replace("unknown",2)

#loan
data["loan"] = data["loan"].replace("yes",1)
data["loan"] = data["loan"].replace("no",0)
data["loan"] = data["loan"].replace("unknown",2)

#month
data["month"] = data["month"].replace("apr",4)
data["month"] = data["month"].replace("aug",8)
data["month"] = data["month"].replace("dec",12)
data["month"] = data["month"].replace("jul",7)
data["month"] = data["month"].replace("jun",6)
data["month"] = data["month"].replace("mar",3)
data["month"] = data["month"].replace("may",5)
data["month"] = data["month"].replace("nov",11)
data["month"] = data["month"].replace("oct",10)
data["month"] = data["month"].replace("sep",9)

#day_of_week
data["day_of_week"] = data["day_of_week"].replace("mon",1)
data["day_of_week"] = data["day_of_week"].replace("tue",2)
data["day_of_week"] = data["day_of_week"].replace("wed",3)
data["day_of_week"] = data["day_of_week"].replace("thu",4)
data["day_of_week"] = data["day_of_week"].replace("fri",5)

data = pd.get_dummies(data, drop_first=True)<jupyter_output><empty_output><jupyter_text>Split train and test<jupyter_code>y = data['y']
x = data.drop(['y'],axis = 1)
x_train, x_test, y_train,y_test = train_test_split(x,y,train_size=0.8,random_state=20,shuffle = True)<jupyter_output><empty_output><jupyter_text>Apply random forest model<jupyter_code>rf = RandomForestRegressor(max_depth=7, random_state=20, n_estimators=100)
rf.fit(x_train, y_train)  
explainer = shap.TreeExplainer(rf)
shap_values = explainer.shap_values(x_test)
shap.initjs()<jupyter_output><empty_output><jupyter_text>Global level interpretFrom the Figure 1, we can conclude that the top 2 features that important to the target variable are "duration" and "number of employees". Specifically, for "duration", it indicates that clients with a longer contact duration are more likely to subscribed a term deposit, while for "number of employees", it indicates that clients with lower number of employees are more likely to subscribed a term deposit.<jupyter_code>shap.summary_plot(shap_values, x_test,show=False)
plt.title("Figure 1. Overall importance graph")
plt.show()<jupyter_output><empty_output><jupyter_text>Then "number of employees" and "education" are checked individually. Figure 2 shows a denpendence graph of "nr.employed", which indicates that 'nr.employed' and 'duration' are correlated. Generally speaking, lower value of 'nr.employed' may occupy a higher duration.<jupyter_code>shap.dependence_plot("nr.employed",shap_values, x_test, show = False)
plt.title("Figure 2. Number of employees dependence graph")
plt.show()
value = data[["euribor3m", "duration","nr.employed","pdays","month"]]
value.mean()
shap.force_plot(explainer.expected_value, shap_values[15,:], x_test.iloc[15,:])<jupyter_output><empty_output><jupyter_text>From the Figure 1, it can be concluded that education status may not have strong impact on the target value as 'number of employees' as only two sub-categroies of education are presented (education_university.degree; education_basic.6y) and their ranks are low. Specifically, for "education_university.degree", Figure 3 indicates that it is related to "consumer price index" however, the relationship between the "university.degree" and "consumer price index" is not quite clear.<jupyter_code>shap.dependence_plot("education_university.degree",shap_values, x_test, show = False)
plt.title("Figure 3. Dependence graph of university degree")
plt.show()<jupyter_output><empty_output><jupyter_text>Furthermore, "education_basic.6y" is dependent with "duration". And Figure 4 indicates that most of the clients with 6 years education tend to have shorter contact duration.<jupyter_code>shap.dependence_plot("education_basic.6y",shap_values, x_test, show = False)
plt.title("Figure 4. Dependence graph of basic 6 years")
plt.show()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup