

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



def 			_A	(      SCREAMING_SNAKE_CASE	,SCREAMING_SNAKE_CASE      ):
	return (-y * np.log(__a      ) - (1 - y) * np.log(1 - h      )).mean()



def 			_A	(      SCREAMING_SNAKE_CASE	,SCREAMING_SNAKE_CASE	,SCREAMING_SNAKE_CASE      ):
	UpperCAmelCase__:	List[str]									= np.dot(__a	,__a      )
	return np.sum(y * scores - np.log(1 + np.exp(__a      )      )      )



def 			_A	(      SCREAMING_SNAKE_CASE	,SCREAMING_SNAKE_CASE	,SCREAMING_SNAKE_CASE	,SCREAMING_SNAKE_CASE=7_0_0_0_0      ):
	UpperCAmelCase__:	Union[str, Any]									= np.zeros(x.shape[1]      )

	for iterations in range(__a      ):
		UpperCAmelCase__:	str									= np.dot(__a	,__a      )
		UpperCAmelCase__:	int									= sigmoid_function(__a      )
		UpperCAmelCase__:	str									= np.dot(x.T	,h - y      ) / y.size
		UpperCAmelCase__:	Tuple									= theta - alpha * gradient  # updating the weights
		UpperCAmelCase__:	Tuple									= np.dot(__a	,__a      )
		UpperCAmelCase__:	Optional[int]									= sigmoid_function(__a      )
		UpperCAmelCase__:	List[Any]									= cost_function(__a	,__a      )
		if iterations % 1_0_0 == 0:
			print(f"loss: {j} \t"      )  # printing the loss after every 100 iterations
	return theta


# In[68]:

if __name__ == "__main__":
					_lowerCAmelCase  :      Tuple			 =datasets.load_iris()
					_lowerCAmelCase  :      int			 =iris.data[:, :2]
					_lowerCAmelCase  :      Tuple			 =(iris.target != 0) * 1

					_lowerCAmelCase  :      Union[str, Any]			 =0.1
					_lowerCAmelCase  :      str			 =logistic_reg(alpha, x, y, max_iterations=7_00_00)
					print("""theta: """, theta)  # printing the theta i.e our weights vector



					def 			_A	(      SCREAMING_SNAKE_CASE      ):
						return sigmoid_function(
						    np.dot(__a	,__a      )      )  # 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)  :      List[Any]			 =(x[:, 0].min(), x[:, 0].max())
					(_lowerCAmelCase)  :      Union[str, Any]			 =(x[:, 1].min(), x[:, 1].max())
					(_lowerCAmelCase)  :      Union[str, Any]			 =np.meshgrid(np.linspace(xa_min, xa_max), np.linspace(xa_min, xa_max))
					_lowerCAmelCase  :      Optional[Any]			 =np.c_[xxa.ravel(), xxa.ravel()]
					_lowerCAmelCase  :      Union[str, Any]			 =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, is_vision_available


A       :   Optional[int]					= {
    '''configuration_pix2struct''': [
        '''PIX2STRUCT_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''Pix2StructConfig''',
        '''Pix2StructTextConfig''',
        '''Pix2StructVisionConfig''',
    ],
    '''processing_pix2struct''': ['''Pix2StructProcessor'''],
}

try:
					if not is_vision_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					A       :   Optional[int]					= ['''Pix2StructImageProcessor''']


try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					A       :   Dict					= [
					    '''PIX2STRUCT_PRETRAINED_MODEL_ARCHIVE_LIST''',
					    '''Pix2StructPreTrainedModel''',
					    '''Pix2StructForConditionalGeneration''',
					    '''Pix2StructVisionModel''',
					    '''Pix2StructTextModel''',
					]

if TYPE_CHECKING:
					from .configuration_pixastruct import (
					    PIX2STRUCT_PRETRAINED_CONFIG_ARCHIVE_MAP,
					    PixaStructConfig,
					    PixaStructTextConfig,
					    PixaStructVisionConfig,
					)
					from .processing_pixastruct import PixaStructProcessor

					try:
										if not is_vision_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .image_processing_pixastruct import PixaStructImageProcessor

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_pixastruct import (
										    PIX2STRUCT_PRETRAINED_MODEL_ARCHIVE_LIST,
										    PixaStructForConditionalGeneration,
										    PixaStructPreTrainedModel,
										    PixaStructTextModel,
										    PixaStructVisionModel,
										)

else:
					import sys

					A       :   Any					= _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)



