Time series classification 06: The numpy unique () method realizes the conversion of classification labels into digital codes (non-one-hot)

In the process of processing the original data, the label column can be saved as a single file in a corresponding manner in order, which is convenient for processing.

Using the numpy unique()method, implemented in order to find the original sequence tag converts a real digital code. code show as below:

def trans_dummy_label(data):
    '''
    函数功能:
    实现将真实标签转换为0,1,2,3;便于绘图显示;
    ------------------------------------------
    参数说明:
    data:DataFrame格式数据
    '''
    y = pd.DataFrame(data[data.columns[-1]].values, columns=['label'])
    map_dict = {} # 键值对对应关系字典
    '''
    np.unique()方法说明:
    返回列表数据中的唯一元素列表;return_index=True参数保留元素顺序,按照第一出现的索引大小排序;
    np.sort(index)方法实现索引。
    '''
    _, index = np.unique(y['label'].values, return_index=True) # 返回标签唯一索引列表
    for key, value in enumerate(y['label'][np.sort(index)], start=0): # data.columns[-1]标签列
        map_dict[value] = key
    
    y['label'] = y.label.map(map_dict) # 映射
    
    return y, map_dict

transfer:

trainy, map_dict_train = trans_dummy_label(trainX)

View result:
Insert picture description here
save as txt file:

trainy.to_csv('trainy_diffp.txt', index=False, header=False)

View Results:
Insert picture description here


When the amount of data is large, it is recommended to use the unique () method in pandas.

The stored mapping dictionary can be used to map digital labels to real labels during prediction or final visualization of the output.

Published 167 original articles · praised 686 · 50,000+ views

Guess you like

Origin blog.csdn.net/weixin_39653948/article/details/105516373