






class 							A_    :






 '''simple docstring'''




 def __init__(	self) ->		Tuple:

     """simple docstring"""



     _UpperCAmelCase				:      str			  =				0
     _UpperCAmelCase				:      Optional[Any]			  =				0
     _UpperCAmelCase				:      Any			  =				{}


 def  snake_case__				(	self							,  _A) ->		Dict:

     """simple docstring"""



     if vertex not in self.adjacency:
         _UpperCAmelCase				:      Union[str, Any]			  =				{}
         self.num_vertices += 1


 def  snake_case__				(	self							,  _A							,  _A							,  _A) ->		Tuple:

     """simple docstring"""



     self.add_vertex(_A)
     self.add_vertex(_A)

     if head == tail:
         return

     _UpperCAmelCase				:      Tuple			  =				weight
     _UpperCAmelCase				:      Optional[int]			  =				weight


 def  snake_case__				(	self) ->		Union[str, Any]:

     """simple docstring"""



     _UpperCAmelCase				:      Tuple			  =				self.get_edges()
     for edge in edges:
         _UpperCAmelCase				:      Optional[Any]			  =				edge
         edges.remove((tail, head, weight))
     for i in range(len(_A)):
         _UpperCAmelCase				:      str			  =				list(edges[i])

     edges.sort(key=lambda _A: e[2])
     for i in range(len(_A) - 1):
         if edges[i][2] >= edges[i + 1][2]:
             _UpperCAmelCase				:      List[str]			  =				edges[i][2] + 1
     for edge in edges:
         _UpperCAmelCase				:      Optional[int]			  =				edge
         _UpperCAmelCase				:      List[Any]			  =				weight
         _UpperCAmelCase				:      Union[str, Any]			  =				weight


 def __str__(	self) ->		Any:

     """simple docstring"""



     _UpperCAmelCase				:      str			  =				''''''
     for tail in self.adjacency:
         for head in self.adjacency[tail]:
             _UpperCAmelCase				:      Optional[Any]			  =				self.adjacency[head][tail]
             string += f'''{head} -> {tail} == {weight}\n'''
     return string.rstrip('''\n''')


 def  snake_case__				(	self) ->		Optional[int]:

     """simple docstring"""



     _UpperCAmelCase				:      Optional[int]			  =				[]
     for tail in self.adjacency:
         for head in self.adjacency[tail]:
             output.append((tail, head, self.adjacency[head][tail]))
     return output


 def  snake_case__				(	self) ->		List[Any]:

     """simple docstring"""



     return self.adjacency.keys()


 @staticmethod
 def  snake_case__				(	_A=None							,  _A=None) ->		Dict:

     """simple docstring"""



     _UpperCAmelCase				:      Tuple			  =				Graph()
     if vertices is None:
         _UpperCAmelCase				:      Optional[int]			  =				[]
     if edges is None:
         _UpperCAmelCase				:      str			  =				[]
     for vertex in vertices:
         g.add_vertex(_A)
     for edge in edges:
         g.add_edge(*_A)
     return g
 class 							A_    :






  '''simple docstring'''




  def __init__(	self) ->		Dict:

      """simple docstring"""



      _UpperCAmelCase				:      Any			  =				{}
      _UpperCAmelCase				:      int			  =				{}


  def __len__(	self) ->		Any:

      """simple docstring"""



      return len(self.parent)


  def  snake_case__				(	self							,  _A) ->		List[str]:

      """simple docstring"""



      if item in self.parent:
          return self.find(_A)

      _UpperCAmelCase				:      str			  =				item
      _UpperCAmelCase				:      Any			  =				0
      return item


  def  snake_case__				(	self							,  _A) ->		List[Any]:

      """simple docstring"""



      if item not in self.parent:
          return self.make_set(_A)
      if item != self.parent[item]:
          _UpperCAmelCase				:      Tuple			  =				self.find(self.parent[item])
      return self.parent[item]


  def  snake_case__				(	self							,  _A							,  _A) ->		Tuple:

      """simple docstring"""



      _UpperCAmelCase				:      Dict			  =				self.find(_A)
      _UpperCAmelCase				:      Union[str, Any]			  =				self.find(_A)

      if roota == roota:
          return roota

      if self.rank[roota] > self.rank[roota]:
          _UpperCAmelCase				:      Union[str, Any]			  =				roota
          return roota

      if self.rank[roota] < self.rank[roota]:
          _UpperCAmelCase				:      int			  =				roota
          return roota

      if self.rank[roota] == self.rank[roota]:
          self.rank[roota] += 1
          _UpperCAmelCase				:      Optional[Any]			  =				roota
          return roota
      return None



 @staticmethod
 def  snake_case__				(	_A) ->		List[str]:

     """simple docstring"""



     _UpperCAmelCase				:      Union[str, Any]			  =				graph.num_vertices

     _UpperCAmelCase				:      Union[str, Any]			  =				Graph.UnionFind()
     _UpperCAmelCase				:      List[Any]			  =				[]
     while num_components > 1:
         _UpperCAmelCase				:      Any			  =				{}
         for vertex in graph.get_vertices():
             _UpperCAmelCase				:      Optional[Any]			  =				-1

         _UpperCAmelCase				:      Tuple			  =				graph.get_edges()
         for edge in edges:
             _UpperCAmelCase				:      List[str]			  =				edge
             edges.remove((tail, head, weight))
         for edge in edges:
             _UpperCAmelCase				:      Optional[int]			  =				edge
             _UpperCAmelCase				:      str			  =				union_find.find(_A)
             _UpperCAmelCase				:      List[str]			  =				union_find.find(_A)
             if seta != seta:
                 if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight:
                     _UpperCAmelCase				:      Union[str, Any]			  =				[head, tail, weight]

                 if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight:
                     _UpperCAmelCase				:      Optional[int]			  =				[head, tail, weight]
         for vertex in cheap_edge:
             if cheap_edge[vertex] != -1:
                 _UpperCAmelCase				:      Optional[int]			  =				cheap_edge[vertex]
                 if union_find.find(_A) != union_find.find(_A):
                     union_find.union(_A							,  _A)
                     mst_edges.append(cheap_edge[vertex])
                     _UpperCAmelCase				:      Dict			  =				num_components - 1
     _UpperCAmelCase				:      Dict			  =				Graph.build(edges=_A)
     return mst












