class EnumCurveModelTest(test_util.PWLFitTest, parameterized.TestCase): def test_simple_properties(self): model = model_util.EnumCurveModel('x', {1: 1.5, 2: 2.5}) self.assertEqual('x', model.feature_name) self.assertEqual('f_x', model.name) self.assertEqual('f_x', model.clean_name) @parameterized.named_parameters(( 'not too sensitive', model_util.EnumCurveModel('x', { 1: 1, 2: 2 }), model_util.EnumCurveModel('x', { 1: 1.0, 2: 2.0 }), True, ), ( 'not a EnumCurveModel', model_util.EnumCurveModel('x', { 1: 1.0, 2: 2.0 }), 's', False, ), ( 'different mappings', model_util.EnumCurveModel('x', { 1: 1.0, 2: 2.0 }), model_util.EnumCurveModel('x', { 1: 1.5, 2: 2.5 }), False, )) def test_eq(self, m1, m2, expected): self.assertEqual(expected, m1 == m2) def test_eval(self): model = model_util.EnumCurveModel('x', {1: 1.5, 2: 2.5}) self.assert_allclose( np.array([1.5, 1.5, 2.5, 2.5]), model.eval([1, 1, 2, 2])) self.assert_allclose( np.array([1.5, 1.5, 2.5, 2.5]), model.eval(pd.DataFrame({'x': [1, 1, 2, 2]}))) def test_expr(self): model = model_util.EnumCurveModel('x', {1: 1.5, 2: 2.5}) self.assertEqual('EnumCurve("x", {1: 1.5, 2: 2.5})', model.expr()) @parameterized.named_parameters( ('simple', 'EnumCurve("x", {1: 1.5, 2: 2.5})'), ('extra spaces', 'EnumCurve( "x" , {1: 1.5, 2: 2.5})')) def test_from_expr(self, s): model = model_util.EnumCurveModel('x', {1: 1.5, 2: 2.5}) self.assertEqual(model, model_util.EnumCurveModel.from_expr(s))
def test_eval(self): model = model_util.EnumCurveModel('x', {1: 1.5, 2: 2.5}) self.assert_allclose( np.array([1.5, 1.5, 2.5, 2.5]), model.eval([1, 1, 2, 2])) self.assert_allclose( np.array([1.5, 1.5, 2.5, 2.5]), model.eval(pd.DataFrame({'x': [1, 1, 2, 2]})))
def test_from_expr(self, expr): model = model_util.AdditiveModel([ model_util.PWLCurveModel('x1', [(1, 1), (2, 2)]), model_util.EnumCurveModel('x2', { 1: 1, 2: 2 }), model_util.ColumnModel('x3') ], 'my_model') self.assertEqual(model, model_util.AdditiveModel.from_expr(expr, 'my_model'))
def test_eval(self): model = model_util.AdditiveModel([ model_util.PWLCurveModel('x1', [(1, 1), (2, 2)]), model_util.EnumCurveModel('x2', { 1: 10, 2: 20 }), model_util.ColumnModel('x3') ], 'my_model') df = pd.DataFrame({'x1': [1, 1, 2], 'x2': [1, 1, 2], 'x3': [3, 3, 3]}) self.assert_allclose(np.array([14, 14, 25]), model.eval(df))
def test_simple_properties(self): model = model_util.AdditiveModel([ model_util.PWLCurveModel('x1', [(1, 1), (2, 2)]), model_util.EnumCurveModel('x2', { 1: 1, 2: 2 }), model_util.ColumnModel('x3') ], 'my_model') self.assertEqual('my_model', model.name) self.assertEqual(['x1', 'x2', 'x3'], model.feature_names)
def test_expr(self): model = model_util.AdditiveModel([ model_util.PWLCurveModel('x1', [(1, 1), (2, 2)]), model_util.EnumCurveModel('x2', { 1: 1, 2: 2 }), model_util.ColumnModel('x3') ], 'my_model') expr = """score = sum([ PWLCurve("x1", [(1, 1), (2, 2)]), EnumCurve("x2", {1: 1, 2: 2}), Column("x3"), ])""" self.assertEqual(expr, model.expr())
def test_from_expr(self, s): model = model_util.EnumCurveModel('x', {1: 1.5, 2: 2.5}) self.assertEqual(model, model_util.EnumCurveModel.from_expr(s))
def test_expr(self): model = model_util.EnumCurveModel('x', {1: 1.5, 2: 2.5}) self.assertEqual('EnumCurve("x", {1: 1.5, 2: 2.5})', model.expr())
def test_simple_properties(self): model = model_util.EnumCurveModel('x', {1: 1.5, 2: 2.5}) self.assertEqual('x', model.feature_name) self.assertEqual('f_x', model.name) self.assertEqual('f_x', model.clean_name)