Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing Partial dependency plots - looks too small

I have created partial dependency plots via Scikit learn's library. However, I'm facing a challenge in resizing the plots to be much bigger as I could read all the plots entirely. Is there a method available to change plot view and size ?

Code:

from sklearn.ensemble.partial_dependence import partial_dependence, plot_partial_dependence
import pandas as pd
from pandas import read_csv, DataFrame
from sklearn.ensemble import GradientBoostingRegressor
import numpy as np

my_model = GradientBoostingRegressor()
my_model.fit(X, y)

my_plots = plot_partial_dependence(my_model,       
                        features=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22],
                        X=X,        
                        feature_names=['core_self_evaluations', '1b_score', 'investigate','respect_for_people','social','mastery_orientation','realistic','conventional','astronaut_score','innovation','agreeableness','gradeClass_second_lower','AC_TeamPlayer','enterprising','AC_Problemsolving','AC_StartsConversation','verbal','leadership_score','Race_chinese','performance_orientation','self_monitoring','UniLoc_overseas','attention_to_detail'], # labels on graphs
                        grid_resolution=5) 

Plot created:

enter image description here

like image 542
Dinesh Avatar asked Jan 28 '23 14:01

Dinesh


1 Answers

I have solved the sizing problem with the below code:

import matplotlib.pyplot as plt
fig, ax = plot_partial_dependence(my_model,       
                        features=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22],
                        X=X,        
                        feature_names=['core_self_evaluations', '1b_score', 'investigate','respect_for_people','social','mastery_orientation','realistic','conventional','astronaut_score','innovation','agreeableness','gradeClass_second_lower','AC_TeamPlayer','enterprising','AC_Problemsolving','AC_StartsConversation','verbal','leadership_score','Race_chinese','performance_orientation','self_monitoring','UniLoc_overseas','attention_to_detail'], # labels on graphs
                        grid_resolution=5) 
fig.set_figwidth(8)
fig.set_figheight(15)
fig.tight_layout()

This provides really good layout with customisable height and width. The hint to look at is the return value of the method (which is "fig" and "ax"). Using these two return values, extra options become available such as setting the width and height, independently.

like image 138
Dinesh Avatar answered Jan 31 '23 23:01

Dinesh