Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib.pyplot as plt
- import random
- import numpy as np
- area_of_house = [10]
- prices=[10]
- for i in range(1,10):
- value = random.randint(0,100)
- area_of_house.append(value)
- prices.append(value*100)
- area_of_house.sort()
- prices.sort()
- n = len(area_of_house)
- theta_0 = 0
- theta_1 = 0
- alpha = 0.00009
- def cal_error(theta_0,theta_1, n,area_of_price,prices):
- new_theta_0 = 0
- new_theta_1 = 0
- for i in range(n):
- new_theta_0 += ( theta_0 + theta_1 *area_of_house[i]) - prices[i]
- new_theta_1 += ((theta_0 + theta_1 * area_of_house[i])- prices[i])* area_of_house[i]
- return (new_theta_0/n, new_theta_1/n)
- def gradient_descent():
- theta_0_ = 0
- theta_1_ = 0
- list_theta_0 = []
- list_theta_1 = []
- counter = 0
- arraycounter=[]
- while True:
- counter+=1
- arraycounter.append(counter)
- error_1,error_2 = cal_error(theta_0_,theta_1_, n, area_of_house,prices)
- theta_0_ = theta_0_ - alpha * (error_1)
- theta_1_ = theta_1_ - alpha * (error_2)
- list_theta_0.append(theta_0_)
- list_theta_1.append(theta_1_)
- print(error_1, " ",error_2)
- if error_1<=0.01 or error_2 <=0.01 or counter == 300:
- list_theta_0 = np.asarray(list_theta_0)
- list_theta_1 = np.asarray(list_theta_1)
- plt.figure(figsize=(10, 6), dpi=80)
- plt.plot(list_theta_0, arraycounter, color="blue", linewidth=2.5, linestyle="-")
- plt.plot(list_theta_1,arraycounter, color="red", linewidth=2.5, linestyle="-")
- break
- return
- gradient_descent()
Add Comment
Please, Sign In to add comment