@@ -146,7 +146,8 @@ def plot_qini_curve(y_true, uplift, treatment, random=True, perfect=True, negati
146146 return ax
147147
148148
149- def plot_uplift_by_percentile (y_true , uplift , treatment , strategy = 'overall' , kind = 'line' , bins = 10 ):
149+ def plot_uplift_by_percentile (y_true , uplift , treatment , strategy = 'overall' ,
150+ kind = 'line' , bins = 10 , string_percentiles = True ):
150151 """Plot uplift score, treatment response rate and control response rate at each percentile.
151152
152153 Treatment response rate ia a target mean in the treatment group.
@@ -175,6 +176,7 @@ def plot_uplift_by_percentile(y_true, uplift, treatment, strategy='overall', kin
175176 Generates a traditional bar-style plot.
176177
177178 bins (int): Determines а number of bins (and the relative percentile) in the test data. Default is 10.
179+ string_percentiles (bool): type of xticks: float or string to plot. Default is True (string).
178180
179181 Returns:
180182 Object that stores computed values.
@@ -203,8 +205,12 @@ def plot_uplift_by_percentile(y_true, uplift, treatment, strategy='overall', kin
203205 raise ValueError (
204206 f'Number of bins = { bins } should be smaller than the length of y_true { n_samples } ' )
205207
208+ if not isinstance (string_percentiles , bool ):
209+ raise ValueError (f'string_percentiles flag should be bool: True or False.'
210+ f' Invalid value string_percentiles: { string_percentiles } ' )
211+
206212 df = uplift_by_percentile (y_true , uplift , treatment , strategy = strategy ,
207- std = True , total = True , bins = bins )
213+ std = True , total = True , bins = bins , string_percentiles = False )
208214
209215 percentiles = df .index [:bins ].values .astype (float )
210216
@@ -219,7 +225,8 @@ def plot_uplift_by_percentile(y_true, uplift, treatment, strategy='overall', kin
219225
220226 uplift_weighted_avg = df .loc ['total' , 'uplift' ]
221227
222- check_consistent_length (percentiles , response_rate_trmnt , response_rate_ctrl , uplift_score ,
228+ check_consistent_length (percentiles , response_rate_trmnt ,
229+ response_rate_ctrl , uplift_score ,
223230 std_trmnt , std_ctrl , std_uplift )
224231
225232 if kind == 'line' :
@@ -235,7 +242,15 @@ def plot_uplift_by_percentile(y_true, uplift, treatment, strategy='overall', kin
235242
236243 if np .amin (uplift_score ) < 0 :
237244 axes .axhline (y = 0 , color = 'black' , linewidth = 1 )
238- axes .set_xticks (percentiles )
245+
246+ if string_percentiles : # string percentiles for plotting
247+ percentiles_str = [f"0-{ percentiles [0 ]:.0f} " ] + \
248+ [f"{ percentiles [i ]:.0f} -{ percentiles [i + 1 ]:.0f} " for i in range (len (percentiles ) - 1 )]
249+ axes .set_xticks (percentiles )
250+ axes .set_xticklabels (percentiles_str , rotation = 45 )
251+ else :
252+ axes .set_xticks (percentiles )
253+
239254 axes .legend (loc = 'upper right' )
240255 axes .set_title (
241256 f'Uplift by percentile\n weighted average uplift = { uplift_weighted_avg :.4f} ' )
@@ -245,8 +260,7 @@ def plot_uplift_by_percentile(y_true, uplift, treatment, strategy='overall', kin
245260
246261 else : # kind == 'bar'
247262 delta = percentiles [0 ]
248- fig , axes = plt .subplots (ncols = 1 , nrows = 2 , figsize = (
249- 8 , 6 ), sharex = True , sharey = True )
263+ fig , axes = plt .subplots (ncols = 1 , nrows = 2 , figsize = (8 , 6 ), sharex = True , sharey = True )
250264 fig .text (0.04 , 0.5 , 'Uplift = treatment response rate - control response rate' ,
251265 va = 'center' , ha = 'center' , rotation = 'vertical' )
252266
@@ -263,7 +277,15 @@ def plot_uplift_by_percentile(y_true, uplift, treatment, strategy='overall', kin
263277 axes [0 ].set_title (
264278 f'Uplift by percentile\n weighted average uplift = { uplift_weighted_avg :.4f} ' )
265279
266- axes [1 ].set_xticks (percentiles )
280+ if string_percentiles : # string percentiles for plotting
281+ percentiles_str = [f"0-{ percentiles [0 ]:.0f} " ] + \
282+ [f"{ percentiles [i ]:.0f} -{ percentiles [i + 1 ]:.0f} " for i in range (len (percentiles ) - 1 )]
283+ axes [1 ].set_xticks (percentiles )
284+ axes [1 ].set_xticklabels (percentiles_str , rotation = 45 )
285+
286+ else :
287+ axes [1 ].set_xticks (percentiles )
288+
267289 axes [1 ].legend (loc = 'upper right' )
268290 axes [1 ].axhline (y = 0 , color = 'black' , linewidth = 1 )
269291 axes [1 ].set_xlabel ('Percentile' )
0 commit comments