


"""simple docstring"""







import numpy

# List of input, output pairs
a__    :							int            =					(
    ((5, 2, 3), 1_5),
    ((6, 5, 9), 2_5),
    ((1_1, 1_2, 1_3), 4_1),
    ((1, 1, 1), 8),
    ((1_1, 1_2, 1_3), 4_1),
)
a__    :							str            =					(((5_1_5, 2_2, 1_3), 5_5_5), ((6_1, 3_5, 4_9), 1_5_0))
a__    :							List[Any]            =					[2, 4, 1, 5]
a__    :							Union[str, Any]            =					len(train_data)
a__    :							Tuple            =					0.0_09




def        UpperCAmelCase__	(lowerCAmelCase_  ,						lowerCAmelCase_="train"       ):




					'''simple docstring'''


					return calculate_hypothesis_value(lowerCAmelCase_  ,						lowerCAmelCase_       ) - output(
					    lowerCAmelCase_  ,						lowerCAmelCase_       )




def        UpperCAmelCase__	(lowerCAmelCase_       ):




					'''simple docstring'''


					__SCREAMING_SNAKE_CASE						       =    0
					for i in range(len(lowerCAmelCase_       ) - 1       ):
										hyp_val += data_input_tuple[i] * parameter_vector[i + 1]
					hyp_val += parameter_vector[0]
					return hyp_val




def        UpperCAmelCase__	(lowerCAmelCase_  ,						lowerCAmelCase_       ):




					'''simple docstring'''


					if data_set == "train":
										return train_data[example_no][1]
					elif data_set == "test":
										return test_data[example_no][1]
					return None




def        UpperCAmelCase__	(lowerCAmelCase_  ,						lowerCAmelCase_       ):




					'''simple docstring'''


					if data_set == "train":
										return _hypothesis_value(train_data[example_no][0]       )
					elif data_set == "test":
										return _hypothesis_value(test_data[example_no][0]       )
					return None




def        UpperCAmelCase__	(lowerCAmelCase_  ,						lowerCAmelCase_=m       ):




					'''simple docstring'''


					__SCREAMING_SNAKE_CASE						       =    0
					for i in range(lowerCAmelCase_       ):
										if index == -1:
															summation_value += _error(lowerCAmelCase_       )
										else:
															summation_value += _error(lowerCAmelCase_       ) * train_data[i][0][index]
					return summation_value




def        UpperCAmelCase__	(lowerCAmelCase_       ):




					'''simple docstring'''


					__SCREAMING_SNAKE_CASE						       =    summation_of_cost_derivative(lowerCAmelCase_  ,						lowerCAmelCase_       ) / m
					return cost_derivative_value




def        UpperCAmelCase__	():




					'''simple docstring'''


					global parameter_vector
					# Tune these values to set a tolerance value for predicted output
					__SCREAMING_SNAKE_CASE						       =    0.000002
					__SCREAMING_SNAKE_CASE						       =    0
					__SCREAMING_SNAKE_CASE						       =    0
					while True:
										j += 1
										__SCREAMING_SNAKE_CASE						       =    [0, 0, 0, 0]
										for i in range(0  ,						len(lowerCAmelCase_       )       ):
															__SCREAMING_SNAKE_CASE						       =    get_cost_derivative(i - 1       )
															__SCREAMING_SNAKE_CASE						       =    (
															    parameter_vector[i] - LEARNING_RATE * cost_derivative
															)
										if numpy.allclose(
										    lowerCAmelCase_  ,						lowerCAmelCase_  ,						atol=lowerCAmelCase_  ,						rtol=lowerCAmelCase_  ,						):
															break
										__SCREAMING_SNAKE_CASE						       =    temp_parameter_vector
					print(("Number of iterations:", j)       )




def        UpperCAmelCase__	():




					'''simple docstring'''


					for i in range(len(lowerCAmelCase_       )       ):
										print(("Actual output value:", output(lowerCAmelCase_  ,						"test"       ))       )
										print(("Hypothesis output:", calculate_hypothesis_value(lowerCAmelCase_  ,						"test"       ))       )


if __name__ == "__main__":
		run_gradient_descent()
		print('''\nTesting gradient descent for a linear hypothesis function.\n''')
		test_gradient_descent()




"""simple docstring"""







from __future__ import annotations

import math

import numpy as np
from numpy.linalg import norm




def        UpperCAmelCase__	(lowerCAmelCase_  ,						lowerCAmelCase_       ):




					'''simple docstring'''


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




def        UpperCAmelCase__	(lowerCAmelCase_  ,						lowerCAmelCase_       ):




					'''simple docstring'''


					if dataset.ndim != value_array.ndim:
										__SCREAMING_SNAKE_CASE						       =    (
										    "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]:
															__SCREAMING_SNAKE_CASE						       =    (
															    "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:
										__SCREAMING_SNAKE_CASE						       =    (
										    "Input data have different datatype... "
										    f"""dataset : {dataset.dtype}, value_array : {value_array.dtype}"""
										)
										raise TypeError(lowerCAmelCase_       )

					__SCREAMING_SNAKE_CASE						       =    []

					for value in value_array:
										__SCREAMING_SNAKE_CASE						       =    euclidean(lowerCAmelCase_  ,						dataset[0]       )
										__SCREAMING_SNAKE_CASE						       =    dataset[0].tolist()

										for dataset_value in dataset[1:]:
															__SCREAMING_SNAKE_CASE						       =    euclidean(lowerCAmelCase_  ,						lowerCAmelCase_       )

															if dist > temp_dist:
																				__SCREAMING_SNAKE_CASE						       =    temp_dist
																				__SCREAMING_SNAKE_CASE						       =    dataset_value.tolist()

										answer.append([vector, dist]       )

					return answer




def        UpperCAmelCase__	(lowerCAmelCase_  ,						lowerCAmelCase_       ):




					'''simple docstring'''


					return np.dot(lowerCAmelCase_  ,						lowerCAmelCase_       ) / (norm(lowerCAmelCase_       ) * norm(lowerCAmelCase_       ))


if __name__ == "__main__":
		import doctest

		doctest.testmod()
