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






def     lowerCAmelCase_			(   _SCREAMING_SNAKE_CASE     :				Optional[Any]		, _SCREAMING_SNAKE_CASE     :				str			):
						return (-y * np.log(_SCREAMING_SNAKE_CASE			) - (1 - y) * np.log(1 - h			)).mean()






def     lowerCAmelCase_			(   _SCREAMING_SNAKE_CASE     :				Union[str, Any]		, _SCREAMING_SNAKE_CASE     :				Any		, _SCREAMING_SNAKE_CASE     :				int			):
						UpperCamelCase_		:							Dict  =		np.dot(_SCREAMING_SNAKE_CASE		, _SCREAMING_SNAKE_CASE			)
						return np.sum(y * scores - np.log(1 + np.exp(_SCREAMING_SNAKE_CASE			)			)			)






def     lowerCAmelCase_			(   _SCREAMING_SNAKE_CASE     :				Optional[Any]		, _SCREAMING_SNAKE_CASE     :				Optional[Any]		, _SCREAMING_SNAKE_CASE     :				Tuple		, _SCREAMING_SNAKE_CASE     :				Tuple=7_0000			):
						UpperCamelCase_		:							Any  =		np.zeros(x.shape[1]			)

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

	SCREAMING_SNAKE_CASE		:     Optional[int]         =	0.1
	SCREAMING_SNAKE_CASE		:     str         =	logistic_reg(alpha, x, y, max_iterations=70000)
	print("theta: ", theta)  # printing the theta i.e our weights vector






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

	plt.legend()







	plt.show()







import numpy as np






def     lowerCAmelCase_			(   _SCREAMING_SNAKE_CASE     :				np.array			):
						return (2 / (1 + np.exp(-2 * vector			))) - 1


if __name__ == "__main__":
	import doctest

	doctest.testmod()






