

# 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_     (							UpperCAmelCase_							:     str	)						->				Optional[int]:




	'''simple docstring'''




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






def 					lowerCamelCase_     (							UpperCAmelCase_							:     List[Any]							,       UpperCAmelCase_							:     Optional[Any]	)						->				List[str]:




	'''simple docstring'''




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






def 					lowerCamelCase_     (							UpperCAmelCase_							:     Any							,       UpperCAmelCase_							:     int							,       UpperCAmelCase_							:     List[str]	)						->				Tuple:




	'''simple docstring'''




	_UpperCamelCase   :		Dict			=						np.dot(UpperCAmelCase_							,       UpperCAmelCase_	)
	return np.sum(y * scores - np.log(1 + np.exp(UpperCAmelCase_	)	)	)






def 					lowerCamelCase_     (							UpperCAmelCase_							:     Any							,       UpperCAmelCase_							:     str							,       UpperCAmelCase_							:     Dict							,       UpperCAmelCase_							:     Any=7_0_0_0_0	)						->				List[Any]:




	'''simple docstring'''




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

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

							lowerCAmelCase__								=		0.1
							lowerCAmelCase__								=		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_     (							UpperCAmelCase_							:     Any	)						->				List[str]:




								'''simple docstring'''




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







def 					lowerCamelCase_     (							UpperCAmelCase_							:     list	)						->				list:




	'''simple docstring'''




	if len(UpperCAmelCase_	) <= 1:
		return [tuple(UpperCAmelCase_	)]

	_UpperCamelCase   :		List[Any]			=						[]

	def generate(UpperCAmelCase_							:     int							,       UpperCAmelCase_							:     list	):
		_UpperCamelCase   :		Optional[int]			=						[0] * n
		res.append(tuple(UpperCAmelCase_	)	)

		_UpperCamelCase   :		List[Any]			=						0
		while i < n:
			if c[i] < i:
				if i % 2 == 0:
					_UpperCamelCase	,		_UpperCamelCase   :		List[str]			=						arr[i], arr[0]
				else:
					_UpperCamelCase	,		_UpperCamelCase   :		List[str]			=						arr[i], arr[c[i]]
				res.append(tuple(UpperCAmelCase_	)	)
				c[i] += 1
				_UpperCamelCase   :		Tuple			=						0
			else:
				_UpperCamelCase   :		Tuple			=						0
				i += 1

	generate(len(UpperCAmelCase_	)							,       UpperCAmelCase_	)
	return res


if __name__ == "__main__":
							lowerCAmelCase__								=		input("""Enter numbers separated by a comma:\n""").strip()
							lowerCAmelCase__								=		[int(item) for item in user_input.split(""",""")]
							print(heaps(arr))




