示例#1
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))
示例#2
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))