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


def     __lowerCAmelCase			(			a__						,    a__  )       ->  Tuple:
				return (-y * np.log(a__  ) - (1 - y) * np.log(1 - h  )).mean()


def     __lowerCAmelCase			(			a__						,    a__						,    a__  )       ->  List[Any]:
				__a										=    np.dot(a__						,    a__  )
				return np.sum(y * scores - np.log(1 + np.exp(a__  )  )  )


def     __lowerCAmelCase			(			a__						,    a__						,    a__						,    a__=7_0000  )       ->  Tuple:
				__a										=    np.zeros(x.shape[1]  )

				for iterations in range(a__  ):
								__a										=    np.dot(a__						,    a__  )
								__a										=    sigmoid_function(a__  )
								__a										=    np.dot(x.T						,    h - y  ) / y.size
								__a										=    theta - alpha * gradient  # updating the weights
								__a										=    np.dot(a__						,    a__  )
								__a										=    sigmoid_function(a__  )
								__a										=    cost_function(a__						,    a__  )
								if iterations % 100 == 0:
												print(F"""loss: {j} \t"""  )  # printing the loss after every 100 iterations
				return theta


# In[68]:

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

					A     :	Dict    = 0.1
					A     :	str    = logistic_reg(alpha, x, y, max_iterations=7_0_0_0_0)
					print('theta: ', theta)  # printing the theta i.e our weights vector


					def     __lowerCAmelCase			(			a__  )       ->  Union[str, Any]:
									return sigmoid_function(
									    np.dot(a__						,    a__  )  )  # 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')
					((A) ,   (A))     :	Tuple    = (x[:, 0].min(), x[:, 0].max())
					((A) ,   (A))     :	int    = (x[:, 1].min(), x[:, 1].max())
					((A) ,   (A))     :	List[str]    = np.meshgrid(np.linspace(xa_min, xa_max), np.linspace(xa_min, xa_max))
					A     :	int    = np.c_[xxa.ravel(), xxa.ravel()]
					A     :	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 List, Optional, Union

import numpy as np
import PIL.Image

from ...image_processing_utils import BaseImageProcessor, BatchFeature
from ...image_transforms import rescale, resize, to_channel_dimension_format
from ...image_utils import (
    ChannelDimension,
    PILImageResampling,
    get_image_size,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


__lowercase							 =					logging.get_logger(__name__)


class 	lowerCamelCase_     (   UpperCAmelCase_ ):






    '''simple docstring'''






    a__	:						Optional[Any]						=      ["""pixel_values"""]







    def __init__(	self , __lowercase = True , __lowercase = 32 , __lowercase=PILImageResampling.BILINEAR , __lowercase = True , **__lowercase , )       ->	None:
        __UpperCamelCase     :Optional[int]			  =		do_resize
        __UpperCamelCase     :Any			  =		do_rescale
        __UpperCamelCase     :str			  =		size_divisor
        __UpperCamelCase     :Dict			  =		resample
        super().__init__(**__lowercase)







    def 			UpperCamelCase__   (	self , __lowercase , __lowercase , __lowercase , __lowercase = None , **__lowercase)       ->	np.ndarray:
        __UpperCamelCase  ,       __UpperCamelCase     :int			  =		get_image_size(__lowercase)
        # Rounds the height and width down to the closest multiple of size_divisor
        __UpperCamelCase     :List[Any]			  =		height // size_divisor * size_divisor
        __UpperCamelCase     :List[str]			  =		width // size_divisor * size_divisor
        __UpperCamelCase     :str			  =		resize(__lowercase , (new_h, new_w) , resample=__lowercase , data_format=__lowercase , **__lowercase)
        return image







    def 			UpperCamelCase__   (	self , __lowercase , __lowercase , __lowercase = None , **__lowercase)       ->	np.ndarray:
        return rescale(image=__lowercase , scale=__lowercase , data_format=__lowercase , **__lowercase)







    def 			UpperCamelCase__   (	self , __lowercase , __lowercase = None , __lowercase = None , __lowercase=None , __lowercase = None , __lowercase = None , __lowercase = ChannelDimension.FIRST , **__lowercase , )       ->	BatchFeature:
        __UpperCamelCase     :Union[str, Any]			  =		do_resize if do_resize is not None else self.do_resize
        __UpperCamelCase     :Tuple			  =		do_rescale if do_rescale is not None else self.do_rescale
        __UpperCamelCase     :List[str]			  =		size_divisor if size_divisor is not None else self.size_divisor
        __UpperCamelCase     :List[Any]			  =		resample if resample is not None else self.resample

        if do_resize and size_divisor is None:
            raise ValueError('''size_divisor is required for resizing''')

        __UpperCamelCase     :List[Any]			  =		make_list_of_images(__lowercase)

        if not valid_images(__lowercase):
            raise ValueError('''Invalid image(s)''')

        # All transformations expect numpy arrays.
        __UpperCamelCase     :Optional[Any]			  =		[to_numpy_array(__lowercase) for img in images]

        if do_resize:
            __UpperCamelCase     :List[str]			  =		[self.resize(__lowercase , size_divisor=__lowercase , resample=__lowercase) for image in images]

        if do_rescale:
            __UpperCamelCase     :Dict			  =		[self.rescale(__lowercase , scale=1 / 255) for image in images]

        __UpperCamelCase     :str			  =		[to_channel_dimension_format(__lowercase , __lowercase) for image in images]

        __UpperCamelCase     :int			  =		{'''pixel_values''': images}
        return BatchFeature(data=__lowercase , tensor_type=__lowercase)





