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







 '''simple docstring'''
 return 1 / (1 + np.exp(-z		))




def    lowerCAmelCase_  (		lowercase:				Tuple	,       lowercase:				List[Any]		)     ->					List[str]:







 '''simple docstring'''
 return (-y * np.log(lowercase		) - (1 - y) * np.log(1 - h		)).mean()




def    lowerCAmelCase_  (		lowercase:				Any	,       lowercase:				List[str]	,       lowercase:				str		)     ->					Optional[int]:







 '''simple docstring'''
 _UpperCamelCase:  Dict    						=			np.dot(lowercase	,       lowercase		)
 return np.sum(y * scores - np.log(1 + np.exp(lowercase		)		)		)




def    lowerCAmelCase_  (		lowercase:				str	,       lowercase:				Dict	,       lowercase:				Optional[Any]	,       lowercase:				Tuple=70_000		)     ->					int:







 '''simple docstring'''
 _UpperCamelCase:  List[Any]    						=			np.zeros(x.shape[1]		)

 for iterations in range(lowercase		):
  _UpperCamelCase:  Any    						=			np.dot(lowercase	,       lowercase		)
  _UpperCamelCase:  Tuple    						=			sigmoid_function(lowercase		)
  _UpperCamelCase:  Any    						=			np.dot(x.T	,       h - y		) / y.size
  _UpperCamelCase:  List[str]    						=			theta - alpha * gradient  # updating the weights
  _UpperCamelCase:  Tuple    						=			np.dot(lowercase	,       lowercase		)
  _UpperCamelCase:  List[str]    						=			sigmoid_function(lowercase		)
  _UpperCamelCase:  Optional[Any]    						=			cost_function(lowercase	,       lowercase		)
  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=7_0_0_0_0)
  print('''theta: ''', theta)  # printing the theta i.e our weights vector




  def    lowerCAmelCase_  (		lowercase:				Tuple		)     ->					Optional[int]:







   '''simple docstring'''
   return sigmoid_function(
       np.dot(lowercase	,       lowercase		)		)  # predicting the value of probability from the logistic regression algorithm

  plt.figure(figsize=(1_0, 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 typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_torch_available,
)


UpperCAmelCase_           =				{'''configuration_encoder_decoder''': ['''EncoderDecoderConfig''']}

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase_           =				['''EncoderDecoderModel''']

try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase_           =				['''TFEncoderDecoderModel''']

try:
  if not is_flax_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase_           =				['''FlaxEncoderDecoderModel''']

if TYPE_CHECKING:
  from .configuration_encoder_decoder import EncoderDecoderConfig

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_encoder_decoder import EncoderDecoderModel

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_encoder_decoder import TFEncoderDecoderModel

  try:
    if not is_flax_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_flax_encoder_decoder import FlaxEncoderDecoderModel

else:
  import sys

  UpperCAmelCase_           =				_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)