Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- x = [[1,0.1,0.6,1.1],
- [1,0.2,0.7,1.2],
- [1,0.3,0.8,1.3],
- [1,0.4,0.9,1.4],
- [1,0.5,1.,1.5]]
- theta = [-2,-1,1,2]
- y = [1,0,1,0,1]
- def sigmoid(a):
- a *= -1
- return 1/(1+(math.exp(a)))
- def get_predictions(x,theta):
- answers = []
- for i in range(len(x)):
- temp = 0
- for j in range(len(theta)):
- print(x[i][j], theta[j])
- thetax = x[i][j]*theta[j]
- temp += thetax
- temp = sigmoid(temp)
- answers.append(temp)
- return answers
- def cost(y, pred):
- total_cost = 0
- for i in range(len(y)):
- cost_1 = -(y[i])*math.log(pred[i])
- cost_2 = (1-y[i])*math.log(1-pred[i])
- total_cost += cost_1-cost_2
- total_cost = (1/len(y))*total_cost
- return total_cost
- new = get_predictions(x, theta)
- print(new)
- t_cost = cost(y, new)
- print(t_cost)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement