MCPcopy Create free account
hub / github.com/catboost/catboost / plot_pdp

Function plot_pdp

catboost/python-package/catboost/monoforest.py:37–97  ·  view source on GitHub ↗
(arg, size_per_plot=(5, 5), plots_per_row=None)

Source from the content-addressed store, hash-verified

35
36
37def plot_pdp(arg, size_per_plot=(5, 5), plots_per_row=None):
38 with _import_matplotlib() as _plt:
39 plt = _plt
40 if isinstance(arg, CatBoost):
41 arg = explain_features(arg)
42 if isinstance(arg, _catboost.FeatureExplanation):
43 arg = [arg]
44 assert len(arg) > 0
45 assert isinstance(arg, list)
46 for element in arg:
47 assert isinstance(element, _catboost.FeatureExplanation)
48
49 figs = []
50 for feature_explanation in arg:
51 dimension = feature_explanation.dimension()
52 if not plots_per_row:
53 plots_per_row = min(5, dimension)
54 rows = int(math.ceil(dimension / plots_per_row))
55 fig, axes = plt.subplots(rows, plots_per_row)
56 fig.suptitle("Feature #{}".format(feature_explanation.feature))
57 if rows == 1:
58 axes = [axes]
59 if plots_per_row == 1:
60 axes = [[row_axes] for row_axes in axes]
61 fig.set_size_inches(size_per_plot[0] * plots_per_row, size_per_plot[1] * rows)
62
63 for dim in range(dimension):
64 ax = axes[dim // plots_per_row][dim % plots_per_row]
65 ax.set_title("Dimension={}".format(dim))
66 ax.set_xlabel("feature value")
67 ax.set_ylabel("model value")
68
69 borders, values = feature_explanation.calc_pdp(dim)
70 xs = []
71 ys = []
72 if feature_explanation.type == "Float":
73 if len(borders) == 0:
74 xs.append(-0.1)
75 xs.append(0.1)
76 ys.append(feature_explanation.expected_bias[dim])
77 ys.append(feature_explanation.expected_bias[dim])
78 ax.plot(xs, ys)
79 else:
80 offset = max(0.1, (borders[0] + borders[-1]) / 2)
81 xs.append(borders[0] - offset)
82 ys.append(feature_explanation.expected_bias[dim])
83 for border, value in zip(borders, values):
84 xs.append(border)
85 ys.append(ys[-1])
86 xs.append(border)
87 ys.append(value)
88 xs.append(borders[-1] + offset)
89 ys.append(ys[-1])
90 ax.plot(xs, ys)
91 else:
92 xs = ['bias'] + list(map(str, borders))
93 ys = feature_explanation.expected_bias[dim] + values
94 ax.bar(xs, ys)

Callers

nothing calls this directly

Calls 12

_import_matplotlibFunction · 0.85
isinstanceFunction · 0.85
explain_featuresFunction · 0.85
lenFunction · 0.85
mapClass · 0.85
minFunction · 0.50
intFunction · 0.50
rangeFunction · 0.50
maxFunction · 0.50
listClass · 0.50
formatMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected