





# 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					:       int						):





  """simple docstring"""



  return 1 / (1 + np.exp(-z						))
def 					_A	(		SCREAMING_SNAKE_CASE					:       str	,	SCREAMING_SNAKE_CASE					:       Optional[Any]						):





  """simple docstring"""



  return (-y * np.log(SCREAMING_SNAKE_CASE						) - (1 - y) * np.log(1 - h						)).mean()
def 					_A	(		SCREAMING_SNAKE_CASE					:       Optional[int]	,	SCREAMING_SNAKE_CASE					:       List[str]	,	SCREAMING_SNAKE_CASE					:       Tuple						):





  """simple docstring"""



  a__     : int  =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					:       Optional[int]	,	SCREAMING_SNAKE_CASE					:       Union[str, Any]	,	SCREAMING_SNAKE_CASE					:       Optional[Any]	,	SCREAMING_SNAKE_CASE					:       str=70_000						):





  """simple docstring"""



  a__     : List[str]  =np.zeros(x.shape[1]						)

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

     UpperCAmelCase     :     str							=	0.1
     UpperCAmelCase     :     Optional[Any]							=	logistic_reg(alpha, x, y, max_iterations=70000)
     print("""theta: """, theta)  # printing the theta i.e our weights vector
     def 					_A	(		SCREAMING_SNAKE_CASE					:       Tuple						):





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

     plt.legend()





     plt.show()













import warnings

from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCAmelCase     :     Dict							=	logging.get_logger(__name__)

UpperCAmelCase     :     Dict							=	{
    """RUCAIBox/mvp""": """https://huggingface.co/RUCAIBox/mvp/resolve/main/config.json""",
}







class     __lowerCAmelCase	(   UpperCamelCase__):
      _lowercase    : List[Any]            =      """mvp"""
      _lowercase    : Tuple            =      ["""past_key_values"""]
      _lowercase    : Dict            =      {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}







      def __init__(   self		,				lowerCAmelCase__=5_0_2_6_7		,				lowerCAmelCase__=1_0_2_4		,				lowerCAmelCase__=1_2		,				lowerCAmelCase__=4_0_9_6		,				lowerCAmelCase__=1_6		,				lowerCAmelCase__=1_2		,				lowerCAmelCase__=4_0_9_6		,				lowerCAmelCase__=1_6		,				lowerCAmelCase__=0.0		,				lowerCAmelCase__=0.0		,				lowerCAmelCase__="gelu"		,				lowerCAmelCase__=1_0_2_4		,				lowerCAmelCase__=0.1		,				lowerCAmelCase__=0.0		,				lowerCAmelCase__=0.0		,				lowerCAmelCase__=0.02		,				lowerCAmelCase__=0.0		,				lowerCAmelCase__=False		,				lowerCAmelCase__=True		,				lowerCAmelCase__=1		,				lowerCAmelCase__=0		,				lowerCAmelCase__=2		,				lowerCAmelCase__=True		,				lowerCAmelCase__=2		,				lowerCAmelCase__=2		,				lowerCAmelCase__=False		,				lowerCAmelCase__=1_0_0		,				lowerCAmelCase__=8_0_0		,				**lowerCAmelCase__		,				)       ->						str:



        '''simple docstring'''




        a__     : Dict  =vocab_size
        a__     : List[str]  =max_position_embeddings
        a__     : List[str]  =d_model
        a__     : Optional[Any]  =encoder_ffn_dim
        a__     : Dict  =encoder_layers
        a__     : List[str]  =encoder_attention_heads
        a__     : List[str]  =decoder_ffn_dim
        a__     : Tuple  =decoder_layers
        a__     : Tuple  =decoder_attention_heads
        a__     : Any  =dropout
        a__     : str  =attention_dropout
        a__     : str  =activation_dropout
        a__     : Optional[int]  =activation_function
        a__     : Union[str, Any]  =init_std
        a__     : Dict  =encoder_layerdrop
        a__     : List[str]  =decoder_layerdrop
        a__     : Union[str, Any]  =classifier_dropout
        a__     : Union[str, Any]  =use_cache
        a__     : Optional[Any]  =encoder_layers
        a__     : List[str]  =scale_embedding  # scale factor will be sqrt(d_model) if True
        a__     : int  =use_prompt
        a__     : Union[str, Any]  =prompt_length
        a__     : Dict  =prompt_mid_dim

        super().__init__(
            pad_token_id=lowerCAmelCase__		,				bos_token_id=lowerCAmelCase__		,				eos_token_id=lowerCAmelCase__		,				is_encoder_decoder=lowerCAmelCase__		,				decoder_start_token_id=lowerCAmelCase__		,				forced_eos_token_id=lowerCAmelCase__		,				**lowerCAmelCase__		,				)

        if self.forced_bos_token_id is None and kwargs.get("force_bos_token_to_be_generated"		,				lowerCAmelCase__							):
          a__     : Tuple  =self.bos_token_id
          warnings.warn(
              F'''Please make sure the config includes `forced_bos_token_id={self.bos_token_id}` in future versions. '''
              "The config can simply be saved and uploaded again to be fixed."							)






