




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







def 				lowerCAmelCase_	(     snake_case_,snake_case_     ):
      return (-y * np.log(snake_case_     ) - (1 - y) * np.log(1 - h     )).mean()







def 				lowerCAmelCase_	(     snake_case_,snake_case_,snake_case_     ):
      _A							:     Optional[Any]			=						np.dot(snake_case_,snake_case_     )
      return np.sum(y * scores - np.log(1 + np.exp(snake_case_     )     )     )







def 				lowerCAmelCase_	(     snake_case_,snake_case_,snake_case_,snake_case_=70000     ):
      _A							:     Optional[Any]			=						np.zeros(x.shape[1]     )

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

  _snake_case										=       0.1
  _snake_case										=       logistic_reg(alpha, x, y, max_iterations=70000)
  print("theta: ", theta)  # printing the theta i.e our weights vector







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

  plt.legend()







  plt.show()







import multiprocessing
from typing import TYPE_CHECKING, Optional, Union

from .. import Dataset, Features, config
from ..formatting import query_table
from ..packaged_modules.sql.sql import Sql
from ..utils import logging
from .abc import AbstractDatasetInputStream


if TYPE_CHECKING:
  import sqlitea

  import sqlalchemy







class 						lowercase							(   UpperCamelCase__		):

     def __init__(  self		,     _a		,     _a		,     _a = None		,     _a = None		,     _a = False		,     **_a		,     )						->							int:
           super().__init__(features=_a		,     cache_dir=_a		,     keep_in_memory=_a		,     **_a				)
           _A							:     Optional[int]			=						Sql(
               cache_dir=_a		,     features=_a		,     sql=_a		,     con=_a		,     **_a		,     )



     def 					a__	(  self				)						->							Optional[Any]:
           _A							:     Tuple			=						None
           _A							:     int			=						None
           _A							:     Tuple			=						None
           _A							:     Union[str, Any]			=						None

           self.builder.download_and_prepare(
               download_config=_a		,     download_mode=_a		,     verification_mode=_a		,     base_path=_a		,     )

           # Build dataset for splits
           _A							:     int			=						self.builder.as_dataset(
               split="""train"""		,     verification_mode=_a		,     in_memory=self.keep_in_memory				)
           return dataset







class 						lowercase							:

     def __init__(  self		,     _a		,     _a		,     _a		,     _a = None		,     _a = None		,     **_a		,     )						->							Union[str, Any]:
           if num_proc is not None and num_proc <= 0:
                 raise ValueError(F'''num_proc {num_proc} must be an integer > 0.'''				)

           _A							:     Dict			=						dataset
           _A							:     int			=						name
           _A							:     Union[str, Any]			=						con
           _A							:     str			=						batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE
           _A							:     str			=						num_proc
           _A							:     Optional[Any]			=						to_sql_kwargs

     def 					a__	(  self				)						->							int:
           _A							:     Any			=						self.to_sql_kwargs.pop("""sql"""		,     _a				)
           _A							:     List[str]			=						self.to_sql_kwargs.pop("""con"""		,     _a				)
           _A							:     int			=						self.to_sql_kwargs.pop("""index"""		,     _a				)

           _A							:     List[str]			=						self._write(index=_a		,     **self.to_sql_kwargs				)
           return written

     def 					a__	(  self		,     _a				)						->							Optional[int]:
           _A			,   _A			,   _A							:     List[str]			=						args
           _A							:     int			=						{**to_sql_kwargs, """if_exists""": """append"""} if offset > 0 else to_sql_kwargs
           _A							:     str			=						query_table(
               table=self.dataset.data		,     key=slice(_a		,     offset + self.batch_size				)		,     indices=self.dataset._indices		,     )
           _A							:     Tuple			=						batch.to_pandas()
           _A							:     Union[str, Any]			=						df.to_sql(self.name		,     self.con		,     index=_a		,     **_a				)
           return num_rows or len(_a				)



     def 					a__	(  self		,     _a		,     **_a				)						->							int:
           _A							:     Any			=						0

           if self.num_proc is None or self.num_proc == 1:
                 for offset in logging.tqdm(
                     range(0		,     len(self.dataset				)		,     self.batch_size				)		,     unit="""ba"""		,     disable=not logging.is_progress_bar_enabled()		,     desc="""Creating SQL from Arrow format"""		,     ):
                       written += self._batch_sql((offset, index, to_sql_kwargs)				)
           else:
                 _A			,   _A							:     Tuple			=						len(self.dataset				), self.batch_size
                 with multiprocessing.Pool(self.num_proc				) as pool:
                       for num_rows in logging.tqdm(
                           pool.imap(
                               self._batch_sql		,     [(offset, index, to_sql_kwargs) for offset in range(0		,     _a		,     _a				)]		,     )		,     total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size		,     unit="""ba"""		,     disable=not logging.is_progress_bar_enabled()		,     desc="""Creating SQL from Arrow format"""		,     ):
                             written += num_rows

           return written

