application_vnd.jupyter.stderr_output_1.png image_output_1.png
import numpy as np

import pandas as pd

import seaborn as sns

import wfdb

data = '../input/mit-bih-arrhythmia-database/'

patients = ['100', '101', '102', '103', '104', '105', '106', '107', '108', '109', '111', '112', '113', '114', '115', '116', '117', '118', '119', '121', '122', '123', '124', '200', '201', '202', '203', '205', '207', '208', '209', '210', '212', '213', '214', '215', '217', '219', '220', '221', '222', '223', '228', '230', '231', '232', '233', '234']

dataframe = pd.DataFrame()

for pts in patients:
    file = data + pts
    annotation = wfdb.rdann(file, 'atr')
    sym = annotation.symbol
    values, counts = np.unique(sym, return_counts=True)
    df_sub = pd.DataFrame({'symbol': values, 'Counts': counts, 'Patient Number': [pts] * len(counts)})
    dataframe = pd.concat([dataframe, df_sub], axis=0)

ax = sns.countplot(dataframe.symbol)

nonbeat = ['[', '!', ']', 'x', '(', ')', 'p', 't', 'u', '`', "'", '^', '|', '~', '+', 's', 'T', '*', 'D', '=', '"', '@', 'Q', '?']
abnormal = ['L', 'R', 'V', '/', 'A', 'f', 'F', 'j', 'a', 'E', 'J', 'e', 'S']
normal = ['N']

dataframe['category'] = -1
dataframe.loc[dataframe.symbol == 'N', 'category'] = 0
dataframe.loc[dataframe.symbol.isin(abnormal), 'category'] = 1
dataframe.groupby('category').Counts.sum()

dataframe = dataframe.loc[~(dataframe['category'] == -1)]
dataframe.groupby('category').Counts.sum()