2016-06-13 126 views
0

在2个字典值之间的平均绝对误差百分比我的位置的字典,然后属性值对,像这样:计算蟒蛇

{"Russia": 
    {"/location/statistical_region/size_of_armed_forces": 65700.0, 
    "/location/statistical_region/gni_per_capita_in_ppp_dollars": 42530.0, 
    "/location/statistical_region/gdp_nominal": 1736050505050.0, 
    "/location/statistical_region/foreign_direct_investment_net_inflows": 8683048195.0, 
    "/location/statistical_region/life_expectancy": 80.929, ... 

等等,对每一个国家。

,然后将含有单个阵列字典,数组中的每个值是3个键的字典:

{ 
    "sentences": [ 
     { 
      "location-value-pair": { 
       "Russia": 6.1 
      }, 
      "parsedSentence": "On Tuesday , the Federal State Statistics Service -LRB- Rosstat -RRB- reported that consumer price inflation in LOCATION_SLOT hit a historic post-Soviet period low of NUMBER_SLOT percent in 2011 , citing final data .", 
      "sentence": "On Tuesday , the Federal State Statistics Service -LRB- Rosstat -RRB- reported that consumer price inflation in Russia hit a historic post-Soviet period low of 6.1 percent in 2011 , citing final data ." 
     }, 
     { 
      "location-value-pair": { 
       "Russia": 8.8 
      }, 
      "parsedSentence": "In 2010 , annual inflation in LOCATION_SLOT hit NUMBER_SLOT percent due to the summer drought , exceeding forecasts and equalling the figure for 2009 , the year of the global financial meltdown .", 
      "sentence": "In 2010 , annual inflation in Russia hit 8.8 percent due to the summer drought , exceeding forecasts and equalling the figure for 2009 , the year of the global financial meltdown ." 
     }, ... 

我想要做的就是比较每个句子,每个位置和价值计算与第一个字典中的位置 - 值对匹配的最接近的匹配值,然后返回其对应的顶部统计属性,并将其添加为句子字典的新关键字。

例如:

句子1,我看到,我们正在寻找在俄罗斯和6.1的值。我想索引第一本字典,找到“俄罗斯”,并查看所有存在的值,例如65700.0,42530.0,1736050505050.0,8683048195.0。然后,我想找出每个属性的平均绝对误差,例如想着当

{ 
       "location-value-pair": { 
        "Russia": 6.1 
       }, 
       "predictedRegion": "/location/statistical_region/gni_in_ppp_dollars" 
       "meanabserror": 2% 
       "parsedSentence": "On Tuesday , the Federal State Statistics Service -LRB- Rosstat -RRB- reported that consumer price inflation in LOCATION_SLOT hit a historic post-Soviet period low of NUMBER_SLOT percent in 2011 , citing final data .", 
       "sentence": "On Tuesday , the Federal State Statistics Service -LRB- Rosstat -RRB- reported that consumer price inflation in Russia hit a historic post-Soviet period low of 6.1 percent in 2011 , citing final data ." 
      }, 

我的困惑:23%的size_of_armed_forces价值,为gni_per_capita财产等的话,我想找到10%的最小的一个假设,并将其添加为重点,以第二字典,所以写这只是如何访问另一个字典的键值作为另一个字典的条件。我现在的想法是:

def predictRegion(sentenceArray,trueDict): 

    absPercentageErrors = {} 

    for location, property2value in trueDict.items(): 
     print location 
     absPercentageErrors['location'] = {} 
     for property,trueValue in property2value.iteritems(): 
      print property 
      absError = abs(sentenceArray['sentences']['location-value-pair'].key() - trueValue) 
      absPercentageErrors['location']['property'] = absError/numpy.abs(trueValue) 

    for index, dataTriples in enumerate(sentenceArray["sentences"]): 
     for location, trueValue in dataTriples['location-value-pair'].items(): 
      print location 

但是很明显,我不能在此行中访问sentenceArray['sentences']['location-value-pair'].key()absError = abs(sentenceArray['sentences']['location-value-pair'].key() - trueValue)因为它是循环之外。

我怎样才能获得从环指的是完全不同的变量如此重要呢?

+0

您无缘*最小*在[最小,完整的,并且Verifable](http://stackoverflow.com/help/mcve)实施例部分。你发布了这样一本大字典,所有“俄罗斯”的价值都被切断了,所以你不可能完全理解你想要做什么。 –

+0

我发布的第一个字典是一个例子(只有一个国家),但我已经修改它是俄罗斯而不是加拿大,以便更清楚。 –

+0

请进一步修改它,让你展示**您使用您的示例中的实际数字。**你使用'[23,421,24,412]'但是我没有看到任何地方的那些当然 –

回答

1

今后阅读如何制定一个很好的问题:https://stackoverflow.com/help/mcve

最小的,完整的和可验证的。


我想这就是你要找的。

countries = {'Canada': {'a': 10, 'b': 150, 'c': 1000}, 
      'Russia': {'d': 9, 'e': 5, 'f': 1e5}} 
sentences = [ 
     {"location-value-pair": {"Russia": 6.1}, 
     "parsedSentence": "bob loblaw", 
     "sentence": "lobs law bomb" 
     }, 
     {"location-value-pair": {"Russia": 8.8}, 
      "parsedSentence": "some sentence", 
      "sentence": "lorem ipsum test" 
     }] 


def absError(numer,denom): 
    return abs(numer-denom)/float(denom) 

def findMatch(target, country): 
    return min(country, key= lambda x: absError(target, country.get(x))) 

def update(sentence): 
    (c,target), = sentence.get("location-value-pair").items() 
    country = countries[c] 
    matched = findMatch(target,country) 
    error = absError(target, country.get(matched)) 
    res = sentence.copy() 
    res.update({'predictedRegion': matched, 'meanabserror': "{:.2f}%".format(100*error)}) 
    return res 

updated = [update(sentence) for sentence in sentences]  
updated 

输出:

[{'location-value-pair': {'Russia': 6.1}, 
    'meanabserror': '22.00%', 
    'parsedSentence': 'bob loblaw', 
    'predictedRegion': 'e', 
    'sentence': 'lobs law bomb'}, 
{'location-value-pair': {'Russia': 8.8}, 
    'meanabserror': '2.22%', 
    'parsedSentence': 'some sentence', 
    'predictedRegion': 'd', 
    'sentence': 'lorem ipsum test'}] 
+0

你好,先生,是一个真正的英雄。我现在也学会了不要把'...'放在我的输入句子里,就像瘟疫一样。 'lambda'等的使用对我来说是新的,谢谢你给我看这个! –