This repository has been archived by the owner on Aug 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_sklearn.py
57 lines (41 loc) · 1.61 KB
/
test_sklearn.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from functools import lru_cache
from typing import Tuple
import numpy as np
from sklearn.ensemble import IsolationForest
from jackdaw_ml import loads, saves
from jackdaw_ml.artefact_decorator import artefacts
from jackdaw_ml.serializers.pickle import PickleSerializer
@artefacts({PickleSerializer: ["model"]})
class BasicSklearnWrapper:
"""
Replicating SKLearn's interface here for convenience
"""
model: IsolationForest
def __init__(self):
self.model = IsolationForest()
def fit(self, X: np.ndarray, y: np.ndarray) -> None:
self.model.fit(X, y)
def predict(self, X: np.ndarray) -> np.ndarray:
return self.model.predict(X)
@lru_cache(maxsize=1)
def example_data() -> Tuple[np.ndarray, np.ndarray]:
data = np.random.rand(500, 10) # 500 entities, each contains 10 features
label = np.random.randint(2, size=500) # binary target
return data, label
@lru_cache(maxsize=1)
def example_prediction_data() -> np.ndarray:
data = np.random.rand(500, 10) # 500 entities, each contains 10 features
return data
def np_float_equivalence(a: np.ndarray, b: np.ndarray) -> bool:
# Are `a` and `b` within a reasonable distance of each other, accounting for internal machine error?
return np.sum(a - b) <= np.finfo(np.float32).eps
def test_sklearn_equivalence():
m1 = BasicSklearnWrapper()
m2 = BasicSklearnWrapper()
m1.fit(*example_data())
m1.predict(example_prediction_data())
model_id = saves(m1)
loads(m2, model_id)
assert np_float_equivalence(
m1.predict(example_prediction_data()), m2.predict(example_prediction_data())
)