





# 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__       (       UpperCamelCase__     :    Dict  )     ->	Dict:



					'''simple docstring'''




					return 1 / (1 + np.exp(-z  ))





def   lowerCamelCase__       (       UpperCamelCase__     :    Union[str, Any]    ,      UpperCamelCase__     :    Optional[Any]  )     ->	str:



					'''simple docstring'''




					return (-y * np.log(UpperCamelCase__  ) - (1 - y) * np.log(1 - h  )).mean()





def   lowerCamelCase__       (       UpperCamelCase__     :    Optional[Any]    ,      UpperCamelCase__     :    List[Any]    ,      UpperCamelCase__     :    List[Any]  )     ->	int:



					'''simple docstring'''




					_snake_case													=			np.dot(UpperCamelCase__    ,      UpperCamelCase__  )
					return np.sum(y * scores - np.log(1 + np.exp(UpperCamelCase__  )  )  )





def   lowerCamelCase__       (       UpperCamelCase__     :    List[Any]    ,      UpperCamelCase__     :    int    ,      UpperCamelCase__     :    int    ,      UpperCamelCase__     :    Any=70_000  )     ->	Tuple:



					'''simple docstring'''




					_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__":
					UpperCAmelCase_ 							=       datasets.load_iris()
					UpperCAmelCase_ 							=       iris.data[:, :2]
					UpperCAmelCase_ 							=       (iris.target != 0) * 1

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





					def   lowerCamelCase__       (       UpperCamelCase__     :    Dict  )     ->	Any:



										'''simple docstring'''




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

					plt.legend()




					plt.show()










from collections.abc import Sequence





def   lowerCamelCase__       (       UpperCamelCase__     :    Sequence[float]    ,      UpperCamelCase__     :    bool = False  )     ->	float:



					'''simple docstring'''




					if not arr:
										return 0

					_snake_case													=			0 if allow_empty_subarrays else float('-inf'  )
					_snake_case													=			0.0
					for num in arr:
										_snake_case													=			max(0 if allow_empty_subarrays else num    ,      curr_sum + num  )
										_snake_case													=			max(UpperCamelCase__    ,      UpperCamelCase__  )

					return max_sum


if __name__ == "__main__":
					from doctest import testmod

					testmod()

					UpperCAmelCase_ 							=       [-2, 1, -3, 4, -1, 2, 1, -5, 4]
					print(F"{max_subarray_sum(nums) = }")



