


"""simple docstring"""





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


def    _snake_case  (  _snake_case :	Tuple       ,      _snake_case :	str	):
      return (-y * np.log(_snake_case	) - (1 - y) * np.log(1 - h	)).mean()


def    _snake_case  (  _snake_case :	Optional[Any]       ,      _snake_case :	List[Any]       ,      _snake_case :	str	):
      lowerCAmelCase    :		Any				=      np.dot(_snake_case       ,      _snake_case	)
      return np.sum(y * scores - np.log(1 + np.exp(_snake_case	)	)	)


def    _snake_case  (  _snake_case :	Tuple       ,      _snake_case :	Dict       ,      _snake_case :	Any       ,      _snake_case :	int=70000	):
      lowerCAmelCase    :		int				=      np.zeros(x.shape[1]	)

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

 snake_case__						:   str			 = 0.1
 snake_case__						:   str			 = logistic_reg(alpha, x, y, max_iterations=70_000)
 print('''theta: ''', theta)  # printing the theta i.e our weights vector


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

 plt.legend()






 plt.show()






"""simple docstring"""





from __future__ import annotations

import math

import numpy as np
from numpy.linalg import norm


def    _snake_case  (  _snake_case :	np.ndarray       ,      _snake_case :	np.ndarray	):
      return math.sqrt(sum(pow(a - b       ,      2	) for a, b in zip(_snake_case       ,      _snake_case	)	)	)


def    _snake_case  (  _snake_case :	np.ndarray       ,      _snake_case :	np.ndarray	):

      if dataset.ndim != value_array.ndim:
            lowerCAmelCase    :		List[Any]				=      (
                '''Wrong input data\'s dimensions... '''
                f'''dataset : {dataset.ndim}, value_array : {value_array.ndim}'''
            )
            raise ValueError(_snake_case	)

      try:
            if dataset.shape[1] != value_array.shape[1]:
                  lowerCAmelCase    :		Dict				=      (
                      '''Wrong input data\'s shape... '''
                      f'''dataset : {dataset.shape[1]}, value_array : {value_array.shape[1]}'''
                  )
                  raise ValueError(_snake_case	)
      except IndexError:
            if dataset.ndim != value_array.ndim:
                  raise TypeError('''Wrong shape'''	)

      if dataset.dtype != value_array.dtype:
            lowerCAmelCase    :		Optional[Any]				=      (
                '''Input data have different datatype... '''
                f'''dataset : {dataset.dtype}, value_array : {value_array.dtype}'''
            )
            raise TypeError(_snake_case	)

      lowerCAmelCase    :		str				=      []

      for value in value_array:
            lowerCAmelCase    :		int				=      euclidean(_snake_case       ,      dataset[0]	)
            lowerCAmelCase    :		Union[str, Any]				=      dataset[0].tolist()

            for dataset_value in dataset[1:]:
                  lowerCAmelCase    :		Any				=      euclidean(_snake_case       ,      _snake_case	)

                  if dist > temp_dist:
                        lowerCAmelCase    :		List[Any]				=      temp_dist
                        lowerCAmelCase    :		Tuple				=      dataset_value.tolist()

            answer.append([vector, dist]	)

      return answer


def    _snake_case  (  _snake_case :	np.ndarray       ,      _snake_case :	np.ndarray	):
      return np.dot(_snake_case       ,      _snake_case	) / (norm(_snake_case	) * norm(_snake_case	))


if __name__ == "__main__":
 import doctest

 doctest.testmod()


