Gavy Littlewolf - 7 months ago 80

Python Question

I'm trying to understand a python code, a specific line of the code has troubled me a bit:

`mean = np.average(data[:,index])`

I understand that this is an average calculation of

`data`

`[:,index]`

I apologise if this question is duplicated, but please link me a solution before you mark it down. This is the first day I'm exposed to Python, please excuse my ignorance. Appreciate for any kind advice!

below is part of the original code

`data = np.genfromtxt(args.inputfile)`

def doBlocking(data,index):

ndata = data.shape[0]

ncols = data.shape[1]-1

#things unimportant

mean = np.average(data[:,index])

#more unimportance

Answer

In this case `data`

is a two dimensional `numpy.array`

. `Numpy`

supports slicing similar to that of `Matlab`

```
In [1]: import numpy as np
In [2]: data = np.arange(15)
In [3]: data
Out[3]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
In [4]: data = data.reshape([5,3])
In [5]: data
Out[5]:
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11],
[12, 13, 14]])
In [6]: data[:, 1]
Out[6]: array([ 1, 4, 7, 10, 13])
```

As you can see it selects the second column

Your code above will get the mean of column `index`

. It basically says "Compute the mean for data in every line, and column `index`

"

Source (Stackoverflow)