To use this function, you should install plotly. data: numpy.ndarray or pandas.DataFrame or polars.DataFrame or catboost.Pool features_to_change: list-like with int (for indices) or str (for names) elements Numerical features indices or names in `data` for which
(self, data, features_to_change, plot=True, plot_file=None)
| 3845 | return self |
| 3846 | |
| 3847 | def plot_predictions(self, data, features_to_change, plot=True, plot_file=None): |
| 3848 | """ |
| 3849 | To use this function, you should install plotly. |
| 3850 | |
| 3851 | data: numpy.ndarray or pandas.DataFrame or polars.DataFrame or catboost.Pool |
| 3852 | features_to_change: list-like with int (for indices) or str (for names) elements |
| 3853 | Numerical features indices or names in `data` for which you want to vary prediction value. |
| 3854 | plot: bool |
| 3855 | Plot predictions. |
| 3856 | plot_file: str |
| 3857 | Output file for plot predictions. |
| 3858 | Returns |
| 3859 | ------- |
| 3860 | List of list of predictions for all buckets for all samples in data |
| 3861 | """ |
| 3862 | |
| 3863 | def predict(doc, feature_idx, borders, nan_treatment): |
| 3864 | left_extend_border = min(2 * borders[0], -1) |
| 3865 | right_extend_border = max(2 * borders[-1], 1) |
| 3866 | extended_borders = [left_extend_border] + borders + [right_extend_border] |
| 3867 | points = [] |
| 3868 | predictions = [] |
| 3869 | border_idx = None |
| 3870 | if np.isnan(doc[feature_idx]): |
| 3871 | border_idx = len(borders) if nan_treatment == 'AsTrue' else 0 |
| 3872 | for i in range(len(extended_borders) - 1): |
| 3873 | points += [(extended_borders[i] + extended_borders[i + 1]) / 2.] |
| 3874 | if border_idx is None and doc[feature_idx] < extended_borders[i + 1]: |
| 3875 | border_idx = i |
| 3876 | buf = doc[feature_idx] |
| 3877 | doc[feature_idx] = points[-1] |
| 3878 | predictions += [self.predict(doc)] |
| 3879 | doc[feature_idx] = buf |
| 3880 | if border_idx is None: |
| 3881 | border_idx = len(borders) |
| 3882 | return predictions, border_idx |
| 3883 | |
| 3884 | def get_layout(go, feature, xaxis): |
| 3885 | return go.Layout( |
| 3886 | title="Prediction variation for feature '{}'".format(feature), |
| 3887 | yaxis={ |
| 3888 | 'title': 'Prediction', |
| 3889 | 'side': 'left', |
| 3890 | 'overlaying': 'y2' |
| 3891 | }, |
| 3892 | xaxis=xaxis |
| 3893 | ) |
| 3894 | try: |
| 3895 | import plotly.graph_objs as go |
| 3896 | except ImportError as e: |
| 3897 | warnings.warn("To draw plots you should install plotly.") |
| 3898 | raise ImportError(str(e)) |
| 3899 | |
| 3900 | model_borders = self._get_borders() |
| 3901 | |
| 3902 | data, _ = self._process_predict_input_data(data, "vary_feature_value_and_apply", thread_count=-1) |
| 3903 | figs = [] |
| 3904 | all_predictions = [{}] * data.num_row() |