"""simple docstring"""




import numpy

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






def   UpperCAmelCase					(	UpperCamelCase__       ,    UpperCamelCase__="train"				):






							"""simple docstring"""





							return calculate_hypothesis_value(UpperCamelCase__       ,    UpperCamelCase__				) - output(
							    UpperCamelCase__       ,    UpperCamelCase__				)






def   UpperCAmelCase					(	UpperCamelCase__				):






							"""simple docstring"""





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






def   UpperCAmelCase					(	UpperCamelCase__       ,    UpperCamelCase__				):






							"""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					(	UpperCamelCase__       ,    UpperCamelCase__				):






							"""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					(	UpperCamelCase__       ,    UpperCamelCase__=m				):






							"""simple docstring"""





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






def   UpperCAmelCase					(	UpperCamelCase__				):






							"""simple docstring"""





							A__	     =							summation_of_cost_derivative(UpperCamelCase__       ,    UpperCamelCase__				) / m
							return cost_derivative_value






def   UpperCAmelCase					(	):






							"""simple docstring"""





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






def   UpperCAmelCase					(	):






							"""simple docstring"""





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


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






"""simple docstring"""




import numpy as np
from PIL import Image






def   UpperCAmelCase					(	UpperCamelCase__       ,    UpperCamelCase__       ,    UpperCamelCase__				):






							"""simple docstring"""





							A__	     =							np.array(UpperCamelCase__				)
							if arr.shape[0] != arr.shape[1]:
														raise ValueError('The input array is not a square matrix'				)
							A__	     =							0
							A__	     =							0
							A__	     =							0
							A__	     =							0

							# compute the shape of the output matrix
							A__	     =							(arr.shape[0] - size) // stride + 1
							# initialize the output matrix with zeros of shape maxpool_shape
							A__	     =							np.zeros((maxpool_shape, maxpool_shape)				)

							while i < arr.shape[0]:
														if i + size > arr.shape[0]:
																					# if the end of the matrix is reached, break
																					break
														while j < arr.shape[1]:
																					# if the end of the matrix is reached, break
																					if j + size > arr.shape[1]:
																												break
																					# compute the maximum of the pooling matrix
																					A__	     =							np.max(arr[i : i + size, j : j + size]				)
																					# shift the pooling matrix by stride of column pixels
																					j += stride
																					mat_j += 1

														# shift the pooling matrix by stride of row pixels
														i += stride
														mat_i += 1

														# reset the column index to 0
														A__	     =							0
														A__	     =							0

							return updated_arr






def   UpperCAmelCase					(	UpperCamelCase__       ,    UpperCamelCase__       ,    UpperCamelCase__				):






							"""simple docstring"""





							A__	     =							np.array(UpperCamelCase__				)
							if arr.shape[0] != arr.shape[1]:
														raise ValueError('The input array is not a square matrix'				)
							A__	     =							0
							A__	     =							0
							A__	     =							0
							A__	     =							0

							# compute the shape of the output matrix
							A__	     =							(arr.shape[0] - size) // stride + 1
							# initialize the output matrix with zeros of shape avgpool_shape
							A__	     =							np.zeros((avgpool_shape, avgpool_shape)				)

							while i < arr.shape[0]:
														# if the end of the matrix is reached, break
														if i + size > arr.shape[0]:
																					break
														while j < arr.shape[1]:
																					# if the end of the matrix is reached, break
																					if j + size > arr.shape[1]:
																												break
																					# compute the average of the pooling matrix
																					A__	     =							int(np.average(arr[i : i + size, j : j + size]				)				)
																					# shift the pooling matrix by stride of column pixels
																					j += stride
																					mat_j += 1

														# shift the pooling matrix by stride of row pixels
														i += stride
														mat_i += 1
														# reset the column index to 0
														A__	     =							0
														A__	     =							0

							return updated_arr


# Main Function
if __name__ == "__main__":
				from doctest import testmod

				testmod(name="avgpooling", verbose=True)

				# Loading the image
				__lowerCamelCase											=  Image.open("path_to_image")

				# Converting the image to numpy array and maxpooling, displaying the result
				# Ensure that the image is a square matrix

				Image.fromarray(maxpooling(np.array(image), size=3, stride=2)).show()

				# Converting the image to numpy array and averagepooling, displaying the result
				# Ensure that the image is a square matrix

				Image.fromarray(avgpooling(np.array(image), size=3, stride=2)).show()





