# Fog-Computing-Based Heartbeat Detection and Arrhythmia Classification Using Machine Learning
## 4 Implementation Details
### Arrhythmia-Classification Module  
For the arrhythmia-classification module, we designed a set of classifiers using a LSTM neural-network architecture. We trained the classifiers following an _intra-patient_ approach, resulting in a unique classifier model suitable for all patients. For this purpose, we partitioned the MIT-BIT dataset vertically such as to include a fraction of heartbeats from each patient; in particular, we used the first 50% of each ECG record for training the classifier. We tuned the model hyperparameters using the subsequent 10% of the dataset. The remaining part of the dataset was used for evaluating the overall performance. In this module we did not employ a cross-validation process. The relative proportions of the classes of the available training examples influences the performance of an LSTM classifier. If a few classes dominate the training-data examples, then the  
Figure 4: Fine tuning of the performance of the heartbeat detection module by evaluating the achieved precision, recall and F1 score when using different training set lengths (\(t\)) expressed as a percentage of the total size of the ECG recording and different window sizes. Figure 5: Erroneous detection of an \(R\) peak on a specific segment of ECG recording labeled _”101”_ of the dataset when \(k=1\) and using simple majority voting. The signal plotted is the result of the filtering and the squaring phases. classifier training process is heavily influenced by these classes [54]. The operation of the heart is characterized by large periods of normal activity and short periods of arrhythmic activity. Therefore, the ECG data collected will contain a large volume of normal activity and a small volume (if any at all) of abnormal activity. Many techniques are known to overcome this issue; for example by adopting a penalization model, or by undersampling/oversampling data. We decided to balance the MIT-BIH dataset by augmenting the number of samples in the least populated classes. We increased the number of samples in the under-represented class \(c\) as follows:  
```
for each beat \(b\in c:\) \(r_{i}=\) random number in \((0,1)\) r = random vector s.t. \(size(r)=size(b)\) and \(r_{j}=r_{i},\ \forall j\in[0,size(r)]\) synthetic\(\_\)sample \(=b+r\) Training\(\_\)set \(\cup\) syntethic\(\_\)sample
```  
where \(Training\_\)set is the set of beats for training, \(r_{i}\) is the generic element of the random vector \(r\), and the operator \(+\) stands for vector addition. The main idea used is that the shape of a heartbeat is the most relevant feature to discriminate between arrhythmic beats. Adding the same value \(r_{i}\) to each sample in the beat would in fact preserve the shape, providing more training data to the network, as if they were new data. Note that an approach that simply replicates the instances of the minority classes without any further processing would not suffice as it would lead the network to _overfit_ on such data, memorizing the specific amplitudes without being capable of generalizing the unseen data. For this reason, the approach presented above uses randomization. Although this approach does not eliminate the problem of overfitting completely (additional real data would be required for that), it reduces the effect. Moreover, to reduce the number of instances belonging to the over-represented class (i.e., the normal class (N)), we use a _random-sampling technique_, which consists of randomly selecting a subset of heartbeats in the normal class. We reduce the cardinality of the normal class by a _reduction factor_, more precisely: \(|N_{r}|=\frac{|N|}{r}\), where \(|N|\) denotes the cardinality of the set of instances initially belonging to the normal class, \(|N_{r}|\) is the cardinality of the reduced set, and \(r\) is the reduction factor.