





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







  """simple docstring"""





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





def      lowerCAmelCase_ (       __a	,    __a		)     ->		Dict:







  """simple docstring"""





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





def      lowerCAmelCase_ (       __a	,    __a	,    __a		)     ->		Optional[Any]:







  """simple docstring"""





  lowerCamelCase__:      Union[str, Any]			  =np.dot(__a	,    __a		)
  return np.sum(y * scores - np.log(1 + np.exp(__a		)		)		)





def      lowerCAmelCase_ (       __a	,    __a	,    __a	,    __a=70000		)     ->		List[str]:







  """simple docstring"""





  lowerCamelCase__:      Optional[Any]			  =np.zeros(x.shape[1]		)

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


# In[68]:

if __name__ == "__main__":
     __A									=				datasets.load_iris()
     __A									=				iris.data[:, :2]
     __A									=				(iris.target != 0) * 1

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





     def      lowerCAmelCase_ (       __a		)     ->		Union[str, Any]:







       """simple docstring"""





       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")
     ((__A)					,	(__A))									=				(x[:, 0].min(), x[:, 0].max())
     ((__A)					,	(__A))									=				(x[:, 1].min(), x[:, 1].max())
     ((__A)					,	(__A))									=				np.meshgrid(np.linspace(xa_min, xa_max), np.linspace(xa_min, xa_max))
     __A									=				np.c_[xxa.ravel(), xxa.ravel()]
     __A									=				predict_prob(grid).reshape(xxa.shape)
     plt.contour(xxa, xxa, probs, [0.5], linewidths=1, colors="black")

     plt.legend()






     plt.show()








from typing import Any





def      lowerCAmelCase_ (       __a	,    __a	,    __a	,    __a	,    __a	,    )     ->		list:







  """simple docstring"""





  _validation(
      __a	,    __a	,    __a	,    __a	,    __a	,    )
  # Creates data structures and fill initial step
  lowerCamelCase__:      dict			  ={}
  lowerCamelCase__:      dict			  ={}
  for state in states_space:
    lowerCamelCase__:      Optional[Any]			  =observations_space[0]
    lowerCamelCase__:      List[Any]			  =(
        initial_probabilities[state] * emission_probabilities[state][observation]
    )
    lowerCamelCase__:      int			  =None

  # Fills the data structure with the probabilities of
  # different transitions and pointers to previous states
  for o in range(1	,    len(__a		)		):
    lowerCamelCase__:      Tuple			  =observations_space[o]
    lowerCamelCase__:      Optional[Any]			  =observations_space[o - 1]
    for state in states_space:
      # Calculates the argmax for probability function
      lowerCamelCase__:      Tuple			  =""
      lowerCamelCase__:      Optional[Any]			  =-1
      for k_state in states_space:
        lowerCamelCase__:      int			  =(
            probabilities[(k_state, prior_observation)]
            * transition_probabilities[k_state][state]
            * emission_probabilities[state][observation]
        )
        if probability > max_probability:
          lowerCamelCase__:      List[str]			  =probability
          lowerCamelCase__:      int			  =k_state

            # Update probabilities and pointers dicts
      lowerCamelCase__:      Any			  =(
          probabilities[(arg_max, prior_observation)]
          * transition_probabilities[arg_max][state]
          * emission_probabilities[state][observation]
      )

      lowerCamelCase__:      int			  =arg_max

    # The final observation
  lowerCamelCase__:      Any			  =observations_space[len(__a		) - 1]

  # argmax for given final observation
  lowerCamelCase__:      Optional[Any]			  =""
  lowerCamelCase__:      int			  =-1
  for k_state in states_space:
    lowerCamelCase__:      Tuple			  =probabilities[(k_state, final_observation)]
    if probability > max_probability:
      lowerCamelCase__:      List[Any]			  =probability
      lowerCamelCase__:      Dict			  =k_state
  lowerCamelCase__:      str			  =arg_max

  # Process pointers backwards
  lowerCamelCase__:      Union[str, Any]			  =last_state
  lowerCamelCase__:      List[str]			  =[]
  for o in range(len(__a		) - 1	,    -1	,    -1		):
    result.append(__a		)
    lowerCamelCase__:      Union[str, Any]			  =pointers[previous, observations_space[o]]
  result.reverse()

  return result





def      lowerCAmelCase_ (       __a	,    __a	,    __a	,    __a	,    __a	,    )     ->		None:







  """simple docstring"""





  _validate_not_empty(
      __a	,    __a	,    __a	,    __a	,    __a	,    )
  _validate_lists(__a	,    __a		)
  _validate_dicts(
      __a	,    __a	,    __a		)





def      lowerCAmelCase_ (       __a	,    __a	,    __a	,    __a	,    __a	,    )     ->		None:







  """simple docstring"""





  if not all(
      [
          observations_space,
          states_space,
          initial_probabilities,
          transition_probabilities,
          emission_probabilities,
      ]		):
    raise ValueError("There's an empty parameter"		)





def      lowerCAmelCase_ (       __a	,    __a		)     ->		None:







  """simple docstring"""





  _validate_list(__a	,    "observations_space"		)
  _validate_list(__a	,    "states_space"		)





def      lowerCAmelCase_ (       __a	,    __a		)     ->		None:







  """simple docstring"""





  if not isinstance(_object	,    __a		):
    lowerCamelCase__:      Tuple			  =F"""{var_name} must be a list"""
    raise ValueError(__a		)
  else:
    for x in _object:
      if not isinstance(__a	,    __a		):
        lowerCamelCase__:      str			  =F"""{var_name} must be a list of strings"""
        raise ValueError(__a		)





def      lowerCAmelCase_ (       __a	,    __a	,    __a	,    )     ->		None:







  """simple docstring"""





  _validate_dict(__a	,    "initial_probabilities"	,    __a		)
  _validate_nested_dict(__a	,    "transition_probabilities"		)
  _validate_nested_dict(__a	,    "emission_probabilities"		)





def      lowerCAmelCase_ (       __a	,    __a		)     ->		None:







  """simple docstring"""





  _validate_dict(_object	,    __a	,    __a		)
  for x in _object.values():
    _validate_dict(__a	,    __a	,    __a	,    __a		)





def      lowerCAmelCase_ (       __a	,    __a	,    __a	,    __a = False		)     ->		None:







  """simple docstring"""





  if not isinstance(_object	,    __a		):
    lowerCamelCase__:      Optional[int]			  =F"""{var_name} must be a dict"""
    raise ValueError(__a		)
  if not all(isinstance(__a	,    __a		) for x in _object		):
    lowerCamelCase__:      Tuple			  =F"""{var_name} all keys must be strings"""
    raise ValueError(__a		)
  if not all(isinstance(__a	,    __a		) for x in _object.values()		):
    lowerCamelCase__:      Dict			  ="nested dictionary " if nested else ""
    lowerCamelCase__:      List[str]			  =F"""{var_name} {nested_text}all values must be {value_type.__name__}"""
    raise ValueError(__a		)


if __name__ == "__main__":
     from doctest import testmod

     testmod()

