def test_negative_with_log():
    y_predicted = np.array([-1, 10, 30])
    y_true = np.array([-1, 0, 1])
    for objective in [MeanSquaredLogError(), RootMeanSquaredLogError()]:
        with pytest.raises(
                ValueError,
                match=
                "Mean Squared Logarithmic Error cannot be used when targets contain negative values."
        ):
            objective.score(y_true, y_predicted)
def test_log_linear_model():
    obj = MeanSquaredLogError()
    root_obj = RootMeanSquaredLogError()

    s1_predicted = np.array([0, 0, 0, 1, 1, 1, 2, 2, 2])
    s1_actual = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0])

    s2_predicted = np.array([0, 0, 0, 1, 1, 1, 2, 2, 2])
    s2_actual = np.array([0, 0, 0, 1, 1, 1, 2, 2, 2])

    s3_predicted = np.array([0, 0, 0, 1, 1, 1, 2, 2, 2])
    s3_actual = np.array([2, 2, 2, 0, 0, 0, 1, 1, 1])

    assert obj.score(s1_predicted, s1_actual) == pytest.approx(0.562467324910)
    assert obj.score(s2_predicted, s2_actual) == pytest.approx(0)
    assert obj.score(s3_predicted,
                     s3_actual) == pytest.approx(0.617267976207983)

    assert root_obj.score(s1_predicted,
                          s1_actual) == pytest.approx(np.sqrt(0.562467324910))
    assert root_obj.score(s2_predicted, s2_actual) == pytest.approx(0)
    assert root_obj.score(s3_predicted, s3_actual) == pytest.approx(
        np.sqrt(0.617267976207983))
Example #3
0
def test_log_metrics_only_passed_directly(X_y_regression):
    X, y = X_y_regression
    with pytest.raises(
            ObjectiveNotFoundError,
            match="RootMeanSquaredLogError is not a valid Objective!"):
        AutoMLSearch(X_train=X,
                     y_train=y,
                     problem_type='regression',
                     additional_objectives=[
                         'RootMeanSquaredLogError', 'MeanSquaredLogError'
                     ])

    ar = AutoMLSearch(X_train=X,
                      y_train=y,
                      problem_type='regression',
                      additional_objectives=[
                          RootMeanSquaredLogError(),
                          MeanSquaredLogError()
                      ])
    assert ar.additional_objectives[0].name == 'Root Mean Squared Log Error'
    assert ar.additional_objectives[1].name == 'Mean Squared Log Error'
    y = pd.Series([2, 3, 0, 1, 1])

    invalid_targets_check = InvalidTargetDataCheck(problem_type="regression", objective=objective)

    assert invalid_targets_check.validate(X, y) == {
        "warnings": [],
        "errors": [DataCheckError(
            message=f"Target has non-positive values which is not supported for {objective}",
            data_check_name=invalid_targets_data_check_name,
            message_code=DataCheckMessageCode.TARGET_INCOMPATIBLE_OBJECTIVE,
            details={"Count of offending values": sum(val <= 0 for val in y.values.flatten())}).to_dict()],
        "actions": []
    }


@pytest.mark.parametrize("objective", [RootMeanSquaredLogError(), MeanSquaredLogError(), MAPE()])
def test_invalid_target_data_check_invalid_labels_for_nonnegative_objective_instances(objective):
    X = pd.DataFrame({'column_one': [100, 200, 100, 200, 200, 100, 200, 100] * 25})
    y = pd.Series([2, 2, 3, 3, -1, -1, 1, 1] * 25)

    data_checks = DataChecks([InvalidTargetDataCheck], {"InvalidTargetDataCheck": {"problem_type": "multiclass",
                                                                                   "objective": objective}})

    assert data_checks.validate(X, y) == {
        "warnings": [],
        "errors": [DataCheckError(
            message=f"Target has non-positive values which is not supported for {objective.name}",
            data_check_name=invalid_targets_data_check_name,
            message_code=DataCheckMessageCode.TARGET_INCOMPATIBLE_OBJECTIVE,
            details={"Count of offending values": sum(val <= 0 for val in y.values.flatten())}).to_dict()],
        "actions": []
            DataCheckError(
                message=
                f"Target has non-positive values which is not supported for {objective}",
                data_check_name=invalid_targets_data_check_name,
                message_code=DataCheckMessageCode.
                TARGET_INCOMPATIBLE_OBJECTIVE,
                details={
                    "Count of offending values":
                    sum(val <= 0 for val in y.values.flatten())
                }).to_dict()
        ]
    }


@pytest.mark.parametrize(
    "objective", [RootMeanSquaredLogError(),
                  MeanSquaredLogError(),
                  MAPE()])
def test_invalid_target_data_check_invalid_labels_for_nonnegative_objective_instances(
        objective):
    X = pd.DataFrame(
        {'column_one': [100, 200, 100, 200, 200, 100, 200, 100] * 25})
    y = pd.Series([2, 2, 3, 3, -1, -1, 1, 1] * 25)

    data_checks = DataChecks(
        [InvalidTargetDataCheck], {
            "InvalidTargetDataCheck": {
                "problem_type": "multiclass",
                "objective": objective
            }
        })