-
Notifications
You must be signed in to change notification settings - Fork 853
/
test_classifier.py
100 lines (75 loc) · 2.82 KB
/
test_classifier.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Sebastian Raschka 2014-2023
# mlxtend Machine Learning Library Extensions
# Author: Sebastian Raschka <sebastianraschka.com>
#
# License: BSD 3 clause
import numpy as np
import pytest
from mlxtend._base import _BaseModel, _Classifier
class BlankClassifier(_BaseModel, _Classifier):
def __init__(self, print_progress=0, random_seed=1):
self.print_progress = print_progress
self.random_seed = random_seed
def _fit(self, X, y, init_params=True):
pass
def _predict(self, X):
pass
def test_init():
cl = BlankClassifier(print_progress=0, random_seed=1)
assert hasattr(cl, "print_progress")
assert hasattr(cl, "random_seed")
def test_check_labels_ok_1():
y = np.array([1, 1, 0])
cl = BlankClassifier(print_progress=0, random_seed=1)
cl._check_target_array(y=y, allowed={(0, 1)})
def test_check_labels_ok_2():
y = np.array([1, 2, 2])
cl = BlankClassifier(print_progress=0, random_seed=1)
cl._check_target_array(y=y, allowed={(1, 2), (0, 1)})
def test_check_labels_not_ok_1():
y = np.array([1, 3, 2])
cl = BlankClassifier(print_progress=0, random_seed=1)
with pytest.raises(AttributeError) as excinfo:
cl._check_target_array(y, {(0, 1), (1, 2)})
assert excinfo.value.message == (
"Labels not in {(1, 2), (0, 1)}" ".\nFound (1, 2, 3)"
)
def test_check_labels_integer_notok():
y = np.array([1.0, 2.0], dtype=np.float_)
cl = BlankClassifier(print_progress=0, random_seed=1)
with pytest.raises(AttributeError) as excinfo:
cl._check_target_array(y)
assert excinfo.value.message == (
"y must be an integer" " array.\nFound float64"
)
def test_check_labels_positive_notok():
y = np.array([1, 1, -1])
cl = BlankClassifier(print_progress=0, random_seed=1)
with pytest.raises(AttributeError) as excinfo:
cl._check_target_array(y)
assert excinfo.value.message == (
"y array must not " "contain negative " "labels.\nFound [-1 1]"
)
def test_predict_fail():
X = np.array([[1], [2], [3]])
est = BlankClassifier(print_progress=0, random_seed=1)
est._is_fitted = False
with pytest.raises(AttributeError) as excinfo:
est.predict(X)
assert excinfo.value.message == ("Model is not " "fitted, yet.")
def test_predict_pass():
X = np.array([[1], [2], [3]])
y = np.array([1, 2, 3])
est = BlankClassifier(print_progress=0, random_seed=1)
est.fit(X, y)
est.predict(X)
with pytest.raises(TypeError) as excinfo:
est.fit(X)
assert excinfo.value.message == (
"fit() missing 1 " "required positional " "argument: 'y'"
)
def test_fit_2():
X = np.array([[1], [2], [3]])
y = np.array([1, 2, 3])
est = BlankClassifier(print_progress=0, random_seed=1)
est.fit(X=X, y=y)