+10 XP
Array Operations on 2D Data
The power of NumPy: do calculations across rows or columns without loops.
python
import numpy as np
# Population voltage data
V = np.array([
[-70, -60, -50, 20], # neuron 0
[-65, -55, 10, 15], # neuron 1
[-68, -62, -45, -40], # neuron 2
])
# shape: (3 neurons, 4 time steps)
# Along time (axis=1): get mean voltage for each neuron
mean_V = V.mean(axis=1) # → [-40, -8.75, -53.75]
# Along neurons (axis=0): get mean voltage at each time
mean_V_time = V.mean(axis=0) # → [-67.67, -59, -28.33, -8.33]
# Find spikes: voltage > -55 for each neuron
threshold = -55
spikes = V > threshold # boolean array, same shape as V
print(spikes)
# [[False False False True]
# [False False True True]
# [False False False False]]
# Count spikes per neuron
spike_count = (V > threshold).sum(axis=1) # → [1, 2, 0]
print(f'Neuron spike counts: {spike_count}')Boolean indexing works on 2D arrays. axis=0 is down the rows. axis=1 is across columns.
The formula for working with 2D data: (data > threshold).sum(axis=X) counts how many elements exceed threshold for each row (axis=1) or column (axis=0). This pattern appears in almost every NMA analysis.