Skip to content

Instantly share code, notes, and snippets.

@mblondel
Last active May 13, 2017 16:47
Show Gist options
  • Save mblondel/6914661 to your computer and use it in GitHub Desktop.
Save mblondel/6914661 to your computer and use it in GitHub Desktop.
Variable-length curve averaging
"""Variable-length curve averaging"""
# Author: Mathieu Blondel <mathieu@mblondel.org>
# License: BSD 3 clause
import numpy as np
from scipy.interpolate import interp1d
def curves_mean_std(X, Y, kind="linear"):
all_x = np.sort(np.unique(np.concatenate(X)))
funcs = [interp1d(x, y, bounds_error=False, kind=kind) \
for x, y in zip(X, Y)]
Y = np.array([f(all_x) for f in funcs])
return all_x, Y.mean(axis=0), Y.std(axis=0)
if __name__ == '__main__':
import pylab as pl
# Curve1 (50 points)
x1 = np.sort(np.random.random(50) * 10)
y1 = np.exp(-x1)
# Curve2 (45 points)
x2 = np.sort(np.random.random(45) * 10)
y2 = np.exp(-2 * x2)
# Curve3 (55 points)
x3 = np.sort(np.random.random(55) * 10)
y3 = np.exp(-0.5 * x3)
# Averaging
x, y_mean, y_std = curves_mean_std([x1, x2, x3],
[y1, y2, y3],
kind="linear")
# Plot
pl.plot(x1, y1, label="func 1")
pl.plot(x2, y2, label="func 2")
pl.plot(x3, y3, label="func 3")
pl.plot(x, y_mean, label="mean")
pl.legend(loc="upper right")
pl.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment