LR - Geometric - Understanding - Ipynb - Colaboratory
LR - Geometric - Understanding - Ipynb - Colaboratory
ipynb - Colaboratory
1 import numpy as np
2 import matplotlib.pyplot as plt
3 %matplotlib inline
4 %config InlineBackend.figure_format = 'retina'
5 n = 100
6 p = 10
7 X = np.random.randn(n, p)
8 theta = np.random.randn(p)
9 y = X @ theta + np.random.randn(n)
10 theta_hat = np.linalg.solve(X.T @ X, X.T @ y)
11 theta_hat_inv = np.linalg.inv(X.T @ X) @ X.T @ y
12 print(np.linalg.cond(X.T @ X))
13 print(np.linalg.cond(np.linalg.inv(X.T @ X)))
14 plt.plot(theta_hat - theta_hat_inv)
15 plt.title('Difference between solutions')
16 plt.xlabel('Index')
17 plt.ylabel('Difference')
18 plt.show()
output 2.792374562999563
2.7923745629995635
1 v1 = np.array([1, 1, 1])
2 v2 = np.array([2, -2, 2])
3 y = np.array([2.5, -0.8, 1.2])
4 fig = plt.figure(figsize=(8, 8))
5 ax = fig.add_subplot(111, projection='3d')
6 ax.quiver(0, 0, 0, v1[0], v1[1], v1[2], color='r', label='v1')
7 ax.quiver(0, 0, 0, v2[0], v2[1], v2[2], color='b', label='v2')
8 ax.quiver(0, 0, 0, y[0], y[1], y[2], color='g', label='y')
9 ax.set_xlim(0, 3)
10 ax.set_ylim(0, 4)
11 ax.set_zlim(0, 3)
12 ax.set_xlabel('x')
13 ax.set_ylabel('y')
14 ax.set_zlabel('z')
15 ax.legend()
16 ax.view_init(elev=45, azim=60)
https://colab.research.google.com/drive/1tNts578_R3HdTcgf2URPCamsQEUu0ibF#scrollTo=cpTQSx1EoK1e&printMode=true 1/3
10/03/2024, 10:43 LR_Geometric_Understanding.ipynb - Colaboratory
https://colab.research.google.com/drive/1tNts578_R3HdTcgf2URPCamsQEUu0ibF#scrollTo=cpTQSx1EoK1e&printMode=true 2/3
10/03/2024, 10:43 LR_Geometric_Understanding.ipynb - Colaboratory
<matplotlib.legend.Legend at 0x7fd35838c040>
https://colab.research.google.com/drive/1tNts578_R3HdTcgf2URPCamsQEUu0ibF#scrollTo=cpTQSx1EoK1e&printMode=true 3/3