Skip to content

Instantly share code, notes, and snippets.

@tranductam2802
Created October 16, 2019 06:19
Show Gist options
  • Save tranductam2802/8d383f3e8bd3856392a6acb97284522b to your computer and use it in GitHub Desktop.
Save tranductam2802/8d383f3e8bd3856392a6acb97284522b to your computer and use it in GitHub Desktop.
# w = (A^T)^+b = ((X^T)X)^+(X^T)y
import numpy as np
import matplotlib.pyplot as plt
x = [0, 1, 2]
X = np.array([x]).T
y = [0, 1, 2]
Y = np.array([y]).T
# Building Xbar
one = np.ones((X.shape[0], 1))
Xbar = np.concatenate((one, X), axis = 1)
# Calculating weights of the fitting line
A = np.dot(Xbar.T, Xbar)
b = np.dot(Xbar.T, y)
w = np.dot(np.linalg.pinv(A), b)
# Preparing the fitting line
w_0 = w[0][0]
w_1 = w[1][0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment