Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to implement the mean function in NumPy without using the np.mean()?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading