Skip to content

Instantly share code, notes, and snippets.

@VinitaSilaparasetty
Created July 31, 2019 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VinitaSilaparasetty/43692aac4dd562fe2695a6544f522e84 to your computer and use it in GitHub Desktop.
Save VinitaSilaparasetty/43692aac4dd562fe2695a6544f522e84 to your computer and use it in GitHub Desktop.
Regularization of the Cost Function in Python
# import numpy library
import numpy as np
# Create a test dataset
Z= np.random.rand(2,1)
y= np.random.rand(2,1)
# define sigmoid function
def sigmoid(x, derivative=False):
sigm = 1. / (1. + np.exp(-x))
if derivative:
return sigm * (1. - sigm)
return sigm
# define cost function
def cost_func(init_theta,Z,y):
m=len(Z)
cost=sum(-1*(y*(np.log(sigmoid(np.dot(Z,init_theta))))+(1-y)*(np.log(1-(sigmoid(np.dot(Z,init_theta)))))))*(1/m)
return cost
# initialize variables
init_theta=np.zeros((Z.shape[1],1))
cost=cost_func(init_theta,Z,y)
# check the output
print(cost)
# regularize the cost function
def cost_func_reg(init_theta,Z,y,lambbda):
m=len(Z)
cost=sum((y*(np.log(sigmoid(np.dot(Z,init_theta))))+(1-y)*(np.log(1-(sigmoid(np.dot(Z,init_theta)))))))*(-1/m)+((lambbda/(2*m))*sum(theta**2))
return cost
# check the output
cost_func_reg(init_theta,Z,y,(np.random.rand(2,1)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment