





'''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 					_a   (__SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

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


def 					_a   (__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

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


def 					_a   (__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  _UpperCamelCase       			=np.dot(__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   )
  return np.sum(y * scores - np.log(1 + np.exp(__SCREAMING_SNAKE_CASE   )   )   )


def 					_a   (__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE=7_0000   ):


  """simple docstring"""

  _UpperCamelCase       			=np.zeros(x.shape[1]   )

  for iterations in range(__SCREAMING_SNAKE_CASE   ):
    _UpperCamelCase       			=np.dot(__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   )
    _UpperCamelCase       			=sigmoid_function(__SCREAMING_SNAKE_CASE   )
    _UpperCamelCase       			=np.dot(x.T  ,      h - y   ) / y.size
    _UpperCamelCase       			=theta - alpha * gradient  # updating the weights
    _UpperCamelCase       			=np.dot(__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   )
    _UpperCamelCase       			=sigmoid_function(__SCREAMING_SNAKE_CASE   )
    _UpperCamelCase       			=cost_function(__SCREAMING_SNAKE_CASE  ,      __SCREAMING_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__":
      __lowerCamelCase						: Optional[int]		=   datasets.load_iris()
      __lowerCamelCase						: int		=   iris.data[:, :2]
      __lowerCamelCase						: Tuple		=   (iris.target != 0) * 1

      __lowerCamelCase						: Any		=   0.1
      __lowerCamelCase						: List[str]		=   logistic_reg(alpha, x, y, max_iterations=7_0000)
      print('theta: ', theta)  # printing the theta i.e our weights vector


      def 					_a   (__SCREAMING_SNAKE_CASE   ):


        """simple docstring"""

        return sigmoid_function(
            np.dot(__SCREAMING_SNAKE_CASE  ,      __SCREAMING_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))						: List[Any]		=   (x[:, 0].min(), x[:, 0].max())
      ((__lowerCamelCase)		, (__lowerCamelCase))						: str		=   (x[:, 1].min(), x[:, 1].max())
      ((__lowerCamelCase)		, (__lowerCamelCase))						: str		=   np.meshgrid(np.linspace(xa_min, xa_max), np.linspace(xa_min, xa_max))
      __lowerCamelCase						: int		=   np.c_[xxa.ravel(), xxa.ravel()]
      __lowerCamelCase						: List[str]		=   predict_prob(grid).reshape(xxa.shape)
      plt.contour(xxa, xxa, probs, [0.5], linewidths=1, colors='black')

      plt.legend()
      plt.show()













'''simple docstring'''






from collections.abc import Generator
from math import sin


def 					_a   (__SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  if len(__SCREAMING_SNAKE_CASE   ) != 32:
    raise ValueError('''Input must be of length 32'''   )

  _UpperCamelCase       			=b''''''
  for i in [3, 2, 1, 0]:
    little_endian += string_aa[8 * i : 8 * i + 8]
  return little_endian


def 					_a   (__SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  if i < 0:
    raise ValueError('''Input must be non-negative'''   )

  _UpperCamelCase       			=format(__SCREAMING_SNAKE_CASE  ,      '''08x'''   )[-8:]
  _UpperCamelCase       			=b''''''
  for i in [3, 2, 1, 0]:
    little_endian_hex += hex_rep[2 * i : 2 * i + 2].encode('''utf-8'''   )
  return little_endian_hex


def 					_a   (__SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  _UpperCamelCase       			=b''''''
  for char in message:
    bit_string += format(__SCREAMING_SNAKE_CASE  ,      '''08b'''   ).encode('''utf-8'''   )
  _UpperCamelCase       			=format(len(__SCREAMING_SNAKE_CASE   )  ,      '''064b'''   ).encode('''utf-8'''   )

  # Pad bit_string to a multiple of 512 chars
  bit_string += b"1"
  while len(__SCREAMING_SNAKE_CASE   ) % 512 != 448:
    bit_string += b"0"
  bit_string += to_little_endian(start_len[32:]   ) + to_little_endian(start_len[:32]   )

  return bit_string


def 					_a   (__SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  if len(__SCREAMING_SNAKE_CASE   ) % 512 != 0:
    raise ValueError('''Input must have length that\'s a multiple of 512'''   )

  for pos in range(0  ,      len(__SCREAMING_SNAKE_CASE   )  ,      512   ):
    _UpperCamelCase       			=bit_string[pos : pos + 512]
    _UpperCamelCase       			=[]
    for i in range(0  ,      512  ,      32   ):
      block_words.append(int(to_little_endian(block[i : i + 32]   )  ,      2   )   )
    yield block_words


