1

I am learning how to code and wondered how to take the mean without using a builtin function (I know these are optimized and they should be used in real life, this is more of a thought experiment for myself).

For example, this works for vectors:

def take_mean(arr):
  sum = 0
  for i in arr:
    sum += i
  mean = sum/np.size(arr)
  
  return mean

But, of course, if I try to pass a matrix, it already fails. Clearly, I can change the code to work for matrices by doing:

def take_mean(arr):
  sum = 0
  for i in arr:
    for j in i:
      sum += i
  mean = sum/np.size(arr)
  
  return mean

And this fails for vectors and any >=3 dimensional arrays.

So I'm wondering how I can sum over a n-dimensional array without using any built-in functions. Any tips on how to achieve this?

1
  • 1
    Consider using for i in arr.flatten(). Note that Numpy do no use a naive sum like this which is known not to be numerically stable for large FP-based arrays. Also note that this code will be slow like hell compared to Numpy vectorized functions (implemented in C). If you do not want to use flatten, then you can do that using recusive function doing a per-axis reduction and operating with views but this will be significantly more complex and even slower. Commented Jul 30, 2022 at 12:28

2 Answers 2

1

You can use a combination of recursion and loop to achieve your objective without using any of numpy's methods.

import numpy as np

def find_mean_of_arrays(array):
   sum = 0
   for element in array:
      if type(element) == type(np.array([1])):
         sum += find_mean_of_arrays(element)
      else:
         sum += element
   return sum/len(array)

Recursion is a powerful tool and it makes code more elegant and readable. This is yet another example

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! This is exactly what I was looking for. But I still don't get how it works exactly.
Welcome, Think of recursion as dynamic looping. Here instead of adding nested loops we are using recursion. Recursion has a set of characteristics that makes this possible. Here, If type of element is array as in case of nested arrays, we again call the method passing the inner array, this would continue recursively till the end is reached, in that case code block in else would be used, method is not called there, so recursion would stop and mean value till now is returned for the first inner array and next loop would continue and this goes on till sum and mean is calculated
1

Unless you need to mean across a specific axis, the shape of the array does not matter to compute the mean. Making your first solution possible.

def take_mean(arr):
  sum = 0
  for i in arr.reshape(-1): # or arr.flatten()
    sum += i
    mean = sum/np.size(arr)
  
  return mean

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.