<jupyter_start><jupyter_text>Generate Data for Robust Regressionshttps://machinelearningmastery.com/robust-regression-for-machine-learning-in-python/  Create a regression dataset with outliers<jupyter_code>from random import random, randint, seed

from sklearn.datasets import make_regression

from matplotlib import pyplot
# Prepare the dataset
def get_dataset():
    X, y = make_regression(n_samples=100, n_features=1,
                           tail_strength=0.9, effective_rank=1,
                           n_informative=1, noise=3,
                           bias=50, random_state=1)
    # Add artificial outliers
    seed(1)
    for i in range(10):
        factor = randint(2, 4)
        if random() > 0.5:
            X[i] += factor * X.std()
        else:
            X[i] -= factor * X.std()

    return X, y<jupyter_output><empty_output><jupyter_text>Theil Sen Regression<jupyter_code>from numpy import arange, mean, std, absolute

from sklearn.linear_model import TheilSenRegressor
from sklearn.model_selection import cross_val_score, RepeatedKFold
# Evaluate a model
def evaluate_model(X, y, model):
    # Define model evaluation method
    cv = RepeatedKFold(n_splits=10, n_repeats=3, random_state=1)
    # Evaluate model
    scores = cross_val_score(model, X, y, scoring='neg_mean_absolute_error', cv=cv, n_jobs=1)
    # Force scores to be positive
    return absolute(scores)
# Plot the dataset and the model's line of best fit
def plot_best_fit(X, y, model):
    # Fit the model to all data
    model.fit(X, y)
    # Plot the dataset
    pyplot.scatter(X, y)
    # Plot the line of the best fit
    xaxis = arange(X.min(), X.max(), 0.01)
    yaxis = model.predict(xaxis.reshape((len(xaxis),1)))
    pyplot.plot(xaxis, yaxis, color='r')
    # Show the plot
    pyplot.title(type(model).__name__)
    pyplot.show()
# Generate dataset
X, y = get_dataset()
# Define model
# TheilSenRegressor(*, fit_intercept=True, copy_X=True, max_subpopulation=10000.0,
# n_subsamples=None, max_iter=300, tol=0.001, random_state=None, n_jobs=None, verbose=False)
model = TheilSenRegressor()
# Evaluate model
results = evaluate_model(X, y, model)
print(f'Mean MAE: {mean(results)} ({std(results)})')
# Plot line of the best fit
plot_best_fit(X, y, model)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10872424375274002, 'fraction_numerical': 0.01797457255589654, 'mean_word_length': 3.7344398340248963, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '6398', 'license': 'MIT', 'n_tokens_mistral': 809, 'n_tokens_neox': 766, 'n_words': 197, 'path': 'RobustRegression/TheilSenRegression.ipynb', 'repo_name': 'vladiant/MachineLearningUtils', 'size': '2281'}