


# 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						(							snake_case	:    List[Any] )       ->							Union[str, Any]:
   return 1 / (1 + np.exp(-z ))



def     __lowerCAmelCase						(							snake_case	:    Any						,      snake_case	:    Optional[Any] )       ->							str:
   return (-y * np.log(lowerCAmelCase__ ) - (1 - y) * np.log(1 - h )).mean()



def     __lowerCAmelCase						(							snake_case	:    Any						,      snake_case	:    List[str]						,      snake_case	:    Union[str, Any] )       ->							List[str]:
   __lowerCamelCase:				Union[str, Any]     =	np.dot(lowerCAmelCase__						,      lowerCAmelCase__ )
   return np.sum(y * scores - np.log(1 + np.exp(lowerCAmelCase__ ) ) )



def     __lowerCAmelCase						(							snake_case	:    str						,      snake_case	:    List[str]						,      snake_case	:    List[str]						,      snake_case	:    Dict=70000 )       ->							List[Any]:
   __lowerCamelCase:				Union[str, Any]     =	np.zeros(x.shape[1] )

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


# In[68]:

if __name__ == "__main__":
   _A		:				Any									=       datasets.load_iris()
   _A		:				List[str]									=       iris.data[:, :2]
   _A		:				Union[str, Any]									=       (iris.target != 0) * 1

   _A		:				Optional[Any]									=       0.1
   _A		:				List[str]									=       logistic_reg(alpha, x, y, max_iterations=70_000)
   print('''theta: ''', theta)  # printing the theta i.e our weights vector



   def     __lowerCAmelCase						(							snake_case	:    int )       ->							List[str]:
      return sigmoid_function(
          np.dot(lowerCAmelCase__						,      lowerCAmelCase__ ) )  # 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''')
   ((_A)						,(_A))		:				Dict									=       (x[:, 0].min(), x[:, 0].max())
   ((_A)						,(_A))		:				str									=       (x[:, 1].min(), x[:, 1].max())
   ((_A)						,(_A))		:				Union[str, Any]									=       np.meshgrid(np.linspace(xa_min, xa_max), np.linspace(xa_min, xa_max))
   _A		:				Any									=       np.c_[xxa.ravel(), xxa.ravel()]
   _A		:				Dict									=       predict_prob(grid).reshape(xxa.shape)
   plt.contour(xxa, xxa, probs, [0.5], linewidths=1, colors='''black''')

   plt.legend()







   plt.show()






def     __lowerCAmelCase						(							snake_case	:    int = 100 )       ->							int:
   __lowerCamelCase:				List[Any]     =	0
   __lowerCamelCase:				int     =	0
   for i in range(1						,      n + 1 ):
      sum_of_squares += i**2
      sum_of_ints += i
   return sum_of_ints**2 - sum_of_squares


if __name__ == "__main__":
   print(F"""{solution() = }""")


