


# 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						(			UpperCAmelCase_						:		Union[str, Any]						):




      """simple docstring"""





      return 1 / (1 + np.exp(-z						))
def     __lowerCamelCase						(			UpperCAmelCase_						:		Any			,  UpperCAmelCase_						:		Dict						):




      """simple docstring"""





      return (-y * np.log(UpperCAmelCase_						) - (1 - y) * np.log(1 - h						)).mean()
def     __lowerCamelCase						(			UpperCAmelCase_						:		List[Any]			,  UpperCAmelCase_						:		int			,  UpperCAmelCase_						:		List[Any]						):




      """simple docstring"""





      a						:Optional[int]										=	np.dot(UpperCAmelCase_			,  UpperCAmelCase_						)
      return np.sum(y * scores - np.log(1 + np.exp(UpperCAmelCase_						)						)						)
def     __lowerCamelCase						(			UpperCAmelCase_						:		Optional[Any]			,  UpperCAmelCase_						:		str			,  UpperCAmelCase_						:		Union[str, Any]			,  UpperCAmelCase_						:		Tuple=7_0000						):




      """simple docstring"""





      a						:int										=	np.zeros(x.shape[1]						)

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


# In[68]:

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

       snake_case    :   str	    =					0.1
       snake_case    :   List[Any]	    =					logistic_reg(alpha, x, y, max_iterations=7_00_00)
       print('''theta: ''', theta)  # printing the theta i.e our weights vector
       def     __lowerCamelCase						(			UpperCAmelCase_						:		Tuple						):




             """simple docstring"""





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

       plt.legend()






       plt.show()









from typing import TYPE_CHECKING

from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


snake_case    :   Any	    =					{'''configuration_van''': ['''VAN_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''VanConfig''']}


try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     snake_case    :   Union[str, Any]	    =					[
         '''VAN_PRETRAINED_MODEL_ARCHIVE_LIST''',
         '''VanForImageClassification''',
         '''VanModel''',
         '''VanPreTrainedModel''',
     ]

if TYPE_CHECKING:
     from .configuration_van import VAN_PRETRAINED_CONFIG_ARCHIVE_MAP, VanConfig

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_van import (
              VAN_PRETRAINED_MODEL_ARCHIVE_LIST,
              VanForImageClassification,
              VanModel,
              VanPreTrainedModel,
          )

else:
     import sys

     snake_case    :   Optional[Any]	    =					_LazyModule(__name__, globals()['''__file__'''], _import_structure)





