I have an algorithm that generates two sequences x_values
and y_values
of the same length len(x_values) = len(y_values)
. However, at each different run of the algorithm the length of these two sequences is slightly higher or lower, len(x_values_run1) != len(x_values_run2)
.
I want to eventually make a plot with a single x-axis sequence and a single y-axis sequence and for this I need to average across all runs. If the sequences all had the same length, then I could simply do np.vstack(list_x_values).mean(axis=0)
. However, they have differen lengths.
How can I do this? Perhaps with interpolation.
Minimal Working Example
import numpy as np
def algorithm():
"""Creates 2 list of the same length, different each time."""
length = np.random.randint(low=18, high=22)
x_values = np.random.randn(length)
y_values = 2*x_values + 1
return x_values, y_values
n_runs = 10
results = [algorithm() for _ in range(n_runs)]
list_x_values = [res[0] for res in results]
list_y_values = [res[1] for res in results]
Important Information
The sequence of x
values will typically be on a logarithmic scale.
4