<jupyter_start><jupyter_text><div id="image_img"        class="header_image_7">         Rock, Paper or Scissor Game - Train and Classify [Volume 2]                  Difficulty Level:                                                                                                                                                                                                   Tags                    train_and_classify&9729;machine-learning&9729;features&9729;extraction                                                 Previous Notebooks that are part of "Rock, Paper or Scissor Game - Train and Classify" module    Rock, Paper or Scissor Game - Train and Classify [Volume 1] | Experimental Setup Following Notebooks that are part of "Rock, Paper or Scissor Game - Train and Classify" module    Rock, Paper or Scissor Game - Train and Classify [Volume 3] | Training a Classifier     Rock, Paper or Scissor Game - Train and Classify [Volume 4] | Performance Evaluation                         &9740; After the presentation of data acquisition conditions on the previous Jupyter Notebook , we will follow our Machine Learning Journey by specifying which features will be extracted.                        "Features" are numerical parameters extracted from the training data (in our case physiological signals acquired when executing gestures of "Rock, Paper or Scissor" game), characterizing objectively the training example.            A good feature is a parameter that has the ability to separate the different classes of our classification system, i.e, a parameter with a characteristic range of values for each available class.             Starting Point (Setup)List of Available Classes:    "No Action" [When the hand is relaxed]    "Paper" [All fingers are extended]    "Rock" [All fingers are flexed]    "Scissor" [Forefinger and middle finger are extended and the remaining ones are flexed]                                                                                                                    Paper                            Rock                            Scissor            Acquired Data:    Electromyography (EMG) | 2 muscles | Adductor pollicis and  Flexor digitorum superficialis    Accelerometer (ACC) | 1 axis | Sensor parallel to the thumb nail (Axis perpendicular) Protocol/Feature ExtractionExtracted Features    [From] EMG signal            Standard Deviation &9734;        Maximum sampled value &9757;       Zero-Crossing Rate &9740;        Standard Deviation of the absolute signal &9735;        [From] ACC signal            Average Value &9737;        Standard Deviation &9734;        Maximum sampled value &9757;        Zero-Crossing Rate &9740;        Slope of the regression curve &9741;    Formal definition of parameters&9757; | Maximum Sample Value of a set of elements is equal to the last element of the sorted set&9737; | $\mu = \frac{1}{N}\sum_{i=1}^N (sample_i)$&9734; | $\sigma = \sqrt{\frac{1}{N}\sum_{i=1}^N(sample_i - \mu_{signal})^2}$&9740; | $zcr = \frac{1}{N - 1}\sum_{i=1}^{N-1}bin(i)$ &9735; | $\sigma_{abs} = \sqrt{\frac{1}{N}\sum_{i=1}^N(|sample_i| - \mu_{signal_{abs}})^2}$&9741; | $m = \frac{\Delta signal}{\Delta t}$... being $N$ the number of acquired samples (that are part of the signal), $sample_i$ the value of the sample number $i$, $signal_{abs}$ the absolute signal, $\Delta signal$ is the difference between the y coordinate of two points of the regression curve and $\Delta t$ the difference between the x (time) coordinate of the same two points of the regression curve.... and $bin(i)$ a binary function defined as:$bin(i) = \begin{cases} 1, & \mbox{if } signal_i \times signal_{i-1} \leq 0 \\ 0, & \mbox{if } signal_i \times signal_{i-1}>0 \end{cases}$ 0 - Import of the needed packages for a correct execution of the current Jupyter Notebook<jupyter_code># Package that ensures a programatically interaction with operating system folder hierarchy.
from os import listdir

# Package used for clone a dictionary.
from copy import deepcopy

# Functions intended to extract some statistical parameters.
from numpy import max, std, average, sum, absolute

# With the following import we will be able to extract the linear regression parameters after 
# fitting experimental points to the model.
from scipy.stats import linregress

# biosignalsnotebooks own package that supports some functionalities used on the Jupyter Notebooks.
import biosignalsnotebooks as bsnb<jupyter_output><empty_output><jupyter_text>1 - Loading of all signals that integrates our training samples (storing them inside a dictionary)The acquired signals are stored inside a folder which can be accessed through a relative path "../../signal_samples/classification_game/data" 1.1 - Identification of the list of files/examples<jupyter_code># Transposition of data from signal files to a Python dictionary.
relative_path = "../../signal_samples/classification_game"
data_folder = "data"

# List of files (each file is a training example).
list_examples = listdir(relative_path + "/" + data_folder)
print(list_examples)<jupyter_output><empty_output><jupyter_text>The first digit of filename identifies the class to which the training example belongs and the second digit is the trial number (&lt;class&gt;_&lt;trial&gt;.txt) 1.2 - Access the content of each file and store it on the respective dictionary entry<jupyter_code># Initialization of dictionary.
signal_dict = {}

# Scrolling through each entry in the list.
for example in list_examples:
    if ".txt" in example: # Read only .txt files.
        # Get the class to which the training example under analysis belong.
        example_class = example.split("_")[0]

        # Get the trial number of the training example under analysis.
        example_trial = example.split("_")[1].split(".")[0]

        # Creation of a new "class" entry if it does not exist.
        if example_class not in signal_dict.keys():
            signal_dict[example_class] = {}

        # Load data.
        complete_data = bsnb.load(relative_path + "/" + data_folder + "/" + example)

        # Store data in the dictionary.
        signal_dict[example_class][example_trial] = complete_data<jupyter_output><empty_output><jupyter_text>1.3 - Definition of the content of each channel<jupyter_code># Channels (CH1 Flexor digitorum superficialis | CH2 Aductor policis | CH3 Accelerometer axis Z).
