text: # -*- coding: utf-8 -*-
  """Locally_weighted_regression.ipynb
  
  Automatically generated by Colaboratory.
  
  Original file is located at
      https://colab.research.google.com/drive/14df3mGVhMasaTAgQVHXeBztHVxPKhL5K
  """
  
  import numpy as np
  import matplotlib.pyplot as plt
  
  x = np.linspace(-5,5,10000)
  y = np.log(np.abs((x**2)-1)+0.5)
  x+=np.random.normal(scale=0.05,size=10000)
  plt.scatter(x,y,alpha=0.3)
  
  def local_regression(x0,x,y,tau):
      x0 = np.r_[1,x0]
      x= np.c_[np.ones(len(x)),x]
      xw = x.T*radial_kernel(x0,x,tau)
      beta = np.linalg.pinv(xw@x)@xw@y
      return x0@beta
  
  def radial_kernel(x0,x,tau):
      return np.exp(np.sum((x-x0)**2,axis=1)/(-2*tau**2))
  
  def plot_lr(tau):
      domain = np.linspace(-5,5,num=300)
      pred = [local_regression(x0,x,y,tau) for x0 in domain]
      plt.scatter(x,y,alpha=0.3)
      plt.plot(domain,pred,color="red")
      return plt
  
  plot_lr(10)
  
  plot_lr(1)
  
  plot_lr(0.1)
  
  plot_lr(0.01)
  
  plot_lr(0.001)
  
  