Report¶
Report templates. Report_Tool takes the artifacts a modeling run leaves on disk (performance CSVs, performance and WOE
plot images, variable-importance tables) and lays them out as formatted sheets with ExcelMaster. It does no
modeling or data transformation itself. User guide: Excel Report Generation.
Every function takes an ExcelMaster instance and a worksheet first and writes at the cursor. Import the functions from
their module:
Report Functions: Report_Tool¶
Report_Tool
¶
single_model_perf
¶
Put Single Model Performance Summary.
Writes an optional heading, the performance figure of one model and its performance table,
one below the other, at the cursor of em.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
em
|
ExcelMaster
|
ExcelMaster instance that writes the workbook; its cursor ( |
required |
ws
|
Worksheet
|
Worksheet to write on, as returned by |
required |
fig_path
|
str
|
Path of the performance figure (image file). The file is resized in place to |
required |
res_path
|
str
|
Path of the performance table (CSV file), such as the one written by
|
required |
model_name
|
str
|
Model name used in the table title |
required |
image_size
|
tuple of int
|
Size of the figure as |
required |
text
|
str or None
|
Optional heading written with |
None
|
Returns:
| Type | Description |
|---|---|
tuple of list of int
|
|
Notes
The columns Top10%_Lift (Top10%_TargetRate / avgTrue) and AUC_Shift (AUC of the
previous row divided by the AUC of this row, minus 1) are computed and added to the table
(replacing columns of the same name). The table is rounded to 3 decimals and written with
its header and without the index. Sets em.gap_number to 1 (it is 0 while the heading and
the figure are written).
Source code in Report/Report_Tool.py
write_var_info
¶
Write Var Information Table.
Writes the rows of the data dictionary that describe one variable as a table and returns the description of the variable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
em
|
ExcelMaster
|
ExcelMaster instance that writes the workbook; its cursor ( |
required |
ws
|
Worksheet
|
Worksheet to write on, as returned by |
required |
var
|
str
|
Variable to look up. |
required |
var_name
|
str
|
Name of the column of |
required |
data_dict
|
DataFrame
|
Data dictionary. It must contain the column named by |
required |
var_info_title
|
str
|
Title written above the table; an empty string writes no title. |
""
|
skipby
|
str
|
Where the cursor goes after the table: |
"row"
|
Returns:
| Type | Description |
|---|---|
tuple
|
|
Notes
The table holds every column of the matching rows of data_dict (header included, index
excluded). If var is not found in data_dict[var_name] the function fails with an
IndexError. Sets em.gap_number to 0.
Source code in Report/Report_Tool.py
plot_woe
¶
plot_woe(em, ws, var, woe_bins, x_col, spec_missing_value=-99999, chart_size=(20, 5), var_name='var_name', description='', skipby='row')
Plot WOE Table.
Draws a native Excel chart for the WOE bins of one variable: stacked columns with the bad
(n1) and good (n0) counts, the bad rate (tr) as a line on the secondary axis, and
two dashed reference lines with the mean bad rate of the variable, with and without the
missing-value bin.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
em
|
ExcelMaster
|
ExcelMaster instance that writes the workbook; its cursor ( |
required |
ws
|
Worksheet
|
Worksheet to write on, as returned by |
required |
var
|
str
|
Variable to plot: the rows of |
required |
woe_bins
|
DataFrame
|
WOE bin table with one row per bin. Required columns: the variable column named by
|
required |
x_col
|
str
|
Column of |
required |
spec_missing_value
|
int or float
|
Special value that codes missing values. Bins whose |
-99999
|
chart_size
|
tuple of int
|
Size of the chart as |
(20, 5)
|
var_name
|
str
|
Name of the column of |
"var_name"
|
description
|
str
|
Text used as the x-axis title. |
""
|
skipby
|
str
|
Where the cursor goes after the chart: |
"row"
|
Returns:
| Type | Description |
|---|---|
list of int
|
Zero-based cell range |
Notes
The chart title is the variable name followed by (iv: <IV>), where the IV is the sum of
the iv column over the rows of the variable, rounded to 4 decimals. The two reference
lines are simple, unweighted means of tr over the bins. The column bin_value is read
by that name whatever x_col is (KeyError if it is missing). If var has no row in
woe_bins, an empty chart is still written. Sets em.gap_number to 1.
Source code in Report/Report_Tool.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |
get_woe_plot_report
¶
Generate Plots for WOE BINS in Excel.
Older layout of the WOE plot report. Under the heading Bivar Table, every variable of
varlist gets one block: its WOE plot and its grouped WOE plot side by side and, if
means_rpt is given, a table of descriptive statistics to their right. Column A repeats
the variable name on every row of its block.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
em
|
ExcelMaster
|
ExcelMaster instance that writes the workbook; its cursor ( |
required |
ws
|
Worksheet
|
Worksheet to write on, as returned by |
required |
analysis_dir
|
str
|
Directory of the analysis output. It must contain |
required |
varlist
|
list of str
|
Variables to report. A variable is skipped when |
required |
means_rpt
|
DataFrame or None
|
Optional descriptive statistics, one row per variable, with the variable name in a column
named |
None
|
Returns:
| Type | Description |
|---|---|
int
|
Always 0. |
Notes
Every image is resized in place to 30 rows by 9 columns of worksheet cells, so the PNG files
are overwritten. The function moves the cursor of em to fixed locations and sets
em.gap_number to 0 once at least one variable has been written.
Source code in Report/Report_Tool.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | |
get_woe_plot_report_new
¶
Generate Plots for WOE BINS in Excel.
Under the heading Bivar Table, every variable of varlist gets one block: its overall
WOE plot and its WOE plot by group side by side and, if means_rpt is given, a table of
descriptive statistics to their right. Column A repeats the variable name on every row of
its block.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
em
|
ExcelMaster
|
ExcelMaster instance that writes the workbook; its cursor ( |
required |
ws
|
Worksheet
|
Worksheet to write on, as returned by |
required |
woe_plot_dir
|
str
|
Directory with the plot images |
required |
grp_name
|
str
|
Suffix of the by-group images. A variable is skipped when |
required |
varlist
|
list of str
|
Variables to report, in the order of the blocks. |
required |
means_rpt
|
DataFrame or None
|
Optional descriptive statistics, one row per variable, with the variable name in a column
named |
None
|
Returns:
| Type | Description |
|---|---|
int
|
Always 0. |
Notes
Every image is resized in place to 40 rows by 9 columns of worksheet cells, so the PNG files
are overwritten. The function moves the cursor of em to fixed locations and sets
em.gap_number to 0 once at least one variable has been written.
Source code in Report/Report_Tool.py
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 | |
get_multi_model_perf_report
¶
Create Multi-models Evaluation Report.
Writes a fixed layout that compares five models: XGBoost and LightGBM on the original
features, then logistic regression, XGBoost and LightGBM on the WOE-transformed features.
Each model block is a performance figure followed by its performance table (see
single_model_perf).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
em
|
ExcelMaster
|
ExcelMaster instance that writes the workbook; its cursor ( |
required |
ws
|
Worksheet
|
Worksheet to write on, as returned by |
required |
eval_img_path
|
str
|
Directory with the five performance figures |
required |
eval_res_path
|
str
|
Directory with the five matching performance tables, named like the figures with the
extension |
required |
Returns:
| Type | Description |
|---|---|
None
|
|
Notes
The file names, the order of the models and the headings (Multi-Model Evaluation
(Untuned), Using Third-Party Features Directly, Modeling on WOE-Transformed
Features) are fixed. The function registers the formats CUS_#, CUS_## and
bg_tmp on em (a name that is already registered is kept) and leaves
em.gap_number at 1.
Source code in Report/Report_Tool.py
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 | |
get_multi_model_varimp
¶
Varimp for Multi Model Evaluation.
Writes the heading Feature Importance Evaluation and the variable-importance tables of
several models: the original-feature table and, to its right, the WOE-feature table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
em
|
ExcelMaster
|
ExcelMaster instance that writes the workbook; its cursor ( |
required |
ws
|
Worksheet
|
Worksheet to write on, as returned by |
required |
raw_varimp
|
DataFrame or None
|
Importance of the original features, with the columns |
None
|
woe_varimp
|
DataFrame or None
|
Importance of the WOE features, with the columns |
None
|
Returns:
| Type | Description |
|---|---|
int
|
Always 0. |
Notes
The heading uses the format CUS_#, which must be registered on em beforehand (for
example with em.add_new_format), otherwise a KeyError is raised. The tables are
written under the titles Variable Importance (Original Feature) and Variable
Importance (WOE-ed Feature), rounded to 4 decimals. Sets em.gap_number to 1.
Source code in Report/Report_Tool.py
get_fnl_model_report
¶
Create Final Model Performance Report.
Writes a fixed layout for a final XGBoost model: the headings Final Model Evaluation and
Modeling on Original Features, then the performance figure and table of the model (see
single_model_perf). The headings and the table title say XGBoost (Without Monotonic
Constraints) and XGBoost (Without MC); for any other model use single_model_perf.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
em
|
ExcelMaster
|
ExcelMaster instance that writes the workbook; its cursor ( |
required |
ws
|
Worksheet
|
Worksheet to write on, as returned by |
required |
result_dir
|
str
|
Directory with the figure |
required |
Returns:
| Type | Description |
|---|---|
int
|
Always 0. |
Notes
Registers the formats CUS_# and CUS_## on em (a name that is already registered
is kept) and leaves em.gap_number at 1.
Source code in Report/Report_Tool.py
get_model_varimp
¶
Varimp for a single model: write one variable-importance table.
Writes the heading Feature Importance Evaluation and one importance table titled
Variable Importance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
em
|
ExcelMaster
|
ExcelMaster instance that writes the workbook; its cursor ( |
required |
ws
|
Worksheet
|
Worksheet to write on, as returned by |
required |
varimp
|
DataFrame
|
Variable-importance table (any columns). It is rounded to 4 decimals and written with its header and without the index. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Always 0. |
Notes
The heading uses the format CUS_#, which must be registered on em beforehand (for
example with em.add_new_format), otherwise a KeyError is raised. Sets
em.gap_number to 1.