Exemplo n.º 1
0
def test_min_max_scaler_zero_variance_features(setup):
    # Check min max scaler on toy data with zero variance features
    X = [[0., 1., +0.5], [0., 1., -0.1], [0., 1., +1.1]]

    X_new = [[+0., 2., 0.5], [-1., 1., 0.0], [+0., 1., 1.5]]

    # default params
    scaler = MinMaxScaler()
    X_trans = scaler.fit_transform(X)
    X_expected_0_1 = [[0., 0., 0.5], [0., 0., 0.0], [0., 0., 1.0]]
    assert_array_almost_equal(X_trans, X_expected_0_1)
    X_trans_inv = scaler.inverse_transform(X_trans)
    assert_array_almost_equal(X, X_trans_inv)

    X_trans_new = scaler.transform(X_new)
    X_expected_0_1_new = [[+0., 1., 0.500], [-1., 0., 0.083], [+0., 0., 1.333]]
    assert_array_almost_equal(X_trans_new, X_expected_0_1_new, decimal=2)

    # not default params
    scaler = MinMaxScaler(feature_range=(1, 2))
    X_trans = scaler.fit_transform(X)
    X_expected_1_2 = [[1., 1., 1.5], [1., 1., 1.0], [1., 1., 2.0]]
    assert_array_almost_equal(X_trans, X_expected_1_2)

    # function interface
    X_trans = minmax_scale(X)
    assert_array_almost_equal(X_trans, X_expected_0_1)
    X_trans = minmax_scale(X, feature_range=(1, 2))
    assert_array_almost_equal(X_trans, X_expected_1_2)
Exemplo n.º 2
0
def test_minmax_scaler_clip(setup, feature_range):
    # test behaviour of the parameter 'clip' in MinMaxScaler
    X = iris
    scaler = MinMaxScaler(feature_range=feature_range, clip=True).fit(X)
    X_min, X_max = mt.min(X, axis=0), mt.max(X, axis=0)
    X_test = [mt.r_[X_min[:2] - 10, X_max[2:] + 10]]
    X_transformed = scaler.transform(X_test)
    assert_allclose(X_transformed, [[
        feature_range[0], feature_range[0], feature_range[1], feature_range[1]
    ]])
Exemplo n.º 3
0
 def testMinmaxScalerClip(self):
     for feature_range in [(0, 1), (-10, 10)]:
         # test behaviour of the paramter 'clip' in MinMaxScaler
         X = self.iris
         scaler = MinMaxScaler(feature_range=feature_range,
                               clip=True).fit(X)
         X_min, X_max = mt.min(X, axis=0), mt.max(X, axis=0)
         X_test = [mt.r_[X_min[:2] - 10, X_max[2:] + 10]]
         X_transformed = scaler.transform(X_test)
         assert_allclose(X_transformed, [[
             feature_range[0], feature_range[0], feature_range[1],
             feature_range[1]
         ]])