<jupyter_start><jupyter_text>TSFRESH Human Activity Recognition ExampleThis example show shows how to use [tsfresh](https://tsfresh.readthedocs.io/) to exctract useful features from multiple timeseries and use them to improve classification performance.<jupyter_code>%matplotlib inline
import matplotlib.pylab as plt
from tsfresh.examples.har_dataset import download_har_dataset, load_har_dataset, load_har_classes
import seaborn as sns
from tsfresh import extract_features, extract_relevant_features, select_features
from tsfresh.utilities.dataframe_functions import impute
from tsfresh.feature_extraction import FeatureExtractionSettings
from sklearn.tree import DecisionTreeClassifier
from sklearn.cross_validation import train_test_split
from sklearn.metrics import classification_report
import pandas as pd
import numpy as np<jupyter_output><empty_output><jupyter_text>Load and visualize dataThe dataset consists of timeseries for 7352 accelerometer readings. Each reading represents an accelerometer reading for 2.56 sec at 50hz (for a total of 128 samples per reading). Furthermore, each reading corresponds one of six activities (walking, walking upstairs, walking downstairs, sitting, standing and laying)For more information, or to fetch dataset, go to https://archive.ics.uci.edu/ml/datasets/Human+Activity+Recognition+Using+Smartphones<jupyter_code># fetch dataset from uci
download_har_dataset()
df = load_har_dataset()
df.head()
df.shape
plt.title('accelerometer reading')
plt.plot(df.ix[0,:])
plt.show()<jupyter_output><empty_output><jupyter_text>Extract Features<jupyter_code>extraction_settings = FeatureExtractionSettings()
extraction_settings.IMPUTE = impute    # Fill in Infs and NaNs
# transpose since tsfresh reads times series data column-wise, not row-wise
df_t = df.copy().transpose()
df_t.shape
# rearrange sensor readings column-wise, not row-wise

master_df = pd.DataFrame(df_t[0])
master_df['id'] = 0

# grab first 500 readings to save time
for i in range(1, 500):
    temp_df = pd.DataFrame(df_t[i])
    temp_df['id'] = i
    master_df = pd.DataFrame(np.vstack([master_df, temp_df]))
print(master_df.shape)
master_df.head()

%time X = extract_features(master_df, column_id=1, feature_extraction_settings=extraction_settings);
# 206 features are extracted for each reading

X.shape<jupyter_output><empty_output><jupyter_text>Train and evaluate classifier<jupyter_code>y = load_har_classes()[:500]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.2)
cl = DecisionTreeClassifier()
cl.fit(X_train, y_train)
print(classification_report(y_test, cl.predict(X_test)))<jupyter_output><empty_output><jupyter_text>Compare against naive classification accuracyBy extracting using time-series features (as opposed to using raw data points), we can meaningfully increase classification accuracy.<jupyter_code>X_1 = df.ix[:499,:]
X_1.shape
X_train, X_test, y_train, y_test = train_test_split(X_1, y, test_size=.2)
cl = DecisionTreeClassifier()
cl.fit(X_train, y_train)
print(classification_report(y_test, cl.predict(X_test)))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08576051779935275, 'fraction_numerical': 0.01197411003236246, 'mean_word_length': 7.586111111111111, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '449377', 'license': 'MIT', 'n_tokens_mistral': 1012, 'n_tokens_neox': 958, 'n_words': 263, 'path': 'notebooks/human_activity_recognition_example.ipynb', 'repo_name': 'mynameisvinn/tsfresh', 'size': '3090'}