示例#1
0
文件: test_data.py 项目: qinxuye/mars
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)
示例#2
0
文件: test_data.py 项目: qinxuye/mars
def test_min_max_scaler1d(setup):
    X_list_1row = X_1row.to_numpy().tolist()
    X_list_1col = X_1col.to_numpy().tolist()

    # Test scaling of dataset along single axis
    for X in [X_1row, X_1col, X_list_1row, X_list_1col]:

        scaler = MinMaxScaler(copy=True)
        X_scaled = scaler.fit(X).transform(X)

        if isinstance(X, list):
            X = mt.array(X)  # cast only after scaling done

        if _check_dim_1axis(X) == 1:
            assert_array_almost_equal(X_scaled.min(axis=0),
                                      mt.zeros(n_features))
            assert_array_almost_equal(X_scaled.max(axis=0),
                                      mt.zeros(n_features))
        else:
            assert_array_almost_equal(X_scaled.min(axis=0), .0)
            assert_array_almost_equal(X_scaled.max(axis=0), 1.)
        assert scaler.n_samples_seen_ == X.shape[0]

        # check inverse transform
        X_scaled_back = scaler.inverse_transform(X_scaled)
        assert_array_almost_equal(X_scaled_back, X)

    # Constant feature
    X = mt.ones((5, 1))
    scaler = MinMaxScaler()
    X_scaled = scaler.fit(X).transform(X)
    assert X_scaled.min().to_numpy() >= 0.
    assert X_scaled.max().to_numpy() <= 1.
    assert scaler.n_samples_seen_ == X.shape[0]

    # Function interface
    X_1d = X_1row.ravel()
    min_ = X_1d.min()
    max_ = X_1d.max()
    assert_array_almost_equal((X_1d - min_) / (max_ - min_),
                              minmax_scale(X_1d, copy=True))
示例#3
0
文件: test_data.py 项目: qinxuye/mars
def test_minmax_scale_axis1(setup):
    X = iris
    X_trans = minmax_scale(X, axis=1)
    assert_array_almost_equal(mt.min(X_trans, axis=1), 0)
    assert_array_almost_equal(mt.max(X_trans, axis=1), 1)
示例#4
0
 def testMinmaxScaleAxis1(self):
     X = self.iris
     X_trans = minmax_scale(X, axis=1)
     assert_array_almost_equal(mt.min(X_trans, axis=1), 0)
     assert_array_almost_equal(mt.max(X_trans, axis=1), 1)