1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 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 214 215 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 334 335 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 448 449 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 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
|
import numbers
from itertools import chain
from math import ceil
import numpy as np
from scipy import sparse
from scipy.stats.mstats import mquantiles
from ...base import is_regressor
from ...utils import (
Bunch,
_safe_indexing,
check_array,
check_matplotlib_support, # noqa
check_random_state,
)
from ...utils._encode import _unique
from ...utils.parallel import Parallel, delayed
from .. import partial_dependence
from .._pd_utils import _check_feature_names, _get_feature_index
class PartialDependenceDisplay:
"""Partial Dependence Plot (PDP).
This can also display individual partial dependencies which are often
referred to as: Individual Condition Expectation (ICE).
It is recommended to use
:func:`~sklearn.inspection.PartialDependenceDisplay.from_estimator` to create a
:class:`~sklearn.inspection.PartialDependenceDisplay`. All parameters are
stored as attributes.
Read more in
:ref:`sphx_glr_auto_examples_miscellaneous_plot_partial_dependence_visualization_api.py`
and the :ref:`User Guide <partial_dependence>`.
.. versionadded:: 0.22
Parameters
----------
pd_results : list of Bunch
Results of :func:`~sklearn.inspection.partial_dependence` for
``features``.
features : list of (int,) or list of (int, int)
Indices of features for a given plot. A tuple of one integer will plot
a partial dependence curve of one feature. A tuple of two integers will
plot a two-way partial dependence curve as a contour plot.
feature_names : list of str
Feature names corresponding to the indices in ``features``.
target_idx : int
- In a multiclass setting, specifies the class for which the PDPs
should be computed. Note that for binary classification, the
positive class (index 1) is always used.
- In a multioutput setting, specifies the task for which the PDPs
should be computed.
Ignored in binary classification or classical regression settings.
deciles : dict
Deciles for feature indices in ``features``.
kind : {'average', 'individual', 'both'} or list of such str, \
default='average'
Whether to plot the partial dependence averaged across all the samples
in the dataset or one line per sample or both.
- ``kind='average'`` results in the traditional PD plot;
- ``kind='individual'`` results in the ICE plot;
- ``kind='both'`` results in plotting both the ICE and PD on the same
plot.
A list of such strings can be provided to specify `kind` on a per-plot
basis. The length of the list should be the same as the number of
interaction requested in `features`.
.. note::
ICE ('individual' or 'both') is not a valid option for 2-ways
interactions plot. As a result, an error will be raised.
2-ways interaction plots should always be configured to
use the 'average' kind instead.
.. note::
The fast ``method='recursion'`` option is only available for
`kind='average'` and `sample_weights=None`. Computing individual
dependencies and doing weighted averages requires using the slower
`method='brute'`.
.. versionadded:: 0.24
Add `kind` parameter with `'average'`, `'individual'`, and `'both'`
options.
.. versionadded:: 1.1
Add the possibility to pass a list of string specifying `kind`
for each plot.
subsample : float, int or None, default=1000
Sampling for ICE curves when `kind` is 'individual' or 'both'.
If float, should be between 0.0 and 1.0 and represent the proportion
of the dataset to be used to plot ICE curves. If int, represents the
maximum absolute number of samples to use.
Note that the full dataset is still used to calculate partial
dependence when `kind='both'`.
.. versionadded:: 0.24
random_state : int, RandomState instance or None, default=None
Controls the randomness of the selected samples when subsamples is not
`None`. See :term:`Glossary <random_state>` for details.
.. versionadded:: 0.24
is_categorical : list of (bool,) or list of (bool, bool), default=None
Whether each target feature in `features` is categorical or not.
The list should be same size as `features`. If `None`, all features
are assumed to be continuous.
.. versionadded:: 1.2
Attributes
----------
bounding_ax_ : matplotlib Axes or None
If `ax` is an axes or None, the `bounding_ax_` is the axes where the
grid of partial dependence plots are drawn. If `ax` is a list of axes
or a numpy array of axes, `bounding_ax_` is None.
axes_ : ndarray of matplotlib Axes
If `ax` is an axes or None, `axes_[i, j]` is the axes on the i-th row
and j-th column. If `ax` is a list of axes, `axes_[i]` is the i-th item
in `ax`. Elements that are None correspond to a nonexisting axes in
that position.
lines_ : ndarray of matplotlib Artists
If `ax` is an axes or None, `lines_[i, j]` is the partial dependence
curve on the i-th row and j-th column. If `ax` is a list of axes,
`lines_[i]` is the partial dependence curve corresponding to the i-th
item in `ax`. Elements that are None correspond to a nonexisting axes
or an axes that does not include a line plot.
deciles_vlines_ : ndarray of matplotlib LineCollection
If `ax` is an axes or None, `vlines_[i, j]` is the line collection
representing the x axis deciles of the i-th row and j-th column. If
`ax` is a list of axes, `vlines_[i]` corresponds to the i-th item in
`ax`. Elements that are None correspond to a nonexisting axes or an
axes that does not include a PDP plot.
.. versionadded:: 0.23
deciles_hlines_ : ndarray of matplotlib LineCollection
If `ax` is an axes or None, `vlines_[i, j]` is the line collection
representing the y axis deciles of the i-th row and j-th column. If
`ax` is a list of axes, `vlines_[i]` corresponds to the i-th item in
`ax`. Elements that are None correspond to a nonexisting axes or an
axes that does not include a 2-way plot.
.. versionadded:: 0.23
contours_ : ndarray of matplotlib Artists
If `ax` is an axes or None, `contours_[i, j]` is the partial dependence
plot on the i-th row and j-th column. If `ax` is a list of axes,
`contours_[i]` is the partial dependence plot corresponding to the i-th
item in `ax`. Elements that are None correspond to a nonexisting axes
or an axes that does not include a contour plot.
bars_ : ndarray of matplotlib Artists
If `ax` is an axes or None, `bars_[i, j]` is the partial dependence bar
plot on the i-th row and j-th column (for a categorical feature).
If `ax` is a list of axes, `bars_[i]` is the partial dependence bar
plot corresponding to the i-th item in `ax`. Elements that are None
correspond to a nonexisting axes or an axes that does not include a
bar plot.
.. versionadded:: 1.2
heatmaps_ : ndarray of matplotlib Artists
If `ax` is an axes or None, `heatmaps_[i, j]` is the partial dependence
heatmap on the i-th row and j-th column (for a pair of categorical
features) . If `ax` is a list of axes, `heatmaps_[i]` is the partial
dependence heatmap corresponding to the i-th item in `ax`. Elements
that are None correspond to a nonexisting axes or an axes that does not
include a heatmap.
.. versionadded:: 1.2
figure_ : matplotlib Figure
Figure containing partial dependence plots.
See Also
--------
partial_dependence : Compute Partial Dependence values.
PartialDependenceDisplay.from_estimator : Plot Partial Dependence.
Examples
--------
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> from sklearn.datasets import make_friedman1
>>> from sklearn.ensemble import GradientBoostingRegressor
>>> from sklearn.inspection import PartialDependenceDisplay
>>> from sklearn.inspection import partial_dependence
>>> X, y = make_friedman1()
>>> clf = GradientBoostingRegressor(n_estimators=10).fit(X, y)
>>> features, feature_names = [(0,)], [f"Features #{i}" for i in range(X.shape[1])]
>>> deciles = {0: np.linspace(0, 1, num=5)}
>>> pd_results = partial_dependence(
... clf, X, features=0, kind="average", grid_resolution=5)
>>> display = PartialDependenceDisplay(
... [pd_results], features=features, feature_names=feature_names,
... target_idx=0, deciles=deciles
... )
>>> display.plot(pdp_lim={1: (-1.38, 0.66)})
<...>
>>> plt.show()
"""
def __init__(
self,
pd_results,
*,
features,
feature_names,
target_idx,
deciles,
kind="average",
subsample=1000,
random_state=None,
is_categorical=None,
):
self.pd_results = pd_results
self.features = features
self.feature_names = feature_names
self.target_idx = target_idx
self.deciles = deciles
self.kind = kind
self.subsample = subsample
self.random_state = random_state
self.is_categorical = is_categorical
@classmethod
def from_estimator(
cls,
estimator,
X,
features,
*,
sample_weight=None,
categorical_features=None,
feature_names=None,
target=None,
response_method="auto",
n_cols=3,
grid_resolution=100,
percentiles=(0.05, 0.95),
method="auto",
n_jobs=None,
verbose=0,
line_kw=None,
ice_lines_kw=None,
pd_line_kw=None,
contour_kw=None,
ax=None,
kind="average",
centered=False,
subsample=1000,
random_state=None,
):
"""Partial dependence (PD) and individual conditional expectation (ICE) plots.
Partial dependence plots, individual conditional expectation plots or an
overlay of both of them can be plotted by setting the ``kind``
parameter. The ``len(features)`` plots are arranged in a grid with
``n_cols`` columns. Two-way partial dependence plots are plotted as
contour plots. The deciles of the feature values will be shown with tick
marks on the x-axes for one-way plots, and on both axes for two-way
plots.
Read more in the :ref:`User Guide <partial_dependence>`.
.. note::
:func:`PartialDependenceDisplay.from_estimator` does not support using the
same axes with multiple calls. To plot the partial dependence for
multiple estimators, please pass the axes created by the first call to the
second call::
>>> from sklearn.inspection import PartialDependenceDisplay
>>> from sklearn.datasets import make_friedman1
>>> from sklearn.linear_model import LinearRegression
>>> from sklearn.ensemble import RandomForestRegressor
>>> X, y = make_friedman1()
>>> est1 = LinearRegression().fit(X, y)
>>> est2 = RandomForestRegressor().fit(X, y)
>>> disp1 = PartialDependenceDisplay.from_estimator(est1, X,
... [1, 2])
>>> disp2 = PartialDependenceDisplay.from_estimator(est2, X, [1, 2],
... ax=disp1.axes_)
.. warning::
For :class:`~sklearn.ensemble.GradientBoostingClassifier` and
:class:`~sklearn.ensemble.GradientBoostingRegressor`, the
`'recursion'` method (used by default) will not account for the `init`
predictor of the boosting process. In practice, this will produce
the same values as `'brute'` up to a constant offset in the target
response, provided that `init` is a constant estimator (which is the
default). However, if `init` is not a constant estimator, the
partial dependence values are incorrect for `'recursion'` because the
offset will be sample-dependent. It is preferable to use the `'brute'`
method. Note that this only applies to
:class:`~sklearn.ensemble.GradientBoostingClassifier` and
:class:`~sklearn.ensemble.GradientBoostingRegressor`, not to
:class:`~sklearn.ensemble.HistGradientBoostingClassifier` and
:class:`~sklearn.ensemble.HistGradientBoostingRegressor`.
.. versionadded:: 1.0
Parameters
----------
estimator : BaseEstimator
A fitted estimator object implementing :term:`predict`,
:term:`predict_proba`, or :term:`decision_function`.
Multioutput-multiclass classifiers are not supported.
X : {array-like, dataframe} of shape (n_samples, n_features)
``X`` is used to generate a grid of values for the target
``features`` (where the partial dependence will be evaluated), and
also to generate values for the complement features when the
`method` is `'brute'`.
features : list of {int, str, pair of int, pair of str}
The target features for which to create the PDPs.
If `features[i]` is an integer or a string, a one-way PDP is created;
if `features[i]` is a tuple, a two-way PDP is created (only supported
with `kind='average'`). Each tuple must be of size 2.
If any entry is a string, then it must be in ``feature_names``.
sample_weight : array-like of shape (n_samples,), default=None
Sample weights are used to calculate weighted means when averaging the
model output. If `None`, then samples are equally weighted. If
`sample_weight` is not `None`, then `method` will be set to `'brute'`.
Note that `sample_weight` is ignored for `kind='individual'`.
.. versionadded:: 1.3
categorical_features : array-like of shape (n_features,) or shape \
(n_categorical_features,), dtype={bool, int, str}, default=None
Indicates the categorical features.
- `None`: no feature will be considered categorical;
- boolean array-like: boolean mask of shape `(n_features,)`
indicating which features are categorical. Thus, this array has
the same shape has `X.shape[1]`;
- integer or string array-like: integer indices or strings
indicating categorical features.
.. versionadded:: 1.2
feature_names : array-like of shape (n_features,), dtype=str, default=None
Name of each feature; `feature_names[i]` holds the name of the feature
with index `i`.
By default, the name of the feature corresponds to their numerical
index for NumPy array and their column name for pandas dataframe.
target : int, default=None
- In a multiclass setting, specifies the class for which the PDPs
should be computed. Note that for binary classification, the
positive class (index 1) is always used.
- In a multioutput setting, specifies the task for which the PDPs
should be computed.
Ignored in binary classification or classical regression settings.
response_method : {'auto', 'predict_proba', 'decision_function'}, \
default='auto'
Specifies whether to use :term:`predict_proba` or
:term:`decision_function` as the target response. For regressors
this parameter is ignored and the response is always the output of
:term:`predict`. By default, :term:`predict_proba` is tried first
and we revert to :term:`decision_function` if it doesn't exist. If
``method`` is `'recursion'`, the response is always the output of
:term:`decision_function`.
n_cols : int, default=3
The maximum number of columns in the grid plot. Only active when `ax`
is a single axis or `None`.
grid_resolution : int, default=100
The number of equally spaced points on the axes of the plots, for each
target feature.
percentiles : tuple of float, default=(0.05, 0.95)
The lower and upper percentile used to create the extreme values
for the PDP axes. Must be in [0, 1].
method : str, default='auto'
The method used to calculate the averaged predictions:
- `'recursion'` is only supported for some tree-based estimators
(namely
:class:`~sklearn.ensemble.GradientBoostingClassifier`,
:class:`~sklearn.ensemble.GradientBoostingRegressor`,
:class:`~sklearn.ensemble.HistGradientBoostingClassifier`,
:class:`~sklearn.ensemble.HistGradientBoostingRegressor`,
:class:`~sklearn.tree.DecisionTreeRegressor`,
:class:`~sklearn.ensemble.RandomForestRegressor`
but is more efficient in terms of speed.
With this method, the target response of a
classifier is always the decision function, not the predicted
probabilities. Since the `'recursion'` method implicitly computes
the average of the ICEs by design, it is not compatible with ICE and
thus `kind` must be `'average'`.
- `'brute'` is supported for any estimator, but is more
computationally intensive.
- `'auto'`: the `'recursion'` is used for estimators that support it,
and `'brute'` is used otherwise. If `sample_weight` is not `None`,
then `'brute'` is used regardless of the estimator.
Please see :ref:`this note <pdp_method_differences>` for
differences between the `'brute'` and `'recursion'` method.
n_jobs : int, default=None
The number of CPUs to use to compute the partial dependences.
Computation is parallelized over features specified by the `features`
parameter.
``None`` means 1 unless in a :obj:`joblib.parallel_backend` context.
``-1`` means using all processors. See :term:`Glossary <n_jobs>`
for more details.
verbose : int, default=0
Verbose output during PD computations.
line_kw : dict, default=None
Dict with keywords passed to the ``matplotlib.pyplot.plot`` call.
For one-way partial dependence plots. It can be used to define common
properties for both `ice_lines_kw` and `pdp_line_kw`.
ice_lines_kw : dict, default=None
Dictionary with keywords passed to the `matplotlib.pyplot.plot` call.
For ICE lines in the one-way partial dependence plots.
The key value pairs defined in `ice_lines_kw` takes priority over
`line_kw`.
pd_line_kw : dict, default=None
Dictionary with keywords passed to the `matplotlib.pyplot.plot` call.
For partial dependence in one-way partial dependence plots.
The key value pairs defined in `pd_line_kw` takes priority over
`line_kw`.
contour_kw : dict, default=None
Dict with keywords passed to the ``matplotlib.pyplot.contourf`` call.
For two-way partial dependence plots.
ax : Matplotlib axes or array-like of Matplotlib axes, default=None
- If a single axis is passed in, it is treated as a bounding axes
and a grid of partial dependence plots will be drawn within
these bounds. The `n_cols` parameter controls the number of
columns in the grid.
- If an array-like of axes are passed in, the partial dependence
plots will be drawn directly into these axes.
- If `None`, a figure and a bounding axes is created and treated
as the single axes case.
kind : {'average', 'individual', 'both'}, default='average'
Whether to plot the partial dependence averaged across all the samples
in the dataset or one line per sample or both.
- ``kind='average'`` results in the traditional PD plot;
- ``kind='individual'`` results in the ICE plot.
Note that the fast `method='recursion'` option is only available for
`kind='average'` and `sample_weights=None`. Computing individual
dependencies and doing weighted averages requires using the slower
`method='brute'`.
centered : bool, default=False
If `True`, the ICE and PD lines will start at the origin of the
y-axis. By default, no centering is done.
.. versionadded:: 1.1
subsample : float, int or None, default=1000
Sampling for ICE curves when `kind` is 'individual' or 'both'.
If `float`, should be between 0.0 and 1.0 and represent the proportion
of the dataset to be used to plot ICE curves. If `int`, represents the
absolute number samples to use.
Note that the full dataset is still used to calculate averaged partial
dependence when `kind='both'`.
random_state : int, RandomState instance or None, default=None
Controls the randomness of the selected samples when subsamples is not
`None` and `kind` is either `'both'` or `'individual'`.
See :term:`Glossary <random_state>` for details.
Returns
-------
display : :class:`~sklearn.inspection.PartialDependenceDisplay`
See Also
--------
partial_dependence : Compute Partial Dependence values.
Examples
--------
>>> import matplotlib.pyplot as plt
>>> from sklearn.datasets import make_friedman1
>>> from sklearn.ensemble import GradientBoostingRegressor
>>> from sklearn.inspection import PartialDependenceDisplay
>>> X, y = make_friedman1()
>>> clf = GradientBoostingRegressor(n_estimators=10).fit(X, y)
>>> PartialDependenceDisplay.from_estimator(clf, X, [0, (0, 1)])
<...>
>>> plt.show()
"""
check_matplotlib_support(f"{cls.__name__}.from_estimator") # noqa
import matplotlib.pyplot as plt # noqa
# set target_idx for multi-class estimators
if hasattr(estimator, "classes_") and np.size(estimator.classes_) > 2:
if target is None:
raise ValueError("target must be specified for multi-class")
target_idx = np.searchsorted(estimator.classes_, target)
if (
not (0 <= target_idx < len(estimator.classes_))
or estimator.classes_[target_idx] != target
):
raise ValueError("target not in est.classes_, got {}".format(target))
else:
# regression and binary classification
target_idx = 0
# Use check_array only on lists and other non-array-likes / sparse. Do not
# convert DataFrame into a NumPy array.
if not (hasattr(X, "__array__") or sparse.issparse(X)):
X = check_array(X, force_all_finite="allow-nan", dtype=object)
n_features = X.shape[1]
feature_names = _check_feature_names(X, feature_names)
# expand kind to always be a list of str
kind_ = [kind] * len(features) if isinstance(kind, str) else kind
if len(kind_) != len(features):
raise ValueError(
"When `kind` is provided as a list of strings, it should contain "
f"as many elements as `features`. `kind` contains {len(kind_)} "
f"element(s) and `features` contains {len(features)} element(s)."
)
# convert features into a seq of int tuples
tmp_features, ice_for_two_way_pd = [], []
for kind_plot, fxs in zip(kind_, features):
if isinstance(fxs, (numbers.Integral, str)):
fxs = (fxs,)
try:
fxs = tuple(
_get_feature_index(fx, feature_names=feature_names) for fx in fxs
)
except TypeError as e:
raise ValueError(
"Each entry in features must be either an int, "
"a string, or an iterable of size at most 2."
) from e
if not 1 <= np.size(fxs) <= 2:
raise ValueError(
"Each entry in features must be either an int, "
"a string, or an iterable of size at most 2."
)
# store the information if 2-way PD was requested with ICE to later
# raise a ValueError with an exhaustive list of problematic
# settings.
ice_for_two_way_pd.append(kind_plot != "average" and np.size(fxs) > 1)
tmp_features.append(fxs)
if any(ice_for_two_way_pd):
# raise an error and be specific regarding the parameter values
# when 1- and 2-way PD were requested
kind_ = [
"average" if forcing_average else kind_plot
for forcing_average, kind_plot in zip(ice_for_two_way_pd, kind_)
]
raise ValueError(
"ICE plot cannot be rendered for 2-way feature interactions. "
"2-way feature interactions mandates PD plots using the "
"'average' kind: "
f"features={features!r} should be configured to use "
f"kind={kind_!r} explicitly."
)
features = tmp_features
if categorical_features is None:
is_categorical = [
(False,) if len(fxs) == 1 else (False, False) for fxs in features
]
else:
# we need to create a boolean indicator of which features are
# categorical from the categorical_features list.
categorical_features = np.asarray(categorical_features)
if categorical_features.dtype.kind == "b":
# categorical features provided as a list of boolean
if categorical_features.size != n_features:
raise ValueError(
"When `categorical_features` is a boolean array-like, "
"the array should be of shape (n_features,). Got "
f"{categorical_features.size} elements while `X` contains "
f"{n_features} features."
)
is_categorical = [
tuple(categorical_features[fx] for fx in fxs) for fxs in features
]
elif categorical_features.dtype.kind in ("i", "O", "U"):
# categorical features provided as a list of indices or feature names
categorical_features_idx = [
_get_feature_index(cat, feature_names=feature_names)
for cat in categorical_features
]
is_categorical = [
tuple([idx in categorical_features_idx for idx in fxs])
for fxs in features
]
else:
raise ValueError(
"Expected `categorical_features` to be an array-like of boolean,"
f" integer, or string. Got {categorical_features.dtype} instead."
)
for cats in is_categorical:
if np.size(cats) == 2 and (cats[0] != cats[1]):
raise ValueError(
"Two-way partial dependence plots are not supported for pairs"
" of continuous and categorical features."
)
# collect the indices of the categorical features targeted by the partial
# dependence computation
categorical_features_targeted = set(
[
fx
for fxs, cats in zip(features, is_categorical)
for fx in fxs
if any(cats)
]
)
if categorical_features_targeted:
min_n_cats = min(
[
len(_unique(_safe_indexing(X, idx, axis=1)))
for idx in categorical_features_targeted
]
)
if grid_resolution < min_n_cats:
raise ValueError(
"The resolution of the computed grid is less than the "
"minimum number of categories in the targeted categorical "
"features. Expect the `grid_resolution` to be greater than "
f"{min_n_cats}. Got {grid_resolution} instead."
)
for is_cat, kind_plot in zip(is_categorical, kind_):
if any(is_cat) and kind_plot != "average":
raise ValueError(
"It is not possible to display individual effects for"
" categorical features."
)
# Early exit if the axes does not have the correct number of axes
if ax is not None and not isinstance(ax, plt.Axes):
axes = np.asarray(ax, dtype=object)
if axes.size != len(features):
raise ValueError(
"Expected ax to have {} axes, got {}".format(
len(features), axes.size
)
)
for i in chain.from_iterable(features):
if i >= len(feature_names):
raise ValueError(
"All entries of features must be less than "
"len(feature_names) = {0}, got {1}.".format(len(feature_names), i)
)
if isinstance(subsample, numbers.Integral):
if subsample <= 0:
raise ValueError(
f"When an integer, subsample={subsample} should be positive."
)
elif isinstance(subsample, numbers.Real):
if subsample <= 0 or subsample >= 1:
raise ValueError(
f"When a floating-point, subsample={subsample} should be in "
"the (0, 1) range."
)
# compute predictions and/or averaged predictions
pd_results = Parallel(n_jobs=n_jobs, verbose=verbose)(
delayed(partial_dependence)(
estimator,
X,
fxs,
sample_weight=sample_weight,
feature_names=feature_names,
categorical_features=categorical_features,
response_method=response_method,
method=method,
grid_resolution=grid_resolution,
percentiles=percentiles,
kind=kind_plot,
)
for kind_plot, fxs in zip(kind_, features)
)
# For multioutput regression, we can only check the validity of target
# now that we have the predictions.
# Also note: as multiclass-multioutput classifiers are not supported,
# multiclass and multioutput scenario are mutually exclusive. So there is
# no risk of overwriting target_idx here.
pd_result = pd_results[0] # checking the first result is enough
n_tasks = (
pd_result.average.shape[0]
if kind_[0] == "average"
else pd_result.individual.shape[0]
)
if is_regressor(estimator) and n_tasks > 1:
if target is None:
raise ValueError("target must be specified for multi-output regressors")
if not 0 <= target <= n_tasks:
raise ValueError(
"target must be in [0, n_tasks], got {}.".format(target)
)
target_idx = target
deciles = {}
for fxs, cats in zip(features, is_categorical):
for fx, cat in zip(fxs, cats):
if not cat and fx not in deciles:
X_col = _safe_indexing(X, fx, axis=1)
deciles[fx] = mquantiles(X_col, prob=np.arange(0.1, 1.0, 0.1))
display = cls(
pd_results=pd_results,
features=features,
feature_names=feature_names,
target_idx=target_idx,
deciles=deciles,
kind=kind,
subsample=subsample,
random_state=random_state,
is_categorical=is_categorical,
)
return display.plot(
ax=ax,
n_cols=n_cols,
line_kw=line_kw,
ice_lines_kw=ice_lines_kw,
pd_line_kw=pd_line_kw,
contour_kw=contour_kw,
centered=centered,
)
def _get_sample_count(self, n_samples):
"""Compute the number of samples as an integer."""
if isinstance(self.subsample, numbers.Integral):
if self.subsample < n_samples:
return self.subsample
return n_samples
elif isinstance(self.subsample, numbers.Real):
return ceil(n_samples * self.subsample)
return n_samples
def _plot_ice_lines(
self,
preds,
feature_values,
n_ice_to_plot,
ax,
pd_plot_idx,
n_total_lines_by_plot,
individual_line_kw,
):
"""Plot the ICE lines.
Parameters
----------
preds : ndarray of shape \
(n_instances, n_grid_points)
The predictions computed for all points of `feature_values` for a
given feature for all samples in `X`.
feature_values : ndarray of shape (n_grid_points,)
The feature values for which the predictions have been computed.
n_ice_to_plot : int
The number of ICE lines to plot.
ax : Matplotlib axes
The axis on which to plot the ICE lines.
pd_plot_idx : int
The sequential index of the plot. It will be unraveled to find the
matching 2D position in the grid layout.
n_total_lines_by_plot : int
The total number of lines expected to be plot on the axis.
individual_line_kw : dict
Dict with keywords passed when plotting the ICE lines.
"""
rng = check_random_state(self.random_state)
# subsample ice
ice_lines_idx = rng.choice(
preds.shape[0],
n_ice_to_plot,
replace=False,
)
ice_lines_subsampled = preds[ice_lines_idx, :]
# plot the subsampled ice
for ice_idx, ice in enumerate(ice_lines_subsampled):
line_idx = np.unravel_index(
pd_plot_idx * n_total_lines_by_plot + ice_idx, self.lines_.shape
)
self.lines_[line_idx] = ax.plot(
feature_values, ice.ravel(), **individual_line_kw
)[0]
def _plot_average_dependence(
self,
avg_preds,
feature_values,
ax,
pd_line_idx,
line_kw,
categorical,
bar_kw,
):
"""Plot the average partial dependence.
Parameters
----------
avg_preds : ndarray of shape (n_grid_points,)
The average predictions for all points of `feature_values` for a
given feature for all samples in `X`.
feature_values : ndarray of shape (n_grid_points,)
The feature values for which the predictions have been computed.
ax : Matplotlib axes
The axis on which to plot the average PD.
pd_line_idx : int
The sequential index of the plot. It will be unraveled to find the
matching 2D position in the grid layout.
line_kw : dict
Dict with keywords passed when plotting the PD plot.
categorical : bool
Whether feature is categorical.
bar_kw: dict
Dict with keywords passed when plotting the PD bars (categorical).
"""
if categorical:
bar_idx = np.unravel_index(pd_line_idx, self.bars_.shape)
self.bars_[bar_idx] = ax.bar(feature_values, avg_preds, **bar_kw)[0]
ax.tick_params(axis="x", rotation=90)
else:
line_idx = np.unravel_index(pd_line_idx, self.lines_.shape)
self.lines_[line_idx] = ax.plot(
feature_values,
avg_preds,
**line_kw,
)[0]
def _plot_one_way_partial_dependence(
self,
kind,
preds,
avg_preds,
feature_values,
feature_idx,
n_ice_lines,
ax,
n_cols,
pd_plot_idx,
n_lines,
ice_lines_kw,
pd_line_kw,
categorical,
bar_kw,
pdp_lim,
):
"""Plot 1-way partial dependence: ICE and PDP.
Parameters
----------
kind : str
The kind of partial plot to draw.
preds : ndarray of shape \
(n_instances, n_grid_points) or None
The predictions computed for all points of `feature_values` for a
given feature for all samples in `X`.
avg_preds : ndarray of shape (n_grid_points,)
The average predictions for all points of `feature_values` for a
given feature for all samples in `X`.
feature_values : ndarray of shape (n_grid_points,)
The feature values for which the predictions have been computed.
feature_idx : int
The index corresponding to the target feature.
n_ice_lines : int
The number of ICE lines to plot.
ax : Matplotlib axes
The axis on which to plot the ICE and PDP lines.
n_cols : int or None
The number of column in the axis.
pd_plot_idx : int
The sequential index of the plot. It will be unraveled to find the
matching 2D position in the grid layout.
n_lines : int
The total number of lines expected to be plot on the axis.
ice_lines_kw : dict
Dict with keywords passed when plotting the ICE lines.
pd_line_kw : dict
Dict with keywords passed when plotting the PD plot.
categorical : bool
Whether feature is categorical.
bar_kw: dict
Dict with keywords passed when plotting the PD bars (categorical).
pdp_lim : dict
Global min and max average predictions, such that all plots will
have the same scale and y limits. `pdp_lim[1]` is the global min
and max for single partial dependence curves.
"""
from matplotlib import transforms # noqa
if kind in ("individual", "both"):
self._plot_ice_lines(
preds[self.target_idx],
feature_values,
n_ice_lines,
ax,
pd_plot_idx,
n_lines,
ice_lines_kw,
)
if kind in ("average", "both"):
# the average is stored as the last line
if kind == "average":
pd_line_idx = pd_plot_idx
else:
pd_line_idx = pd_plot_idx * n_lines + n_ice_lines
self._plot_average_dependence(
avg_preds[self.target_idx].ravel(),
feature_values,
ax,
pd_line_idx,
pd_line_kw,
categorical,
bar_kw,
)
trans = transforms.blended_transform_factory(ax.transData, ax.transAxes)
# create the decile line for the vertical axis
vlines_idx = np.unravel_index(pd_plot_idx, self.deciles_vlines_.shape)
if self.deciles.get(feature_idx[0], None) is not None:
self.deciles_vlines_[vlines_idx] = ax.vlines(
self.deciles[feature_idx[0]],
0,
0.05,
transform=trans,
color="k",
)
# reset ylim which was overwritten by vlines
min_val = min(val[0] for val in pdp_lim.values())
max_val = max(val[1] for val in pdp_lim.values())
ax.set_ylim([min_val, max_val])
# Set xlabel if it is not already set
if not ax.get_xlabel():
ax.set_xlabel(self.feature_names[feature_idx[0]])
if n_cols is None or pd_plot_idx % n_cols == 0:
if not ax.get_ylabel():
ax.set_ylabel("Partial dependence")
else:
ax.set_yticklabels([])
if pd_line_kw.get("label", None) and kind != "individual" and not categorical:
ax.legend()
def _plot_two_way_partial_dependence(
self,
avg_preds,
feature_values,
feature_idx,
ax,
pd_plot_idx,
Z_level,
contour_kw,
categorical,
heatmap_kw,
):
"""Plot 2-way partial dependence.
Parameters
----------
avg_preds : ndarray of shape \
(n_instances, n_grid_points, n_grid_points)
The average predictions for all points of `feature_values[0]` and
`feature_values[1]` for some given features for all samples in `X`.
feature_values : seq of 1d array
A sequence of array of the feature values for which the predictions
have been computed.
feature_idx : tuple of int
The indices of the target features
ax : Matplotlib axes
The axis on which to plot the ICE and PDP lines.
pd_plot_idx : int
The sequential index of the plot. It will be unraveled to find the
matching 2D position in the grid layout.
Z_level : ndarray of shape (8, 8)
The Z-level used to encode the average predictions.
contour_kw : dict
Dict with keywords passed when plotting the contours.
categorical : bool
Whether features are categorical.
heatmap_kw: dict
Dict with keywords passed when plotting the PD heatmap
(categorical).
"""
if categorical:
import matplotlib.pyplot as plt
default_im_kw = dict(interpolation="nearest", cmap="viridis")
im_kw = {**default_im_kw, **heatmap_kw}
data = avg_preds[self.target_idx]
im = ax.imshow(data, **im_kw)
text = None
cmap_min, cmap_max = im.cmap(0), im.cmap(1.0)
text = np.empty_like(data, dtype=object)
# print text with appropriate color depending on background
thresh = (data.max() + data.min()) / 2.0
for flat_index in range(data.size):
row, col = np.unravel_index(flat_index, data.shape)
color = cmap_max if data[row, col] < thresh else cmap_min
values_format = ".2f"
text_data = format(data[row, col], values_format)
text_kwargs = dict(ha="center", va="center", color=color)
text[row, col] = ax.text(col, row, text_data, **text_kwargs)
fig = ax.figure
fig.colorbar(im, ax=ax)
ax.set(
xticks=np.arange(len(feature_values[1])),
yticks=np.arange(len(feature_values[0])),
xticklabels=feature_values[1],
yticklabels=feature_values[0],
xlabel=self.feature_names[feature_idx[1]],
ylabel=self.feature_names[feature_idx[0]],
)
plt.setp(ax.get_xticklabels(), rotation="vertical")
heatmap_idx = np.unravel_index(pd_plot_idx, self.heatmaps_.shape)
self.heatmaps_[heatmap_idx] = im
else:
from matplotlib import transforms # noqa
XX, YY = np.meshgrid(feature_values[0], feature_values[1])
Z = avg_preds[self.target_idx].T
CS = ax.contour(XX, YY, Z, levels=Z_level, linewidths=0.5, colors="k")
contour_idx = np.unravel_index(pd_plot_idx, self.contours_.shape)
self.contours_[contour_idx] = ax.contourf(
XX,
YY,
Z,
levels=Z_level,
vmax=Z_level[-1],
vmin=Z_level[0],
**contour_kw,
)
ax.clabel(CS, fmt="%2.2f", colors="k", fontsize=10, inline=True)
trans = transforms.blended_transform_factory(ax.transData, ax.transAxes)
# create the decile line for the vertical axis
xlim, ylim = ax.get_xlim(), ax.get_ylim()
vlines_idx = np.unravel_index(pd_plot_idx, self.deciles_vlines_.shape)
self.deciles_vlines_[vlines_idx] = ax.vlines(
self.deciles[feature_idx[0]],
0,
0.05,
transform=trans,
color="k",
)
# create the decile line for the horizontal axis
hlines_idx = np.unravel_index(pd_plot_idx, self.deciles_hlines_.shape)
self.deciles_hlines_[hlines_idx] = ax.hlines(
self.deciles[feature_idx[1]],
0,
0.05,
transform=trans,
color="k",
)
# reset xlim and ylim since they are overwritten by hlines and
# vlines
ax.set_xlim(xlim)
ax.set_ylim(ylim)
# set xlabel if it is not already set
if not ax.get_xlabel():
ax.set_xlabel(self.feature_names[feature_idx[0]])
ax.set_ylabel(self.feature_names[feature_idx[1]])
def plot(
self,
*,
ax=None,
n_cols=3,
line_kw=None,
ice_lines_kw=None,
pd_line_kw=None,
contour_kw=None,
bar_kw=None,
heatmap_kw=None,
pdp_lim=None,
centered=False,
):
"""Plot partial dependence plots.
Parameters
----------
ax : Matplotlib axes or array-like of Matplotlib axes, default=None
- If a single axis is passed in, it is treated as a bounding axes
and a grid of partial dependence plots will be drawn within
these bounds. The `n_cols` parameter controls the number of
columns in the grid.
- If an array-like of axes are passed in, the partial dependence
plots will be drawn directly into these axes.
- If `None`, a figure and a bounding axes is created and treated
as the single axes case.
n_cols : int, default=3
The maximum number of columns in the grid plot. Only active when
`ax` is a single axes or `None`.
line_kw : dict, default=None
Dict with keywords passed to the `matplotlib.pyplot.plot` call.
For one-way partial dependence plots.
ice_lines_kw : dict, default=None
Dictionary with keywords passed to the `matplotlib.pyplot.plot` call.
For ICE lines in the one-way partial dependence plots.
The key value pairs defined in `ice_lines_kw` takes priority over
`line_kw`.
.. versionadded:: 1.0
pd_line_kw : dict, default=None
Dictionary with keywords passed to the `matplotlib.pyplot.plot` call.
For partial dependence in one-way partial dependence plots.
The key value pairs defined in `pd_line_kw` takes priority over
`line_kw`.
.. versionadded:: 1.0
contour_kw : dict, default=None
Dict with keywords passed to the `matplotlib.pyplot.contourf`
call for two-way partial dependence plots.
bar_kw : dict, default=None
Dict with keywords passed to the `matplotlib.pyplot.bar`
call for one-way categorical partial dependence plots.
.. versionadded:: 1.2
heatmap_kw : dict, default=None
Dict with keywords passed to the `matplotlib.pyplot.imshow`
call for two-way categorical partial dependence plots.
.. versionadded:: 1.2
pdp_lim : dict, default=None
Global min and max average predictions, such that all plots will have the
same scale and y limits. `pdp_lim[1]` is the global min and max for single
partial dependence curves. `pdp_lim[2]` is the global min and max for
two-way partial dependence curves. If `None` (default), the limit will be
inferred from the global minimum and maximum of all predictions.
.. versionadded:: 1.1
centered : bool, default=False
If `True`, the ICE and PD lines will start at the origin of the
y-axis. By default, no centering is done.
.. versionadded:: 1.1
Returns
-------
display : :class:`~sklearn.inspection.PartialDependenceDisplay`
Returns a :class:`~sklearn.inspection.PartialDependenceDisplay`
object that contains the partial dependence plots.
"""
check_matplotlib_support("plot_partial_dependence")
import matplotlib.pyplot as plt # noqa
from matplotlib.gridspec import GridSpecFromSubplotSpec # noqa
if isinstance(self.kind, str):
kind = [self.kind] * len(self.features)
else:
kind = self.kind
if self.is_categorical is None:
is_categorical = [
(False,) if len(fx) == 1 else (False, False) for fx in self.features
]
else:
is_categorical = self.is_categorical
if len(kind) != len(self.features):
raise ValueError(
"When `kind` is provided as a list of strings, it should "
"contain as many elements as `features`. `kind` contains "
f"{len(kind)} element(s) and `features` contains "
f"{len(self.features)} element(s)."
)
valid_kinds = {"average", "individual", "both"}
if any([k not in valid_kinds for k in kind]):
raise ValueError(
f"Values provided to `kind` must be one of: {valid_kinds!r} or a list"
f" of such values. Currently, kind={self.kind!r}"
)
# Center results before plotting
if not centered:
pd_results_ = self.pd_results
else:
pd_results_ = []
for kind_plot, pd_result in zip(kind, self.pd_results):
current_results = {"grid_values": pd_result["grid_values"]}
if kind_plot in ("individual", "both"):
preds = pd_result.individual
preds = preds - preds[self.target_idx, :, 0, None]
current_results["individual"] = preds
if kind_plot in ("average", "both"):
avg_preds = pd_result.average
avg_preds = avg_preds - avg_preds[self.target_idx, 0, None]
current_results["average"] = avg_preds
pd_results_.append(Bunch(**current_results))
if pdp_lim is None:
# get global min and max average predictions of PD grouped by plot type
pdp_lim = {}
for kind_plot, pdp in zip(kind, pd_results_):
values = pdp["grid_values"]
preds = pdp.average if kind_plot == "average" else pdp.individual
min_pd = preds[self.target_idx].min()
max_pd = preds[self.target_idx].max()
# expand the limits to account so that the plotted lines do not touch
# the edges of the plot
span = max_pd - min_pd
min_pd -= 0.05 * span
max_pd += 0.05 * span
n_fx = len(values)
old_min_pd, old_max_pd = pdp_lim.get(n_fx, (min_pd, max_pd))
min_pd = min(min_pd, old_min_pd)
max_pd = max(max_pd, old_max_pd)
pdp_lim[n_fx] = (min_pd, max_pd)
if line_kw is None:
line_kw = {}
if ice_lines_kw is None:
ice_lines_kw = {}
if pd_line_kw is None:
pd_line_kw = {}
if bar_kw is None:
bar_kw = {}
if heatmap_kw is None:
heatmap_kw = {}
if ax is None:
_, ax = plt.subplots()
if contour_kw is None:
contour_kw = {}
default_contour_kws = {"alpha": 0.75}
contour_kw = {**default_contour_kws, **contour_kw}
n_features = len(self.features)
is_average_plot = [kind_plot == "average" for kind_plot in kind]
if all(is_average_plot):
# only average plots are requested
n_ice_lines = 0
n_lines = 1
else:
# we need to determine the number of ICE samples computed
ice_plot_idx = is_average_plot.index(False)
n_ice_lines = self._get_sample_count(
len(pd_results_[ice_plot_idx].individual[0])
)
if any([kind_plot == "both" for kind_plot in kind]):
n_lines = n_ice_lines + 1 # account for the average line
else:
n_lines = n_ice_lines
if isinstance(ax, plt.Axes):
# If ax was set off, it has most likely been set to off
# by a previous call to plot.
if not ax.axison:
raise ValueError(
"The ax was already used in another plot "
"function, please set ax=display.axes_ "
"instead"
)
ax.set_axis_off()
self.bounding_ax_ = ax
self.figure_ = ax.figure
n_cols = min(n_cols, n_features)
n_rows = int(np.ceil(n_features / float(n_cols)))
self.axes_ = np.empty((n_rows, n_cols), dtype=object)
if all(is_average_plot):
self.lines_ = np.empty((n_rows, n_cols), dtype=object)
else:
self.lines_ = np.empty((n_rows, n_cols, n_lines), dtype=object)
self.contours_ = np.empty((n_rows, n_cols), dtype=object)
self.bars_ = np.empty((n_rows, n_cols), dtype=object)
self.heatmaps_ = np.empty((n_rows, n_cols), dtype=object)
axes_ravel = self.axes_.ravel()
gs = GridSpecFromSubplotSpec(
n_rows, n_cols, subplot_spec=ax.get_subplotspec()
)
for i, spec in zip(range(n_features), gs):
axes_ravel[i] = self.figure_.add_subplot(spec)
else: # array-like
ax = np.asarray(ax, dtype=object)
if ax.size != n_features:
raise ValueError(
"Expected ax to have {} axes, got {}".format(n_features, ax.size)
)
if ax.ndim == 2:
n_cols = ax.shape[1]
else:
n_cols = None
self.bounding_ax_ = None
self.figure_ = ax.ravel()[0].figure
self.axes_ = ax
if all(is_average_plot):
self.lines_ = np.empty_like(ax, dtype=object)
else:
self.lines_ = np.empty(ax.shape + (n_lines,), dtype=object)
self.contours_ = np.empty_like(ax, dtype=object)
self.bars_ = np.empty_like(ax, dtype=object)
self.heatmaps_ = np.empty_like(ax, dtype=object)
# create contour levels for two-way plots
if 2 in pdp_lim:
Z_level = np.linspace(*pdp_lim[2], num=8)
self.deciles_vlines_ = np.empty_like(self.axes_, dtype=object)
self.deciles_hlines_ = np.empty_like(self.axes_, dtype=object)
for pd_plot_idx, (axi, feature_idx, cat, pd_result, kind_plot) in enumerate(
zip(
self.axes_.ravel(),
self.features,
is_categorical,
pd_results_,
kind,
)
):
avg_preds = None
preds = None
feature_values = pd_result["grid_values"]
if kind_plot == "individual":
preds = pd_result.individual
elif kind_plot == "average":
avg_preds = pd_result.average
else: # kind_plot == 'both'
avg_preds = pd_result.average
preds = pd_result.individual
if len(feature_values) == 1:
# define the line-style for the current plot
default_line_kws = {
"color": "C0",
"label": "average" if kind_plot == "both" else None,
}
if kind_plot == "individual":
default_ice_lines_kws = {"alpha": 0.3, "linewidth": 0.5}
default_pd_lines_kws = {}
elif kind_plot == "both":
# by default, we need to distinguish the average line from
# the individual lines via color and line style
default_ice_lines_kws = {
"alpha": 0.3,
"linewidth": 0.5,
"color": "tab:blue",
}
default_pd_lines_kws = {
"color": "tab:orange",
"linestyle": "--",
}
else:
default_ice_lines_kws = {}
default_pd_lines_kws = {}
ice_lines_kw = {
**default_line_kws,
**default_ice_lines_kws,
**line_kw,
**ice_lines_kw,
}
del ice_lines_kw["label"]
pd_line_kw = {
**default_line_kws,
**default_pd_lines_kws,
**line_kw,
**pd_line_kw,
}
default_bar_kws = {"color": "C0"}
bar_kw = {**default_bar_kws, **bar_kw}
default_heatmap_kw = {}
heatmap_kw = {**default_heatmap_kw, **heatmap_kw}
self._plot_one_way_partial_dependence(
kind_plot,
preds,
avg_preds,
feature_values[0],
feature_idx,
n_ice_lines,
axi,
n_cols,
pd_plot_idx,
n_lines,
ice_lines_kw,
pd_line_kw,
cat[0],
bar_kw,
pdp_lim,
)
else:
self._plot_two_way_partial_dependence(
avg_preds,
feature_values,
feature_idx,
axi,
pd_plot_idx,
Z_level,
contour_kw,
cat[0] and cat[1],
heatmap_kw,
)
return self
|