+10 XP
Population Statistics with NumPy
np.mean(V, axis=0) averages over neurons (rows). np.std(V, axis=0) does the same for standard deviation. The result shape is (T,) — one stat per time step.
Axis intuition for V[N, T]:
• np.mean(V, axis=0) → collapse rows → shape (T,) → mean voltage at each time
• np.mean(V, axis=1) → collapse columns → shape (N,) → mean voltage of each neuron over time
• np.mean(V) → grand mean → one number
python
import numpy as npimport matplotlib.pyplot as pltnp.random.seed(42)N, T = 20, 150dt=1e-3; tau=20e-3; el=-60e-3; vr=-70e-3; vth=-50e-3r=100e6; i_mean=25e-11; i_std=0.5e-11V = np.zeros((N, T))V[:, 0] = elfor n in range(N): np.random.seed(n) for t_step in range(T - 1): t = t_step * dt i_t = i_mean + i_std * np.random.normal() * np.sqrt(dt)→ V[n, t+1] = next step value; V[n, t] = current value for RHS
V[n, t_step+1] = V[n, t_step] + (dt/tau)*(el - V[n,t_step] + r*i_t) if V[n, t_step+1] >= vth: V[n, t_step+1] = vr→ np.mean(V, axis=0): average over N neurons → shape (T,)
t_ms = np.arange(T) * dt * 1000v_mean = np.mean(V, axis=0) * 1000v_std = np.std(V, axis=0) * 1000→ fill_between: shaded band from mean-std to mean+std
fig, ax = plt.subplots(figsize=(8, 3))ax.plot(t_ms, v_mean, color='#1CB0F6', label='Mean V')ax.fill_between(t_ms, v_mean-v_std, v_mean+v_std, alpha=0.3, color='#1CB0F6', label='±1 std')ax.set_xlabel('Time (ms)'); ax.set_ylabel('V (mV)')ax.set_title(f'Population Average: N={N}')ax.legend(); plt.show()The shaded band shows trial-to-trial variability across 20 neurons. Wider band = more variable population.