0

I have a numpy array like this example:

example:

arr = array([[31, 18],
       [ 27,  9],
       [21, 20]])

and I want to get the mean of every inner list separately and also standard deviation of every inner list separately. until here I would have 2 lists (for mean and std) and every list would have 3 items (one per inner list in arr). then I will multiply every single item of std list and then will add mean list and new std list item by item. so at the end the results would be a list with 3 items. here is the steps for the example:

std = [9.19238815542512, 12.7279220613579, 0.707106781186548]
std2 = [18.3847763108502, 25.4558441227157, 1.4142135623731]
mean = [24.5, 18, 20.5]

and here is the expected output:

final = [42.8847763108502, 43.4558441227157, 21.9142135623731]

to make such results I wrote the following code in python:

import numpy as np
for item in arr:
    mean, std = [np.mean(), np.std()*2]
    results = mean + std

but it does not return expected output. do you know how to fix it?

user10657934
  • 137
  • 10
  • You'll get further in numpy if you think in terms of dimensions or axes,and shapes. Your example is (3,2) shaped. Look at the function docs. Experiment. – hpaulj Aug 01 '19 at 21:30
  • [This answer](https://stackoverflow.com/a/15820007/7389264) to [Calculate mean across dimension in a 2D array](https://stackoverflow.com/q/15819980/7389264) should cover this question as well. – jirassimok Aug 01 '19 at 21:40

1 Answers1

2

There are two issues in your code. First, you are calling np.mean without an argument, which should result in an error. Instead, you want to call either arr.mean(...) or np.mean(arr, ...). Second, you are overwriting the result variable in every iteration of the loop. You probably wanted to declare the result arrays outside the loop, and use list.append to add to them.

However, there is a specialized solution to your question built in to Numpy: many Numpy functions have an axis parameter that lets you take a mean along one axis of an array.

import numpy as np

arr = np.array([[0, 100],
                [1, 101],
                [2, 102]])

arr.mean(axis=0) # => [1, 101]
arr.mean(1) # => [50, 51, 52]

To tell which axis to use, remember that the given axis will be deleted. So for a 3 by 2 array, operating over axis 0 will leave you with a length-2 array, and using axis 1 will leave length 3.

Numpy also lets you perform elementwise arithmetic on arrays1 of the same shape, or between arrays and numbers.2

np.array([1, 2, 3]) + np.array([4, 5, 6]) # => [5, 7, 9]

Using those hints, it should be pretty straightforward to get the results you want.


The Numpy documentation is a great place to start if you want to learn the ins and outs of Numpy's features.

1 You can also add arrays to lists, and add lists using np.add. This applies to anything Numpy considers "array-like," such as tuples.

2 Numpy also allows certain operations between different shaped arrays, using its broadcasting rule, but that's a bit off-topic here.

jirassimok
  • 3,850
  • 2
  • 14
  • 23