




# Logistic Regression from scratch

# In[62]:

# In[63]:

# importing all the required libraries

import numpy as np
from matplotlib import pyplot as plt
from sklearn import datasets




def 					lowerCamelCase__       (						A__       :					str	):





       '''simple docstring'''



       return 1 / (1 + np.exp(-z	))




def 					lowerCamelCase__       (						A__       :					Optional[Any]  ,			A__       :					int	):





       '''simple docstring'''



       return (-y * np.log(A__	) - (1 - y) * np.log(1 - h	)).mean()




def 					lowerCamelCase__       (						A__       :					Optional[int]  ,			A__       :					List[Any]  ,			A__       :					str	):





       '''simple docstring'''



       __lowerCamelCase   =						np.dot(A__  ,			A__	)
       return np.sum(y * scores - np.log(1 + np.exp(A__	)	)	)




def 					lowerCamelCase__       (						A__       :					Any  ,			A__       :					List[Any]  ,			A__       :					Dict  ,			A__       :					Any=70000	):





       '''simple docstring'''



       __lowerCamelCase   =						np.zeros(x.shape[1]	)

       for iterations in range(A__	):
              __lowerCamelCase   =						np.dot(A__  ,			A__	)
              __lowerCamelCase   =						sigmoid_function(A__	)
              __lowerCamelCase   =						np.dot(x.T  ,			h - y	) / y.size
              __lowerCamelCase   =						theta - alpha * gradient  # updating the weights
              __lowerCamelCase   =						np.dot(A__  ,			A__	)
              __lowerCamelCase   =						sigmoid_function(A__	)
              __lowerCamelCase   =						cost_function(A__  ,			A__	)
              if iterations % 100 == 0:
                     print(f'loss: {j} \t'	)  # printing the loss after every 100 iterations
       return theta


# In[68]:

if __name__ == "__main__":
       UpperCAmelCase_									=	datasets.load_iris()
       UpperCAmelCase_									=	iris.data[:, :2]
       UpperCAmelCase_									=	(iris.target != 0) * 1

       UpperCAmelCase_									=	0.1
       UpperCAmelCase_									=	logistic_reg(alpha, x, y, max_iterations=70_000)
       print('theta: ', theta)  # printing the theta i.e our weights vector




       def 					lowerCamelCase__       (						A__       :					Dict	):





              '''simple docstring'''



              return sigmoid_function(
                  np.dot(A__  ,			A__	)	)  # 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')
       ((UpperCAmelCase_)				,			(UpperCAmelCase_))									=	(x[:, 0].min(), x[:, 0].max())
       ((UpperCAmelCase_)				,			(UpperCAmelCase_))									=	(x[:, 1].min(), x[:, 1].max())
       ((UpperCAmelCase_)				,			(UpperCAmelCase_))									=	np.meshgrid(np.linspace(xa_min, xa_max), np.linspace(xa_min, xa_max))
       UpperCAmelCase_									=	np.c_[xxa.ravel(), xxa.ravel()]
       UpperCAmelCase_									=	predict_prob(grid).reshape(xxa.shape)
       plt.contour(xxa, xxa, probs, [0.5], linewidths=1, colors='black')

       plt.legend()




       plt.show()







def 					lowerCamelCase__       (						A__       :					list	):





       '''simple docstring'''



       __lowerCamelCase   =						len(A__	)
       for _ in range(A__	):
              for i in range(_ % 2  ,			arr_size - 1  ,			2	):
                     if arr[i + 1] < arr[i]:
                            __lowerCamelCase,   __lowerCamelCase   =						arr[i + 1], arr[i]

       return arr


if __name__ == "__main__":
       UpperCAmelCase_									=	list(range(10, 0, -1))
       print(f"""Original: {arr}. Sorted: {odd_even_transposition(arr)}""")

