Exemplo n.º 1
0
    def test_infer_target_shape(self):
        """Check if it infers the shape of a transformed `Dimension`."""
        t = Compose([Enumerate([2, 'asfa', 'ipsi']), OneHotEncode(3)], 'categorical')
        assert t.infer_target_shape((2, 5)) == (2, 5, 3)

        t = Compose([Enumerate([2, 'asfa']), OneHotEncode(2)], 'categorical')
        assert t.infer_target_shape((2, 5)) == (2, 5)

        t = Compose([Enumerate([2]), OneHotEncode(1)], 'categorical')
        assert t.infer_target_shape((2, 5)) == (2, 5)
Exemplo n.º 2
0
def tdim2(dim2):
    """Create a second example of `TransformedDimension`."""
    transformers = [
        Enumerate(dim2.categories),
        OneHotEncode(len(dim2.categories))
    ]
    tdim2 = TransformedDimension(Compose(transformers, dim2.type), dim2)
    return tdim2
Exemplo n.º 3
0
    def test_domain_and_target_type(self):
        """Check if attribute-like `domain_type` and `target_type` do
        what's expected.
        """
        t = Compose([])
        assert t.domain_type is None
        assert t.target_type is None

        t = Compose([], 'real')
        assert t.domain_type == 'real'
        assert t.target_type == 'real'

        t = Compose([Quantize()], 'real')
        assert t.domain_type == 'real'
        assert t.target_type == 'integer'

        t = Compose([Enumerate([2, 'asfa', 'ipsi']), OneHotEncode(3)], 'categorical')
        assert t.domain_type == 'categorical'
        assert t.target_type == 'real'
Exemplo n.º 4
0
    def test_domain_and_target_type(self):
        """Check if attribute-like `domain_type` and `target_type` do
        what's expected.
        """
        t = Compose([])
        assert t.domain_type is None
        assert t.target_type is None

        t = Compose([], "real")
        assert t.domain_type == "real"
        assert t.target_type == "real"

        t = Compose([Quantize()], "real")
        assert t.domain_type == "real"
        assert t.target_type == "integer"

        t = Compose([Enumerate([2, "asfa", "ipsi"]), OneHotEncode(3)], "categorical")
        assert t.domain_type == "categorical"
        assert t.target_type == "real"
Exemplo n.º 5
0
def test_quantization_does_not_violate_bounds():
    """Regress on bug that converts valid float in tdim to non valid excl. upper bound."""
    dim = Integer("yo", "uniform", 3, 7)
    transformers = [Reverse(Quantize())]
    tdim = TransformedDimension(Compose(transformers, dim.type), dim)
    assert 11 not in dim
    assert 10 in dim
    # but be careful, because upper bound is inclusive
    assert 11.5 not in tdim
    assert 10.6 in tdim
    assert tdim.reverse(9.6) in dim
    # solution is to quantize with 'floor' instead of 'round'
    assert tdim.reverse(9.6) == 9
Exemplo n.º 6
0
def tdim(dim):
    """Create an example of `TransformedDimension`."""
    transformers = [Quantize()]
    tdim = TransformedDimension(Compose(transformers, dim.type),
                                dim)
    return tdim
Exemplo n.º 7
0
 def test_repr_format(self):
     """Check representation of a transformed dimension."""
     t = Compose([Enumerate([2, 'asfa', 'ipsi']), OneHotEncode(3)], 'categorical')
     assert t.repr_format('asfa') == 'OneHotEncode(Enumerate(asfa))'
Exemplo n.º 8
0
    def test_reverse(self):
        """Check if it reverses `transform` properly, if possible."""
        t = Compose([Enumerate([2, 'asfa', 'ipsi']), OneHotEncode(3)], 'categorical')
        assert t.reverse((0.9, 0.8, 0.3)) == 2
        assert t.reverse((-0.3, 2., 0.)) == 'asfa'
        assert t.reverse((0., 0., 1.)) == 'ipsi'
        with pytest.raises(AssertionError):
            t.reverse((0., 0., 0., 1.))
        assert(numpy.all(t.reverse(numpy.array([[(0., 0., 1.), (0., 1., 0.)],
                                                [(1., 0., 0.), (0., 0., 1.)]])) ==
               numpy.array([['ipsi', 'asfa'], [2, 'ipsi']], dtype=numpy.object)))

        t = Compose([Enumerate([2, 'asfa']), OneHotEncode(2)], 'categorical')
        assert t.reverse(0.3) == 2
        assert t.reverse(2.) == 'asfa'
        assert numpy.all(t.reverse((0., 0., 0., 1.)) == numpy.array([2, 2, 2, 'asfa'],
                                                                    dtype=numpy.object))
        assert(numpy.all(t.reverse(numpy.array([[0.55, 3.], [-0.6, 1.]])) ==
               numpy.array([['asfa', 'asfa'], [2, 'asfa']], dtype=numpy.object)))

        # for the crazy enough
        t = Compose([Enumerate([2]), OneHotEncode(1)], 'categorical')
        assert t.reverse(0) == 2
        assert t.reverse(5.0) == 2
        assert t.reverse(0.2) == 2
        assert t.reverse(-0.2) == 2
        assert numpy.all(t.reverse([[0.5, 0], [1.0, 55]]) == numpy.array([[2, 2], [2, 2]],
                                                                         dtype=numpy.object))
