From ade3ed3784fe8e34978563df76a7b4f7d5fe3665 Mon Sep 17 00:00:00 2001 From: Taru <32308101+epicalyx@users.noreply.github.com> Date: Wed, 17 Oct 2018 00:22:44 +0530 Subject: [PATCH 1/5] Logistic regression implementation implementation of logistic regression for binary classification --- machine_learning/logistic_regression.py | 97 +++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 machine_learning/logistic_regression.py diff --git a/machine_learning/logistic_regression.py b/machine_learning/logistic_regression.py new file mode 100644 index 000000000000..70c0b2807b7f --- /dev/null +++ b/machine_learning/logistic_regression.py @@ -0,0 +1,97 @@ +#!/usr/bin/env python +# coding: utf-8 + +# # Logistic Regression from scratch + +# In[62]: + + +''' Implementing logistic regression for classification problem + Helpful resources : 1.Coursera ML course 2.https://medium.com/@martinpella/logistic-regression-from-scratch-in-python-124c5636b8ac''' + + +# In[63]: + + +#importing all the required libraries +import numpy as np +import matplotlib.pyplot as plt +get_ipython().run_line_magic('matplotlib', 'inline') +from sklearn import datasets + + +# In[67]: + + +#sigmoid function or logistic function is used as a hypothesis function in classification problems +def sigmoid_function(z): + return 1/(1+np.exp(-z)) + + +def cost_function(h,y): + return (-y*np.log(h)-(1-y)*np.log(1-h)).mean() + +# here alpha is the learning rate, X is the featue matrix,y is the target matrix +def logistic_reg(alpha,X,y,max_iterations=70000): + converged=False + iterations=0 + theta=np.zeros(X.shape[1]) + + num_iterations=0 + while not converged: + z=np.dot(X,theta) + h=sigmoid_function(z) + gradient = np.dot(X.T,(h-y))/y.size + theta=theta-(alpha)*gradient + + z=np.dot(X,theta) + h=sigmoid_function(z) + e=cost_function(h,y) + print('J=',e) + J=e + + iterations+=1 #update iterations + + + if iterations== max_iterations: + print("Maximum iterations exceeded!") + converged=True + + return theta + + + + + + + + +# In[68]: + + +if __name__=='__main__': + iris=datasets.load_iris() + X = iris.data[:, :2] + y = (iris.target != 0) * 1 + + alpha=0.1 + theta=logistic_reg(alpha,X,y,max_iterations=70000) + print(theta) + def predict_prob(X): + return sigmoid_function(np.dot(X,theta)) # predicting the value of probability from the logistic regression algorithm + + + plt.figure(figsize=(10, 6)) + plt.scatter(X[y == 0][:, 0], X[y == 0][:, 1], color='b', label='0') + plt.scatter(X[y == 1][:, 0], X[y == 1][:, 1], color='r', label='1') + x1_min, x1_max = X[:,0].min(), X[:,0].max(), + x2_min, x2_max = X[:,1].min(), X[:,1].max(), + xx1, xx2 = np.meshgrid(np.linspace(x1_min, x1_max), np.linspace(x2_min, x2_max)) + grid = np.c_[xx1.ravel(), xx2.ravel()] + probs = predict_prob(grid).reshape(xx1.shape) + plt.contour(xx1, xx2, probs, [0.5], linewidths=1, colors='black'); + + plt.legend(); + + + From f018ddc4c00f71e7123b780d409550e101e3c673 Mon Sep 17 00:00:00 2001 From: Taru <32308101+epicalyx@users.noreply.github.com> Date: Wed, 17 Oct 2018 00:52:32 +0530 Subject: [PATCH 2/5] requested changes addressed --- machine_learning/logistic_regression.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/machine_learning/logistic_regression.py b/machine_learning/logistic_regression.py index 70c0b2807b7f..2e28fd96bf01 100644 --- a/machine_learning/logistic_regression.py +++ b/machine_learning/logistic_regression.py @@ -31,13 +31,13 @@ def sigmoid_function(z): def cost_function(h,y): return (-y*np.log(h)-(1-y)*np.log(1-h)).mean() -# here alpha is the learning rate, X is the featue 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(alpha,X,y,max_iterations=70000): converged=False iterations=0 theta=np.zeros(X.shape[1]) - num_iterations=0 + while not converged: z=np.dot(X,theta) h=sigmoid_function(z) @@ -46,9 +46,9 @@ def logistic_reg(alpha,X,y,max_iterations=70000): z=np.dot(X,theta) h=sigmoid_function(z) - e=cost_function(h,y) - print('J=',e) - J=e + J=cost_function(h,y) + + iterations+=1 #update iterations From 7105f6f648aa728c44e04e2389ad141314e2818f Mon Sep 17 00:00:00 2001 From: Taru <32308101+epicalyx@users.noreply.github.com> Date: Wed, 17 Oct 2018 01:07:29 +0530 Subject: [PATCH 3/5] minor changes requested changes are addressed --- machine_learning/logistic_regression.py | 1 + 1 file changed, 1 insertion(+) diff --git a/machine_learning/logistic_regression.py b/machine_learning/logistic_regression.py index 2e28fd96bf01..de0cfd54d2c6 100644 --- a/machine_learning/logistic_regression.py +++ b/machine_learning/logistic_regression.py @@ -55,6 +55,7 @@ def logistic_reg(alpha,X,y,max_iterations=70000): if iterations== max_iterations: print("Maximum iterations exceeded!") + print("Minimal cost function J=",J) converged=True return theta From 93de559ff631f3001c06d895f96e24ab638878e0 Mon Sep 17 00:00:00 2001 From: Robert Bergers Date: Fri, 19 Oct 2018 09:36:37 -0400 Subject: [PATCH 4/5] Just emboldened text and standardized some formatting throughout the file. (#358) modified: README.md modified: README.md modified: README.md From c0f7df7e22021baf4f16d7584ee2eb03db138326 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20G=C3=B3mez?= Date: Fri, 19 Oct 2018 08:42:19 -0500 Subject: [PATCH 5/5] Fixed error on chr function when decrypt (#359) On line 23 when make the operations returns a float and chr function doesn't permit float values as parameters. --- ciphers/Onepad_Cipher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ciphers/Onepad_Cipher.py b/ciphers/Onepad_Cipher.py index 7e1be5fdc077..6afbd45249ec 100644 --- a/ciphers/Onepad_Cipher.py +++ b/ciphers/Onepad_Cipher.py @@ -20,7 +20,7 @@ def decrypt(self, cipher, key): '''Function to decrypt text using psedo-random numbers.''' plain = [] for i in range(len(key)): - p = (cipher[i]-(key[i])**2)/key[i] + p = int((cipher[i]-(key[i])**2)/key[i]) plain.append(chr(p)) plain = ''.join([i for i in plain]) return plain