


# 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 			_a  (				SCREAMING_SNAKE_CASE	:				Optional[Any]     ):







 """simple docstring"""



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

def 			_a  (				SCREAMING_SNAKE_CASE	:				Tuple    , SCREAMING_SNAKE_CASE	:				Dict     ):







 """simple docstring"""



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

def 			_a  (				SCREAMING_SNAKE_CASE	:				Union[str, Any]    , SCREAMING_SNAKE_CASE	:				Optional[Any]    , SCREAMING_SNAKE_CASE	:				Union[str, Any]     ):







 """simple docstring"""



 UpperCamelCase__					:    List[str]          =   np.dot(SCREAMING_SNAKE_CASE    , SCREAMING_SNAKE_CASE     )
 return np.sum(y * scores - np.log(1 + np.exp(SCREAMING_SNAKE_CASE     )     )     )

def 			_a  (				SCREAMING_SNAKE_CASE	:				Union[str, Any]    , SCREAMING_SNAKE_CASE	:				Optional[Any]    , SCREAMING_SNAKE_CASE	:				List[str]    , SCREAMING_SNAKE_CASE	:				Union[str, Any]=70000     ):







 """simple docstring"""



 UpperCamelCase__					:    Union[str, Any]          =   np.zeros(x.shape[1]     )

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


# In[68]:

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

       __UpperCamelCase       :       Any						       =				0.1
       __UpperCamelCase       :       List[Any]						       =				logistic_reg(alpha, x, y, max_iterations=7_0000)
       print("theta: ", theta)  # printing the theta i.e our weights vector

       def 			_a  (				SCREAMING_SNAKE_CASE	:				str     ):







        """simple docstring"""



        return sigmoid_function(
            np.dot(SCREAMING_SNAKE_CASE    , SCREAMING_SNAKE_CASE     )     )  # 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))       :       int						       =				(x[:, 0].min(), x[:, 0].max())
       ((__UpperCamelCase)       ,						(__UpperCamelCase))       :       Any						       =				(x[:, 1].min(), x[:, 1].max())
       ((__UpperCamelCase)       ,						(__UpperCamelCase))       :       Dict						       =				np.meshgrid(np.linspace(xa_min, xa_max), np.linspace(xa_min, xa_max))
       __UpperCamelCase       :       Optional[Any]						       =				np.c_[xxa.ravel(), xxa.ravel()]
       __UpperCamelCase       :       List[str]						       =				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_tf_available, is_torch_available


__UpperCamelCase       :       Optional[int]						       =				{
    "configuration_rag": ["RagConfig"],
    "retrieval_rag": ["RagRetriever"],
    "tokenization_rag": ["RagTokenizer"],
}

try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       __UpperCamelCase       :       Optional[int]						       =				[
           "RagModel",
           "RagPreTrainedModel",
           "RagSequenceForGeneration",
           "RagTokenForGeneration",
       ]

try:
       if not is_tf_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       __UpperCamelCase       :       List[Any]						       =				[
           "TFRagModel",
           "TFRagPreTrainedModel",
           "TFRagSequenceForGeneration",
           "TFRagTokenForGeneration",
       ]


if TYPE_CHECKING:
       from .configuration_rag import RagConfig
       from .retrieval_rag import RagRetriever
       from .tokenization_rag import RagTokenizer

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_rag import RagModel, RagPreTrainedModel, RagSequenceForGeneration, RagTokenForGeneration

       try:
              if not is_tf_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_tf_rag import (
                  TFRagModel,
                  TFRagPreTrainedModel,
                  TFRagSequenceForGeneration,
                  TFRagTokenForGeneration,
              )

else:
       import sys

       __UpperCamelCase       :       List[Any]						       =				_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)


