Creating an array with uniform sampling from unevenly selected data in NumPy

The problem is that I want to reduce the amount of data for graphs and analysis. I use Python and Numpy. The data is unevenly selected, so there is an array of timestamps and an array of corresponding values. I want this to be at least a certain amount of time between data points. I have a simple solution written in Python where the indexes are found where there is at least one second between the samples:

import numpy as np

t = np.array([0, 0.1, 0.2, 0.3, 1.0, 2.0, 4.0, 4.1, 4.3, 5.0 ]) # seconds
v = np.array([0, 0.0, 2.0, 2.0, 2.0, 4.0, 4.0, 5.0, 5.0, 5.0 ])

idx = [0]
last_t = t[0]
min_dif = 1.0 # Minimum distance between samples in time
for i in range(1, len(t)):
    if last_t + min_dif <= t[i]:
        last_t = t[i]
        idx.append(i)

If we look at the result:

--> print idx
[0, 4, 5, 6, 9]

--> print t[idx]
[ 0.  1.  2.  4.  5.]

The question is, how can this be done more efficiently, especially if the arrays are really long? Are there any built-in NumPy or SciPy methods that do something like this?

+5
source share
4

, @1443118, pandas, - np.histogram.

-, ( min_dif s), :

>>> bins = np.arange(t[0], t[-1]+min_dif, min_dif) - 1e-12

t[-1]+min_dif , , , -1e-12 , , 4.0 : , .

>>> (counts, _) = np.histogram(t, bins)
>>> counts
array([4, 1, 1, 0, 3])
>>> counts.cumsum()
array([4, 5, 6, 6, 9])

, v[0:4] - , v[4:5] ... .

+2

, , numpy.interp:

vsampled = numpy.interp(numpy.arange(t[0], t[-1]), t, v)

. t, .

+1

, , , , , , , . ( ) :

# Assuming that t is sorted...
# Create all full seconds.
seconds = np.arange(int(t[0]), int(t[-1]) + 1)

# find the indexes for all
idx = np.searchsorted(t, seconds)
idx = np.unique(idx) # there might be duplicates if a second has no data in it.

, ( 0 2 )...

+1

pandas. , . . .

+1

All Articles