def 					_a   (__SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  if i < 0:
    raise ValueError('''Input must be non-negative'''   )

  _UpperCamelCase       			=format(__SCREAMING_SNAKE_CASE  ,      '''032b'''   )
  _UpperCamelCase       			=''''''
  for c in i_str:
    new_str += "1" if c == "0" else "0"
  return int(__SCREAMING_SNAKE_CASE  ,      2   )


def 					_a   (__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  return (a + b) % 2**32


def 					_a   (__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  if i < 0:
    raise ValueError('''Input must be non-negative'''   )
  if shift < 0:
    raise ValueError('''Shift must be non-negative'''   )
  return ((i << shift) ^ (i >> (32 - shift))) % 2**32


def 					_a   (__SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  _UpperCamelCase       			=preprocess(__SCREAMING_SNAKE_CASE   )

  _UpperCamelCase       			=[int(2**32 * abs(sin(i + 1   )   )   ) for i in range(64   )]

  # Starting states
  _UpperCamelCase       			=0X6745_2301
  _UpperCamelCase       			=0Xefcd_ab89
  _UpperCamelCase       			=0X98ba_dcfe
  _UpperCamelCase       			=0X1032_5476

  _UpperCamelCase       			=[
      7,
      12,
      17,
      22,
      7,
      12,
      17,
      22,
      7,
      12,
      17,
      22,
      7,
      12,
      17,
      22,
      5,
      9,
      14,
      20,
      5,
      9,
      14,
      20,
      5,
      9,
      14,
      20,
      5,
      9,
      14,
      20,
      4,
      11,
      16,
      23,
      4,
      11,
      16,
      23,
      4,
      11,
      16,
      23,
      4,
      11,
      16,
      23,
      6,
      10,
      15,
      21,
      6,
      10,
      15,
      21,
      6,
      10,
      15,
      21,
      6,
      10,
      15,
      21,
  ]

  # Process bit string in chunks, each with 16 32-char words
  for block_words in get_block_words(__SCREAMING_SNAKE_CASE   ):
    _UpperCamelCase       			=aa
    _UpperCamelCase       			=ba
    _UpperCamelCase       			=ca
    _UpperCamelCase       			=da

    # Hash current chunk
    for i in range(64   ):
      if i <= 15:
        # f = (b & c) | (not_32(b) & d)     # Alternate definition for f
        _UpperCamelCase       			=d ^ (b & (c ^ d))
        _UpperCamelCase       			=i
      elif i <= 31:
        # f = (d & b) | (not_32(d) & c)     # Alternate definition for f
        _UpperCamelCase       			=c ^ (d & (b ^ c))
        _UpperCamelCase       			=(5 * i + 1) % 16
      elif i <= 47:
        _UpperCamelCase       			=b ^ c ^ d
        _UpperCamelCase       			=(3 * i + 5) % 16
      else:
        _UpperCamelCase       			=c ^ (b | not_aa(__SCREAMING_SNAKE_CASE   ))
        _UpperCamelCase       			=(7 * i) % 16
      _UpperCamelCase       			=(f + a + added_consts[i] + block_words[g]) % 2**32
      _UpperCamelCase       			=d
      _UpperCamelCase       			=c
      _UpperCamelCase       			=b
      _UpperCamelCase       			=sum_aa(__SCREAMING_SNAKE_CASE  ,      left_rotate_aa(__SCREAMING_SNAKE_CASE  ,      shift_amounts[i]   )   )

    # Add hashed chunk to running total
    _UpperCamelCase       			=sum_aa(__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   )
    _UpperCamelCase       			=sum_aa(__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   )
    _UpperCamelCase       			=sum_aa(__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   )
    _UpperCamelCase       			=sum_aa(__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   )

  _UpperCamelCase       			=reformat_hex(__SCREAMING_SNAKE_CASE   ) + reformat_hex(__SCREAMING_SNAKE_CASE   ) + reformat_hex(__SCREAMING_SNAKE_CASE   ) + reformat_hex(__SCREAMING_SNAKE_CASE   )
  return digest


if __name__ == "__main__":
      import doctest

      doctest.testmod()






