



import numpy

# List of input, output pairs
lowercase_													=      (
    ((5, 2, 3), 15),
    ((6, 5, 9), 25),
    ((11, 12, 13), 41),
    ((1, 1, 1), 8),
    ((11, 12, 13), 41),
)
lowercase_													=      (((5_15, 22, 13), 5_55), ((61, 35, 49), 1_50))
lowercase_													=      [2, 4, 1, 5]
lowercase_													=      len(train_data)
lowercase_													=      0.0_0_9






def        __lowerCAmelCase		(	__lowerCamelCase						:     List[Any]					, __lowerCamelCase						:     Optional[Any]="train"						)	->    str:
					return calculate_hypothesis_value(__lowerCamelCase					, __lowerCamelCase						) - output(
					    __lowerCamelCase					, __lowerCamelCase						)






def        __lowerCAmelCase		(	__lowerCamelCase						:     Dict						)	->    List[str]:
					__lowerCAmelCase						 =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        __lowerCAmelCase		(	__lowerCamelCase						:     Optional[int]					, __lowerCamelCase						:     List[Any]						)	->    List[str]:
					if data_set == "train":
										return train_data[example_no][1]
					elif data_set == "test":
										return test_data[example_no][1]
					return None






def        __lowerCAmelCase		(	__lowerCamelCase						:     int					, __lowerCamelCase						:     Optional[int]						)	->    Tuple:
					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        __lowerCAmelCase		(	__lowerCamelCase						:     Optional[int]					, __lowerCamelCase						:     str=m						)	->    Optional[Any]:
					__lowerCAmelCase						 =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        __lowerCAmelCase		(	__lowerCamelCase						:     Optional[Any]						)	->    Optional[int]:
					__lowerCAmelCase						 =summation_of_cost_derivative(__lowerCamelCase					, __lowerCamelCase						) / m
					return cost_derivative_value






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






def        __lowerCAmelCase		(	)	->    List[str]:
					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()









from collections import Counter

import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split

lowercase_													=      datasets.load_iris()

lowercase_													=      np.array(data['''data'''])
lowercase_													=      np.array(data['''target'''])
lowercase_													=      data['''target_names''']

lowercase_							,							lowercase_							,							lowercase_							,							lowercase_													=      train_test_split(X, y)






def        __lowerCAmelCase		(	__lowerCamelCase						:     Optional[Any]					, __lowerCamelCase						:     Optional[Any]						)	->    Optional[int]:
					return np.linalg.norm(np.array(__lowerCamelCase						) - np.array(__lowerCamelCase						)						)






def        __lowerCAmelCase		(	__lowerCamelCase						:     List[str]					, __lowerCamelCase						:     List[Any]					, __lowerCamelCase						:     List[Any]					, __lowerCamelCase						:     Union[str, Any]					, __lowerCamelCase						:     int=5						)	->    str:
					__lowerCAmelCase						 =zip(__lowerCamelCase					, __lowerCamelCase						)
					# List of distances of all points from the point to be classified
					__lowerCAmelCase						 =[]
					for data_point in data:
										__lowerCAmelCase						 =euclidean_distance(data_point[0]					, __lowerCamelCase						)
										distances.append((distance, data_point[1])						)
					# Choosing 'k' points with the least distances.
					__lowerCAmelCase						 =[i[1] for i in sorted(__lowerCamelCase						)[:k]]
					# Most commonly occurring class among them
					# is the class into which the point is classified
					__lowerCAmelCase						 =Counter(__lowerCamelCase						).most_common(1						)[0][0]
					return classes[result]


if __name__ == "__main__":
				print(classifier(X_train, y_train, classes, [4.4, 3.1, 1.3, 1.4]))




