def b(matrix):
    result = numpy.zeros(matrix.shape)

    sums = numpy.zeros(matrix.shape[1])

    for j in range(matrix.shape[1]):
        for i in range(matrix.shape[0]):
            sums[j] += matrix[i, j]

    for i in range(matrix.shape[0]):
        for j in range(matrix.shape[1]):
            result[i, j] = matrix[i, j] / sums[j]
        
    return result

Function definition

def b(matrix):
	...
	return result

The function is called b and takes one argument called matrix, which is assumed to be a 2D numpy array. It returns a new 2D array (called result) that is the normalised version of matrix along its columns.


Step 1: Create a Result Array of the Same Shape

result = numpy.zeros(matrix.shape)

Step 2: Initialise an Array for the Column Sums

sums = numpy.zeros(matrix.shape[1])

Step 3: Calculate the Sum of Each Column

for j in range(matrix.shape[1]):
        for i in range(matrix.shape[0]):
            sums[j] += matrix[i, j]