

# 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      snake_case_				(__A					:						Dict					)      ->     Tuple:
   return 1 / (1 + np.exp(-z					))





def      snake_case_				(__A					:						Dict ,       __A					:						List[Any]					)      ->     Optional[Any]:
   return (-y * np.log(__A					) - (1 - y) * np.log(1 - h					)).mean()





def      snake_case_				(__A					:						Optional[Any] ,       __A					:						str ,       __A					:						Optional[int]					)      ->     str:
   __lowerCAmelCase  :      Optional[int]    =						np.dot(__A ,       __A					)
   return np.sum(y * scores - np.log(1 + np.exp(__A					)					)					)





def      snake_case_				(__A					:						str ,       __A					:						Optional[int] ,       __A					:						int ,       __A					:						Tuple=7_0_0_0_0					)      ->     List[str]:
   __lowerCAmelCase  :      int    =						np.zeros(x.shape[1]					)

   for iterations in range(__A					):
      __lowerCAmelCase  :      List[str]    =						np.dot(__A ,       __A					)
      __lowerCAmelCase  :      str    =						sigmoid_function(__A					)
      __lowerCAmelCase  :      Tuple    =						np.dot(x.T ,       h - y					) / y.size
      __lowerCAmelCase  :      Dict    =						theta - alpha * gradient  # updating the weights
      __lowerCAmelCase  :      List[Any]    =						np.dot(__A ,       __A					)
      __lowerCAmelCase  :      str    =						sigmoid_function(__A					)
      __lowerCAmelCase  :      Dict    =						cost_function(__A ,       __A					)
      if iterations % 1_0_0 == 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      snake_case_				(__A					:						Optional[int]					)      ->     str:
      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()





from __future__ import annotations





def      snake_case_				(__A					:						list[float] ,       __A					:						list[float]					)      ->     float:
   __lowerCAmelCase  :      Union[str, Any]    =						sorted(numsa + numsa					)
   __lowerCAmelCase   ,__lowerCAmelCase  :      Optional[int]    =						divmod(len(__A					) ,       2					)
   if mod == 1:
      return all_numbers[div]
   else:
      return (all_numbers[div] + all_numbers[div - 1]) / 2


if __name__ == "__main__":
   import doctest

   doctest.testmod()
   __UpperCAmelCase								=			[float(x) for x in input("""Enter the elements of first array: """).split()]
   __UpperCAmelCase								=			[float(x) for x in input("""Enter the elements of second array: """).split()]
   print(F'The median of two arrays is: {median_of_two_arrays(array_a, array_a)}')


