<commit_before>import numpy as np
from numpy.testing import assert_equal, assert_raises

from sklearn import linear_model


# Generate coordinates of line
X = np.arange(-200, 200)
y = 0.2 * X + 20
data = np.column_stack([X, y])

# Add some faulty data
outliers = np.array((10, 30, 200))
data[outliers[0], :] = (1000, 1000)
data[outliers[1], :] = (-1000, -1000)
data[outliers[2], :] = (-100, -50)

X = data[:, 0][:, np.newaxis]
y = data[:, 1]


def test_ransac_inliers_outliers():

    base_estimator = linear_model.LinearRegression()
    ransac_estimator = linear_model.RANSAC(base_estimator, 2, 5,
                                           random_state=0)

    # Estimate parameters of corrupted data
    ransac_estimator.fit(X, y)

    # Ground truth / reference inlier mask
    ref_inlier_mask = np.ones_like(ransac_estimator.inlier_mask_,
                                   dtype=np.bool_)
    ref_inlier_mask[outliers] = False

    assert_equal(ransac_estimator.inlier_mask_, ref_inlier_mask)


def test_ransac_is_data_valid():
    def is_data_valid(X, y):
        return False

    X = np.random.rand(10, 2)
    y = np.random.rand(10, 1)

    base_estimator = linear_model.LinearRegression()
    ransac_estimator = linear_model.RANSAC(base_estimator, 2, 5,
                                           is_data_valid=is_data_valid,
                                           random_state=0)

    assert_raises(ValueError, ransac_estimator.fit, X, y)


def test_ransac_is_model_valid():
    def is_model_valid(estimator, X, y):
        return False

    base_estimator = linear_model.LinearRegression()
    ransac_estimator = linear_model.RANSAC(base_estimator, 2, 5,
                                           is_model_valid=is_model_valid,
                                           random_state=0)

    estimator = linear_model.LinearRegression()
    assert_raises(ValueError, ransac_estimator.fit, X, y)


def test_ransac_max_trials():
    base_estimator = linear_model.LinearRegression()

    ransac_estimator = linear_model.RANSAC(base_estimator, 2, 5, max_trials=0,
                                           random_state=0)
    assert_raises(ValueError, ransac_estimator.fit, X, y)

    ransac_estimator = linear_model.RANSAC(base_estimator, 2, 5, max_trials=11,
                                           random_state=0)
    assert getattr(ransac_estimator, 'n_trials_', None) is None
    ransac_estimator.fit(X, y)
    assert ransac_estimator.n_trials_ == 11


def test_ransac_stop_n_inliers():
    base_estimator = linear_model.LinearRegression()
    ransac_estimator = linear_model.RANSAC(base_estimator, 2, 5,
                                           stop_n_inliers=2, random_state=0)
    ransac_estimator.fit(X, y)

    assert ransac_estimator.n_trials_ == 1


def test_ransac_stop_score():
    base_estimator = linear_model.LinearRegression()
    ransac_estimator = linear_model.RANSAC(base_estimator, 2, 5,
                                           stop_score=0, random_state=0)
    ransac_estimator.fit(X, y)

    assert ransac_estimator.n_trials_ == 1


if __name__ == "__main__":
    np.testing.run_module_suite()<commit_msg>Add specific test case for score method<commit_after>import numpy as np
from numpy.testing import assert_equal, assert_raises

from sklearn import linear_model


# Generate coordinates of line
X = np.arange(-200, 200)
y = 0.2 * X + 20
data = np.column_stack([X, y])

# Add some faulty data
outliers = np.array((10, 30, 200))
data[outliers[0], :] = (1000, 1000)
data[outliers[1], :] = (-1000, -1000)
data[outliers[2], :] = (-100, -50)

X = data[:, 0][:, np.newaxis]
y = data[:, 1]


def test_ransac_inliers_outliers():

    base_estimator = linear_model.LinearRegression()
    ransac_estimator = linear_model.RANSAC(base_estimator, 2, 5,
                                           random_state=0)

    # Estimate parameters of corrupted data
    ransac_estimator.fit(X, y)

    # Ground truth / reference inlier mask
    ref_inlier_mask = np.ones_like(ransac_estimator.inlier_mask_,
                                   dtype=np.bool_)
    ref_inlier_mask[outliers] = False

    assert_equal(ransac_estimator.inlier_mask_, ref_inlier_mask)


def test_ransac_is_data_valid():
    def is_data_valid(X, y):
        return False

    X = np.random.rand(10, 2)
    y = np.random.rand(10, 1)

    base_estimator = linear_model.LinearRegression()
    ransac_estimator = linear_model.RANSAC(base_estimator, 2, 5,
                                           is_data_valid=is_data_valid,
                                           random_state=0)

    assert_raises(ValueError, ransac_estimator.fit, X, y)


def test_ransac_is_model_valid():
    def is_model_valid(estimator, X, y):
        return False

    base_estimator = linear_model.LinearRegression()
    ransac_estimator = linear_model.RANSAC(base_estimator, 2, 5,
                                           is_model_valid=is_model_valid,
                                           random_state=0)

    estimator = linear_model.LinearRegression()
    assert_raises(ValueError, ransac_estimator.fit, X, y)


def test_ransac_max_trials():
    base_estimator = linear_model.LinearRegression()

    ransac_estimator = linear_model.RANSAC(base_estimator, 2, 5, max_trials=0,
                                           random_state=0)
    assert_raises(ValueError, ransac_estimator.fit, X, y)

    ransac_estimator = linear_model.RANSAC(base_estimator, 2, 5, max_trials=11,
                                           random_state=0)
    assert getattr(ransac_estimator, 'n_trials_', None) is None
    ransac_estimator.fit(X, y)
    assert ransac_estimator.n_trials_ == 11


def test_ransac_stop_n_inliers():
    base_estimator = linear_model.LinearRegression()
    ransac_estimator = linear_model.RANSAC(base_estimator, 2, 5,
                                           stop_n_inliers=2, random_state=0)
    ransac_estimator.fit(X, y)

    assert ransac_estimator.n_trials_ == 1


def test_ransac_stop_score():
    base_estimator = linear_model.LinearRegression()
    ransac_estimator = linear_model.RANSAC(base_estimator, 2, 5,
                                           stop_score=0, random_state=0)
    ransac_estimator.fit(X, y)

    assert ransac_estimator.n_trials_ == 1


def test_ransac_score():
    X = np.arange(100)[:, None]
    y = np.zeros((100, ))
    y[0] = 1
    y[1] = 100

    base_estimator = linear_model.LinearRegression()
    ransac_estimator = linear_model.RANSAC(base_estimator, 2, 5,
                                           stop_score=0, random_state=0)
    ransac_estimator.fit(X, y)

    assert ransac_estimator.score(X[2:], y[2:]) == 0
    assert ransac_estimator.score(X[:2], y[:2]) > 0


if __name__ == "__main__":
    np.testing.run_module_suite()
{'fraction_non_alphanumeric': 0.09589243498817968, 'fraction_numerical': 0.026595744680851064, 'mean_word_length': 2.5458355159769512, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 40, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3618620', 'n_tokens_mistral': 2518, 'n_tokens_neox': 2282, 'n_words': 416}