emg_flexor = "CH1"
emg_adductor = "CH2"
acc_z = "CH3"<jupyter_output><empty_output><jupyter_text>2 - Extraction of features according to the signal under analysisThe extracted values of each feature will be stored in a dictionary with the same hierarchical structure as "signal_dict"<jupyter_code># Clone "signal_dict".
features_dict = deepcopy(signal_dict)

# Navigate through "signal_dict" hierarchy.
list_classes = signal_dict.keys()
for class_i in list_classes:
    list_trials = signal_dict[class_i].keys()
    for trial in list_trials:
        # Initialise "features_dict" entry content.
        features_dict[class_i][trial] = []
        
        for chn in [emg_flexor, emg_adductor, acc_z]:
            # Temporary storage of signal inside a reusable variable.
            signal = signal_dict[class_i][trial][chn]
            
            # Start the feature extraction procedure accordingly to the channel under analysis.
            if chn == emg_flexor or chn == emg_adductor: # EMG Features.
                # Converted signal (taking into consideration that our device is a "biosignalsplux", the resolution is
                # equal to 16 bits and the output unit should be in "mV").
                signal = bsnb.raw_to_phy("EMG", device="biosignalsplux", raw_signal=signal, resolution=16, option="mV")
                
                # Standard Deviation.
                features_dict[class_i][trial] += [std(signal)]
                # Maximum Value.
                features_dict[class_i][trial] += [max(signal)]
                # Zero-Crossing Rate.
                features_dict[class_i][trial] += [sum([1 for i in range(1, len(signal)) 
                                                       if signal[i]*signal[i-1] <= 0]) / (len(signal) - 1)]
                # Standard Deviation of the absolute signal.
                features_dict[class_i][trial] += [std(absolute(signal))]
            else: # ACC Features.
                # Converted signal (taking into consideration that our device is a "biosignalsplux", the resolution is
                # equal to 16 bits and the output unit should be in "g").
                signal = bsnb.raw_to_phy("ACC", device="biosignalsplux", raw_signal=signal, resolution=16, option="g")
                
                # Average value.
                features_dict[class_i][trial] += [average(signal)]
                # Standard Deviation.
                features_dict[class_i][trial] += [std(signal)]
                # Maximum Value.
                features_dict[class_i][trial] += [max(signal)]
                # Zero-Crossing Rate.
                features_dict[class_i][trial] += [sum([1 for i in range(1, len(signal)) 
                                                       if signal[i]*signal[i-1] <= 0]) / (len(signal) - 1)]
                # Slope of the regression curve.
                x_axis = range(0, len(signal))
                features_dict[class_i][trial] += [linregress(x_axis, signal)[0]]<jupyter_output><empty_output><jupyter_text>Each training array has the following structure/content:\[$\sigma_{emg\,flexor}$, $max_{emg\,flexor}$, $zcr_{emg\,flexor}$, $\sigma_{emg\,flexor}^{abs}$, $\sigma_{emg\,adductor}$, $max_{emg\,adductor}$, $zcr_{emg\,adductor}$, $\sigma_{emg\,adductor}^{abs}$, $\mu_{acc\,z}$, $\sigma_{acc\,z}$, $max_{acc\,z}$, $zcr_{acc\,z}$, $m_{acc\,z}$\]  3 - Storage of the content inside the filled "features_dict" to an external file (.json )With this procedure it is possible to ensure a "permanent" memory of the results produced during feature extraction, reusable in the future by simple reading the file (without the need to reprocess again).<jupyter_code># Package dedicated to the manipulation of json files.
from json import dump

filename = "classification_game_features.json"

# Generation of .json file in our previously mentioned "relative_path".
# [Generation of new file]
with open(relative_path + "/features/" + filename, 'w') as file:
    dump(features_dict, file)<jupyter_output><empty_output><jupyter_text>We reach the end of the "Classification Game" second volume. Now all the features of training examples are in our possession.If you are feeling your interest increasing, please jump to the next volume We hope that you have enjoyed this guide. biosignalsnotebooks is an environment in continuous expansion, so don't stop your journey and learn more with the remaining Notebooks  ! **Auxiliary Code Segment (should not be replicated bythe user)**<jupyter_code>from biosignalsnotebooks.__notebook_support__ import css_style_apply
css_style_apply()
%%html
<script>
    // AUTORUN ALL CELLS ON NOTEBOOK-LOAD!
    require(
        ['base/js/namespace', 'jquery'],
        function(jupyter, $) {
            $(jupyter.events).on("kernel_ready.Kernel", function () {
                console.log("Auto-running all cells-below...");
                jupyter.actions.call('jupyter-notebook:run-all-cells-below');
                jupyter.actions.call('jupyter-notebook:save-notebook');
            });
        }
    );
</script><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09234667132622751, 'fraction_numerical': 0.012231347195526822, 'mean_word_length': 2.828428093645485, 'pattern_counts': {'":': 0, '<': 34, '<?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': 21, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '4255', 'license': 'MIT', 'n_tokens_mistral': 3190, 'n_tokens_neox': 2977, 'n_words': 1113, 'path': 'biosignalsnotebooks_notebooks/Categories/Train_And_Classify/classification_game_volume_2.ipynb', 'repo_name': 'csavur/biosignalsnotebooks', 'size': '11446'}