In [1]:
import numpy as np
import math
import matplotlib.pyplot as plt

In [2]:
 def plot_effect(A):
assert A.shape == (2, 2)
# create points evenly spaced on a sphere
rad = np.arange(0, 2*math.pi, 0.1)
# apply "A" to the points
Ax = np.matmul(A, np.stack([xs, ys]))

lim = np.max(np.abs(A)) * 2

# plot the figure
plt.figure(figsize=(11,5))
plt.subplot(1, 2, 1)
plt.xlim([-lim, lim])
plt.ylim([-lim, lim])
plt.title("x")
plt.subplot(1, 2, 2)
plt.xlim([-lim, lim])
plt.ylim([-lim, lim])
plt.title("Ax")

In [3]:
plot_effect(np.array([[1., 2.],
[2., 1.]]))

In [4]:
plot_effect(np.array([[0.780, 0.563],
[0.913, 0.659]]))

In [9]:
plot_effect(np.array([[0, 2.], [2., 0.]]))

In [23]:
# Least Squares

import pandas as pd
df = pd.read_csv('hws.csv', sep=',')
data = df.values
np.random.shuffle(data)
X = data[:, :2]
b = data[:, 2]

# add noise before displaying any data (privacy)
X = np.maximum(5, X + np.random.normal(0, 2.5, X.shape))

In [19]:
X.shape

Out[19]:
(73, 2)
In [13]:
# np.round(X, 1)[:5]

In [16]:
np.matmul(np.matmul( np.linalg.inv(np.dot(X.T, X)) , X.T), b)

Out[16]:
array([0.51341166, 0.5301357 ])
In [25]:
X = np.concatenate([X, np.ones([X.shape[0], 1])], 1)

In [26]:
X.shape

Out[26]:
(73, 3)
In [27]:
np.matmul(np.matmul( np.linalg.inv(np.dot(X.T, X)) , X.T), b)

Out[27]:
array([0.33950017, 0.36806857, 7.78307657])