2

I am currently researching on Time Series Classification in Python and so far I have tried the sktime library. As I understand, most of the methods when doing prediction, would need a time series:

prediction (a time series) = class_label

My question is: How do I search and apply the algorithms, which when making a prediction, require a single data point at time t0, and not a complete time series? Like:

prediction (data at time t0) = class_label
artemis
  • 6,857
  • 11
  • 46
  • 99
  • If some functions need just one element from time series instead of whole series then you do `some_func(time_serie[k])`, i.e. `time_serie[k]` gives `k-th` item of serie, this way you just pass one time point instead of whole serie. Also you may need to do `for` loop for all such `k`s. – Arty Nov 02 '20 at 15:00
  • If your provide some of your code that doesn't work or more details on what you want to achieve, then we can help you solving task! – Arty Nov 02 '20 at 15:02
  • Thank you for your comment, Arty. Let us take an example of the ROCKET algorithm for Time Series Classification: https://medium.com/towards-artificial-intelligence/rocket-fast-and-accurate-time-series-classification-f54923ad0ac9 It can only make a prediction on the time series, right? Are there ML algorithms for Time Series Classification, which take as an input for prediction a datapoint at time t0, and not a complete time series? Thank you. – Sofiia Kosovan Nov 02 '20 at 15:09
  • Maybe you have already some code examples of these two cases that you tried and that don't work? Basically as I told before if you have a `time_serie` e.g. as `NumPy` array, and if some function like `func1()` accepts whole time serie then you just do `result1 = func1(time_serie)`. If other function like `func2()` accepts only one time point then you do `result2 = func2(time_serie[k])`, i.e. you take `k`-th element of array and pass it to function. – Arty Nov 02 '20 at 15:32
  • Hey Arty I do not have code examples, because my question is a general question. I have been doing research on Time Series Analysis, but I am not sure it applies to my data. What I am asking for here, is a hint on which resources I could look at. I have data, where first column is a timestamp, then I have 20 features and then a label (binary classification problem). I will need to predict label at every timestamp. I am not sure that sktime methods suit my problem, as they work on time series only. So, my question is, is my problem still a time series problem, or is it classic ML problem? – Sofiia Kosovan Nov 03 '20 at 09:14
  • All machine learning kits can work both with data consisting of several elements and with data consisting of one element. The only thing to know that data of one element is just a data with several elements having 1 entry. In other words if you have any data for single time point, lets say you have some numpy 1D array `a0` (for single time point), e.g. it has 10 features, meaning that this array has shape `(10,)`, in order for it to make multi-point you just need it to have extra dimension of size 1,i.e. to make shape `(1, 10)`, this is achieved by `amult=a0[None]` then use `amult` in `sktime` – Arty Nov 03 '20 at 09:36
  • Posted more detailed response as [my answer](https://stackoverflow.com/a/64660657/941531). – Arty Nov 03 '20 at 10:05

1 Answers1

1

The only difference between data for single time point and data for multiple time points is that single time point data is just same multiple time points data but having just 1 entry.

In that sense any function working with multiple points can also work with single point just by providing to them array consisting of 1 entry with this time point data.

Suppose you have some data for single time point, this data is any array a0 of any shape, for example you have 1D array a0 of shape (10,) i.e. having 10 features.

Then you can convert this array to 2D array of multiple time point datas just by converting it to shape (1, 10) this is done for numpy array by short syntax a_mult = a0[None] or another way to do that with same result is a_mult = np.expand_dims(a0, 0), just longer syntax.

After your a0 is converted to a_mult you can use a_mult in any function later that accepts multiple time points datas. Example code below:

Try it online!

import numpy as np
a0 = np.array([1,2,3,4,5])
print(a0.shape, a0)
a_mult = a0[None]
print(a_mult.shape, a_mult)
# Now call your multi-point function f_predict(a_mult)

outputs

(5,) [1 2 3 4 5]
(1, 5) [[1 2 3 4 5]]

Similar thing can be done for opposite case, i.e. when you have multiple time points datas and you want to give them to a function that accepts just one time point. This is also achieved easily by indexing your array and having loop, e.g. if you have a_mult array with multiple points then to convert it to single point just do a0 = a_mult[k] where k is any integer ranging from 0 through number_of_time_points - 1. k can be iterated through regular Python loop. Example code below:

Try it online!

import numpy as np
a_mult = np.array([
    [1,2,3,4,5],
    [6,7,8,9,10],
])
print(a_mult.shape)
print(a_mult)
f_predict = lambda x: np.sum(x) # Simple function, just does a sum of elements
res = np.zeros((a_mult.shape[0],), dtype = np.int64)
for k in range(a_mult.shape[0]):
    a0 = a_mult[k]
    # Now call your single-point function f_predict
    # and store result into res
    res[k] = f_predict(a0)
print(res)
# Same as in code above can be achieved by next short syntax
res = np.array([f_predict(a_mult[k]) for k in range(a_mult.shape[0])])
print(res)

outputs

(2, 5)
[[ 1  2  3  4  5]
 [ 6  7  8  9 10]]
[15 40]
[15 40]
Arty
  • 14,883
  • 6
  • 36
  • 69
  • Arty, I understand, what you mean. But that does not help me further. sktime requires a special type of input data: https://www.sktime.org/en/latest/examples/loading_data.html Please see at [10], under the line "As shown below, applying the from_long_to_nested method..". Do most of Time Series Classification algorithms require data in that format? The algorithms of sktime work on time series. So, as I understand, they cannot be applied on a data at a particulat time stamp, right? – Sofiia Kosovan Nov 03 '20 at 11:19
  • @SofiiaKosovan As I told whatever algorithm or library is, either if it can work on multiple or single entry data, doesn't matter. You can just easily convert data from any format to any other. Simple case of conversion `single<->multiple` I showed in my answer above, but if there is some special non-array type then you need some extra conversion code. But I can tell in advance that anything can be converted, so don't worry, your code and data will finally work with sktime. To provide you further help you need to start with some coding and trying and give us that code if it doesn't work. – Arty Nov 03 '20 at 11:34
  • @SofiiaKosovan You have to read docs and investigate yourself what data format sktime needs and the way to convert your data to this format. But good news! Any format can be converted to any, if you have single time points they can be easily converted to multiple points. But you have to start doing at least something, so that some errors appear and you can update your question post with provided errors. Also in your question good if you provide some of your data in format that you have, maybe someone knows already what format sktime needs and can help you straight away. – Arty Nov 03 '20 at 11:55
  • Thank you for your answers, Arty – Sofiia Kosovan Nov 03 '20 at 14:22