


'''simple docstring'''


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






def  __lowerCAmelCase (__lowerCAmelCase   ):
		return 1 / (1 + np.exp(-z   ))






def  __lowerCAmelCase (__lowerCAmelCase      , __lowerCAmelCase   ):
		return (-y * np.log(a__   ) - (1 - y) * np.log(1 - h   )).mean()






def  __lowerCAmelCase (__lowerCAmelCase      , __lowerCAmelCase      , __lowerCAmelCase   ):
		_UpperCAmelCase		:       Any								=      np.dot(a__      , a__   )
		return np.sum(y * scores - np.log(1 + np.exp(a__   )   )   )






def  __lowerCAmelCase (__lowerCAmelCase      , __lowerCAmelCase      , __lowerCAmelCase      , __lowerCAmelCase=70_000   ):
		_UpperCAmelCase		:       Any								=      np.zeros(x.shape[1]   )

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


# In[68]:

if __name__ == "__main__":
		lowerCamelCase__    	=     datasets.load_iris()
		lowerCamelCase__    	=     iris.data[:, :2]
		lowerCamelCase__    	=     (iris.target != 0) * 1

		lowerCamelCase__    	=     0.1
		lowerCamelCase__    	=     logistic_reg(alpha, x, y, max_iterations=70_000)
		print('theta: ', theta)  # printing the theta i.e our weights vector






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

		plt.legend()





		plt.show()







'''simple docstring'''


from __future__ import annotations

import math

import numpy as np
from numpy.linalg import norm






def  __lowerCAmelCase (__lowerCAmelCase      , __lowerCAmelCase   ):
		return math.sqrt(sum(pow(a - b      , 2   ) for a, b in zip(__lowerCAmelCase      , __lowerCAmelCase   )   )   )






def  __lowerCAmelCase (__lowerCAmelCase      , __lowerCAmelCase   ):

		if dataset.ndim != value_array.ndim:
				_UpperCAmelCase		:       Optional[Any]								=      (
				    "Wrong input data's dimensions... "
				    F"""dataset : {dataset.ndim}, value_array : {value_array.ndim}"""
				)
				raise ValueError(__lowerCAmelCase   )

		try:
				if dataset.shape[1] != value_array.shape[1]:
						_UpperCAmelCase		:       Optional[int]								=      (
						    "Wrong input data's shape... "
						    F"""dataset : {dataset.shape[1]}, value_array : {value_array.shape[1]}"""
						)
						raise ValueError(__lowerCAmelCase   )
		except IndexError:
				if dataset.ndim != value_array.ndim:
						raise TypeError("Wrong shape"   )

		if dataset.dtype != value_array.dtype:
				_UpperCAmelCase		:       Union[str, Any]								=      (
				    "Input data have different datatype... "
				    F"""dataset : {dataset.dtype}, value_array : {value_array.dtype}"""
				)
				raise TypeError(__lowerCAmelCase   )

		_UpperCAmelCase		:       Optional[int]								=      []

		for value in value_array:
				_UpperCAmelCase		:       List[str]								=      euclidean(__lowerCAmelCase      , dataset[0]   )
				_UpperCAmelCase		:       Dict								=      dataset[0].tolist()

				for dataset_value in dataset[1:]:
						_UpperCAmelCase		:       int								=      euclidean(__lowerCAmelCase      , __lowerCAmelCase   )

						if dist > temp_dist:
								_UpperCAmelCase		:       Tuple								=      temp_dist
								_UpperCAmelCase		:       Dict								=      dataset_value.tolist()

				answer.append([vector, dist]   )

		return answer






def  __lowerCAmelCase (__lowerCAmelCase      , __lowerCAmelCase   ):
		return np.dot(__lowerCAmelCase      , __lowerCAmelCase   ) / (norm(__lowerCAmelCase   ) * norm(__lowerCAmelCase   ))


if __name__ == "__main__":
						import doctest

						doctest.testmod()



