Genfromtxt And Numpy
I have data in files such as 'file.csv'. I would like to read them with np.genfromtxt and do some statistics like average, variance etc. on some columns (X, Y, Z). However I want t
Solution 1:
In order to average over only some fields, you break down your averaging as follows:
- Find the indexes (ind) of those elements that meet a certain criteria
- Find the mean of the array indexed only with the the values in ind
The following code does exactly this:
indexes = np.where(X>1)[0] # We index with '0' here to get to the 1st element of the returned tupleMean = np.mean(X[indexes])
Solution 2:
You could use so-called "fancy-indexing", X[X>1]
, to select the part of the array you want:
import numpy as np
X,Y,Z = np.genfromtxt('file.csv', delimiter=',', dtype=float, unpack=True, skiprows = 0)
print(X)
# [ nan 1. 4. 15.]print(X[X>1])
# [ 4. 15.]print(np.average(X[X>1]))
# 9.5
To combine two masks (boolean arrays) with bit-wise logical-and, use the &
operator:
print(np.average(X[(X>1)&(X<10)]))
# 4.0
Post a Comment for "Genfromtxt And Numpy"