簡體   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