Beispiel #1
0
def test_replace_zeros_multifeature():
    """ Replace zeros using multiple features """

    A = np.array([[0.0, 0.0, 0.0, 0.0], [0.25, 0.25, 0.0, 0.0],
                  [0.3, 0.9, 0.6, 0.0], [0.0, 0.0, 0.0, 0.0]],
                 dtype=np.float)
    # Axis=0, feature = [0,1]
    A_transform_ax0 = np.array([[0.0, 0.0, 0.0, 0.5], [0.25, 0.25, 0.0, 0.5],
                                [0.3, 0.9, 0.6, 0.0], [0.0, 0.0, 0.0, 0.0]],
                               dtype=np.float)
    # Axis=1, feature = [0,1]
    A_transform_ax1 = np.array([[0.5, 0.5, 0.0, 0.0], [0.25, 0.25, 0.0, 0.0],
                                [0.3, 0.9, 0.6, 0.0], [0.5, 0.5, 0.0, 0.0]],
                               dtype=np.float)

    # Axis 0, copy=True, feature=[0,1]
    constr = ConstraintReplaceZeros(copy=True, axis=0, feature=[0, 1])
    out = constr.transform(A)
    assert_allclose(out, A_transform_ax0)

    # Axis 1, copy=True, feature=[0,1]
    constr = ConstraintReplaceZeros(copy=True, axis=1, feature=[0, 1])
    out = constr.transform(A)
    assert_allclose(out, A_transform_ax1)

    # Axis 1, copy=True, feature=np.array([0,1])
    constr = ConstraintReplaceZeros(copy=True,
                                    axis=1,
                                    feature=np.array([0, 1]))
    out = constr.transform(A)
    assert_allclose(out, A_transform_ax1)

    # Axis 1, copy=True, feature=None
    constr = ConstraintReplaceZeros(copy=True, axis=1, feature=None)
    out = constr.transform(A)
    assert_allclose(out, A)

    # Axis 0, copy=FALSE, feature=[0,1]
    A = np.array([[0.0, 0.0, 0.0, 0.0], [0.25, 0.25, 0.0, 0.0],
                  [0.3, 0.9, 0.6, 0.0], [0.0, 0.0, 0.0, 0.0]],
                 dtype=np.float)

    constr = ConstraintReplaceZeros(copy=False, axis=0, feature=[0, 1])
    out = constr.transform(A)
    assert_allclose(A, A_transform_ax0)

    # Axis 1, copy=FALSE, feature=[0,1]
    A = np.array([[0.0, 0.0, 0.0, 0.0], [0.25, 0.25, 0.0, 0.0],
                  [0.3, 0.9, 0.6, 0.0], [0.0, 0.0, 0.0, 0.0]],
                 dtype=np.float)

    constr = ConstraintReplaceZeros(copy=False, axis=1, feature=[0, 1])
    out = constr.transform(A)
    assert_allclose(A, A_transform_ax1)
Beispiel #2
0
def test_replace_zeros_non1fval_multifeature():
    """ Test replace zeros using a 2 features with fval != 1 """
    A = np.array([[0.0, 0.0, 0.0, 0.0], [0.25, 0.25, 0.0, 0.0],
                  [0.3, 0.9, 0.6, 0.0], [0.0, 0.0, 0.0, 0.0]],
                 dtype=np.float)
    # Axis=0, feature = 0
    A_transform_ax0 = np.array([[0.0, 0.0, 0.0, 2.0], [0.25, 0.25, 0.0, 2.0],
                                [0.3, 0.9, 0.6, 0.0], [0.0, 0.0, 0.0, 0.0]],
                               dtype=np.float)
    # Axis=1, feature = 0
    A_transform_ax1 = np.array([[2.0, 2.0, 0.0, 0.0], [0.25, 0.25, 0.0, 0.0],
                                [0.3, 0.9, 0.6, 0.0], [2.0, 2.0, 0.0, 0.0]],
                               dtype=np.float)

    # Axis 0, copy=True
    constr = ConstraintReplaceZeros(copy=True, axis=0, feature=[0, 1], fval=4)
    out = constr.transform(A)
    assert_allclose(out, A_transform_ax0)

    # Axis 1, copy=True
    constr = ConstraintReplaceZeros(copy=True, axis=1, feature=[0, 1], fval=4)
    out = constr.transform(A)
    assert_allclose(out, A_transform_ax1)

    # Axis 0, copy=FALSE
    A = np.array([[0.0, 0.0, 0.0, 0.0], [0.25, 0.25, 0.0, 0.0],
                  [0.3, 0.9, 0.6, 0.0], [0.0, 0.0, 0.0, 0.0]],
                 dtype=np.float)

    constr = ConstraintReplaceZeros(copy=False, axis=0, feature=[0, 1], fval=4)
    out = constr.transform(A)
    assert_allclose(A, A_transform_ax0)

    # Axis 1, copy=FALSE
    A = np.array([[0.0, 0.0, 0.0, 0.0], [0.25, 0.25, 0.0, 0.0],
                  [0.3, 0.9, 0.6, 0.0], [0.0, 0.0, 0.0, 0.0]],
                 dtype=np.float)

    constr = ConstraintReplaceZeros(copy=False, axis=1, feature=[0, 1], fval=4)
    out = constr.transform(A)
    assert_allclose(A, A_transform_ax1)
Beispiel #3
0
def test_replace_zeros():
    """ Test replace zeros using a single feature """
    A = np.array([[0.0, 0.0, 0.0, 0.0], [0.25, 0.25, 0.0, 0.0],
                  [0.3, 0.9, 0.6, 0.0], [0.0, 0.0, 0.0, 0.0]],
                 dtype=np.float)
    # Axis=0, feature = 0
    A_transform_ax0 = np.array([[0.0, 0.0, 0.0, 1.0], [0.25, 0.25, 0.0, 0.0],
                                [0.3, 0.9, 0.6, 0.0], [0.0, 0.0, 0.0, 0.0]],
                               dtype=np.float)
    # Axis=1, feature = 0
    A_transform_ax1 = np.array([[1.0, 0.0, 0.0, 0.0], [0.25, 0.25, 0.0, 0.0],
                                [0.3, 0.9, 0.6, 0.0], [1.0, 0.0, 0.0, 0.0]],
                               dtype=np.float)

    # Axis 0, copy=True
    constr = ConstraintReplaceZeros(copy=True, axis=0, feature=0)
    out = constr.transform(A)
    assert_allclose(out, A_transform_ax0)

    # Axis 1, copy=True
    constr = ConstraintReplaceZeros(copy=True, axis=1, feature=0)
    out = constr.transform(A)
    assert_allclose(out, A_transform_ax1)

    # Axis 0, copy=FALSE
    A = np.array([[0.0, 0.0, 0.0, 0.0], [0.25, 0.25, 0.0, 0.0],
                  [0.3, 0.9, 0.6, 0.0], [0.0, 0.0, 0.0, 0.0]],
                 dtype=np.float)

    constr = ConstraintReplaceZeros(copy=False, axis=0, feature=0)
    out = constr.transform(A)
    assert_allclose(A, A_transform_ax0)

    # Axis 1, copy=FALSE
    A = np.array([[0.0, 0.0, 0.0, 0.0], [0.25, 0.25, 0.0, 0.0],
                  [0.3, 0.9, 0.6, 0.0], [0.0, 0.0, 0.0, 0.0]],
                 dtype=np.float)

    constr = ConstraintReplaceZeros(copy=False, axis=1, feature=0)
    out = constr.transform(A)
    assert_allclose(A, A_transform_ax1)