






# 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 _UpperCAmelCase   (	a						:	Optional[int] ):
							return 1 / (1 + np.exp(-z ))

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

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

def _UpperCAmelCase   (	a						:	Optional[Any]  ,					a						:	Optional[int]  ,					a						:	List[Any]  ,					a						:	str=7_0000 ):
							snake_case__	  =       np.zeros(x.shape[1] )

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

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

	def _UpperCAmelCase   (	a						:	Optional[int] ):
								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__))       =     (x[:, 0].min(), x[:, 0].max())
	((a__)  ,		(a__))       =     (x[:, 1].min(), x[:, 1].max())
	((a__)  ,		(a__))       =     np.meshgrid(np.linspace(xa_min, xa_max), np.linspace(xa_min, xa_max))
	a__       =     np.c_[xxa.ravel(), xxa.ravel()]
	a__       =     predict_prob(grid).reshape(xxa.shape)
	plt.contour(xxa, xxa, probs, [0.5], linewidths=1, colors="""black""")

	plt.legend()



	plt.show()








def _UpperCAmelCase   (	a						:	int  ,					a						:	Optional[Any] ):
							# "extended trapezoidal rule"
							# int(f) = dx/2 * (f1 + 2f2 + ... + fn)
							snake_case__	  =       (boundary[1] - boundary[0]) / steps
							snake_case__	  =       boundary[0]
							snake_case__	  =       boundary[1]
							snake_case__	  =       make_points(a  ,					a  ,					a )
							snake_case__	  =       0.0
							y += (h / 2.0) * f(a )
							for i in x_i:
														# print(i)
														y += h * f(a )
							y += (h / 2.0) * f(a )
							return y

def _UpperCAmelCase   (	a						:	Dict  ,					a						:	Union[str, Any]  ,					a						:	Union[str, Any] ):
							snake_case__	  =       a + h
							while x < (b - h):
														yield x
														snake_case__	  =       x + h

def _UpperCAmelCase   (	a						:	Any ):  # enter your function here
							snake_case__	  =       (x - 0) * (x - 0)
							return y

def _UpperCAmelCase   (	):
							snake_case__	  =       0.0  # Lower bound of integration
							snake_case__	  =       1.0  # Upper bound of integration
							snake_case__	  =       10.0  # define number of steps or resolution
							snake_case__	  =       [a, b]  # define boundary of integration
							snake_case__	  =       method_a(a  ,					a )
							print(F'''y = {y}''' )


if __name__ == "__main__":
	main()
