





import numpy as np
from matplotlib import pyplot as plt
from sklearn import datasets

def 	lowerCAmelCase__			(      a__:		Optional[int]	)  ->		List[str]:


							'''simple docstring'''





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

def 	lowerCAmelCase__			(      a__:		List[Any]      ,  a__:		Any	)  ->		int:


							'''simple docstring'''





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

def 	lowerCAmelCase__			(      a__:		Dict      ,  a__:		Optional[Any]      ,  a__:		Optional[int]	)  ->		str:


							'''simple docstring'''





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

def 	lowerCAmelCase__			(      a__:		Dict      ,  a__:		List[Any]      ,  a__:		int      ,  a__:		Dict=7_0_0_0_0	)  ->		Dict:


							'''simple docstring'''





							_UpperCAmelCase					=      np.zeros(x.shape[1]	)

							for iterations in range(SCREAMING_SNAKE_CASE_	):
														_UpperCAmelCase					=      np.dot(SCREAMING_SNAKE_CASE_      ,  SCREAMING_SNAKE_CASE_	)
														_UpperCAmelCase					=      sigmoid_function(SCREAMING_SNAKE_CASE_	)
														_UpperCAmelCase					=      np.dot(x.T      ,  h - y	) / y.size
														_UpperCAmelCase					=      theta - alpha * gradient  # updating the weights
														_UpperCAmelCase					=      np.dot(SCREAMING_SNAKE_CASE_      ,  SCREAMING_SNAKE_CASE_	)
														_UpperCAmelCase					=      sigmoid_function(SCREAMING_SNAKE_CASE_	)
														_UpperCAmelCase					=      cost_function(SCREAMING_SNAKE_CASE_      ,  SCREAMING_SNAKE_CASE_	)
														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__  :Union[str, Any]             =							datasets.load_iris()
		lowerCAmelCase__  :Optional[int]             =							iris.data[:, :2]
		lowerCAmelCase__  :str             =							(iris.target != 0) * 1

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

		def 	lowerCAmelCase__			(      a__:		int	)  ->		Dict:


									'''simple docstring'''





									return sigmoid_function(
									    np.dot(SCREAMING_SNAKE_CASE_      ,  SCREAMING_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__))  :Any             =							(x[:, 1].min(), x[:, 1].max())
		((lowerCAmelCase__)      ,  (lowerCAmelCase__))  :Any             =							np.meshgrid(np.linspace(xa_min, xa_max), np.linspace(xa_min, xa_max))
		lowerCAmelCase__  :Union[str, Any]             =							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__			(      a__:		int	)  ->		None:


							'''simple docstring'''





							_UpperCAmelCase					=      generate_pascal_triangle(a__	)
							for row_idx in range(a__	):
														# Print left spaces
														for _ in range(num_rows - row_idx - 1	):
																					print(end=' '	)
														# Print row values
														for col_idx in range(row_idx + 1	):
																					if col_idx != row_idx:
																												print(triangle[row_idx][col_idx]      ,  end=' '	)
																					else:
																												print(triangle[row_idx][col_idx]      ,  end=''	)
														print()

def 	lowerCAmelCase__			(      a__:		int	)  ->		list[list[int]]:


							'''simple docstring'''





							if not isinstance(a__      ,  a__	):
														raise TypeError('The input value of \'num_rows\' should be \'int\''	)

							if num_rows == 0:
														return []
							elif num_rows < 0:
														raise ValueError(
														    'The input value of \'num_rows\' should be greater than or equal to 0'	)

							_UpperCAmelCase					=      []
							for current_row_idx in range(a__	):
														_UpperCAmelCase					=      populate_current_row(a__      ,  a__	)
														triangle.append(a__	)
							return triangle

def 	lowerCAmelCase__			(      a__:		list[list[int]]      ,  a__:		int	)  ->		list[int]:


							'''simple docstring'''





							_UpperCAmelCase					=      [-1] * (current_row_idx + 1)
							# first and last elements of current row are equal to 1
							_UpperCAmelCase		,	_UpperCAmelCase					=      1, 1
							for current_col_idx in range(1      ,  a__	):
														calculate_current_element(
														    a__      ,  a__      ,  a__      ,  a__	)
							return current_row

def 	lowerCAmelCase__			(      a__:		list[list[int]]      ,  a__:		list[int]      ,  a__:		int      ,  a__:		int      ,  )  ->		None:


							'''simple docstring'''





							_UpperCAmelCase					=      triangle[current_row_idx - 1][current_col_idx - 1]
							_UpperCAmelCase					=      triangle[current_row_idx - 1][current_col_idx]
							_UpperCAmelCase					=      above_to_left_elt + above_to_right_elt

def 	lowerCAmelCase__			(      a__:		int	)  ->		list[list[int]]:


							'''simple docstring'''





							if not isinstance(a__      ,  a__	):
														raise TypeError('The input value of \'num_rows\' should be \'int\''	)

							if num_rows == 0:
														return []
							elif num_rows < 0:
														raise ValueError(
														    'The input value of \'num_rows\' should be greater than or equal to 0'	)

							_UpperCAmelCase					=      [[1]]

							for row_index in range(1      ,  a__	):
														_UpperCAmelCase					=      [0] + result[-1] + [0]
														_UpperCAmelCase					=      row_index + 1
														# Calculate the number of distinct elements in a row
														_UpperCAmelCase					=      sum(divmod(a__      ,  2	)	)
														_UpperCAmelCase					=      [
														    temp_row[i - 1] + temp_row[i] for i in range(1      ,  distinct_elements + 1	)
														]
														_UpperCAmelCase					=      row_first_half[: (row_index + 1) // 2]
														row_second_half.reverse()
														_UpperCAmelCase					=      row_first_half + row_second_half
														result.append(a__	)

							return result

def 	lowerCAmelCase__			(      )  ->		None:


							'''simple docstring'''





							from collections.abc import Callable
							from timeit import timeit

							def benchmark_a_function(a__:		Callable      ,  a__:		int	) -> None:
														_UpperCAmelCase					=      F'''{func.__name__}({value})'''
														_UpperCAmelCase					=      timeit(F'''__main__.{call}'''      ,  setup='import __main__'	)
														# print(f"{call:38} = {func(value)} -- {timing:.4f} seconds")
														print(F'''{call:38} -- {timing:.4f} seconds'''	)

							for value in range(1_5	):  # (1, 7, 14):
														for func in (generate_pascal_triangle, generate_pascal_triangle_optimized):
																					benchmark_a_function(a__      ,  a__	)
														print()


if __name__ == "__main__":
		import doctest

		doctest.testmod()
		benchmark()
