mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Log_likelihood update (#1008)
* Add files via upload This is a simple exploratory notebook that heavily expolits pandas and seaborn * Update logistic_regression.py * Update logistic_regression.py * Rename Food wastage analysis from 1961-2013 (FAO).ipynb to other/Food wastage analysis from 1961-2013 (FAO).ipynb * Update logistic_regression.py * Update logistic_regression.py * Update logistic_regression.py * Update logistic_regression.py * Update logistic_regression.py * Update logistic_regression.py * Update logistic_regression.py
This commit is contained in:
parent
d72586c5f4
commit
0d61539883
@ -31,13 +31,16 @@ def sigmoid_function(z):
|
|||||||
def cost_function(h, y):
|
def cost_function(h, y):
|
||||||
return (-y * np.log(h) - (1 - y) * np.log(1 - h)).mean()
|
return (-y * np.log(h) - (1 - y) * np.log(1 - h)).mean()
|
||||||
|
|
||||||
|
def log_likelihood(X, Y, weights):
|
||||||
|
scores = np.dot(X, weights)
|
||||||
|
return np.sum(Y*scores - np.log(1 + np.exp(scores)) )
|
||||||
|
|
||||||
# here alpha is the learning rate, X is the feature matrix,y is the target matrix
|
# here alpha is the learning rate, X is the feature matrix,y is the target matrix
|
||||||
|
|
||||||
def logistic_reg(
|
def logistic_reg(
|
||||||
alpha,
|
alpha,
|
||||||
X,
|
X,
|
||||||
y,
|
y,
|
||||||
|
num_steps,
|
||||||
max_iterations=70000,
|
max_iterations=70000,
|
||||||
):
|
):
|
||||||
converged = False
|
converged = False
|
||||||
@ -49,21 +52,24 @@ def logistic_reg(
|
|||||||
h = sigmoid_function(z)
|
h = sigmoid_function(z)
|
||||||
gradient = np.dot(X.T, h - y) / y.size
|
gradient = np.dot(X.T, h - y) / y.size
|
||||||
theta = theta - alpha * gradient
|
theta = theta - alpha * gradient
|
||||||
|
|
||||||
z = np.dot(X, theta)
|
z = np.dot(X, theta)
|
||||||
h = sigmoid_function(z)
|
h = sigmoid_function(z)
|
||||||
J = cost_function(h, y)
|
J = cost_function(h, y)
|
||||||
|
|
||||||
iterations += 1 # update iterations
|
iterations += 1 # update iterations
|
||||||
|
weights = np.zeros(X.shape[1])
|
||||||
|
for step in range(num_steps):
|
||||||
|
scores = np.dot(X, weights)
|
||||||
|
predictions = sigmoid_function(scores)
|
||||||
|
if step % 10000 == 0:
|
||||||
|
print(log_likelihood(X,y,weights)) # Print log-likelihood every so often
|
||||||
|
return weights
|
||||||
|
|
||||||
if iterations == max_iterations:
|
if iterations == max_iterations:
|
||||||
print ('Maximum iterations exceeded!')
|
print ('Maximum iterations exceeded!')
|
||||||
print ('Minimal cost function J=', J)
|
print ('Minimal cost function J=', J)
|
||||||
converged = True
|
converged = True
|
||||||
|
|
||||||
return theta
|
return theta
|
||||||
|
|
||||||
|
|
||||||
# In[68]:
|
# In[68]:
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
@ -72,7 +78,7 @@ if __name__ == '__main__':
|
|||||||
y = (iris.target != 0) * 1
|
y = (iris.target != 0) * 1
|
||||||
|
|
||||||
alpha = 0.1
|
alpha = 0.1
|
||||||
theta = logistic_reg(alpha, X, y, max_iterations=70000)
|
theta = logistic_reg(alpha,X,y,max_iterations=70000,num_steps=30000)
|
||||||
print (theta)
|
print (theta)
|
||||||
|
|
||||||
|
|
||||||
|
5916
other/Food wastage analysis from 1961-2013 (FAO).ipynb
Normal file
5916
other/Food wastage analysis from 1961-2013 (FAO).ipynb
Normal file
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user