Пример #1
0
class TestNumpyXyMatrices:
    @given(data(), shape_X_y_matrices())
    def test_input_as_tuples(self, data, shape_X_y):
        X, y = data.draw(numpy_X_y_matrices(shape_X_y))
        assert X.shape == shape_X_y[0]
        assert y.shape == shape_X_y[1]
        assert len(y.shape) == 1

    @given(data(), shape_X_y_matrices(y_as_vector=False))
    def test_input_as_tuples_y_matrix(self, data, shape_X_y):
        X, y = data.draw(numpy_X_y_matrices(shape_X_y))
        assert X.shape == shape_X_y[0]
        assert y.shape == shape_X_y[1]
        assert len(y.shape) == 2

    @given(data())
    def test_input_as_strategy(self, data):
        data.draw(numpy_X_y_matrices(shape_X_y_matrices()))

    @given(data())
    def test_error_shape_0_smaller_shape_1(self, data):
        with pytest.raises(ValueError):
            data.draw(numpy_X_y_matrices([[10, 20], [10, 1]]))

    @given(data())
    def test_error_shape_0_different(self, data):
        with pytest.raises(ValueError):
            data.draw(numpy_X_y_matrices([[10, 5], [4, 1]]))

    @given(data(), shape_X_y_matrices(), ordered_pair(32, 47))
    def test_min_max_values(self, data, shape_X_y, min_max_values):
        min_value, max_value = min_max_values
        X, y = data.draw(
            numpy_X_y_matrices(shape_X_y, min_value=min_value, max_value=max_value)
        )
        assert X.min() >= min_value
        assert y.min() >= min_value
        assert X.max() <= max_value
        assert y.max() <= max_value

    @given(data(), shape_X_y_matrices())
    def test_no_nan(self, data, shape_X_y):
        X, y = data.draw(
            numpy_X_y_matrices(shape_X_y, allow_nan=False, allow_infinity=True)
        )
        assert not np.isnan(X).any()
        assert not np.isnan(y).any()

    @given(data(), shape_X_y_matrices())
    def test_no_infinity(self, data, shape_X_y):
        X, y = data.draw(
            numpy_X_y_matrices(shape_X_y, allow_nan=True, allow_infinity=False)
        )
        assert not np.isinf(X).any()
        assert not np.isinf(y).any()
Пример #2
0
def numpy_X_y_matrices(
    draw,
    X_y_shapes=shape_X_y_matrices(),
    min_value: float = None,
    max_value: float = None,
    allow_nan: bool = False,
    allow_infinity: bool = False,
):
    if isinstance(X_y_shapes, tuple) or isinstance(X_y_shapes, list):
        X_shape, y_shape = X_y_shapes
    else:
        X_shape, y_shape = draw(X_y_shapes)
    if X_shape[0] != y_shape[0]:
        raise ValueError(
            f"X.shape[0] must be == y.shape[0]: {X_shape}, {y_shape}")
    if X_shape[0] <= X_shape[1]:
        raise ValueError(f"X.shape[0] must be <= X.shape[1]: {X_shape}")

    elements = floats(
        min_value=min_value,
        max_value=max_value,
        allow_nan=allow_nan,
        allow_infinity=allow_infinity,
    )
    X = draw(arrays(
        dtype=float,
        shape=X_shape,
        elements=elements,
    ))
    y = draw(arrays(
        dtype=float,
        shape=y_shape,
        elements=elements,
    ))
    return X, y
Пример #3
0
 def test_input_as_strategy(self, data):
     data.draw(numpy_X_y_matrices(shape_X_y_matrices()))
Пример #4
0
class TestMultiFeatureMultiOutputRegressor:
    def test_constructor(self, estimator):
        multi_feature_multi_output_regressor = MultiFeatureMultiOutputRegressor(
            estimator)
        assert multi_feature_multi_output_regressor.n_jobs == 1

    @given(
        data=data(),
        X_y=numpy_X_y_matrices(
            X_y_shapes=shape_X_y_matrices(y_as_vector=False),
            min_value=-10000,
            max_value=10000,
        ),
    )
    def test_fit_bad_y(self, data, estimator, X_y):
        X, y = X_y
        y = y[:, 0].flatten()
        target_to_feature_dict = data.draw(
            numeric_target_to_feature_dicts(n_targets=1,
                                            n_features=X.shape[1]))
        multi_feature_multi_output_regressor = MultiFeatureMultiOutputRegressor(
            estimator)
        with pytest.raises(ValueError):
            multi_feature_multi_output_regressor.fit(
                X, y, target_to_features_dict=target_to_feature_dict)

    @given(X_y=numpy_X_y_matrices(
        X_y_shapes=shape_X_y_matrices(y_as_vector=False),
        min_value=-10000,
        max_value=10000,
    ))
    def test_fit_as_multi_output_regressor_if_target_to_feature_none(
            self, estimator, X_y):
        X, y = X_y
        multi_feature_multi_output_regressor = MultiFeatureMultiOutputRegressor(
            estimator)
        multi_feature_multi_output_regressor.fit(X, y)

        multi_output_regressor = MultiOutputRegressor(estimator)
        multi_output_regressor.fit(X, y)

        assert_almost_equal(
            multi_feature_multi_output_regressor.predict(X),
            multi_output_regressor.predict(X),
        )

    @given(X=numpy_X_matrices(min_value=-10000, max_value=10000))
    def test_error_predict_with_no_fit(self, estimator, X):
        regressor = MultiFeatureMultiOutputRegressor(estimator)
        with pytest.raises(NotFittedError):
            regressor.predict(X)

    @given(
        data=data(),
        X_y=numpy_X_y_matrices(
            X_y_shapes=shape_X_y_matrices(y_as_vector=False),
            min_value=-10000,
            max_value=10000,
        ),
    )
    def test_fit_target_to_feature_dict_working(self, data, X_y, estimator):
        X, y = X_y
        target_to_feature_dict = data.draw(
            numeric_target_to_feature_dicts(n_targets=y.shape[1],
                                            n_features=X.shape[1]))
        multi_feature_multi_output_regressor = MultiFeatureMultiOutputRegressor(
            estimator)
        multi_feature_multi_output_regressor.fit(
            X, y, target_to_features_dict=target_to_feature_dict)

    @given(
        data=data(),
        X_y=numpy_X_y_matrices(
            X_y_shapes=shape_X_y_matrices(y_as_vector=False),
            min_value=-10000,
            max_value=10000,
        ),
    )
    def test_fit_target_to_feature_dict_consistent(self, data, X_y, estimator):
        X, y = X_y
        target_to_feature_dict = data.draw(
            numeric_target_to_feature_dicts(n_targets=y.shape[1],
                                            n_features=X.shape[1]))
        multi_feature_multi_output_regressor = MultiFeatureMultiOutputRegressor(
            estimator)
        multi_feature_multi_output_regressor.fit(
            X, y, target_to_features_dict=target_to_feature_dict)
        for i, estimator_ in enumerate(
                multi_feature_multi_output_regressor.estimators_):
            expected_n_features = len(target_to_feature_dict[i])
            assert len(estimator_.coef_) == expected_n_features

    @given(
        data=data(),
        X_y=numpy_X_y_matrices(
            X_y_shapes=shape_X_y_matrices(y_as_vector=False),
            min_value=-10000,
            max_value=10000,
        ),
    )
    def test_predict_target_to_feature_dict(self, data, X_y, estimator):
        X, y = X_y
        target_to_feature_dict = data.draw(
            numeric_target_to_feature_dicts(n_targets=y.shape[1],
                                            n_features=X.shape[1]))
        multi_feature_multi_output_regressor = MultiFeatureMultiOutputRegressor(
            estimator)
        multi_feature_multi_output_regressor.fit(
            X, y, target_to_features_dict=target_to_feature_dict)
        X_predict = data.draw(numpy_X_matrices([100, X.shape[1]]))
        multi_feature_multi_output_regressor.predict(X_predict)

    @given(
        data=data(),
        X_y=numpy_X_y_matrices(
            X_y_shapes=shape_X_y_matrices(y_as_vector=False),
            min_value=-10000,
            max_value=10000,
        ),
    )
    def test_error_predict_target_to_feature_dict_wrong_X_shape(
            self, data, X_y, estimator):
        X, y = X_y
        target_to_feature_dict = data.draw(
            numeric_target_to_feature_dicts(n_targets=y.shape[1],
                                            n_features=X.shape[1]))
        multi_feature_multi_output_regressor = MultiFeatureMultiOutputRegressor(
            estimator)
        multi_feature_multi_output_regressor.fit(
            X, y, target_to_features_dict=target_to_feature_dict)
        X_predict = data.draw(numpy_X_matrices([100, 30]))
        with pytest.raises(ValueError):
            multi_feature_multi_output_regressor.predict(X_predict)
