text_plain_output_1.png
import matplotlib.pyplot as plt

import numpy as np

# Create simulated data
x_1 = 2 * np.random.rand(100, 1)
x_2 = 50 * np.random.rand(100, 1)
x_3 = 1000 * np.random.rand(100, 1)

# Calculate target variable based on y = 3 + 500x1 + 20x2 + x3 formula
y = 3 + 500 * x_1 + 20 * x_2 + x_3

# Plot the simulated data
fig, axs = plt.subplots(2, 2)
fig.tight_layout(h_pad=2, w_pad=2)

axs[0, 0].plot(x_1, y, 'k.')
axs[0, 0].set(xlabel='$x_1$', ylabel='y')

axs[0, 1].plot(x_2, y, 'k.')
axs[0, 1].set(xlabel='$x_2$', ylabel='y')

axs[1, 0].plot(x_3, y, 'k.')
axs[1, 0].set(xlabel='$x_3$', ylabel='y')

axs[1, 1].remove()
plt.show()

x_bias = np.ones((100, 1))
X = np.concatenate([x_bias, x_1, x_2, x_3], axis=1)

Xt = X.T
Xt_X_inv = np.linalg.inv(Xt.dot(X))
Xt_y = Xt.dot(y)
theta = Xt_X_inv.dot(Xt_y)
theta