<jupyter_start><jupyter_text>Time series interpolating with sktime Suppose we have a set of time series with different lengths, i.e. different number of time points. Currently, most of sktime's functionality requires equal-length time series, so to use sktime, we need to first converted our data into equal-length time series. In this tutorial, you will learn how to use the `TSInterpolator` to do so.<jupyter_code>import random
import numpy as np
import pandas as pd

from sklearn.pipeline import Pipeline
from sktime.datasets import load_basic_motions
from sktime.transformers.series_as_features.compose import ColumnConcatenator
from sktime.classification.compose import TimeSeriesForestClassifier

from sklearn.model_selection import train_test_split<jupyter_output><empty_output><jupyter_text>Ordinary situationHere is a normal situation, when all time series have same length. We load a toy data set from sktime and train a classifier.<jupyter_code>X, y = load_basic_motions(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y)

steps = [
    ('concatenate', ColumnConcatenator()),
    ('classify', TimeSeriesForestClassifier(n_estimators=100))]
clf = Pipeline(steps)
clf.fit(X_train, y_train)
clf.score(X_test, y_test)<jupyter_output><empty_output><jupyter_text>If time series are unequal length, sktime's algorithm may raise an errorNow we are going to spoil the data set a little bit by randomly cutting the time series. This leads to unequal-length time series. Consequently, we have an error while attept to train a classifier.<jupyter_code># randomly cut the data series in-place
def random_cut(df):
    for row_i in range(df.shape[0]):
        for dim_i in range(df.shape[1]):
            ts = df.iloc[row_i][f'dim_{dim_i}']
            df.iloc[row_i][f'dim_{dim_i}'] = pd.Series(ts.tolist()[:random.randint(len(ts)-5, len(ts)-3)]) # here is a problem

            
X, y = load_basic_motions(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y)

for df in [X_train, X_test]:
    random_cut(df)
    
try:
    steps = [
        ('concatenate', ColumnConcatenator()),
        ('classify', TimeSeriesForestClassifier(n_estimators=100))]
    clf = Pipeline(steps)
    clf.fit(X_train, y_train)
    clf.score(X_test, y_test)
except ValueError as e:
    print(f"IndexError: {e}")<jupyter_output>IndexError: Tabularization failed, it's possible that not all series were of equal length<jupyter_text>Now the interpolator entersNow we use our interpolator to resize time series of different lengths to user-defined length. Internally, it uses linear interpolation from scipy and draws equidistant samples on the user-defined number of points. After interpolating the data, the classifier works again.<jupyter_code>from sktime.transformers.series_as_features.interpolate import TSInterpolator 

X, y = load_basic_motions(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y)
            
for df in [X_train, X_test]:
    random_cut(df)
    
steps = [
    ('transform', TSInterpolator(50)),
    ('concatenate', ColumnConcatenator()),
    ('classify', TimeSeriesForestClassifier(n_estimators=100))]
clf = Pipeline(steps)
clf.fit(X_train, y_train)
clf.score(X_test, y_test)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08873929008567931, 'fraction_numerical': 0.004589963280293758, 'mean_word_length': 5.214828897338403, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '163083', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1086, 'n_tokens_neox': 1055, 'n_words': 320, 'path': 'examples/interpolation.ipynb', 'repo_name': 'BandaSaiTejaReddy/sktime', 'size': '3268'}