Пример #5
0
def test_shape_X_Y_value_error(data):
    with pytest.raises(ValueError):
        data.draw(shape_X_y_matrices(1, 8, 9, 10, 10, 20))
Пример #6
0

@given(data=data(), value=integers(0, 10))
def test_ordered_pair_min_equal_max(data, value):
    with pytest.raises(ValueError):
        data.draw(ordered_pair(value, value))


@given(data=data(), shape_0=ordered_pair(10, 100), shape_1=ordered_pair(1, 8))
def test_shape_X(data, shape_0, shape_1):
    shape = data.draw(shape_matrix(*shape_0, *shape_1))
    assert shape_0[0] <= shape[0] <= shape_0[1]
    assert shape_1[0] <= shape[1] <= shape_1[1]


@given(shape_X_y_matrices(123, 243, 12, 34, 1, 6))
def test_shape_X_y_matrices(shape_X_y):
    shape_X, shape_y = shape_X_y
    assert shape_X[0] == shape_y[0]
    assert 12 <= shape_X[1] <= 34
    assert 1 <= shape_y[1] <= 6


@given(shape_X_y_matrices(10, 20, 10, 20, 1, 6))
def test_shape_1_X_smaller_shape_0(shape_X_y):
    shape_X, shape_y = shape_X_y
    assert shape_X[0] > shape_X[1]


@given(data=data())
def test_shape_X_Y_value_error(data):
Пример #7
0

@given(data=data(), value=integers(0, 10))
def test_ordered_pair_min_equal_max(data, value):
    with pytest.raises(ValueError):
        data.draw(ordered_pair(value, value))


@given(data=data(), shape_0=ordered_pair(10, 100), shape_1=ordered_pair(1, 8))
def test_shape_X(data, shape_0, shape_1):
    shape = data.draw(shape_matrix(*shape_0, *shape_1))
    assert shape_0[0] <= shape[0] <= shape_0[1]
    assert shape_1[0] <= shape[1] <= shape_1[1]


@given(shape_X_y_matrices(123, 243, 12, 34, 1, 6, y_as_vector=False))
def test_shape_X_y_matrices_y_matrix(shape_X_y):
    shape_X, shape_y = shape_X_y
    assert shape_X[0] == shape_y[0]
    assert 12 <= shape_X[1] <= 34
    assert 1 <= shape_y[1] <= 6


@given(shape_X_y_matrices(123, 243, 12, 34, 1, 6, y_as_vector=True))
def test_shape_X_y_matrices_y_vector(shape_X_y):
    shape_X, shape_y = shape_X_y
    assert shape_X[0] == shape_y[0]
    assert 12 <= shape_X[1] <= 34
    assert len(shape_y) == 1