Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib
- import pandas as pd
- import numpy as np
- import matplotlib.pyplot as plt
- file_path = "slr12.xls"
- def h_theta(theta,x):
- return theta[0]+theta[1]*x
- def f_costo(dataset,theta):
- size=dataset.shape[0]
- return np.mean((np.sum( np.power(h_theta(theta,x)-y,2) for x,y in dataset.get_values()))/2)
- def minimize(dataset,theta):
- size=dataset.shape[0]
- a=0.001 #learning rate
- tmp=[0,0]
- while f_costo(dataset,theta)>10:
- tmp[0]=theta[0]-a*np.mean(np.sum(h_theta(theta,x)-y for x,y in dataset.get_values()))
- tmp[1]=theta[1]-a*np.mean(np.sum((h_theta(theta,x)-y)*(x) for x,y in dataset.get_values()))
- theta[0]=tmp[0]
- theta[1]=tmp[1]
- print(theta)
- print(f_costo(dataset,theta))
- return theta
- dataset = pd.read_csv(file_path)
- theta = [1,1]
- print(f_costo(dataset,theta))
- theta = minimize(dataset,theta)
- print(theta)
- plt.figure()
- plt.scatter(dataset.X,dataset.Y)
- dataset
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement