Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.98 KB | None | 0 0
  1. import matplotlib
  2. import pandas as pd
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5.  
  6. file_path = "slr12.xls"
  7.  
  8. def h_theta(theta,x):
  9.     return theta[0]+theta[1]*x
  10.  
  11. def f_costo(dataset,theta):
  12.     size=dataset.shape[0]
  13.     return np.mean((np.sum( np.power(h_theta(theta,x)-y,2) for x,y in dataset.get_values()))/2)
  14.  
  15. def minimize(dataset,theta):
  16.     size=dataset.shape[0]
  17.     a=0.001 #learning rate
  18.     tmp=[0,0]
  19.     while f_costo(dataset,theta)>10:
  20.             tmp[0]=theta[0]-a*np.mean(np.sum(h_theta(theta,x)-y for x,y in dataset.get_values()))
  21.             tmp[1]=theta[1]-a*np.mean(np.sum((h_theta(theta,x)-y)*(x) for x,y in dataset.get_values()))
  22.             theta[0]=tmp[0]
  23.             theta[1]=tmp[1]
  24.             print(theta)
  25.     print(f_costo(dataset,theta))
  26.     return theta
  27.  
  28. dataset = pd.read_csv(file_path)
  29. theta = [1,1]
  30. print(f_costo(dataset,theta))
  31. theta = minimize(dataset,theta)
  32. print(theta)
  33.  
  34. plt.figure()
  35. plt.scatter(dataset.X,dataset.Y)
  36.  
  37. dataset
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement