Я получаю 6500 матов около ЭКГ .
И я хочу прочитать их из этих файлов и выполнить некоторые процессы, но я считаю, что затраты времени больше и больше, чем я думали tqdm оценены ранее.
Так что меня озадачивает, если что-то не так с моим кодом.
Вот пример файла mat:
# the number of each array are given same for convience, in fact they are totally not same
mat1 = scipy.io.loadmat('Train/TRAIN0001.mat')
mat1
{'I': array([[-0.02928, -0.02928, -0.02928, ... , 0.46848, 0.53192, 0.5856]]),
'II': array([[-0.02928, -0.02928, -0.02928, ... , 0.46848, 0.53192, 0.5856]]),
'III': array([[-0.02928, -0.02928, -0.02928, ... , 0.46848, 0.53192, 0.5856]]),
'V1': array([[-0.02928, -0.02928, -0.02928, ... , 0.46848, 0.53192, 0.5856]]),
'V2': array([[-0.02928, -0.02928, -0.02928, ... , 0.46848, 0.53192, 0.5856]]),
'V3': array([[-0.02928, -0.02928, -0.02928, ... , 0.46848, 0.53192, 0.5856]]),
'V4': array([[-0.02928, -0.02928, -0.02928, ... , 0.46848, 0.53192, 0.5856]]),
'V5': array([[-0.02928, -0.02928, -0.02928, ... , 0.46848, 0.53192, 0.5856]]),
'V6': array([[-0.02928, -0.02928, -0.02928, ... , 0.46848, 0.53192, 0.5856]]),
'__globals__': [],
'__header__': b'MATLAB 5.0 MAT-file Platform: nt, Created on: Mon May 6 16:56:48 2019',
'__version__': '1.0',
'aVF': array([[-0.02928, -0.02928, -0.02928, ... , 0.46848, 0.53192, 0.5856]]),
'aVL': array([[-0.02928, -0.02928, -0.02928, ... , 0.46848, 0.53192, 0.5856]]),
'aVR': array([[-0.02928, -0.02928, -0.02928, ... , 0.46848, 0.53192, 0.5856]]),
'age': array([[63]], dtype=int32),
'sex': array(['FEMALE'], dtype='<U6'),
}
Вот код:
def read_mat(mat_path, index):
mat = scipy.io.loadmat(mat_path)
mat_df = pd.DataFrame({
'I_' + str(index): mat['I'][0],
'II_' + str(index): mat['II'][0],
'III_' + str(index): mat['III'][0],
'V1_' + str(index): mat['V1'][0],
'V2_' + str(index): mat['V2'][0],
'V3_' + str(index): mat['V3'][0],
'V4_' + str(index): mat['V4'][0],
'V5_' + str(index): mat['V5'][0],
'V6_' + str(index): mat['V6'][0],
'aVF_' + str(index): mat['aVF'][0],
'aVL_' + str(index): mat['aVL'][0],
'aVR_' + str(index): mat['aVR'][0]
})
age = pd.DataFrame({'age': mat['age'][0]})
sex = pd.DataFrame({'sex': mat['sex']})
sex['sex'] = sex['sex'].apply(lambda x: 1 if x == 'male' (0 if x == 'female' else 2))
return mat_df, age, sex
def read_data():
# target.csv save the label of every people
tar = pd.read_csv('target.csv')
# ECG has collected 5000 samples of each people, so I want to treat every sample as a feature
train = pd.DataFrame(columns=[i for i in range(0, 5000)])
for i in tqdm(range(1, 6501)):
tmp_filename = 'TRAIN' + str(i).zfill(4)
train_tmp, age, sex = read_mat('Train/' + tmp_filename, i)
train_tmp = train_tmp.transpose()
train_tmp['age'] = age['age'][0]
train_tmp['sex'] = sex['sex'][0]
train_tmp['target'] = tar['label'][i-1]
# add 5000 samples of each mat file into train DataFrame
train = train.append(train_tmp)
del train_tmp, age, sex
target = pd.Series()
target = train['target']
return train, target, tar
Вот стоимость времени:
0% |11/6500 [00:00 <01:01, 105,36it / s] <br>0% |19/6500 [00:00 <01:08, 94,25it / s] <br>...
...
10% |636/6500 [02:14 <39:37, 2,47it / s] <br>10% |640/6500 [02:15 <39:52, 2,45бит / с] <br>...
...
20% |1322/6500 [09:25 <1:12:56, 1,18it / s] <br>20% |1328/6500 [09:30 <1:13:27, 1,17it / s] <br>...
...
30% |1918/6500 [20:02 <1:13:53, 1,23 с / с] <br>...
...
40% |2586/6500 [35:52 <1:44:42, 1,61 с / с] <br>...
...
50% |3237/6500 [2:08:11 <10:58:41, 12.09s / it] </p>
Когда я прочитал 50% матовых файлов, он оценил, что это будет стоить более 10 часов.
И мне интересно, что с моим кодом что-то не так, поэтому это будет стоить слишком много времени.
Может кто-нибудь дать мне несколько советов по моему коду?
Заранее спасибо.