Contoh Program 3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

import numpy as np

import matplotlib.pyplot as plt

x = np.array([1,2,3])

y = np.array([1,2,3])

m = len(x)

w=0

b=1

alpha =0.25

J = 1/(2*m)*np.dot(w*x + b-y,w*x+b-y)

print('it w b J ')

for i in range(200):

print(f" {i+1:d} {w:<5.4f} {b:<6.4f} {J:<7.5f}")

w_baru= w - alpha *1/m* np.dot(w*x + b - y,x)

b_baru= b - alpha *1/m*np.dot(w*x + b - y,np.ones(m))

J = 1/(2*m)*np.dot(w_baru*x + b_baru-y,w_baru*x+b_baru-y)

w = w_baru

b = b_baru

plt.plot(i,J,'-*')

if abs(J) <= 1e-4:

break

plt.show()

You might also like