Exemplo n.º 9
0
    def test_transform(self):
        """Check if it transforms properly."""
        t = Compose([Enumerate([2, 'asfa', 'ipsi']), OneHotEncode(3)], 'categorical')
        assert numpy.all(t.transform(2) == numpy.array((1., 0., 0.)))
        assert numpy.all(t.transform('asfa') == numpy.array((0., 1., 0.)))
        assert numpy.all(t.transform('ipsi') == numpy.array((0., 0., 1.)))
        with pytest.raises(KeyError):
            t.transform('aafdasfa')
        assert(numpy.all(t.transform([['ipsi', 'asfa'], [2, 'ipsi']]) ==
               numpy.array([[(0., 0., 1.), (0., 1., 0.)], [(1., 0., 0.), (0., 0., 1.)]])))

        t = Compose([Enumerate([2, 'asfa']), OneHotEncode(2)], 'categorical')
        assert t.transform(2) == 0.
        assert t.transform('asfa') == 1.
        with pytest.raises(KeyError):
            t.transform('ipsi')
        assert(numpy.all(t.transform([['asfa', 'asfa'], [2, 'asfa']]) ==
               numpy.array([[1., 1.], [0., 1.]])))

        # for the crazy enough
        t = Compose([Enumerate([2]), OneHotEncode(1)], 'categorical')
        assert t.transform(2) == 0.
        with pytest.raises(KeyError):
            t.transform('ipsi')
        assert numpy.all(t.transform([[2, 2], [2, 2]]) == [[0, 0], [0, 0]])
Exemplo n.º 10
0
 def test_deepcopy(self):
     """Verify that the transformation object can be copied"""
     t = Compose([Enumerate([2, 'asfa', 'ipsi']), OneHotEncode(3)], 'categorical')
     t.transform([2])
     copy.deepcopy(t)
Exemplo n.º 11
0
def tdim3(dim3):
    """Create an example of integer `Dimension`."""
    return TransformedDimension(Compose([], dim3.type), dim3)
Exemplo n.º 12
0
 def test_repr_format(self):
     """Check representation of a transformed dimension."""
     t = Compose([Enumerate([2, "asfa", "ipsi"]),
                  OneHotEncode(3)], "categorical")
     assert t.repr_format("asfa") == "OneHotEncode(Enumerate(asfa))"
Exemplo n.º 13
0
    def test_reverse(self):
        """Check if it reverses `transform` properly, if possible."""
        t = Compose([Enumerate([2, "asfa", "ipsi"]),
                     OneHotEncode(3)], "categorical")
        assert t.reverse((0.9, 0.8, 0.3)) == 2
        assert t.reverse((-0.3, 2.0, 0.0)) == "asfa"
        assert t.reverse((0.0, 0.0, 1.0)) == "ipsi"
        with pytest.raises(AssertionError):
            t.reverse((0.0, 0.0, 0.0, 1.0))
        assert numpy.all(
            t.reverse(
                numpy.array([
                    [(0.0, 0.0, 1.0), (0.0, 1.0, 0.0)],
                    [(1.0, 0.0, 0.0), (0.0, 0.0, 1.0)],
                ])) == numpy.array([["ipsi", "asfa"], [2, "ipsi"]],
                                   dtype=numpy.object))

        t = Compose([Enumerate([2, "asfa"]), OneHotEncode(2)], "categorical")
        assert t.reverse(0.3) == 2
        assert t.reverse(2.0) == "asfa"
        assert numpy.all(
            t.reverse((
                0.0, 0.0, 0.0,
                1.0)) == numpy.array([2, 2, 2, "asfa"], dtype=numpy.object))
        assert numpy.all(
            t.reverse(numpy.array([[0.55, 3.0], [-0.6, 1.0]])) == numpy.array(
                [["asfa", "asfa"], [2, "asfa"]], dtype=numpy.object))

        # for the crazy enough
        t = Compose([Enumerate([2]), OneHotEncode(1)], "categorical")
        assert t.reverse(0) == 2
        assert t.reverse(5.0) == 2
        assert t.reverse(0.2) == 2
        assert t.reverse(-0.2) == 2
        assert numpy.all(
            t.reverse([[0.5, 0], [1.0, 55]]) == numpy.array(
                [[2, 2], [2, 2]], dtype=numpy.object))
Exemplo n.º 14
0
    def test_transform(self):
        """Check if it transforms properly."""
        t = Compose([Enumerate([2, "asfa", "ipsi"]),
                     OneHotEncode(3)], "categorical")
        assert numpy.all(t.transform(2) == numpy.array((1.0, 0.0, 0.0)))
        assert numpy.all(t.transform("asfa") == numpy.array((0.0, 1.0, 0.0)))
        assert numpy.all(t.transform("ipsi") == numpy.array((0.0, 0.0, 1.0)))
        with pytest.raises(KeyError):
            t.transform("aafdasfa")
        assert numpy.all(
            t.transform([["ipsi", "asfa"], [2, "ipsi"]]) == numpy.array([[(
                0.0, 0.0, 1.0), (0.0, 1.0,
                                 0.0)], [(1.0, 0.0, 0.0), (0.0, 0.0, 1.0)]]))

        t = Compose([Enumerate([2, "asfa"]), OneHotEncode(2)], "categorical")
        assert t.transform(2) == 0.0
        assert t.transform("asfa") == 1.0
        with pytest.raises(KeyError):
            t.transform("ipsi")
        assert numpy.all(
            t.transform([["asfa", "asfa"], [2, "asfa"]]) == numpy.array(
                [[1.0, 1.0], [0.0, 1.0]]))

        # for the crazy enough
        t = Compose([Enumerate([2]), OneHotEncode(1)], "categorical")
        assert t.transform(2) == 0.0
        with pytest.raises(KeyError):
            t.transform("ipsi")
        assert numpy.all(t.transform([[2, 2], [2, 2]]) == [[0, 0], [0, 0]])