






# 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__				:		List[Any]      ):





				'''simple docstring'''







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



def 					_lowerCamelCase(			lowerCAmelCase__				:		Any    ,    lowerCAmelCase__				:		Optional[Any]      ):





				'''simple docstring'''







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



def 					_lowerCamelCase(			lowerCAmelCase__				:		Any    ,    lowerCAmelCase__				:		List[str]    ,    lowerCAmelCase__				:		Union[str, Any]      ):





				'''simple docstring'''







				SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   np.dot(lowerCAmelCase__    ,    lowerCAmelCase__      )
				return np.sum(y * scores - np.log(1 + np.exp(lowerCAmelCase__      )      )      )



def 					_lowerCamelCase(			lowerCAmelCase__				:		str    ,    lowerCAmelCase__				:		List[str]    ,    lowerCAmelCase__				:		List[str]    ,    lowerCAmelCase__				:		Dict=7_0000      ):





				'''simple docstring'''







				SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   np.zeros(x.shape[1]      )

				for iterations in range(lowerCAmelCase__      ):
								SCREAMING_SNAKE_CASE_   :	List[str]													=   np.dot(lowerCAmelCase__    ,    lowerCAmelCase__      )
								SCREAMING_SNAKE_CASE_   :	Dict													=   sigmoid_function(lowerCAmelCase__      )
								SCREAMING_SNAKE_CASE_   :	Any													=   np.dot(x.T    ,    h - y      ) / y.size
								SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   theta - alpha * gradient  # updating the weights
								SCREAMING_SNAKE_CASE_   :	Optional[int]													=   np.dot(lowerCAmelCase__    ,    lowerCAmelCase__      )
								SCREAMING_SNAKE_CASE_   :	Dict													=   sigmoid_function(lowerCAmelCase__      )
								SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   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__				:		int      ):





								'''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()







from tempfile import TemporaryDirectory
from unittest import TestCase
from unittest.mock import MagicMock, patch

from transformers import AutoModel, TFAutoModel
from transformers.onnx import FeaturesManager
from transformers.testing_utils import SMALL_MODEL_IDENTIFIER, require_tf, require_torch


@require_torch
@require_tf
class 							__a							(  __A						):





		'''simple docstring'''



		def   __snake_case			(						self       ):
						SCREAMING_SNAKE_CASE_   :	Any													=   SMALL_MODEL_IDENTIFIER
						SCREAMING_SNAKE_CASE_   :	List[Any]													=   'pt'
						SCREAMING_SNAKE_CASE_   :	Tuple													=   'tf'


		def   __snake_case			(						self ,    UpperCamelCase__       ):
						SCREAMING_SNAKE_CASE_   :	Dict													=   AutoModel.from_pretrained(self.test_model       )
						model_pt.save_pretrained(UpperCamelCase__       )


		def   __snake_case			(						self ,    UpperCamelCase__       ):
						SCREAMING_SNAKE_CASE_   :	int													=   TFAutoModel.from_pretrained(self.test_model ,    from_pt=UpperCamelCase__       )
						model_tf.save_pretrained(UpperCamelCase__       )


		def   __snake_case			(						self       ):
						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   'mock_framework'

						# Framework provided - return whatever the user provides
						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   FeaturesManager.determine_framework(self.test_model ,    UpperCamelCase__       )
						self.assertEqual(UpperCamelCase__ ,    UpperCamelCase__       )

						# Local checkpoint and framework provided - return provided framework
						# PyTorch checkpoint
						with TemporaryDirectory() as local_pt_ckpt:
										self._setup_pt_ckpt(UpperCamelCase__       )
										SCREAMING_SNAKE_CASE_   :	Dict													=   FeaturesManager.determine_framework(UpperCamelCase__ ,    UpperCamelCase__       )
										self.assertEqual(UpperCamelCase__ ,    UpperCamelCase__       )

						# TensorFlow checkpoint
						with TemporaryDirectory() as local_tf_ckpt:
										self._setup_tf_ckpt(UpperCamelCase__       )
										SCREAMING_SNAKE_CASE_   :	List[str]													=   FeaturesManager.determine_framework(UpperCamelCase__ ,    UpperCamelCase__       )
										self.assertEqual(UpperCamelCase__ ,    UpperCamelCase__       )


		def   __snake_case			(						self       ):
						# PyTorch checkpoint
						with TemporaryDirectory() as local_pt_ckpt:
										self._setup_pt_ckpt(UpperCamelCase__       )
										SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   FeaturesManager.determine_framework(UpperCamelCase__       )
										self.assertEqual(UpperCamelCase__ ,    self.framework_pt       )

						# TensorFlow checkpoint
						with TemporaryDirectory() as local_tf_ckpt:
										self._setup_tf_ckpt(UpperCamelCase__       )
										SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   FeaturesManager.determine_framework(UpperCamelCase__       )
										self.assertEqual(UpperCamelCase__ ,    self.framework_tf       )

						# Invalid local checkpoint
						with TemporaryDirectory() as local_invalid_ckpt:
										with self.assertRaises(UpperCamelCase__       ):
														SCREAMING_SNAKE_CASE_   :	Any													=   FeaturesManager.determine_framework(UpperCamelCase__       )






		def   __snake_case			(						self       ):
						SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   MagicMock(return_value=UpperCamelCase__       )
						with patch('transformers.onnx.features.is_tf_available' ,    UpperCamelCase__       ):
										SCREAMING_SNAKE_CASE_   :	str													=   FeaturesManager.determine_framework(self.test_model       )
										self.assertEqual(UpperCamelCase__ ,    self.framework_pt       )

						# PyTorch not in environment -> use TensorFlow
						SCREAMING_SNAKE_CASE_   :	List[str]													=   MagicMock(return_value=UpperCamelCase__       )
						with patch('transformers.onnx.features.is_torch_available' ,    UpperCamelCase__       ):
										SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   FeaturesManager.determine_framework(self.test_model       )
										self.assertEqual(UpperCamelCase__ ,    self.framework_tf       )

						# Both in environment -> use PyTorch
						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   MagicMock(return_value=UpperCamelCase__       )
						SCREAMING_SNAKE_CASE_   :	Tuple													=   MagicMock(return_value=UpperCamelCase__       )
						with patch('transformers.onnx.features.is_tf_available' ,    UpperCamelCase__       ), patch(
						    'transformers.onnx.features.is_torch_available' ,    UpperCamelCase__       ):
										SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   FeaturesManager.determine_framework(self.test_model       )
										self.assertEqual(UpperCamelCase__ ,    self.framework_pt       )

						# Both not in environment -> raise error
						SCREAMING_SNAKE_CASE_   :	Tuple													=   MagicMock(return_value=UpperCamelCase__       )
						SCREAMING_SNAKE_CASE_   :	Any													=   MagicMock(return_value=UpperCamelCase__       )
						with patch('transformers.onnx.features.is_tf_available' ,    UpperCamelCase__       ), patch(
						    'transformers.onnx.features.is_torch_available' ,    UpperCamelCase__       ):
										with self.assertRaises(UpperCamelCase__       ):
														SCREAMING_SNAKE_CASE_   :	Optional[int]													=   FeaturesManager.determine_framework(self.test_model       )