<jupyter_start><jupyter_text>Time Series Feature Extraction LibraryGitHub - https://github.com/fraunhoferportugal/tsfelDocs - https://tsfel.readthedocs.io/en/latest/<jupyter_code>import sys
sys.path.append(
    "/Users/dmitrys/Desktop/DataProjects/otus-advanced-ml/Materials/13-ts-feature-extraction/"
)
import warnings
warnings.filterwarnings("ignore")
import tsfel

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

from scipy import interpolate
from sklearn import preprocessing
from sklearn.feature_selection import VarianceThreshold
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
from sklearn.ensemble import RandomForestClassifier
from src.utils import *
INPUT_FOLDER_TRAIN = '../data/raw/UCI_HAR/train/InertialSignals/'
INPUT_FOLDER_TEST = '../data/raw/UCI_HAR/test/InertialSignals/'

LABELFILE_TRAIN = '../data/raw/UCI_HAR/train/y_train.txt'
LABELFILE_TEST = '../data/raw/UCI_HAR/test/y_test.txt'

train_signals, test_signals, train_labels, test_labels = read_data(
    input_folder_train=INPUT_FOLDER_TRAIN,
    input_folder_test=INPUT_FOLDER_TEST,
    labelfile_train=LABELFILE_TRAIN,
    labelfile_test=LABELFILE_TEST
)

train_signals, train_labels = randomize(train_signals, np.array(train_labels))
test_signals, test_labels = randomize(test_signals, np.array(test_labels))
# dataset sampling frequency
fs = 100
cfg_file = tsfel.get_features_by_domain('temporal')
cfg_file
train_signals.shape
EXTRACT = True

if EXTRACT:
    X_train = tsfel.time_series_features_extractor(cfg_file, train_signals, fs=fs)
    X_test = tsfel.time_series_features_extractor(cfg_file, test_signals, fs=fs)
    X_train.to_csv('../data/processed/X_train_tsfel.csv')
    X_test.to_csv('../data/processed/X_test_tsfel.csv')
else:
    X_train = pd.read_csv('../data/processed/X_train_tsfel.csv', index_col=0)
    X_test = pd.read_csv('../data/processed/X_test_tsfel.csv', index_col=0)
# Highly correlated features are removed
corr_features = tsfel.correlated_features(X_train)
X_train.drop(corr_features, axis=1, inplace=True)
X_test.drop(corr_features, axis=1, inplace=True)

# Remove low variance features
selector = VarianceThreshold()
X_train = selector.fit_transform(X_train)
X_test = selector.transform(X_test)
Y_train = np.array(train_labels)
Y_test = np.array(test_labels)
X_train.shape, X_test.shape
plt.plot(X_train[0])
pd.DataFrame(X_train).groupby(pd.Series(Y_train)).mean().T.plot()
clf = RandomForestClassifier(n_estimators=100)
clf.fit(X_train, Y_train)
print("Accuracy on training set is : {}".format(clf.score(X_train, Y_train)))
print("Accuracy on test set is : {}".format(clf.score(X_test, Y_test)))
Y_test_pred = clf.predict(X_test)
print(classification_report(Y_test, Y_test_pred))
activities_description = {
    1: 'walking',
    2: 'walking upstairs',\
    3: 'walking downstairs',
    4: 'sitting',
    5: 'standing',
    6: 'laying'
}   
plot_confusion(Y_test, Y_test_pred, activities_description.values())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11525759577278732, 'fraction_numerical': 0.006274768824306473, 'mean_word_length': 6.211904761904762, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '281703', 'license': 'MIT', 'n_tokens_mistral': 1130, 'n_tokens_neox': 1104, 'n_words': 182, 'path': 'OpenLessons/ts-feature-extraction/notebooks/5-HAR-tsfel.ipynb', 'repo_name': 'GithubJoelRo/Org3Public', 'size': '3028'}