I have a sample array
import numpy as np
a = np.array(
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12],
[13, 14, 15],
]
)
And an array of indices for which I would like to get averages from
b = np.array([[1,3], [1,2], [2,3]])
In addition, I need the final result to have the first row concatenated to each of these averages
I can get the desired result using this
np.concatenate( (np.tile(a[0],(3,1)), a[b].mean(1)), axis=1)
array([[ 1. , 2. , 3. , 7. , 8. , 9. ],
[ 1. , 2. , 3. , 5.5, 6.5, 7.5],
[ 1. , 2. , 3. , 8.5, 9.5, 10.5]])
I am wondering if there is a more computationally efficient way, as I've heard concatenate is slow
Numpy concatenate is slow: any alternative approach?
I'm thinking there might be a way with a combinatin of advanced indexing, .mean()
, and reshape, but I am not able to come up with anything that gives the desired array.