(arg, size_per_plot=(5, 5), plots_per_row=None)
| 35 | |
| 36 | |
| 37 | def 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) |
nothing calls this directly
no test coverage detected