# 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    :							Optional[int] )      ->					str:
    return 1 / (1 + np.exp(-z ))

def      _lowerCamelCase	(						__A    :							Tuple		,		__A    :							List[Any] )      ->					Optional[int]:
    return (-y * np.log(__A ) - (1 - y) * np.log(1 - h )).mean()

def      _lowerCamelCase	(						__A    :							Tuple		,		__A    :							List[Any]		,		__A    :							int )      ->					Any:
    _UpperCAmelCase				:      int			  =				np.dot(__A		,		__A )
    return np.sum(y * scores - np.log(1 + np.exp(__A ) ) )

def      _lowerCamelCase	(						__A    :							List[Any]		,		__A    :							Union[str, Any]		,		__A    :							str		,		__A    :							Dict=70_000 )      ->					Optional[Any]:
    _UpperCAmelCase				:      Union[str, Any]			  =				np.zeros(x.shape[1] )

    for iterations in range(__A ):
        _UpperCAmelCase				:      List[Any]			  =				np.dot(__A		,		__A )
        _UpperCAmelCase				:      Union[str, Any]			  =				sigmoid_function(__A )
        _UpperCAmelCase				:      Optional[Any]			  =				np.dot(x.T		,		h - y ) / y.size
        _UpperCAmelCase				:      Optional[Any]			  =				theta - alpha * gradient  # updating the weights
        _UpperCAmelCase				:      List[Any]			  =				np.dot(__A		,		__A )
        _UpperCAmelCase				:      Any			  =				sigmoid_function(__A )
        _UpperCAmelCase				:      Union[str, Any]			  =				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__":
  SCREAMING_SNAKE_CASE 							=						datasets.load_iris()
  SCREAMING_SNAKE_CASE 							=						iris.data[:, :2]
  SCREAMING_SNAKE_CASE 							=						(iris.target != 0) * 1

  SCREAMING_SNAKE_CASE 							=						0.1
  SCREAMING_SNAKE_CASE 							=						logistic_reg(alpha, x, y, max_iterations=70000)
  print('theta: ', theta)  # printing the theta i.e our weights vector

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

  plt.legend()


  plt.show()




