title: Ridge Polynomial Regression: Direct implementation in python
body: <p>I want to directly implement the ridge polynomial regression in python without using related libraries such as sklearn. The direct calculation is given by:</p>
  <p>w = (XTX + lambda * I)^-1*XTy.</p>
  <p>The code is as follows:</p>
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  from openpyxl import load_workbook
  
  wb = load_workbook('data.xlsx')
  data = wb['data']
  xv=[]
  yv=[]
  for i in range(1,100):
      
      xv = xv +[float(data.cell(row=i,column=1).value)]
      yv = yv +[float(data.cell(row=i,column=2).value)]
  
  n=5 #polynomial degree
  
  ex=1
  m=len(xv)
  
  max=xv[0]
  for u in range(1,m):
      if xv[u]&gt;max:
          max=xv[u]
          
  x=[]
  for i in range(0,m):
      xn=[]
      for j in range(0,n+1):
          xn=xn+[xv[i]**j]
      x=x+[xn]
  
  lam = 5
  X=np.array(x)
  XtX=(X.T).dot(X)
  #XtX_inv=np.linalg.inv(XtX)
  Xty=(X.T).dot(np.array(yv))
  I = np.identity(XtX.shape[0])
  LI = np.dot(lam, I) 
  XtXLI = np.add(XtX, LI)
  XtXLI_inv = XtX_inv=np.linalg.inv(XtXLI)
  teta=XtXLI_inv.dot(Xty)
  
  def h(c):
      h=0
      for i in range(0,n+1):
          h=h+teta[i]*c**i
      return h
  
  hv=[]
  for i in range(0,m):
      hv=hv+[h(xv[i])]
  </code></pre>
  <p>It is expected to achieve better fitting by tuning the lambda parameter. However, the error increases significantly by increasing the lambda. How can I solve the problem?</p>