def interactionPlotPredicted(data, pr, featuresToExamine): fig, axes, summary_df = info_plots.actual_plot_interact( model=pr, X=data, features=featuresToExamine, feature_names=featuresToExamine) save("interactionPlotPredicted", plt=plt, fig=fig)
def actual_interact_plot(self, feature, var_name = None, sample = 10000, which_classes = None, show_outliers=True, **kargs): fig, axes, result = info_plots.actual_plot_interact( model = self.md, X = self.sample(sample), features=feature, feature_names=var_name or feature, which_classes=which_classes, show_outliers= show_outliers, **kargs) self.actual_interact_data = ResultDF(result, 'count') plt.show()
def pred_target_plot_inter(clf,X,Y,features,label,grid_ranges=None): from pdpbox import info_plots pd.options.mode.chained_assignment = None # Turn warning msg off # Extract the classifier object from the clf multilearn object index = Y.columns.to_list().index(label) clf = clf.classifiers_[index] clf.verbose = False #Turn verbose off after this to tidy prints if(grid_ranges is None): fig, ax, summary_df = info_plots.actual_plot_interact(clf,X,features=features,feature_names=features,grid_types=['equal','equal']) else: fig, ax, summary_df = info_plots.actual_plot_interact(clf,X,features=features,feature_names=features,grid_types=['equal','equal'], show_outliers='True',grid_ranges=grid_ranges) pd.options.mode.chained_assignment = 'warn' # Turn warning msg back on clf.verbose = True # reset return fig, ax
# %% target_plot_interact ---------------------------------------------------- fig, axes, summary_df = info_plots.target_plot_interact( df=XY, features=x_cols[2:], feature_names=x_cols[2:], target=y_cols[0], ) # %% actual_plot_interact ---------------------------------------------------- fig, axes, summary_df = info_plots.actual_plot_interact( model=model, X=X, features=x_cols[3:], feature_names=x_cols[3:], which_classes=[2, 5], ) # %% pdp_interact: Preset ---------------------------------------------------- pdp_interacted_tmp = pdp.pdp_interact( model=model, dataset=X, model_features=x_cols, features=x_cols[:2], num_grid_points=[10, 10], percentile_ranges=[None, None], n_jobs=1, )
display(summary_df) #And finally let's obtain the PDP for the feature Age pdp_fare = pdp.pdp_isolate( model=titanic_model, dataset=titanic_data, model_features=titanic_features, feature='Age' ) fig, axes = pdp.pdp_plot(pdp_fare, 'Age', plot_pts_dist=True) #Let's study the link between Age and Pclass #Statistics of survivors based on Age and Pclass fig, axes, summary_df = info_plots.target_plot_interact( df=titanic_data, features=['Age', 'Pclass'], feature_names=['Age', 'Pclass'], target=titanic_target ) display(summary_df.head()) #Prediction of our model, impact if Age and Pclass fig, axes, summary_df = info_plots.actual_plot_interact( model=titanic_model, X=titanic_data[titanic_features], features=['Age', 'Pclass'], feature_names=['Age', 'Pclass'] ) display(summary_df.head()) #PDP for the interaction between Age and Pclass inter1 = pdp.pdp_interact( model=titanic_model, dataset=titanic_data, model_features=titanic_features, features=['Age', 'Pclass'] ) fig, axes = pdp.pdp_interact_plot( pdp_interact_out=inter1, feature_names=['age', 'Pclass'], plot_type='contour', x_quantile=True, plot_pdp=True ) #Let's study the link between Fare and Sex #Statistics of survivors based on Fare and Sex fig, axes, summary_df = info_plots.target_plot_interact( df=titanic_data, features=['Fare', 'Sex'], feature_names=['Fare', 'Sex'], target=titanic_target )
feature_name=_, target=target_variable ) #PDP Plots : Actual Plot for _ in X.columns.tolist(): fig,axes,summary_df=info_plots.actual_plot( model=baseline, X=df[X.columns], feature=_, feature_name=_, predict_kwds={} ) fig, axes, summary_df = info_plots.actual_plot_interact( model=baseline, X=df[X.columns], features=interactions_2way, feature_names=interactions_2way ) #PDP Plot : Grid Plot interactions = pdp.pdp_interact( model=baseline, dataset=df, model_features=X.columns, features=interactions_2way ) fig, axes = pdp.pdp_interact_plot(interactions,interactions_2way , plot_type='grid', x_quantile=True, plot_pdp=False) #SHAP shap.initjs() explainer=shap.TreeExplainer(baseline) shap_values=explainer.shap_values(x_train[_feat]) shap.summary_plot(shap_values,x_train[_feat])
generateInsight(xgb_bin1,'longitude',x_train_bin1) generateInsight(xgb_bin1,'accommodates',x_train_bin1) generateInsight(xgb_bin1,'extra_people',x_train_bin1) generateInsight(xgb_bin1,'minimum_nights',x_train_bin1) generateInsight(xgb_bin1,'guests_included',x_train_bin1) generateInsight(xgb_bin1,[col for col in x_train_bin1 if col.startswith('room_type')],x_train_bin1) generateInsight(xgb_bin1,[col for col in x_train_bin1 if col.startswith('property_type')],x_train_bin1) #interaction plot feats = ['latitude', 'longitude'] p = pdp.pdp_interact(xgb_bin1, x_train_bin1, x_train_bin1.columns,feats) pdp.pdp_interact_plot(p, feats)#the model is correctly able to find to classify the centre to london close to Buckingham palace as most expensive fig, axes, summary_df = info_plots.actual_plot_interact( model=xgb_bin1, X=x_train_bin1, features=['accommodates', [col for col in x_train_bin1 if col.startswith('property_type')]], feature_names=['accommodates', 'Property Type'], ) #Plot Feature importances plotFeatureImportances(xgb_bin1,x_train_bin1.columns) ##############Model for Price Bin 2##################### y_train_bin2=y_train.loc[(y_train.price_bins==2),'price'] y_test_bin2=y_test.loc[y_test.price_bins==2,'price'] x_train_bin2=x_train.loc[y_train_bin2.index,:] x_test_bin2=x_test.loc[y_test_bin2.index,:] y_train_bin2=y_train_bin2.reset_index(drop=True)