


# 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        snake_case_					(  snake_case					)  ->    Any:
       return 1 / (1 + np.exp(-z					))




def        snake_case_					(  snake_case      ,							snake_case					)  ->    Optional[Any]:
       return (-y * np.log(snake_case					) - (1 - y) * np.log(1 - h					)).mean()




def        snake_case_					(  snake_case      ,							snake_case      ,							snake_case					)  ->    Dict:
       lowercase__:       Union[str, Any]        =       np.dot(snake_case      ,							snake_case					)
       return np.sum(y * scores - np.log(1 + np.exp(snake_case					)					)					)




def        snake_case_					(  snake_case      ,							snake_case      ,							snake_case      ,							snake_case=7_00_00					)  ->    Any:
       lowercase__:       Dict        =       np.zeros(x.shape[1]					)

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


# In[68]:

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

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




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

 plt.legend()




 plt.show()






from __future__ import annotations




def        snake_case_					(  snake_case      ,							snake_case					)  ->    list[int]:
       lowercase__:       Tuple        =       0
       lowercase__:       str        =       len(snake_case					) - 1

       while i < j:
              if nums[i] + nums[j] == target:
                     return [i, j]
              elif nums[i] + nums[j] < target:
                     lowercase__:       str        =       i + 1
              else:
                     lowercase__:       Dict        =       j - 1

       return []


if __name__ == "__main__":
 import doctest

 doctest.testmod()
 print(F'''{two_pointer([2, 7, 11, 15], 9) = }''')


