



# 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      SCREAMING_SNAKE_CASE				(				lowercase_    :      List[Any]						):
						return 1 / (1 + np.exp(-z						))







def      SCREAMING_SNAKE_CASE				(				lowercase_    :      Any ,  lowercase_    :      Optional[Any]						):
						return (-y * np.log(lowerCAmelCase__						) - (1 - y) * np.log(1 - h						)).mean()







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







def      SCREAMING_SNAKE_CASE				(				lowercase_    :      str ,  lowercase_    :      List[str] ,  lowercase_    :      List[str] ,  lowercase_    :      Dict=7_0000						):
						lowercase        =							np.zeros(x.shape[1]						)

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

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







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

						plt.legend()
						plt.show()







'''simple docstring'''


import json
import os
import tempfile

import transformers

import datasets
from utils import generate_example_dataset, get_duration


lowercase_      :				int   					=    50_0000

lowercase_					,       lowercase_      :				Union[str, Any]   					=    os.path.split(__file__)
lowercase_      :				Optional[Any]   					=    os.path.join(RESULTS_BASEPATH, '''results''', RESULTS_FILENAME.replace('''.py''', '''.json'''))







@get_duration
def      SCREAMING_SNAKE_CASE				(				lowercase_    :      datasets.Dataset ,  **lowercase_    :      Dict						):
						lowercase        =							dataset.map(**lowercase_						)







@get_duration
def      SCREAMING_SNAKE_CASE				(				lowercase_    :      datasets.Dataset ,  **lowercase_    :      Optional[int]						):
						lowercase        =							dataset.filter(**lowercase_						)







def      SCREAMING_SNAKE_CASE				(				):
						lowercase        =							{"""num examples""": SPEED_TEST_N_EXAMPLES}
						with tempfile.TemporaryDirectory() as tmp_dir:
												lowercase        =							datasets.Features({"""text""": datasets.Value("""string"""						), """numbers""": datasets.Value("""float32"""						)}						)
												lowercase        =							generate_example_dataset(
												    os.path.join(lowercase_ ,  """dataset.arrow"""						) ,  lowercase_ ,  num_examples=lowercase_						)

												lowercase        =							transformers.AutoTokenizer.from_pretrained("""bert-base-cased""" ,  use_fast=lowercase_						)

												def tokenize(lowercase_    :      Dict						):
																		return tokenizer(examples["""text"""]						)

												lowercase        =							map(lowercase_						)

												lowercase        =							map(lowercase_ ,  batched=lowercase_						)

												lowercase        =							map(lowercase_ ,  function=lambda lowercase_						: None ,  batched=lowercase_						)

												with dataset.formatted_as(type="""numpy"""						):
																		lowercase        =							map(lowercase_ ,  function=lambda lowercase_						: None ,  batched=lowercase_						)

												with dataset.formatted_as(type="""pandas"""						):
																		lowercase        =							map(lowercase_ ,  function=lambda lowercase_						: None ,  batched=lowercase_						)

												with dataset.formatted_as(type="""torch""" ,  columns="""numbers"""						):
																		lowercase        =							map(lowercase_ ,  function=lambda lowercase_						: None ,  batched=lowercase_						)

												with dataset.formatted_as(type="""tensorflow""" ,  columns="""numbers"""						):
																		lowercase        =							map(lowercase_ ,  function=lambda lowercase_						: None ,  batched=lowercase_						)

												lowercase        =							map(lowercase_ ,  function=lowercase_ ,  batched=lowercase_						)

												lowercase        =							filter(lowercase_						)

												# Activate later when tokenizer support batched inputs
												# with dataset.formatted_as(type='numpy'):
												#     times[func.__name__ + " fast-tokenizer batched numpy"] = func(dataset, function=tokenize, batched=True)

						with open(lowercase_ ,  """wb"""						) as f:
												f.write(json.dumps(lowercase_						).encode("""utf-8"""						)						)


if __name__ == "__main__":  # useful to run the profiler
						benchmark_map_filter()


