Advertisement
Guest User

Untitled

a guest
Oct 15th, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.89 KB | None | 0 0
  1. import math
  2. x = [[1,0.1,0.6,1.1],
  3.      [1,0.2,0.7,1.2],
  4.      [1,0.3,0.8,1.3],
  5.      [1,0.4,0.9,1.4],
  6.      [1,0.5,1.,1.5]]
  7. theta = [-2,-1,1,2]
  8. y = [1,0,1,0,1]
  9.  
  10. def sigmoid(a):
  11.     a *= -1
  12.     return 1/(1+(math.exp(a)))
  13.  
  14. def get_predictions(x,theta):
  15.     answers = []
  16.     for i in range(len(x)):
  17.         temp = 0
  18.         for j in range(len(theta)):
  19.             print(x[i][j], theta[j])
  20.             thetax = x[i][j]*theta[j]
  21.             temp += thetax
  22.         temp = sigmoid(temp)
  23.         answers.append(temp)
  24.  
  25.     return answers
  26.  
  27. def cost(y, pred):
  28.     total_cost = 0
  29.     for i in range(len(y)):
  30.         cost_1 = -(y[i])*math.log(pred[i])
  31.         cost_2 = (1-y[i])*math.log(1-pred[i])
  32.         total_cost += cost_1-cost_2
  33.     total_cost = (1/len(y))*total_cost
  34.  
  35.     return total_cost
  36.  
  37. new = get_predictions(x, theta)
  38. print(new)
  39. t_cost = cost(y, new)
  40. print(t_cost)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement