File size: 1,041 Bytes
42029e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""Abstract base class for all fraud models.

Every model implements the same minimal interface so they can be
swapped, ensembled, and tracked uniformly.
"""

from __future__ import annotations

from abc import ABC, abstractmethod
from typing import Any

import numpy as np
import pandas as pd


class FraudModel(ABC):
    """Common interface for supervised / unsupervised fraud models."""

    name: str = "base"

    @abstractmethod
    def fit(self, X: pd.DataFrame, y: pd.Series | None = None, **kwargs) -> "FraudModel":
        """Fit the model. For unsupervised models y may be None."""

    @abstractmethod
    def predict_proba(self, X: pd.DataFrame) -> np.ndarray:
        """Return a 1-D array of fraud probabilities in [0, 1]."""

    def predict(self, X: pd.DataFrame, threshold: float = 0.5) -> np.ndarray:
        return (self.predict_proba(X) >= threshold).astype(int)

    def get_params(self) -> dict[str, Any]:
        return {}

    @property
    def trained(self) -> bool:
        return getattr(self, "_trained", False)