update
This commit is contained in:
@@ -583,7 +583,18 @@ $n\times p$ matrix $\bm{X}$.
|
||||
It is rather straightforward to implement the matrix inversion and obtain the parameters $\bm{\beta}$. After having defined the matrix $\bm{X}$ and the outputs $\bm{y}$ we have
|
||||
!bc pycod
|
||||
# matrix inversion to find beta
|
||||
# First we set up the data
|
||||
import numpy as np
|
||||
x = np.random.rand(100)
|
||||
y = 2.0+5*x*x+0.1*np.random.randn(100)
|
||||
# and then the design matrix X including the intercept
|
||||
# The design matrix now as function of a fourth-order polynomial
|
||||
X = np.zeros((len(x),5))
|
||||
X[:,0] = 1.0
|
||||
X[:,1] = x
|
||||
X[:,2] = x**2
|
||||
X[:,3] = x**3
|
||||
X[:,4] = x**4
|
||||
beta = (np.linalg.inv(X.T @ X) @ X.T ) @ y
|
||||
# and then make the prediction
|
||||
ytilde = X @ beta
|
||||
|
||||
Reference in New Issue
Block a user