<jupyter_start><jupyter_text>Classification of time-series using `tslearn`<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from time_series_generators import *

from tslearn.preprocessing import TimeSeriesScalerMinMax
from tslearn.utils import to_time_series_dataset
from tslearn.neighbors import KNeighborsTimeSeriesClassifier
from tslearn.neural_network import TimeSeriesMLPClassifier<jupyter_output><empty_output><jupyter_text>A test helper function<jupyter_code>def random_test(clf):
    idx = np.random.randint(0,n_series)
    plt.figure(figsize=(12,4))
    plt.plot(dataX[idx])
    plt.show()
    x_test = TimeSeriesScalerMinMax().fit_transform(to_time_series_dataset(dataX[idx]))
    pred = int(clf.predict(x_test))
    print("Prediction: This belongs to class", pred)
    print("-"*50)
    print("Ground truth: This belongs to class:", dataY[idx])
    print("\n\n")<jupyter_output><empty_output><jupyter_text>An example time-series generation (with low fraction of anomalies)<jupyter_code>a = gen_series_anomaly(size=1000,anomaly_frac=0.03,anomaly_scale=0.25,
                    loc=4,scale=2,)
plt.figure(figsize=(12,4))
plt.plot(np.arange(len(a)),a)
plt.show()<jupyter_output><empty_output><jupyter_text>An example time-series generation (with high fraction of anomalies)<jupyter_code>a = gen_series_anomaly(size=1000,anomaly_frac=0.3,anomaly_scale=0.25,
                    loc=4,scale=2,)
plt.figure(figsize=(12,4))
plt.plot(np.arange(len(a)),a)
plt.show()<jupyter_output><empty_output><jupyter_text>Generating dataframe of time-series- 1,000 time-series (each of length 100 points)- 75% of the time-series are _normal_- 25% of the time-series contain anomalies<jupyter_code>n_series = 1000
df = gen_ts_dataframe(n_series,prob_anomolous=0.25,size=100)<jupyter_output><empty_output><jupyter_text>Data preparation for `tslearn`<jupyter_code>dataX = list(df['ts'])
dataY = list(df['anomolous'])
X = to_time_series_dataset(dataX)
y = dataY
X_scaled = TimeSeriesScalerMinMax().fit_transform(X)<jupyter_output><empty_output><jupyter_text>kNN fitting<jupyter_code>knn = KNeighborsTimeSeriesClassifier(n_neighbors=10)
knn.fit(X_scaled, y)
for _ in range(5):
    random_test(knn)<jupyter_output><empty_output><jupyter_text>Neural network fitting<jupyter_code>mlp = TimeSeriesMLPClassifier(hidden_layer_sizes=(64, 64),random_state=0,learning_rate_init=0.01,verbose=True)
mlp.fit(X_scaled,y)
mlp.score(X_scaled,y)
for _ in range(5):
    random_test(mlp)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12019040063466878, 'fraction_numerical': 0.02697342324474415, 'mean_word_length': 7.607508532423208, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '543150', 'license': 'MIT', 'n_tokens_mistral': 999, 'n_tokens_neox': 931, 'n_words': 144, 'path': 'Notebooks/classify-time-series.ipynb', 'repo_name': 'tirthajyoti/Synthetic-data-gen', 'size': '2521'}