<jupyter_start><jupyter_text>Regression Models Live Demos<jupyter_code>x = np.linspace(-5, 5, 20)
y = 2 * x + 3
plt.scatter(x, y)
plt.xlabel("x")
plt.ylabel("y")
plt.show()
x = np.linspace(-5, 5, 20)
y = 2 * x + 3
y_noise = np.random.normal(loc = 0, scale = 2.5, size = len(x))
y += y_noise
plt.scatter(x, y)
plt.xlabel("x")
plt.ylabel("y")
plt.show()
x
y
x = np.array([-5.        , -4.47368421, -3.94736842, -3.42105263, -2.89473684,
       -2.36842105, -1.84210526, -1.31578947, -0.78947368, -0.26315789,
        0.26315789,  0.78947368,  1.31578947,  1.84210526,  2.36842105,
        2.89473684,  3.42105263,  3.94736842,  4.47368421,  5.        ])
y = np.array([-8.60267745, -8.04975533, -4.55387595, -3.43429694,  2.09969136,
       -4.09064955, -0.75635273,  0.20198058, -2.65483127,  1.98965128,
        1.67746488,  4.54214862,  7.19450048,  6.60315961,  5.92143454,
       12.44515194,  3.15421522, 13.33765313, 10.82866825, 14.30046017])
plt.scatter(x, y)
plt.xlabel("x")
plt.ylabel("y")
plt.show()
def plot_prediction(x, y, a, b):
    y_pred = a * x + b

    plt.scatter(x, y)
    plt.plot(x, y_pred, c = "r", label = "prediction")
    plt.xlabel("x")
    plt.ylabel("y")
    plt.show()
plot_prediction(x, y, 1, 0)
plot_prediction(x, y, 2, 3)
plot_prediction(x, y, -2, 3)
def compute_loss(x, y, a, b):
    y_pred = a * x + b
    distances = (y - y_pred) ** 2
    return distances.mean()
compute_loss(x, y, 1, 0)
compute_loss(x, y, 2, 3)
compute_loss(x, y, -2, 3)
def plot_prediction(x, y, a, b):
    y_pred = a * x + b

    plt.scatter(x, y)
    plt.plot(x, y_pred, c = "r", label = f"loss:{compute_loss(x, y, a, b)}")
    plt.xlabel("x")
    plt.ylabel("y")
    plt.legend()
    plt.show()
plot_prediction(x, y, 1, 0)
plot_prediction(x, y, 2, 3)
plot_prediction(x, y, 1.9, 3)
def perform_step(x, y, a, b, learning_rate):
    a_gradient = -2 / len(x) * np.sum(x * (y - (a * x + b)))
    b_gradient = -2 / len(y) * np.sum(y - (a * x + b))
    
    a_new = a - learning_rate * a_gradient
    b_new = b - learning_rate * b_gradient
    
    return (a_new, b_new)
a_guess, b_guess = -10, 20
learning_rate = 0.01

for step in range(1000):
    a_guess, b_guess = perform_step(x, y, a_guess, b_guess, learning_rate)
print(a_guess, b_guess)
a_guess, b_guess = 0, 0
learning_rate = 0.01

for step in range(1000):
    a_guess, b_guess = perform_step(x, y, a_guess, b_guess, learning_rate)
print(a_guess, b_guess)
a_guess, b_guess = 10, 200
learning_rate = 0.01

for step in range(1000):
    a_guess, b_guess = perform_step(x, y, a_guess, b_guess, learning_rate)
print(a_guess, b_guess)
a_guess, b_guess = -10, 20
learning_rate = 0.01

for step in range(100_000):
    a_guess, b_guess = perform_step(x, y, a_guess, b_guess, learning_rate)
print(a_guess, b_guess)
plot_prediction(x, y, a_guess, b_guess)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1716101694915254, 'fraction_numerical': 0.1518361581920904, 'mean_word_length': 3.298937784522003, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '518375', 'license': 'MIT', 'n_tokens_mistral': 1608, 'n_tokens_neox': 1343, 'n_words': 294, 'path': 'DataScience/06.RegressionModels/Regression Models Demos.ipynb', 'repo_name': 'LuGeorgiev/Python-SoftUni', 'size': '2832'}