如何基于两个并行数组中的符号为数据条分配标签
问题与代码实现
现有长度为3600的数据条,包含symbol与aux_note两类字段。aux_note为空时需沿用前序有效值。针对百万级长度的数据帧,需生成长度为3600的数据窗口(strip),对每个窗口的symbol与aux_note取唯一值得到两个集合。其中symbol每个位置均有值,aux_note仅在事件触发时赋值,空值表示沿用前序事件状态。
每个数据窗口需按以下规则分配标签:
- 若
aux_note的唯一值与symbol的唯一值一致,标签为该值(例:symbol集合{N}、aux_note集合{N} → 标签=N) - 若
aux_note的唯一值非N且存在于symbol集合中,标签为该aux_note值(例:symbol集合{A,N}、aux_note集合{A} → 标签=A) - 优先采用A作为标签(例:symbol集合{A,V,N}、aux_note集合{A} → 标签=A)
以下为尝试实现的代码:
for i in range(num_strips): strp = {} strp['signal'] = df_data[i * len_ecg_strip : (i+1)*len_ecg_strip]['MLII'] temp = df_descr[df_descr['sample'] >= i * len_ecg_strip] temp = temp[df_descr['sample'] <= (i+1)*len_ecg_strip] strp['sample'] = temp['sample'] strp['symbol'] = temp['symbol'] strp['aux_note'] = temp['aux_note'] prev_aux_note = strip_validate(strp, prev_aux_note) def strip_validate(strip, pre_aux): sym_list = set(strip['symbol']) aux_note_list = list(set(strip['aux_note'])) aux_note_list = list(filter(None,aux_note_list)) pdb.set_trace() if len(aux_note_list)==0: cur_aux_note = pre_aux label = cur_aux_note elif len(aux_note_list) == 1: cur_aux_note = aux_note_list[0] label = cur_aux_note elif 'AFIB' in aux_note_list: cur_aux_note = 'AFIB' label = cur_aux_note else: cur_aux_note = aux_note_list[1] label = cur_aux_note print(sym_list, cur_aux_note) return cur_aux_note, label
内容的提问来源于stack exchange,提问作者user2129623
相关产品推荐
相关产品推荐

