



# 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      UpperCAmelCase__			(				__snake_case				) ->			Union[str, Any]:
  return 1 / (1 + np.exp(-z				))




def      UpperCAmelCase__			(				__snake_case    ,					__snake_case				) ->			List[str]:
  return (-y * np.log(__snake_case				) - (1 - y) * np.log(1 - h				)).mean()




def      UpperCAmelCase__			(				__snake_case    ,					__snake_case    ,					__snake_case				) ->			Optional[int]:
  _A							     =     np.dot(__snake_case    ,					__snake_case				)
  return np.sum(y * scores - np.log(1 + np.exp(__snake_case				)				)				)




def      UpperCAmelCase__			(				__snake_case    ,					__snake_case    ,					__snake_case    ,					__snake_case=70_000				) ->			int:
  _A							     =     np.zeros(x.shape[1]				)

  for iterations in range(__snake_case				):
    _A							     =     np.dot(__snake_case    ,					__snake_case				)
    _A							     =     sigmoid_function(__snake_case				)
    _A							     =     np.dot(x.T    ,					h - y				) / y.size
    _A							     =     theta - alpha * gradient  # updating the weights
    _A							     =     np.dot(__snake_case    ,					__snake_case				)
    _A							     =     sigmoid_function(__snake_case				)
    _A							     =     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__":
    __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_0_0_0_0)
    print("""theta: """, theta)  # printing the theta i.e our weights vector




    def      UpperCAmelCase__			(				__snake_case				) ->			Optional[int]:
      return sigmoid_function(
          np.dot(__snake_case    ,					__snake_case				)				)  # predicting the value of probability from the logistic regression algorithm

    plt.figure(figsize=(1_0, 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 typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_torch_available,
)


UpperCAmelCase_           =				{
    '''configuration_mega''': ['''MEGA_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MegaConfig''', '''MegaOnnxConfig'''],
}

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase_           =				[
      '''MEGA_PRETRAINED_MODEL_ARCHIVE_LIST''',
      '''MegaForCausalLM''',
      '''MegaForMaskedLM''',
      '''MegaForMultipleChoice''',
      '''MegaForQuestionAnswering''',
      '''MegaForSequenceClassification''',
      '''MegaForTokenClassification''',
      '''MegaModel''',
      '''MegaPreTrainedModel''',
  ]

if TYPE_CHECKING:
  from .configuration_mega import MEGA_PRETRAINED_CONFIG_ARCHIVE_MAP, MegaConfig, MegaOnnxConfig

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_mega import (
        MEGA_PRETRAINED_MODEL_ARCHIVE_LIST,
        MegaForCausalLM,
        MegaForMaskedLM,
        MegaForMultipleChoice,
        MegaForQuestionAnswering,
        MegaForSequenceClassification,
        MegaForTokenClassification,
        MegaModel,
        MegaPreTrainedModel,
    )

else:
  import sys

  UpperCAmelCase_           =				_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)