# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

from sklearn import linear_model

sns.set()
# -

# Assume $y = 0.5 \cdot x + 1.5 + \epsilon$, where $\epsilon \sim N\left(0, 0.2^2\right)$. We generate two training sets by sampling $x$ uniformly from $[1, 3)$ and $[4, 6)$, respectively. Each training set has 100 examples. We should have $\bar{y}_1 \approx 2.5$ and $\bar{y}_2 \approx 4$.

# +
n = 100
x1 = 2 * np.random.rand(n, 1) + 1
x2 = 2 * np.random.rand(n, 1) + 4

a = 0.5
b = 1.5
sigma = 0.2
y1 = a * x1 + b + sigma * np.random.randn(n, 1)
y2 = a * x2 + b + sigma * np.random.randn(n, 1)

print('avg_y_1=%f, avg_y_2=%f' % (np.mean(y1), np.mean(y2)))
# -

# We fit simple linear regression models on these two training sets. We expect these two models to have very similar parameters.

# +
lr1 = linear_model.LinearRegression()
lr1.fit(x1, y1)
a1 = lr1.coef_[0][0]
b1 = lr1.intercept_[0]
x3 = np.array([0.7, 3.3])
y3 = a1 * x3 + b1

lr2 = linear_model.LinearRegression()
lr2.fit(x2, y2)
a2 = lr2.coef_[0][0]
b2 = lr2.intercept_[0]
x4 = np.array([3.7, 6.3])
y4 = a2 * x4 + b2

print('a1=%f, b1=%f, a2=%f, b2=%f' % (a1, b1, a2, b2))
# -

# Below we plot the two training sets and their fitted linear regression models.

plt.figure(figsize=(10, 7))
plt.tick_params(labelsize=14)
plt.scatter(x1, y1)
plt.scatter(x2, y2)
plt.plot(x3, y3, 'indianred', x4, y4, 'm', linewidth=3)
plt.xlabel('x', fontsize=14)
plt.ylabel('y', fontsize=14)
plt.show()

StarcoderdataJupyterScriptsDedupFiltered