# 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__       :							Union[str, Any]					):
					return 1 / (1 + np.exp(-z					))





def 		__lowerCamelCase							(UpperCAmelCase__       :							Any ,	UpperCAmelCase__       :							Dict					):
					return (-y * np.log(UpperCAmelCase__					) - (1 - y) * np.log(1 - h					)).mean()





def 		__lowerCamelCase							(UpperCAmelCase__       :							List[Any] ,	UpperCAmelCase__       :							int ,	UpperCAmelCase__       :							List[Any]					):
					SCREAMING_SNAKE_CASE           =    np.dot(UpperCAmelCase__ ,	UpperCAmelCase__					)
					return np.sum(y * scores - np.log(1 + np.exp(UpperCAmelCase__					)					)					)





def 		__lowerCamelCase							(UpperCAmelCase__       :							Optional[Any] ,	UpperCAmelCase__       :							str ,	UpperCAmelCase__       :							Union[str, Any] ,	UpperCAmelCase__       :							Tuple=7_0_0_0_0					):
					SCREAMING_SNAKE_CASE           =    np.zeros(x.shape[1]					)

					for iterations in range(UpperCAmelCase__					):
										SCREAMING_SNAKE_CASE           =    np.dot(UpperCAmelCase__ ,	UpperCAmelCase__					)
										SCREAMING_SNAKE_CASE           =    sigmoid_function(UpperCAmelCase__					)
										SCREAMING_SNAKE_CASE           =    np.dot(x.T ,	h - y					) / y.size
										SCREAMING_SNAKE_CASE           =    theta - alpha * gradient  # updating the weights
										SCREAMING_SNAKE_CASE           =    np.dot(UpperCAmelCase__ ,	UpperCAmelCase__					)
										SCREAMING_SNAKE_CASE           =    sigmoid_function(UpperCAmelCase__					)
										SCREAMING_SNAKE_CASE           =    cost_function(UpperCAmelCase__ ,	UpperCAmelCase__					)
										if iterations % 1_0_0 == 0:
															print(F"loss: {j} \t"					)  # printing the loss after every 100 iterations
					return theta


# In[68]:

if __name__ == "__main__":
					_lowerCamelCase   :				Optional[Any]     			=						datasets.load_iris()
					_lowerCamelCase   :				Dict     			=						iris.data[:, :2]
					_lowerCamelCase   :				Tuple     			=						(iris.target != 0) * 1

					_lowerCamelCase   :				int     			=						0.1
					_lowerCamelCase   :				List[str]     			=						logistic_reg(alpha, x, y, max_iterations=7_00_00)
					print('''theta: ''', theta)  # printing the theta i.e our weights vector





					def 		__lowerCamelCase							(UpperCAmelCase__       :							Tuple					):
										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''')
					((_lowerCamelCase)			,  (_lowerCamelCase))   :				List[str]     			=						(x[:, 0].min(), x[:, 0].max())
					((_lowerCamelCase)			,  (_lowerCamelCase))   :				Any     			=						(x[:, 1].min(), x[:, 1].max())
					((_lowerCamelCase)			,  (_lowerCamelCase))   :				Tuple     			=						np.meshgrid(np.linspace(xa_min, xa_max), np.linspace(xa_min, xa_max))
					_lowerCamelCase   :				Dict     			=						np.c_[xxa.ravel(), xxa.ravel()]
					_lowerCamelCase   :				int     			=						predict_prob(grid).reshape(xxa.shape)
					plt.contour(xxa, xxa, probs, [0.5], linewidths=1, colors='''black''')

					plt.legend()
					plt.show()





def 		__lowerCamelCase							(UpperCAmelCase__       :							str ,	UpperCAmelCase__       :							str = " "					):
					SCREAMING_SNAKE_CASE           =    []

					SCREAMING_SNAKE_CASE           =    0
					for index, char in enumerate(UpperCAmelCase__					):
										if char == separator:
															split_words.append(string[last_index:index]					)
															SCREAMING_SNAKE_CASE           =    index + 1
										elif index + 1 == len(UpperCAmelCase__					):
															split_words.append(string[last_index : index + 1]					)
					return split_words


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

							testmod()




