
'''simple docstring'''




import numpy as np
from PIL import Image







def 						lowerCAmelCase_							(  snake_case__						,							snake_case__						,							snake_case__		):




    '''simple docstring'''



    A			:  List[str]   =		np.array(snake_case__		)
    if arr.shape[0] != arr.shape[1]:
        raise ValueError('''The input array is not a square matrix'''		)
    A			:  int   =		0
    A			:  Any   =		0
    A			:  Optional[Any]   =		0
    A			:  Any   =		0

    # compute the shape of the output matrix
    A			:  List[str]   =		(arr.shape[0] - size) // stride + 1
    # initialize the output matrix with zeros of shape maxpool_shape
    A			:  List[Any]   =		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			:  Tuple   =		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			:  Optional[int]   =		0
        A			:  Any   =		0

    return updated_arr







def 						lowerCAmelCase_							(  snake_case__						,							snake_case__						,							snake_case__		):




    '''simple docstring'''



    A			:  str   =		np.array(snake_case__		)
    if arr.shape[0] != arr.shape[1]:
        raise ValueError('''The input array is not a square matrix'''		)
    A			:  Union[str, Any]   =		0
    A			:  List[str]   =		0
    A			:  List[str]   =		0
    A			:  List[str]   =		0

    # compute the shape of the output matrix
    A			:  str   =		(arr.shape[0] - size) // stride + 1
    # initialize the output matrix with zeros of shape avgpool_shape
    A			:  Tuple   =		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			:  Optional[int]   =		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			:  Any   =		0
        A			:  Optional[int]   =		0

    return updated_arr


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

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

      # Loading the image
      lowercase     :							Tuple       						=     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()






'''simple docstring'''




import numpy

# List of input, output pairs
lowercase     :							Any       						=     (
    ((5, 2, 3), 15),
    ((6, 5, 9), 25),
    ((11, 12, 13), 41),
    ((1, 1, 1), 8),
    ((11, 12, 13), 41),
)
lowercase     :							str       						=     (((5_15, 22, 13), 5_55), ((61, 35, 49), 1_50))
lowercase     :							Union[str, Any]       						=     [2, 4, 1, 5]
lowercase     :							Any       						=     len(train_data)
lowercase     :							Optional[int]       						=     0.0_09







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




    '''simple docstring'''



    return calculate_hypothesis_value(snake_case__						,							snake_case__		) - output(
        snake_case__						,							snake_case__		)







def 						lowerCAmelCase_							(  snake_case__		):




    '''simple docstring'''



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







def 						lowerCAmelCase_							(  snake_case__						,							snake_case__		):




    '''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 						lowerCAmelCase_							(  snake_case__						,							snake_case__		):




    '''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 						lowerCAmelCase_							(  snake_case__						,							snake_case__=m		):




    '''simple docstring'''



    A			:  List[Any]   =		0
    for i in range(snake_case__		):
        if index == -1:
            summation_value += _error(snake_case__		)
        else:
            summation_value += _error(snake_case__		) * train_data[i][0][index]
    return summation_value







def 						lowerCAmelCase_							(  snake_case__		):




    '''simple docstring'''



    A			:  List[Any]   =		summation_of_cost_derivative(snake_case__						,							snake_case__		) / m
    return cost_derivative_value







def 						lowerCAmelCase_							(  ):




    '''simple docstring'''



    global parameter_vector
    # Tune these values to set a tolerance value for predicted output
    A			:  Dict   =		0.00_00_02
    A			:  Optional[Any]   =		0
    A			:  int   =		0
    while True:
        j += 1
        A			:  List[str]   =		[0, 0, 0, 0]
        for i in range(0						,							len(snake_case__		)		):
            A			:  Union[str, Any]   =		get_cost_derivative(i - 1		)
            A			:  Tuple   =		(
                parameter_vector[i] - LEARNING_RATE * cost_derivative
            )
        if numpy.allclose(
            snake_case__						,							snake_case__						,							atol=snake_case__						,							rtol=snake_case__						,							):
            break
        A			:  List[Any]   =		temp_parameter_vector
    print(('''Number of iterations:''', j)		)







def 						lowerCAmelCase_							(  ):




    '''simple docstring'''



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


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




