0
$\begingroup$

I have time-series as my data (one time-series per training example). I would like to encode the data within these series in a fixed-length vector of features using a keras model.

The problem is that my different examples' time-series don't have the same lengths. I haven't found a way of doing that. The problem of the encoder-decoder thing is that if the input lengths vary, the output lengths do this also. But I would like to have an output of length 10 let's say that would encode the data of the time-series, regardless of its length.

My question could have a strong link with: https://deepai.org/publication/towards-a-universal-neural-network-encoder-for-time-series but I haven't found any practical implementation.

Any help is very appreciated.

$\endgroup$
1
  • $\begingroup$ The use of the word "encoder" is confusing here because it reads like you are talking about preprocessing $\endgroup$
    – Kalanos
    Jun 4 at 20:52
1
$\begingroup$

Figure out the max length you want your time series to be and use linear interpolation to fill in missing values in the shorter series so that time series can be same length.

Example of this is:

def interpolate_to_length(time_series, final_length):
    curr_ts_length = time_series.shape[1]
    idx = np.array(range(curr_ts_length))
    idx_new = np.linspace(0, idx.max(), final_length)
    # linear interpolation
    f = interp1d(idx, time_series, kind='cubic')
    new_ts = f(idx_new)
    return new_ts

df = pd.DataFrame(data = {'col1': [1, 5, 10, 15, 20], 'col2': [1, 6, 11, 13, 15]})
print(df)
transposed_values = df.values.transpose()
print(transposed_values)
print(interpolate_to_length(transposed_values, 10))

Output should be:

   col1  col2
0     1     1
1     5     6
2    10    11
3    15    13
4    20    15
[[ 1  5 10 15 20]
 [ 1  6 11 13 15]]
[[ 1.          2.5743027   4.48331047  6.61728395  8.86648377 11.12391404
  13.34567901 15.55098308 17.76177412 20.        ]
 [ 1.          2.93415638  5.36213992  7.88888889 10.11934156 11.67489712
  12.55555556 13.1399177  13.82304527 15.        ]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.