


"""simple docstring"""






from manim import *

class lowerCamelCase		(UpperCamelCase_      ):






					'''simple docstring'''


					def 			lowerCAmelCase_					(						self			:				List[str]		)							->       Any:
									SCREAMING_SNAKE_CASE__		  =				Rectangle(height=0.5   , width=0.5		)
									SCREAMING_SNAKE_CASE__		  =				Rectangle(height=0.46   , width=0.46		).set_stroke(width=0		)

									SCREAMING_SNAKE_CASE__		  =				[mem.copy() for i in range(6		)]
									SCREAMING_SNAKE_CASE__		  =				[mem.copy() for i in range(6		)]
									SCREAMING_SNAKE_CASE__		  =				VGroup(*_a		).arrange(_a   , buff=0		)
									SCREAMING_SNAKE_CASE__		  =				VGroup(*_a		).arrange(_a   , buff=0		)
									SCREAMING_SNAKE_CASE__		  =				VGroup(_a   , _a		).arrange(_a   , buff=0		)
									SCREAMING_SNAKE_CASE__		  =				Text("CPU"   , font_size=24		)
									SCREAMING_SNAKE_CASE__		  =				Group(_a   , _a		).arrange(_a   , buff=0.5   , aligned_edge=_a		)
									cpu.move_to([-2.5, -0.5, 0]		)
									self.add(_a		)

									SCREAMING_SNAKE_CASE__		  =				[mem.copy() for i in range(1		)]
									SCREAMING_SNAKE_CASE__		  =				VGroup(*_a		).arrange(_a   , buff=0		)
									SCREAMING_SNAKE_CASE__		  =				Text("GPU"   , font_size=24		)
									SCREAMING_SNAKE_CASE__		  =				Group(_a   , _a		).arrange(_a   , buff=0.5   , aligned_edge=_a		)
									gpu.align_to(_a   , _a		)
									gpu.set_x(gpu.get_x() - 1		)

									self.add(_a		)

									SCREAMING_SNAKE_CASE__		  =				[mem.copy() for i in range(6		)]
									SCREAMING_SNAKE_CASE__		  =				VGroup(*_a		).arrange(_a   , buff=0		)

									SCREAMING_SNAKE_CASE__		  =				Text("Model"   , font_size=24		)
									SCREAMING_SNAKE_CASE__		  =				Group(_a   , _a		).arrange(_a   , buff=0.5   , aligned_edge=_a		)
									model.move_to([3, -1.0, 0]		)

									self.play(
									    Create(_a   , run_time=1		)   , Create(_a   , run_time=1		)   , Create(_a   , run_time=1		)   , )

									SCREAMING_SNAKE_CASE__		  =				MarkupText(
									    F"""First, an empty model skeleton is loaded\ninto <span fgcolor=\'{YELLOW}\'>memory</span> without using much RAM."""   , font_size=24   , )

									SCREAMING_SNAKE_CASE__		  =				Square(side_length=2.2		)
									key.move_to([-5, 2, 0]		)

									SCREAMING_SNAKE_CASE__		  =				MarkupText(
									    F"""<b>Key:</b>\n\n<span fgcolor=\'{YELLOW}\'>●</span> Empty Model"""   , font_size=18   , )

									key_text.move_to([-5, 2.4, 0]		)

									step_a.move_to([2, 2, 0]		)
									self.play(Write(_a   , run_time=2.5		)   , Write(_a		)   , Write(_a		)		)

									self.add(_a		)

									SCREAMING_SNAKE_CASE__		  =				[]
									SCREAMING_SNAKE_CASE__		  =				[]
									SCREAMING_SNAKE_CASE__		  =				[]
									for i, rect in enumerate(_a		):
													SCREAMING_SNAKE_CASE__		  =				Rectangle(height=0.46   , width=0.46		).set_stroke(width=0.0		).set_fill(_a   , opacity=0.7		)
													cpu_target.move_to(_a		)
													cpu_target.generate_target()
													SCREAMING_SNAKE_CASE__		  =				0.46 / 4
													SCREAMING_SNAKE_CASE__		  =				0.46 / 3

													if i == 0:
																	cpu_target.target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT		)   , buff=0.02   , direction=_a		)
																	cpu_target.target.set_x(cpu_target.target.get_x() + 0.1		)
													elif i == 3:
																	cpu_target.target.next_to(cpu_targs[0].target   , direction=_a   , buff=0.0		)
													else:
																	cpu_target.target.next_to(cpu_targs[i - 1].target   , direction=_a   , buff=0.0		)
													cpu_targs.append(_a		)

													first_animations.append(rect.animate(run_time=0.5		).set_stroke(_a		)		)
													second_animations.append(MoveToTarget(_a   , run_time=1.5		)		)

									self.play(*_a		)
									self.play(*_a		)

									self.wait()





"""simple docstring"""






# 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 						SCREAMING_SNAKE_CASE					(  __UpperCAmelCase       )							->   List[str]:
				return 1 / (1 + np.exp(-z       ))




def 						SCREAMING_SNAKE_CASE					(  __UpperCAmelCase  ,					__UpperCAmelCase       )							->   Dict:
				return (-y * np.log(__UpperCAmelCase       ) - (1 - y) * np.log(1 - h       )).mean()




def 						SCREAMING_SNAKE_CASE					(  __UpperCAmelCase  ,					__UpperCAmelCase  ,					__UpperCAmelCase       )							->   Any:
				SCREAMING_SNAKE_CASE__		  =				np.dot(__UpperCAmelCase  ,					__UpperCAmelCase       )
				return np.sum(y * scores - np.log(1 + np.exp(__UpperCAmelCase       )       )       )




def 						SCREAMING_SNAKE_CASE					(  __UpperCAmelCase  ,					__UpperCAmelCase  ,					__UpperCAmelCase  ,					__UpperCAmelCase=70_000       )							->   Optional[Any]:
				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 % 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_0_0_0_0)
						print('theta: ', theta)  # printing the theta i.e our weights vector




						def 						SCREAMING_SNAKE_CASE					(  __UpperCAmelCase       )							->   Union[str, Any]:
										return sigmoid_function(
										    np.dot(__UpperCAmelCase  ,					__UpperCAmelCase       )       )  # 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')
						((_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()




