



# 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       (					__lowerCAmelCase      )   ->					Any:


				"""simple docstring"""

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



def 			_lowerCAmelCase       (					__lowerCAmelCase  ,  __lowerCAmelCase      )   ->					List[Any]:


				"""simple docstring"""

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



def 			_lowerCAmelCase       (					__lowerCAmelCase  ,  __lowerCAmelCase  ,  __lowerCAmelCase      )   ->					List[str]:


				"""simple docstring"""

				snake_case__	:      List[Any]     		=	np.dot(__lowerCAmelCase  ,  __lowerCAmelCase      )
				return np.sum(y * scores - np.log(1 + np.exp(__lowerCAmelCase      )      )      )



def 			_lowerCAmelCase       (					__lowerCAmelCase  ,  __lowerCAmelCase  ,  __lowerCAmelCase  ,  __lowerCAmelCase=70000      )   ->					Tuple:


				"""simple docstring"""

				snake_case__	:      Any     		=	np.zeros(x.shape[1]      )

				for iterations in range(__lowerCAmelCase      ):
								snake_case__	:      Union[str, Any]     		=	np.dot(__lowerCAmelCase  ,  __lowerCAmelCase      )
								snake_case__	:      str     		=	sigmoid_function(__lowerCAmelCase      )
								snake_case__	:      List[str]     		=	np.dot(x.T  ,  h - y      ) / y.size
								snake_case__	:      Tuple     		=	theta - alpha * gradient  # updating the weights
								snake_case__	:      Any     		=	np.dot(__lowerCAmelCase  ,  __lowerCAmelCase      )
								snake_case__	:      Dict     		=	sigmoid_function(__lowerCAmelCase      )
								snake_case__	:      Tuple     		=	cost_function(__lowerCAmelCase  ,  __lowerCAmelCase      )
								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_0000)
				print('''theta: ''', theta)  # printing the theta i.e our weights vector



				def 			_lowerCAmelCase       (					__lowerCAmelCase      )   ->					List[Any]:


								"""simple docstring"""

								return sigmoid_function(
								    np.dot(__lowerCAmelCase  ,  __lowerCAmelCase      )      )  # 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''')
				((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()






A__								=						[4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5]
A__								=						[3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5]
A__								=						{
    0: '''Sunday''',
    1: '''Monday''',
    2: '''Tuesday''',
    3: '''Wednesday''',
    4: '''Thursday''',
    5: '''Friday''',
    6: '''Saturday''',
}



def 			_lowerCAmelCase       (					__lowerCAmelCase  ,  __lowerCAmelCase  ,  __lowerCAmelCase      )   ->					str:


				"""simple docstring"""

				assert len(str(__lowerCAmelCase      )      ) > 2, "year should be in YYYY format"
				assert 1 <= month <= 12, "month should be between 1 to 12"
				assert 1 <= day <= 31, "day should be between 1 to 31"

				# Doomsday algorithm:
				snake_case__	:      Optional[int]     		=	year // 100
				snake_case__	:      List[str]     		=	(5 * (century % 4) + 2) % 7
				snake_case__	:      Dict     		=	year % 100
				snake_case__	:      Union[str, Any]     		=	centurian % 12
				snake_case__	:      List[Any]     		=	(
				    (centurian // 12) + centurian_m + (centurian_m // 4) + century_anchor
				) % 7
				snake_case__	:      List[str]     		=	(
				    DOOMSDAY_NOT_LEAP[month - 1]
				    if (year % 4 != 0) or (centurian == 0 and (year % 400) == 0)
				    else DOOMSDAY_LEAP[month - 1]
				)
				snake_case__	:      List[str]     		=	(dooms_day + day - day_anchor) % 7
				return WEEK_DAY_NAMES[week_day]


if __name__ == "__main__":
				import doctest

				doctest.testmod()

