Reputation: 592
I'm new to numpy. I have an Nx4 matrix and I want to find the average of each column when the last column equals 1 for instance. In matlab I would do something like mean1 = mean(data[column 4] == 1)
. This would return a matrix (or vector) with the mean of the columns, with the mean of column 4 being equal to 1. I can't find any specific documentation that specifies how to handle this. This shows how to filter the matrix, but I shouldn't have to reassign the matrix to a new variable, doubling storage size. Thanks in advance.
Upvotes: 0
Views: 206
Reputation: 9620
#make artificial data to match problem
data = np.random.random((100,4))
print( id(data) )
data[:,3] = data[:,3] < 0.5
print( id(data) ) #same object (memory location)
#get the filter
dfilter = data[:,3].astype(np.bool_)
#find the means
means = data[dfilter].mean(axis=0)
Upvotes: 1