import numpy as np
import matplotlib.pyplot as plt
x = np.array([[2,3],[4,6],[7,8],[12,15]])
t = np.array([5,8,14,17])
alpha = 0.1
theta0 = np.random.random()
theta1 = np.random.random()
theta2 = np.random.random()
theta = np.array([theta1,theta2])
eps = 1e-4
e0 = 9
e1 = 9
e2 = 9
while e0 >= eps or e1 >=eps or e2>=eps:
i = 0
e0 = (np.sum((x[:,i]*theta[i]))+theta0*1 -t[i])
e1 = e0*x[1,i]
e2 = e0*x[2,i]
theta0 = theta0 - alpha*e0
theta1 = theta1 -alpha*e1
theta2 = theta2 -alpha*e2
i +=1
import numpy as np
import math
import random
x = np.array([[2104], [1600], [2400], [1416], [3000]])
x1 = np.array([[3],[3],[3],[2],[4]])
t = np.array([[400], [330], [369], [232], [540]])
a = 0.0000100
error = 0.00001
dert = random.random()
dert1 = random.random()
dert2 = random.random()
sit = random.random()
sit2 = random.random()
sit3 = random.random()
for i in range(0, 5):
while (dert > error or dert1 > error or dert2 > error):
dert = (sit+sit2*x[i]+sit3*x1[i]-t[i])*1
dert1 = (sit + sit2 * x[i] + sit3 * x1[i] - t[i]) * x[i]
dert2 = (sit + sit2 * x[i] + sit3 * x1[i] - t[i]) * x1[i]
sit = sit - dert * a
sit2 = sit2 - dert1*a
sit3 = sit3 - dert2*a
print(dert)
print(dert1)
print(dert2)
print(sit)
print(sit2)
print(sit3)
x3 = 3600
x4 = 5
h = sit + sit2*x3 + sit3*x4
print(h)
[-29995.60935621]
[-63110762.08546066]
[-89986.82806862]
[0.33085396]
[617.04058107]
[1.37035325]
[2221353.2744685]
|