



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

							'''simple docstring'''






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






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

							'''simple docstring'''






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






def  lowerCamelCase_	(      lowerCAmelCase__	:	Any			,	lowerCAmelCase__	:	Union[str, Any]			,	lowerCAmelCase__	:	List[str]				)       ->							Tuple:

							'''simple docstring'''






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






def  lowerCamelCase_	(      lowerCAmelCase__	:	str			,	lowerCAmelCase__	:	Dict			,	lowerCAmelCase__	:	Any			,	lowerCAmelCase__	:	str=70000				)       ->							int:

							'''simple docstring'''






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

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

				__snake_case     :List[Any]			       =0.1
				__snake_case     :Union[str, Any]			       =logistic_reg(alpha, x, y, max_iterations=70000)
				print('theta: ', theta)  # printing the theta i.e our weights vector






				def  lowerCamelCase_	(      lowerCAmelCase__	:	Union[str, Any]				)       ->							str:

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

				plt.legend()



				plt.show()




from typing import List

import numpy as np






def  lowerCamelCase_	(      lowerCAmelCase__	:	dict				)       ->							int:

							'''simple docstring'''






							A      						=    {key: len(lowerCAmelCase__				) for key, value in gen_kwargs.items() if isinstance(lowerCAmelCase__			,	lowerCAmelCase__				)}
							if len(set(lists_lengths.values()				)				) > 1:
														raise RuntimeError(
														    (
														        'Sharding is ambiguous for this dataset: '
														        + 'we found several data sources lists of different lengths, and we don\'t know over which list we should parallelize:\n'
														        + '\n'.join(F'''\t- key {key} has length {length}''' for key, length in lists_lengths.items()				)
														        + '\nTo fix this, check the \'gen_kwargs\' and make sure to use lists only for data sources, '
														        + 'and use tuples otherwise. In the end there should only be one single list, or several lists with the same length.'
														    )				)
							A      						=    max(lists_lengths.values()			,	default=0				)
							return max(1			,	lowerCAmelCase__				)






def  lowerCamelCase_	(      lowerCAmelCase__	:	int			,	lowerCAmelCase__	:	int				)       ->							List[range]:

							'''simple docstring'''






							A      						=    []
							for group_idx in range(lowerCAmelCase__				):
														A      						=    num_shards // max_num_jobs + (group_idx < (num_shards % max_num_jobs))
														if num_shards_to_add == 0:
																					break
														A      						=    shards_indices_per_group[-1].stop if shards_indices_per_group else 0
														A      						=    range(lowerCAmelCase__			,	start + num_shards_to_add				)
														shards_indices_per_group.append(lowerCAmelCase__				)
							return shards_indices_per_group






def  lowerCamelCase_	(      lowerCAmelCase__	:	dict			,	lowerCAmelCase__	:	int				)       ->							List[dict]:

							'''simple docstring'''






							A      						=    _number_of_shards_in_gen_kwargs(lowerCAmelCase__				)
							if num_shards == 1:
														return [dict(lowerCAmelCase__				)]
							else:
														A      						=    _distribute_shards(num_shards=lowerCAmelCase__			,	max_num_jobs=lowerCAmelCase__				)
														return [
														    {
														        key: [value[shard_idx] for shard_idx in shard_indices_per_group[group_idx]]
														        if isinstance(lowerCAmelCase__			,	lowerCAmelCase__				)
														        else value
														        for key, value in gen_kwargs.items()
														    }
														    for group_idx in range(len(lowerCAmelCase__				)				)
														]






def  lowerCamelCase_	(      lowerCAmelCase__	:	List[dict]				)       ->							dict:

							'''simple docstring'''






							return {
							    key: [value for gen_kwargs in gen_kwargs_list for value in gen_kwargs[key]]
							    if isinstance(gen_kwargs_list[0][key]			,	lowerCAmelCase__				)
							    else gen_kwargs_list[0][key]
							    for key in gen_kwargs_list[0]
							}






def  lowerCamelCase_	(      lowerCAmelCase__	:	np.random.Generator			,	lowerCAmelCase__	:	dict				)       ->							dict:

							'''simple docstring'''






							A      						=    {len(lowerCAmelCase__				) for value in gen_kwargs.values() if isinstance(lowerCAmelCase__			,	lowerCAmelCase__				)}
							A      						=    {}
							for size in list_sizes:
														A      						=    list(range(lowerCAmelCase__				)				)
														rng.shuffle(indices_per_size[size]				)
							# Now let's copy the gen_kwargs and shuffle the lists based on their sizes
							A      						=    dict(lowerCAmelCase__				)
							for key, value in shuffled_kwargs.items():
														if isinstance(lowerCAmelCase__			,	lowerCAmelCase__				):
																					A      						=    [value[i] for i in indices_per_size[len(lowerCAmelCase__				)]]
							return shuffled_kwargs