


'''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 						UpperCAmelCase__(				_SCREAMING_SNAKE_CASE					: Any ):






			"""simple docstring"""




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






def 						UpperCAmelCase__(				_SCREAMING_SNAKE_CASE					: Tuple,_SCREAMING_SNAKE_CASE					: Tuple ):






			"""simple docstring"""




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






def 						UpperCAmelCase__(				_SCREAMING_SNAKE_CASE					: Optional[int],_SCREAMING_SNAKE_CASE					: Optional[int],_SCREAMING_SNAKE_CASE					: Tuple ):






			"""simple docstring"""




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






def 						UpperCAmelCase__(				_SCREAMING_SNAKE_CASE					: Tuple,_SCREAMING_SNAKE_CASE					: Optional[int],_SCREAMING_SNAKE_CASE					: List[Any],_SCREAMING_SNAKE_CASE					: Any=7_0000 ):






			"""simple docstring"""




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

			for iterations in range(_SCREAMING_SNAKE_CASE ):
						__A=	np.dot(_SCREAMING_SNAKE_CASE,_SCREAMING_SNAKE_CASE )
						__A=	sigmoid_function(_SCREAMING_SNAKE_CASE )
						__A=	np.dot(x.T,h - y ) / y.size
						__A=	theta - alpha * gradient  # updating the weights
						__A=	np.dot(_SCREAMING_SNAKE_CASE,_SCREAMING_SNAKE_CASE )
						__A=	sigmoid_function(_SCREAMING_SNAKE_CASE )
						__A=	cost_function(_SCREAMING_SNAKE_CASE,_SCREAMING_SNAKE_CASE )
						if iterations % 100 == 0:
									print(f"""loss: {j} \t""" )  # printing the loss after every 100 iterations
			return theta


# In[68]:

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

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






					def 						UpperCAmelCase__(				_SCREAMING_SNAKE_CASE					: str ):






								"""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''')
					((UpperCAmelCase__)			,  (UpperCAmelCase__))  			=				(x[:, 0].min(), x[:, 0].max())
					((UpperCAmelCase__)			,  (UpperCAmelCase__))  			=				(x[:, 1].min(), x[:, 1].max())
					((UpperCAmelCase__)			,  (UpperCAmelCase__))  			=				np.meshgrid(np.linspace(xa_min, xa_max), np.linspace(xa_min, xa_max))
					UpperCAmelCase__  			=				np.c_[xxa.ravel(), xxa.ravel()]
					UpperCAmelCase__  			=				predict_prob(grid).reshape(xxa.shape)
					plt.contour(xxa, xxa, probs, [0.5], linewidths=1, colors='''black''')

					plt.legend()






					plt.show()







'''simple docstring'''





# Lint as: python3
import itertools
import os
import re


UpperCAmelCase__  			=				re.compile(r'''([A-Z]+)([A-Z][a-z])''')
UpperCAmelCase__  			=				re.compile(r'''([a-z\d])([A-Z])''')

UpperCAmelCase__  			=				re.compile(r'''(?<!_)_(?!_)''')
UpperCAmelCase__  			=				re.compile(r'''(_{2,})''')

UpperCAmelCase__  			=				r'''^\w+(\.\w+)*$'''

UpperCAmelCase__  			=				r'''<>:/\|?*'''






def 						UpperCAmelCase__(				_SCREAMING_SNAKE_CASE					: Union[str, Any] ):






			"""simple docstring"""




			__A=	_uppercase_uppercase_re.sub(r'\1_\2',_SCREAMING_SNAKE_CASE )
			__A=	_lowercase_uppercase_re.sub(r'\1_\2',_SCREAMING_SNAKE_CASE )
			return name.lower()






def 						UpperCAmelCase__(				_SCREAMING_SNAKE_CASE					: int ):






			"""simple docstring"""




			__A=	_single_underscore_re.split(_SCREAMING_SNAKE_CASE )
			__A=	[_multiple_underscores_re.split(_SCREAMING_SNAKE_CASE ) for n in name]
			return "".join(n.capitalize() for n in itertools.chain.from_iterable(_SCREAMING_SNAKE_CASE ) if n != '' )






def 						UpperCAmelCase__(				_SCREAMING_SNAKE_CASE					: str ):






			"""simple docstring"""




			if os.path.basename(_SCREAMING_SNAKE_CASE ) != name:
						raise ValueError(f"""Should be a dataset name, not a path: {name}""" )
			return camelcase_to_snakecase(_SCREAMING_SNAKE_CASE )






def 						UpperCAmelCase__(				_SCREAMING_SNAKE_CASE					: Union[str, Any],_SCREAMING_SNAKE_CASE					: str ):






			"""simple docstring"""




			if os.path.basename(_SCREAMING_SNAKE_CASE ) != name:
						raise ValueError(f"""Should be a dataset name, not a path: {name}""" )
			if not re.match(_split_re,_SCREAMING_SNAKE_CASE ):
						raise ValueError(f"""Split name should match '{_split_re}'' but got '{split}'.""" )
			return f"""{filename_prefix_for_name(_SCREAMING_SNAKE_CASE )}-{split}"""






def 						UpperCAmelCase__(				_SCREAMING_SNAKE_CASE					: Union[str, Any],_SCREAMING_SNAKE_CASE					: Optional[Any],_SCREAMING_SNAKE_CASE					: Optional[int],_SCREAMING_SNAKE_CASE					: Union[str, Any]=None ):






			"""simple docstring"""




			__A=	filename_prefix_for_split(_SCREAMING_SNAKE_CASE,_SCREAMING_SNAKE_CASE )
			if filetype_suffix:
						prefix += f""".{filetype_suffix}"""
			__A=	os.path.join(_SCREAMING_SNAKE_CASE,_SCREAMING_SNAKE_CASE )
			return f"""{filepath}*"""






def 						UpperCAmelCase__(				_SCREAMING_SNAKE_CASE					: int,_SCREAMING_SNAKE_CASE					: List[str],_SCREAMING_SNAKE_CASE					: int,_SCREAMING_SNAKE_CASE					: List[Any]=None,_SCREAMING_SNAKE_CASE					: List[str]=None ):






			"""simple docstring"""




			__A=	filename_prefix_for_split(_SCREAMING_SNAKE_CASE,_SCREAMING_SNAKE_CASE )
			__A=	os.path.join(_SCREAMING_SNAKE_CASE,_SCREAMING_SNAKE_CASE )

			if shard_lengths:
						__A=	len(_SCREAMING_SNAKE_CASE )
						__A=	[f"""{prefix}-{shard_id:05d}-of-{num_shards:05d}""" for shard_id in range(_SCREAMING_SNAKE_CASE )]
						if filetype_suffix:
									__A=	[filename + f""".{filetype_suffix}""" for filename in filenames]
						return filenames
			else:
						__A=	prefix
						if filetype_suffix:
									filename += f""".{filetype_suffix}"""
						return [filename]



