102 Machine Learning Methods in Electrocardiography Classification Iryna Mykoliuk1, Daniel Jancarczyk1, Mikolaj Karpinski1, Viktor Kifer2 1. Department of Computer Science and Automatics, University of Bielsko-Biala, POLAND, Bielsko-Biala, 2 Willowa St, email: i.mykoliuk@gmail.com, djancarczyk@ath.bielsko.pl, mkarpinski@ath.bielsko.pl 2. Faculty of Computer Information Systems and Software Engineering, Ternopil Ivan Puluj National Technical University, UKRAINE, Ternopil, 56 Ruska St, email: kifervictor@gmail.com Abstract. This document reports results that tend to Electrocardiogram confirm the applicability of the machine learning combined with signal processing for automatic atrial ECG is the process of recording the electrical activity of fibrillation detection from a short single lead the heart over a period of time using electrodes placed on the electrocardiography recording. skin. These electrodes detect the tiny electrical changes on Keywords: machine learning, signal, ECG, the skin that arises from the heart muscles. classification, heartbeat. In a conventional 12-lead ECG, 6 electrodes are placed on the surface of the chest, 2 on hands, and 2 on legs. The I. INTRODUCTION overall magnitude of the heart’s electrical potential is then Automated electrocardiography (ECG) analysis has a measured from 12 different angles (“leads”) and is recorded number of ground tasks which include noise removal, QRS over a period of time. A healthy heart has a specific order of detection, P and T waves detection etc. The first two polarization and depolarization during each heartbeat. It starts problems already have a number of techniques which provide with the sinoatrial node, then spreads through the atrium to good results. In particular band pass filters, Fourier based atrioventricular node, and then to ventricles. analysis and transforms and wavelet transformations are commonly used for noise reduction. Pan Tompkins algorithm, various transforms like Wavelet, Hilbert and Empirical Mode Decomposition combined with some decision logic can identify QRS complexes with detection rates over 99%. P and T wave have small amplitudes, which makes them less noise resistant. But position and shape of the waves are the important components of ECG analysis. With the most recent approaches based on advanced Kalman filters and wavelet transforms, detection accuracy for those waves reached over 90%. In general, ECG classification solutions tend to be class Fig. 1.Typical ECG trace testing, when a particular record is checked against some abnormal rhythm pattern. This happens because there’s a The graph of voltage versus time produced by this large variety of ECG shape changes, which are hard to handle noninvasive procedure is called electrocardiogram. Each by a single algorithm. And there is a strong tendency in last ECG consists of periodic PQRST complexes which represent decade of moving from a threshold-based analysis (which a cycle of heart activity. In particular, one complex has: suffers from introducing to validation dataset ECG recordings 1. P-wave (atrial contraction) from new patients or recorded with new devices) to data- 2. QRS-complex (contraction of the ventricles) driven approaches which include classical supervised 3. T-wave (relaxation of the ventricles) machine-learning models and neural networks. The duration of PQRST might differ depending on the heart rate. There are a couple of ways how to measure it, one II. BIOLOGICAL BACKGROUND of the most common is called RR-interval, which represents Understanding of heart activity is an important part. the duration as a distance between two following R peaks Human heart consists of four chambers: left atrium (LA), Common abnormal heart rhythms right atrium (RA), left ventricle (LV) and right ventricle (RV). Heart irregular rhythm can be classified by heart rate into Between RA and RV there is a tricuspid valve, and between tachycardia (heart beats too fast, more than 90 bpm) and LA and LV there is a mitral valve. These valves prevent bradycardia (heart beats too slow, less than 60 bpm); and by blood going directly from atriums to ventricles. Ventricles place of occurrence into supraventricular (atria contracts also have valves which prevent blood from going into veins, irregularly) and ventricular (ventricles contract in an irregular there is a pulmonary valve in right ventricle and an aortic pattern) arrhythmias. valve in the left one. ACIT 2018, June 1-3, 2018, Ceske Budejovice, Czech Republic 103 Atrial fibrillation is an abnormal heart rhythm While applying class weight helped to prevent overfitting characterized by rapid and irregular beating and which has no on early training stages, the model still failed to learn class- symptoms in most of the cases. It is the most common serious specific features. abnormal heart rhythm and it affects approximately 2-3% of The second approach was to apply data balancing. There the population in Europe and North America. Also, the are many techniques available for performing data balancing percentage of people with AF increases with age with 0.14% including: under 50 years old, 4% between 60 and 70 years old and 14% Under-sampling (deleting instances of over-represented over 80 years old being affected. On ECG, atrial fibrillation classes) is usually diagnosed by absence of P-wave and irregular heart Over-sampling (repeating training with under-represented beats pattern. classes) Atrial flutter (AFL) is an abnormal heart rhythm, similar to Generating Synthetic Samples (generating new samples of atrial fibrillation. Both of them are types of supraventricular the under-represented based on available samples) tachycardia. In AFL the electrical signal goes along the Use algorithms which are better in handling imbalanced pathway in a circular motion, which results in atrial muscle data. contractions that are faster than and out of sync with the Under-sampling, over-sampling and using of decision lower chambers (ventricles). Similar to A-Fib the heart beats trees which can handle imbalanced data were used to work fast, but in a regular pattern. out the problem. Itself atrial flutter is not life-threatening, but due to slower Normalizing data blood pumping it creates, the side effects might cause health problems. Measuring ECG values might generate a wide range of On ECG atrial flutter might be diagnosed by the presence values depending on different conditions when the recording of multiple f-waves instead of a P-wave. was performed. This might misdirect model to learn absolute Paroxysmal supraventricular tachycardia (PSVT) is values of one instance instead of the value for all instances. characterized by the episodes of a rapid increase of heart rate To prevent this situation, data normalization is applied (100-250 bpm) and usually regular heart rhythm. It is most which means adjusting measured values to a common scale. often seen in young people and infants. Alcohol, illicit drugs, The first step is to find a baseline (mean value of the record) caffeine, and smoking might be a cause of this arrhythmia. and subtract it from the record values. And after that divide Another abnormal heart rhythm is premature ventricular record values by the absolute value of the record. So the final complex (PVC) characterized by long QRS complexes (> scale would be in the range [-1; 1]. 0.12ms), ST segment and T wave changes (irregular, inverted Band pass filter in leads with a dominant R wave, etc). Ventricular fibrillation (V-Fib) is a life-threatening ECG signal might be altered with the noise of different abnormal heart rhythm when the lower chambers quiver and sources. This might be electrical signals coming from human the heart can’t pump blood to the body. This occurs because body activity (usually electromyography signals from of electrical signal disorder which makes chambers contract muscles contraction), procedure artifacts (eg. connecting/ very fast in an unsynchronized way. It requires immediate disconnecting electrodes), power line interference and treatment and can usually be stopped with a defibrillator. instrumentation noise. Power line interference is not less represented in hand-held III. DATA PREPROCESSING devices, and usually, occurs through inductive mechanism. Handling of imbalanced data The power lines across the world have the frequency of 50Hz or 60Hz, which is higher than the subject information The data in selected dataset is quite imbalanced which frequencies (0.1Hz to 40Hz). So it can easily be eliminated might create problems during training. The model might by filters as presented on Fig. 2. easily overfit and predict all of the time the most represented Both electrode contact noise and electrode motion noise category. This creates a so-called accuracy paradox: suppose cause baseline changes that occur due to variation in the that we have a dataset with two labels A and B and 90% of position of the heart with the respect to the electrodes. More the records are instances of A, the classifier might predict all specifically, the amplitude of the changes is defined by the time class A which will give the accuracy of 90% variation of electrode-skin impedance (ESIV). The larger although it definitely won’t be a good classifier. There are ESIV, the smaller change of impedance needed to cause a several common approaches here: use abnormalities detection shift in the baseline. This kind of noise is not defined by any algorithms or apply class weights or perform data balancing. particular range of frequencies and it isn’t easy to eliminate it The first approach was applying class weights as a with signal processing, the task is to perform the recording logarithmic function of the proportion of total instances procedure without patient disturbance. divided by the number of class instances. Electromyography (EMG) noise is caused by contraction 𝜇𝜇∗𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡 of muscles, not related to the heart activity (moving of hands 𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐ℎ𝑡𝑡 = 𝑚𝑚𝑚𝑚𝑚𝑚 �𝑙𝑙𝑙𝑙𝑙𝑙 � � , 1� 𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐 and/or legs). For the hand-held devices usually, the highest where μ - parameter to tune EMG noise component is waves generated by depolarization and repolarization of hand muscles (as ECG signal goes from heart to the device directly through hands). EMG signal frequency is stochastic in nature, but the significant activity ACIT 2018, June 1-3, 2018, Ceske Budejovice, Czech Republic 3 104 happens in the range of 5Hz and 450Hz. This partially it looks for the position of the higher value. This position is overlaps with the frequencies of the ECG signal so it’s hard marked as potential R-peak location. to eliminate it absolutely. In particular, the problematic areas R-peaks than go through false rejection algorithm which are P and T waves of the ECG. checks the average length of the RR-interval, and rejects those R-peaks, where the length of RR-interval is less than half of the average. Fig.4. Detection of R-peaks There is also additional search back algorithm which again looks on RR-intervals and finds positions where there might be a missed R-peak (RR-interval is higher than 1.5 of the average). There it lowers the threshold and runs the search algorithm once again. Heartbeat extraction is done with the static window. By taking in consideration that normal heart rhythm is in the Fig.2. Signal preprocessing with low-pass and high-pass band filters range of 60 to 100 beats per minute, we considered static So to remove most of the noise the combination of low windows of sizes from 0.5s (120 bpm) to 0.7s (86 bpm). The pass and high pass bands filters were used. The range of window size that served best was 0.6s with the distribution of frequencies to be kept is from 0.1Hz to 40Hz. 0.15s for P-wave, 0.1s for QRS complex and 0.35s for ST The SciPy filter function in combination with NumPy interval and T-wave. convolve function was used to implement both filters. For each R-peak detected, the heartbeat template was extracted with the static window of 0.6s which includes 0.2s IV. FEATURE SELECTION before and 0.4s after R-peak. Feature selection includes: 1. QRS detection. 2. Heartbeat extraction. 3. Features extraction The task of QRS detection is to find R-peaks which represent the heart beats. Beat detection is a procedure preceding any kind of ECG analysis and so it is critical to correctly detect heart beats. One of the standard algorithms used for QRS detection is a Pan-Tompkins algorithm. The original paper states detection rate of 99.3%. Many modifications were made to the algorithm since its publication including additional filtering, search back support, outliers rejection etc. Fig. 3. Extracted heartbeat The filtered signal is introduced as an input to the During the model development process, many features algorithm. The processing of the data starts with a first- were considered as good for prediction. To analyze the noise derivative filter, which helps obtain information about the of the data, transformation coefficients were used. In slope of the QRS. particular, first 400 coefficients of Fourier transformation and details coefficients of 4-level wavelet decomposition. Another range of useful features is heart rate variability. It is computed on RR-intervals, which includes time domain components (RMSSD, SDNN, NN20, PNN20, NN50, PNN50, mean RR, std RR, mean HR) and frequency domain Fig.3. Signal before and after derivative filter components. Frequency domain components are computed for 3 To get even better intensity of the slope, the squaring with frequency bands: very low frequencies (0-0.04), low normalization is applied. This gives a graph where most of frequencies (0.04-0.15) and high frequencies (0.15-0.4). To the values are near to zero, and in the positions of R peaks, it compute frequencies powers, Welch estimation of power has a value much higher (usually over 0.5). spectral density is performed on interpolation of RR-intervals, Additionally moving average is applied, which combines and the total values of power bands are computed by two close peaks on the graph above into one. After that, the integrating (with composite trapezoidal rule) function of algorithm finds positions on the graph that are higher than the frequencies and powers. average (referenced threshold). And for all of those positions ACIT 2018, June 1-3, 2018, Ceske Budejovice, Czech Republic 105 Additionally, minimum and maximum RR-interval lengths normal sinus rhythm, ‘2’ – is other heart rhythm and ‘3’ – is are added to the feature vector. noisy signal. Precision is defined as the number of true The extractor also computes the proportion of the number positives divided by the number of true positives and false of R-peaks detected divided by the length of the record, mean positives and is a measure of a class exactness. Recall is the and standard deviation of normalized R-peak values, number, number of true positives divided by the sum of a number of and proportion of detected R-peak values with the inverse true positives and false negatives and it expresses the sign. measure of classifier completeness. F1 score is used as a Based on the templates of heart beats the module finds a model estimate. F1 measure is defined a combination of median heartbeat as it best represents the most common specificity and sensitivity: template of heartbeat. Median was used rather than mean 2 ∗ 𝑝𝑝𝑝𝑝𝑝𝑝𝑝𝑝𝑝𝑝𝑝𝑝𝑝𝑝𝑝𝑝𝑝𝑝 ∗ 𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟 because it is more noise resistant. The extractor also 𝐹𝐹1 = 𝑃𝑃𝑃𝑃𝑃𝑃𝑃𝑃𝑃𝑃𝑃𝑃𝑃𝑃𝑃𝑃𝑃𝑃 + 𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟 computes the standard deviation of each point of templates. It can be used to express the average of classifier exactness V. CLASSIFICATION and completeness. Scoring algorithm include F1 metrics only for 3 categories: atrial fibrillation, normal rhythm, and other One of approaches to ECG classification was based on rhythm: decision trees. The bagging was used from the very 𝐹𝐹1(𝐴𝐴𝐴𝐴) + 𝐹𝐹1(𝑁𝑁) + 𝐹𝐹1(𝑂𝑂) beginning with the random forest algorithm. 𝐹𝐹1 = 3 Random forest implies divide-and-conquer principle, which makes it easy to execute in parallel. This makes So the final score of the model is an average of F1 scores training process very fast (a couple of seconds). This is for 3 classes. particularly good for experiments with the trees structures optimizations. For classification process was selected VI. CONCLUSIONS Random forest algorithm which consists of 60 decision trees. This paper shows the ECG automated analysis problem The implementation of the algorithm is taken from the Scikit- and one of suitable algorithms for this – Random forest. Was learn library. Results of classification process for random described the main steps of ECG signal preprocessing which forest based model shown in table 1. includes handling of imbalanced data, data normalization and TABLE 1. SCORES FOR RANDOM FOREST MODEL. noises filtering. Also presented QRS complexes detection and the main features for ECG classification. Class Precision Recall f1-score identifier REFERENCES 0 0.77 0.67 0.72 [1] Heart Disease Facts // https://www.cdc.gov/ URL: 1 0.73 0.75 0.74 https://www.cdc.gov/heartdisease/facts.htm (last accessed: 2 0.64 0.68 0.66 2018/03/28). 3 0.68 0.61 0.65 [2] Wang, Zh., Yan, W., Oates, T. Time Series Classification Avg/total 0.70 0.70 0.70 from Scratch with Deep Neural Networks: A Strong Another approach was based on fully convolutional Baseline // arXiv preprint. arXiv:1611.06455, 2016. network, this type of network architecture serves as a feature [3] Brownlee J. Classification accuracy is not enough: More extractor, and the final decision is made by a fully-connected performance measures you can use // layer. Results of classification process for neural network http://machinelearningmastery.com/ URL: based model shown in table 2. http://machinelearningmastery.com/classification- accuracy-is-not-enough-more-performance-measures- TABLE 2. SCORES FOR NEURAL NETWORK MODEL. you-can-use (last accessed: 2018/03/28). Class [4] ECG basics // https://www.osmosis.org/ URL: Precision Recall f1-score identifier https://www.osmosis.org/learn/ECG_basics (last accessed: 0 0.58 0.26 0.36 2018/03/28). 1 0.65 0.94 0.77 [5] Johnson, A. E., Behar, J., Andreotti, F., Clifford, G. D., & 2 0.60 0.28 0.38 Oster, J. (2014, September). R-peak estimation using 3 0.47 0.03 0.06 multimodal lead switching. In Computing in Cardiology Avg/total 0.62 0.64 0.59 Conference (CinC), 2014 (pp. 281-284). IEEE. Thomas Model used 4 categories ‘0’ – is atrial fibrillation, ‘1’ – is Mitchell. Machine Learning / Thomas Mitchell. – McGraw-Hill, New York, 1997. – ISBN:0070428077. ACIT 2018, June 1-3, 2018, Ceske Budejovice, Czech Republic