如何将测试集中的分类变量替换为使用训练集计算的变量?

2024-10-04 05:23:55 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我使用Python的训练集中的实例计算了两个高基数变量(Postal_Code_LManaging_Sales_Office_Nbr)的证据权重。你知道吗

问题是,现在我想用它们的WOE替换测试集实例中这些高基数变量的值。我该怎么做?你知道吗

First 20 instances of the high cardinality variables and their WOE. For instances in the testset, the WOE is missing and should be replaced with the WOE calculated using the instances of the training set

更新: I have also made a dataframe, consisting of all unique postal codes in the training set and their WOE。如果邮政编码出现在训练集中,我尝试用下面的代码替换整个数据集中丢失的WOE值,如果邮政编码没有出现在数据集中,则用WOE的平均值替换

对于TestsetwithWOE['posal\u Code\u L']中的i:

if TestsetwithWOE['Postal_Code_L'][i] in WOEvaluespostalcode['MIN_VALUE']:
    TestsetwithWOE['Postal_Code_L_WOE'][i] == WOEvaluespostalcode['WOE']
else:
    TestsetwithWOE['Postal_Code_L_WOE'][i] == meanWOEPostalCode    

运行此代码会导致以下错误: TypeError:无法对类“”进行标签索引3.core.索引.range.RangeIndex范围索引'使用'float'类的索引器[nan]

如果我将两个数据帧的所有列中的所有缺失值都更改为“未知”。我得到KeyError:'未知'。如何更改代码以使其正常工作?你知道吗


Tags: andofthe数据instances实例代码in