



'''simple docstring'''

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






def 				a  (     UpperCamelCase_    :    List[str]      ,					UpperCamelCase_    :    Any			)       ->	int:
		return (-y * np.log(UpperCamelCase_			) - (1 - y) * np.log(1 - h			)).mean()






def 				a  (     UpperCamelCase_    :    List[Any]      ,					UpperCamelCase_    :    Optional[Any]      ,					UpperCamelCase_    :    Optional[int]			)       ->	Tuple:
		snake_case__	     =np.dot(UpperCamelCase_      ,					UpperCamelCase_			)
		return np.sum(y * scores - np.log(1 + np.exp(UpperCamelCase_			)			)			)






def 				a  (     UpperCamelCase_    :    List[str]      ,					UpperCamelCase_    :    Any      ,					UpperCamelCase_    :    Optional[int]      ,					UpperCamelCase_    :    Tuple=70000			)       ->	Any:
		snake_case__	     =np.zeros(x.shape[1]			)

		for iterations in range(UpperCamelCase_			):
				snake_case__	     =np.dot(UpperCamelCase_      ,					UpperCamelCase_			)
				snake_case__	     =sigmoid_function(UpperCamelCase_			)
				snake_case__	     =np.dot(x.T      ,					h - y			) / y.size
				snake_case__	     =theta - alpha * gradient  # updating the weights
				snake_case__	     =np.dot(UpperCamelCase_      ,					UpperCamelCase_			)
				snake_case__	     =sigmoid_function(UpperCamelCase_			)
				snake_case__	     =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__":
			SCREAMING_SNAKE_CASE__     :						Union[str, Any]	=  datasets.load_iris()
			SCREAMING_SNAKE_CASE__     :						Dict	=  iris.data[:, :2]
			SCREAMING_SNAKE_CASE__     :						Optional[Any]	=  (iris.target != 0) * 1

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






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

			plt.legend()


			plt.show()









'''simple docstring'''

from timeit import timeit






def 				a  (     UpperCamelCase_    :    int			)       ->	int:
		if number < 0:
				raise ValueError('the value of input must not be negative'			)
		snake_case__	     =0
		while number:
				number &= number - 1
				result += 1
		return result






def 				a  (     UpperCamelCase_    :    int			)       ->	int:
		if number < 0:
				raise ValueError('the value of input must not be negative'			)
		snake_case__	     =0
		while number:
				if number % 2 == 1:
						result += 1
				number >>= 1
		return result






def 				a  (     )       ->	None:

		def do_benchmark(UpperCamelCase_    :    int			) -> None:
				snake_case__	     ='import __main__ as z'
				print(f"""Benchmark when {number = }:"""			)
				print(f"""{get_set_bits_count_using_modulo_operator(UpperCamelCase_			) = }"""			)
				snake_case__	     =timeit('z.get_set_bits_count_using_modulo_operator(25)'      ,					setup=UpperCamelCase_			)
				print(f"""timeit() runs in {timing} seconds"""			)
				print(f"""{get_set_bits_count_using_brian_kernighans_algorithm(UpperCamelCase_			) = }"""			)
				snake_case__	     =timeit(
				    'z.get_set_bits_count_using_brian_kernighans_algorithm(25)'      ,					setup=UpperCamelCase_      ,					)
				print(f"""timeit() runs in {timing} seconds"""			)

		for number in (25, 37, 58, 0):
				do_benchmark(UpperCamelCase_			)
				print()


if __name__ == "__main__":
			import doctest

			doctest.testmod()
			benchmark()




