





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







def    A				(							UpperCAmelCase  ,  UpperCAmelCase				):
					return (-y * np.log(UpperCAmelCase				) - (1 - y) * np.log(1 - h				)).mean()







def    A				(							UpperCAmelCase  ,  UpperCAmelCase  ,  UpperCAmelCase				):
					_snake_case       :					int				       =   np.dot(UpperCAmelCase  ,  UpperCAmelCase				)
					return np.sum(y * scores - np.log(1 + np.exp(UpperCAmelCase				)				)				)







def    A				(							UpperCAmelCase  ,  UpperCAmelCase  ,  UpperCAmelCase  ,  UpperCAmelCase=70_000				):
					_snake_case       :					int				       =   np.zeros(x.shape[1]				)

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


# In[68]:

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

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







							def    A				(							UpperCAmelCase				):
												return sigmoid_function(
												    np.dot(UpperCAmelCase  ,  UpperCAmelCase				)				)  # 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)   ,   (__lowerCAmelCase)) :int     =    (x[:, 0].min(), x[:, 0].max())
							((__lowerCAmelCase)   ,   (__lowerCAmelCase)) :List[str]     =    (x[:, 1].min(), x[:, 1].max())
							((__lowerCAmelCase)   ,   (__lowerCAmelCase)) :Union[str, Any]     =    np.meshgrid(np.linspace(xa_min, xa_max), np.linspace(xa_min, xa_max))
							__lowerCAmelCase :List[str]     =    np.c_[xxa.ravel(), xxa.ravel()]
							__lowerCAmelCase :Optional[Any]     =    predict_prob(grid).reshape(xxa.shape)
							plt.contour(xxa, xxa, probs, [0.5], linewidths=1, colors='black')

							plt.legend()

							plt.show()






from __future__ import annotations

import typing
from collections.abc import Iterable

import numpy as np

__lowerCAmelCase :Union[str, Any]     =    typing.Union[Iterable[float], Iterable[int], np.ndarray]  # noqa: UP007
__lowerCAmelCase :Optional[Any]     =    typing.Union[np.floataa, int, float]  # noqa: UP007







def    A				(							UpperCAmelCase  ,  UpperCAmelCase				):
					return np.sqrt(np.sum((np.asarray(UpperCAmelCase				) - np.asarray(UpperCAmelCase				)) ** 2				)				)







def    A				(							UpperCAmelCase  ,  UpperCAmelCase				):
					return sum((va - va) ** 2 for va, va in zip(UpperCAmelCase  ,  UpperCAmelCase				)				) ** (1 / 2)


if __name__ == "__main__":







							def    A				(							):
												from timeit import timeit

												print("Without Numpy"				)
												print(
												    timeit(
												        "euclidean_distance_no_np([1, 2, 3], [4, 5, 6])"  ,  number=10_000  ,  globals=globals()  ,  )				)
												print("With Numpy"				)
												print(
												    timeit(
												        "euclidean_distance([1, 2, 3], [4, 5, 6])"  ,  number=10_000  ,  globals=globals()  ,  )				)

							benchmark()