






# 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 _a (  SCREAMING_SNAKE_CASE__      :  List[Any]       )	->    List[Any]:

							'''simple docstring'''






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




def _a (  SCREAMING_SNAKE_CASE__      :  List[str]			,				SCREAMING_SNAKE_CASE__      :  Optional[Any]       )	->    Optional[int]:

							'''simple docstring'''






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




def _a (  SCREAMING_SNAKE_CASE__      :  int			,				SCREAMING_SNAKE_CASE__      :  List[str]			,				SCREAMING_SNAKE_CASE__      :  Any       )	->    List[str]:

							'''simple docstring'''






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




def _a (  SCREAMING_SNAKE_CASE__      :  Any			,				SCREAMING_SNAKE_CASE__      :  Union[str, Any]			,				SCREAMING_SNAKE_CASE__      :  Any			,				SCREAMING_SNAKE_CASE__      :  int=7_00_00       )	->    Tuple:

							'''simple docstring'''






							SCREAMING_SNAKE_CASE__       :      Dict     =						np.zeros(x.shape[1]       )

							for iterations in range(snake_case__       ):
														SCREAMING_SNAKE_CASE__       :      Optional[Any]     =						np.dot(snake_case__			,				snake_case__       )
														SCREAMING_SNAKE_CASE__       :      int     =						sigmoid_function(snake_case__       )
														SCREAMING_SNAKE_CASE__       :      str     =						np.dot(x.T			,				h - y       ) / y.size
														SCREAMING_SNAKE_CASE__       :      Optional[Any]     =						theta - alpha * gradient  # updating the weights
														SCREAMING_SNAKE_CASE__       :      List[Any]     =						np.dot(snake_case__			,				snake_case__       )
														SCREAMING_SNAKE_CASE__       :      Tuple     =						sigmoid_function(snake_case__       )
														SCREAMING_SNAKE_CASE__       :      int     =						cost_function(snake_case__			,				snake_case__       )
														if iterations % 1_00 == 0:
																					print(f'''loss: {j} \t'''       )  # printing the loss after every 100 iterations
							return theta


# In[68]:

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

		_lowerCamelCase :    Optional[Any]  							= 0.1
		_lowerCamelCase :    Dict  							= logistic_reg(alpha, x, y, max_iterations=7_0_0_0_0)
		print('''theta: ''', theta)  # printing the theta i.e our weights vector




		def _a (  SCREAMING_SNAKE_CASE__      :  List[Any]       )	->    Union[str, Any]:

									'''simple docstring'''






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

		plt.legend()







		plt.show()













from queue import PriorityQueue
from typing import Any

import numpy as np




def _a (  SCREAMING_SNAKE_CASE__      :  dict			,				SCREAMING_SNAKE_CASE__      :  str			,				SCREAMING_SNAKE_CASE__      :  set			,				SCREAMING_SNAKE_CASE__      :  set			,				SCREAMING_SNAKE_CASE__      :  dict			,				SCREAMING_SNAKE_CASE__      :  dict			,				SCREAMING_SNAKE_CASE__      :  PriorityQueue			,				SCREAMING_SNAKE_CASE__      :  dict			,				SCREAMING_SNAKE_CASE__      :  float | int			,				)	->    float | int:

							'''simple docstring'''






							for nxt, d in graph[v]:
														if nxt in visited_forward:
																					continue
														SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						cst_fwd.get(SCREAMING_SNAKE_CASE__			,				np.inf       )
														SCREAMING_SNAKE_CASE__       :      Optional[int]     =						cst_fwd[v] + d
														if new_cost_f < old_cost_f:
																					queue.put((new_cost_f, nxt)       )
																					SCREAMING_SNAKE_CASE__       :      List[Any]     =						new_cost_f
																					SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						v
														if nxt in visited_backward:
																					if cst_fwd[v] + d + cst_bwd[nxt] < shortest_distance:
																												SCREAMING_SNAKE_CASE__       :      Optional[int]     =						cst_fwd[v] + d + cst_bwd[nxt]
							return shortest_distance




def _a (  SCREAMING_SNAKE_CASE__      :  str			,				SCREAMING_SNAKE_CASE__      :  str			,				SCREAMING_SNAKE_CASE__      :  dict			,				SCREAMING_SNAKE_CASE__      :  dict       )	->    int:

							'''simple docstring'''






							SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						-1

							SCREAMING_SNAKE_CASE__       :      List[str]     =						set()
							SCREAMING_SNAKE_CASE__       :      List[Any]     =						set()
							SCREAMING_SNAKE_CASE__       :      int     =						{source: 0}
							SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						{destination: 0}
							SCREAMING_SNAKE_CASE__       :      List[Any]     =						{source: None}
							SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						{destination: None}
							SCREAMING_SNAKE_CASE__       :      PriorityQueue[Any]     =						PriorityQueue()
							SCREAMING_SNAKE_CASE__       :      PriorityQueue[Any]     =						PriorityQueue()

							SCREAMING_SNAKE_CASE__       :      Dict     =						np.inf

							queue_forward.put((0, source)       )
							queue_backward.put((0, destination)       )

							if source == destination:
														return 0

							while not queue_forward.empty() and not queue_backward.empty():
														SCREAMING_SNAKE_CASE__   ,SCREAMING_SNAKE_CASE__       :      List[str]     =						queue_forward.get()
														visited_forward.add(SCREAMING_SNAKE_CASE__       )

														SCREAMING_SNAKE_CASE__   ,SCREAMING_SNAKE_CASE__       :      List[Any]     =						queue_backward.get()
														visited_backward.add(SCREAMING_SNAKE_CASE__       )

														SCREAMING_SNAKE_CASE__       :      Any     =						pass_and_relaxation(
														    SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__			,				)

														SCREAMING_SNAKE_CASE__       :      List[Any]     =						pass_and_relaxation(
														    SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__			,				)

														if cst_fwd[v_fwd] + cst_bwd[v_bwd] >= shortest_distance:
																					break

							if shortest_distance != np.inf:
														SCREAMING_SNAKE_CASE__       :      int     =						shortest_distance
							return shortest_path_distance


_lowerCamelCase :    Optional[Any]  							= {
    '''B''': [['''C''', 1]],
    '''C''': [['''D''', 1]],
    '''D''': [['''F''', 1]],
    '''E''': [['''B''', 1], ['''G''', 2]],
    '''F''': [],
    '''G''': [['''F''', 1]],
}
_lowerCamelCase :    Tuple  							= {
    '''B''': [['''E''', 1]],
    '''C''': [['''B''', 1]],
    '''D''': [['''C''', 1]],
    '''F''': [['''D''', 1], ['''G''', 1]],
    '''E''': [[None, np.inf]],
    '''G''': [['''E''', 2]],
}

if __name__ == "__main__":
		import doctest

		doctest.testmod()






