-
Notifications
You must be signed in to change notification settings - Fork 399
/
Copy pathtest_m_estimate.py
34 lines (26 loc) · 1.13 KB
/
test_m_estimate.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
"""Tests for the MEstimateEncoder."""
from unittest import TestCase # or `from unittest import ...` if on Python 3.4+
import category_encoders as encoders
x = ['A', 'A', 'B', 'B']
y = [1, 1, 0, 1]
x_t = ['A', 'B', 'C']
class TestMEstimateEncoder(TestCase):
"""Tests for the MEstimateEncoder."""
def test_reference_m0(self):
"""Test the MEstimateEncoder with m=0, i.e. no shrinking."""
encoder = encoders.MEstimateEncoder(m=0, handle_unknown='value', handle_missing='value')
encoder.fit(x, y)
scored = encoder.transform(x_t)
expected = [[1], [0.5], [3.0 / 4.0]] # The prior probability
self.assertEqual(scored.to_numpy().tolist(), expected)
def test_reference_m1(self):
"""Test the MEstimateEncoder with m=1."""
encoder = encoders.MEstimateEncoder(m=1, handle_unknown='value', handle_missing='value')
encoder.fit(x, y)
scored = encoder.transform(x_t)
expected = [
[(2 + 3.0 / 4.0) / (2 + 1)],
[(1 + 3.0 / 4.0) / (2 + 1)],
[3.0 / 4.0],
]
self.assertEqual(scored.to_numpy().tolist(), expected)