首页 > 解决方案 > 分类数据的响应编码

问题描述

响应编码是一种矢量化分类数据的技术。假设我们有一个名为“grade_category”的分类特征,它具有以下唯一标签 - ['grades_3_5'、'grades_prek_2'、'grades_9_12'、'grades_6_8']。假设我们正在处理目标类标签为 0 和 1 的分类问题

在响应编码中,您必须为我们的特征中的每个标签输出概率值,该标签与特定的类标签一起出现,例如,grades_prek_2 = [它出现在 class_0 中的概率,它出现在类 1 中的概率]

标签: python-3.xvectorizationcategorical-data

解决方案


def response_coding(xtrain, ytrain, feature):
            """ this method will encode the categorical features 
            using response_coding technique. 
            args:
                xtrain, ytrain, feature (all are ndarray)
            returns:
                dictionary (dict)
            """
    
    dictionary = dict()
    x = PrettyTable()
    x = PrettyTable([feature, 'class 1', 'class 0'])

    unique_cat_labels = xtrain[feature].unique()

    for i in tqdm(range(len(unique_cat_labels))):
        total_count = xtrain.loc[:,feature][(xtrain[feature] == unique_cat_labels[i])].count()
        p_0 = xtrain.loc[:, feature][((xtrain[feature] == unique_cat_labels[i]) & (ytrain==0))].count()
        p_1 = xtrain.loc[:, feature][((xtrain[feature] == unique_cat_labels[i]) & (ytrain==1))].count()

        dictionary[unique_cat_labels[i]] = [p_1/total_count, p_0/total_count]

        row = []
        row.append(unique_cat_labels[i])
        row.append(p_1/total_count)
        row.append(p_0/total_count)
        x.add_row(row)
    print()
    print(x)[![enter image description here][1]][1]
    return dictionary

在此处输入图像描述


推荐阅读