code: # -*- coding: utf-8 -*-
  """DrCIF test code."""
  import numpy as np
  from sklearn.metrics import accuracy_score
  
  from sktime.classification.interval_based import DrCIF
  from sktime.datasets import load_unit_test
  
  
  def test_drcif_train_estimate():
      """Test of DrCIF on unit test data."""
      # load unit test data
      X_train, y_train = load_unit_test(split="train")
  
      # train DrCIF
      drcif = DrCIF(
          n_estimators=2,
          n_intervals=2,
          att_subsample_size=2,
          random_state=0,
          save_transformed_data=True,
      )
      drcif.fit(X_train, y_train)
  
      # test train estimate
      train_probas = drcif._get_train_probs(X_train, y_train)
      assert train_probas.shape == (20, 2)
      train_preds = drcif.classes_[np.argmax(train_probas, axis=1)]
      assert accuracy_score(y_train, train_preds) >= 0.6
  
  
  def test_contracted_drcif():
      """Test of contracted DrCIF on unit test data."""
      # load unit test data
      X_train, y_train = load_unit_test(split="train")
  
      # train contracted DrCIF
      drcif = DrCIF(
          time_limit_in_minutes=0.25,
          contract_max_n_estimators=2,
          n_intervals=2,
          att_subsample_size=2,
          random_state=0,
      )
      drcif.fit(X_train, y_train)
  
      assert len(drcif.estimators_) > 1
   #!/usr/bin/env python3 -u
  # -*- coding: utf-8 -*-
  # copyright: sktime developers, BSD-3-Clause License (see LICENSE file)
  """Implements outlier detection from pyOD."""
  
  import numpy as np
  from sklearn.base import clone
  
  from sktime.annotation.base._base import BaseSeriesAnnotator
  
  __author__ = ["mloning", "satya-pattnaik", "fkiraly"]
  
  import pandas as pd
  
  
  class PyODAnnotator(BaseSeriesAnnotator):
      """Transformer that applies outlier detector from pyOD.
  
      Parameters
      ----------
      estimator : PyOD estimator
          See ``https://pyod.readthedocs.io/en/latest/`` documentation for a detailed
          description of all options.
      fmt : str {"dense", "sparse"}, optional (default="dense")
          Annotation output format:
          * If "sparse", a sub-series of labels for only the outliers in X is returned,
          * If "dense", a series of labels for all values in X is returned.
      labels : str {"indicator", "score"}, optional (default="indicator")
          Annotation output labels:
          * If "indicator", returned values are boolean, indicating whether a value is an
          outlier,
          * If "score", returned values are floats, giving the outlier score.
      """
  
      def __init__(self, estimator, fmt="dense", labels="indicator"):
          self.estimator = estimator  # pyod estimator
          super(PyODAnnotator, self).__init__(fmt=fmt, labels=labels)
  
      def _fit(self, X, Y=None):
          """Fit to training data.
  
          core logic
  
          Parameters
          ----------
          X : pd.DataFrame
              training data to fit model to, time series
          Y : pd.Series, optional
              ground truth annotations for training if annotator is supervised
          Returns
          -------
          self : returns a reference to self
  
          Notes
          -----
          Create fitted model that sets attributes ending in "_".
          """
          X_np = X.to_numpy()
  
          if len(X_np.shape) == 1:
              X_np = X_np.reshape(-1, 1)
  
          self.estimator_ = clone(self.estimator)
          self.estimator_.fit(X_np)
  
          return self
  
      def _predict(self, X):
          """Create annotations on test/deployment data.
  
          Parameters
          ----------
          X : pd.DataFrame - data to annotate, time series
  
          Returns
          -------
          Y : pd.Series - annotations for sequence X
              exact format depends on annotation type
          """
          fmt = self.fmt
          labels = self.labels
  
          X_np = X.to_numpy()
  
          if len(X_np.shape) == 1:
              X_np = X_np.reshape(-1, 1)
  
          Y_np = self.estimator_.predict(X_np)
  
          if labels == "score":
              Y_val_np = self.estimator_.decision_function(X_np)
          elif labels == "indicator":
              Y_val_np = Y_np
  
          if fmt == "dense":
              Y = pd.Series(Y_val_np, index=X.index)
          elif fmt == "sparse":
              Y_loc = np.where(Y_np)
              Y = pd.Series(Y_val_np[Y_loc], index=X.index[Y_loc])
  
          return Y
  
apis: sklearn.metrics.accuracy_score numpy.argmax
apis: numpy.where pandas.Series sklearn.base.clone