繁体   English   中英

如何在python中处理字符串字典

[英]How to process a dictionary of strings in python

我有一个值的字典,它遵循这个字符串模式informationGain_$index$threshold_$index$ 我的目标是检索最大的 informationGain_$index$ 和threshold_$index$

示例字典如下所示:

{'informationGain_0': 0.9949486404805016, 'threshold_0': 5.0, 'informationGain_1': 0.9757921620455572, 'threshold_1': 12.5, 'informationGain_2': 0.7272727272727273, 'threshold_2': 11.5, 'informationGain_3': 0.5509775004326937, 'threshold_3': 8.6, 'informationGain_4': 0.9838614413637048, 'threshold_4': 7.0, 'informationGain_5': 0.9512050593046015, 'threshold_5': 6.0, 'informationGain_6': 0.8013772106338303, 'threshold_6': 5.9, 'informationGain_7': 0.9182958340544896, 'threshold_7': 1.5, 'informationGain_8': 0.0, 'threshold_8': 9.0, 'informationGain_9': 0.6887218755408672, 'threshold_9': 7.8, 'informationGain_10': 0.9182958340544896, 'threshold_10': 2.1, 'informationGain_11': 0.0, 'threshold_11': 13.5}

我编写了代码来生成数据集。

def entropy_discretization(s):

    I = {}
    i = 0
    while(uniqueValue(s)):
        # Step 1: pick a threshold
        threshold = s['A'].iloc[0]

        # Step 2: Partititon the data set into two parttitions
        s1 = s[s['A'] < threshold]
        print("s1 after spitting")
        print(s1)
        print("******************")
        s2 = s[s['A'] >= threshold]
        print("s2 after spitting")
        print(s2)
        print("******************")
            
        # Step 3: calculate the information gain.
        informationGain = information_gain(s1,s2,s)
        I.update({f'informationGain_{i}':informationGain,f'threshold_{i}': threshold})
        print(f'added informationGain_{i}: {informationGain}, threshold_{i}: {threshold}')
        s = s[s['A'] != threshold]
        i += 1

    print(I)

给定示例数据集,最大信息增益与threshold_0informationGain_0相关联。 我想找到一种从数据集中识别这些键值对的通用方法。 有没有办法搜索字典,这样我就可以返回informationGain_*,threshold_*这样的informationGain_* == max

这是使用带有max的自定义键的解决方案。 即使字典没有排序,它也能工作。 这是假设输入字典名为d

M = max((k for k in d if k.startswith('i')),
        key=lambda x: d[x])
T = f'threshold_{M.rsplit("_")[-1]}'
out = {M: d[M], T: d[T]}

输出:

{'informationGain_0': 0.9949486404805016, 'threshold_0': 5.0}

注意。 我对字典键使用了一个简单的测试来检查那些以i开头的键,以便识别informationGain_X键。 如果您有一个更复杂的现实生活词典,您可能希望更新它以使用完全匹配或任何其他方式来使键的识别不二义性。

我也找到了一种方法来做到这一点。 只试了几次

    n = int(((len(I)/2)-1))
    print("Calculating maximum threshold")
    print("*****************************")
    maxInformationGain = 0
    maxThreshold       = 0 
    for i in range(0, n):
        if(I[f'informationGain_{i}'] > maxInformationGain):
            maxInformationGain = I[f'informationGain_{i}']
            maxThreshold       = I[f'threshold_{i}']

    print(f'maxThreshold: {maxThreshold}, maxInformationGain: {maxInformationGain}')

一种方法如下:

假设您的字典名称是d

informationGain_max = max(list(d.values())[::2])
threshold_max = max(list(d.values())[1::2])

这仅在假设自 python 3.6 标准 dict 维护插入顺序的情况下有效。

让我们创建一个列表,该列表的每个成员都应该是一个包含两个元素的元组或列表:首先是信息增益,然后是阈值。 我们可以使用列表的 .sort() 方法或使用 sorted() 函数对这个列表进行排序。 排序列表的最后一个元组将包含您要查找的值。 如果您也对这些值的索引感兴趣,则将它们的索引添加为元组的第三个元素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM