Calculate accuracy. Parameters ---------- X : catboost.Pool or list or numpy.ndarray or pandas.DataFrame or pandas.Series or polars.DataFrame Data to apply model on. y : list or numpy.ndarray or polars.Series True labels. Ret
(self, X, y=None)
| 5835 | return self._staged_predict(data, 'LogProbability', ntree_start, ntree_end, eval_period, thread_count, verbose, 'staged_predict_log_proba') |
| 5836 | |
| 5837 | def score(self, X, y=None): |
| 5838 | """ |
| 5839 | Calculate accuracy. |
| 5840 | |
| 5841 | Parameters |
| 5842 | ---------- |
| 5843 | X : catboost.Pool or list or numpy.ndarray or pandas.DataFrame or pandas.Series or polars.DataFrame |
| 5844 | Data to apply model on. |
| 5845 | y : list or numpy.ndarray or polars.Series |
| 5846 | True labels. |
| 5847 | |
| 5848 | Returns |
| 5849 | ------- |
| 5850 | accuracy : float |
| 5851 | """ |
| 5852 | if isinstance(X, Pool): |
| 5853 | if y is not None: |
| 5854 | raise CatBoostError("Wrong initializing y: X is catboost.Pool object, y must be initialized inside catboost.Pool.") |
| 5855 | y = X.get_label() |
| 5856 | if y is None: |
| 5857 | raise CatBoostError("Label in X has not initialized.") |
| 5858 | if isinstance(y, pd.DataFrame): |
| 5859 | if len(y.columns) != 1: |
| 5860 | raise CatBoostError("y is pandas.DataFrame and has {} columns, but must have exactly one.".format(len(y.columns))) |
| 5861 | y = y[y.columns[0]] |
| 5862 | elif isinstance(y, pl.DataFrame): |
| 5863 | if y.width != 1: |
| 5864 | raise CatBoostError("y is polars.DataFrame and has {} columns, but must have exactly one.".format(y.width)) |
| 5865 | y = y.to_series(0) |
| 5866 | elif y is None: |
| 5867 | raise CatBoostError("y should be specified.") |
| 5868 | y = np.array(y) |
| 5869 | predicted_classes = self._predict( |
| 5870 | X, |
| 5871 | prediction_type='Class', |
| 5872 | ntree_start=0, |
| 5873 | ntree_end=0, |
| 5874 | thread_count=-1, |
| 5875 | verbose=None, |
| 5876 | parent_method_name='score' |
| 5877 | ).reshape(-1) |
| 5878 | if np.issubdtype(predicted_classes.dtype, np.number): |
| 5879 | if np.issubdtype(y.dtype, np.character): |
| 5880 | raise CatBoostError('predicted classes have numeric type but specified y contains strings') |
| 5881 | elif predicted_classes.dtype == np.bool_: |
| 5882 | if np.issubdtype(y.dtype, np.character): |
| 5883 | raise CatBoostError('predicted classes have boolean type but specified y contains strings') |
| 5884 | else: |
| 5885 | if np.issubdtype(y.dtype, np.number): |
| 5886 | raise CatBoostError('predicted classes have string type but specified y is numeric') |
| 5887 | elif np.issubdtype(y.dtype, np.bool_): |
| 5888 | raise CatBoostError('predicted classes have string type but specified y is boolean') |
| 5889 | return np.mean(np.array(predicted_classes) == np.array(y)) |
| 5890 | |
| 5891 | def set_probability_threshold(self, binclass_probability_threshold=None): |
| 5892 | """ |