


# 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 	__lowercase		(  _UpperCamelCase   )			->Any:







      """simple docstring"""

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




def 	__lowercase		(  _UpperCamelCase,			_UpperCamelCase   )			->int:







      """simple docstring"""

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




def 	__lowercase		(  _UpperCamelCase,			_UpperCamelCase,			_UpperCamelCase   )			->List[Any]:







      """simple docstring"""

      lowercase	:      Optional[Any]          =						np.dot(snake_case_,			snake_case_   )
      return np.sum(y * scores - np.log(1 + np.exp(snake_case_   )   )   )




def 	__lowercase		(  _UpperCamelCase,			_UpperCamelCase,			_UpperCamelCase,			_UpperCamelCase=70000   )			->Tuple:







      """simple docstring"""

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

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




  def 	__lowercase		(  _UpperCamelCase   )			->Any:







        """simple docstring"""

        return sigmoid_function(
            np.dot(snake_case_,			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''')
  ((__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()







def 				lowerCAmelCase_	(     snake_case_,snake_case_     ):
      _enforce_args(snake_case_,snake_case_     )
      if n == 0:
            return 0
      _A							:     Tuple			=						float("""-inf"""     )
      for i in range(1,n + 1     ):
            _A							:     str			=						max(
                snake_case_,prices[i - 1] + naive_cut_rod_recursive(n - i,snake_case_     )     )

      return max_revue







def 				lowerCAmelCase_	(     snake_case_,snake_case_     ):
      _enforce_args(snake_case_,snake_case_     )
      _A							:     Dict			=						[float("""-inf"""     ) for _ in range(n + 1     )]
      return _top_down_cut_rod_recursive(snake_case_,snake_case_,snake_case_     )







def 				lowerCAmelCase_	(     snake_case_,snake_case_,snake_case_     ):
      if max_rev[n] >= 0:
            return max_rev[n]
      elif n == 0:
            return 0
      else:
            _A							:     List[str]			=						float("""-inf"""     )
            for i in range(1,n + 1     ):
                  _A							:     Optional[Any]			=						max(
                      snake_case_,prices[i - 1] + _top_down_cut_rod_recursive(n - i,snake_case_,snake_case_     ),)

            _A							:     Tuple			=						max_revenue

      return max_rev[n]







def 				lowerCAmelCase_	(     snake_case_,snake_case_     ):
      _enforce_args(snake_case_,snake_case_     )

      # length(max_rev) = n + 1, to accommodate for the revenue obtainable from a rod of
      # length 0.
      _A							:     List[Any]			=						[float("""-inf"""     ) for _ in range(n + 1     )]
      _A							:     Any			=						0

      for i in range(1,n + 1     ):
            _A							:     Optional[Any]			=						max_rev[i]
            for j in range(1,i + 1     ):
                  _A							:     int			=						max(snake_case_,prices[j - 1] + max_rev[i - j]     )

            _A							:     int			=						max_revenue_i

      return max_rev[n]







def 				lowerCAmelCase_	(     snake_case_,snake_case_     ):
      if n < 0:
            _A							:     Optional[Any]			=						f'''n must be greater than or equal to 0. Got n = {n}'''
            raise ValueError(snake_case_     )

      if n > len(snake_case_     ):
            _A							:     Any			=						(
                """Each integral piece of rod must have a corresponding price. """
                f'''Got n = {n} but length of prices = {len(snake_case_     )}'''
            )
            raise ValueError(snake_case_     )







def 				lowerCAmelCase_	(     ):
      _A							:     Tuple			=						[6, 10, 12, 15, 20, 23]
      _A							:     List[Any]			=						len(snake_case_     )

      # the best revenue comes from cutting the rod into 6 pieces, each
      # of length 1 resulting in a revenue of 6 * 6 = 36.
      _A							:     Any			=						36

      _A							:     List[Any]			=						top_down_cut_rod(snake_case_,snake_case_     )
      _A							:     List[Any]			=						bottom_up_cut_rod(snake_case_,snake_case_     )
      _A							:     Dict			=						naive_cut_rod_recursive(snake_case_,snake_case_     )

      assert expected_max_revenue == max_rev_top_down
      assert max_rev_top_down == max_rev_bottom_up
      assert max_rev_bottom_up == max_rev_naive


if __name__ == "__main__":
  main()

