2013-07-04 118 views
1
def dailyTimeDistributionFeatures (dailyCallDistribution_dictionary, missingValue = -999, lowSampleValue = -666, numberOfFeatures = 14, sampleSizeThreshold = 3):  
    featureSelection = {} 
    for date in dailyCallDistribution_dictionary: 
      date_timestruct = datetime.datetime.fromtimestamp(time.mktime(time.strptime(date, "%Y-%m-%d"))) 
      timeSample = dailyCallDistribution_dictionary[ date ] 
      if len(timeSample) <= sampleSizeThreshold : 
      if len(timeSample) == 0 : 
       featureSelection[ date ] = [ date_timestruct.timetuple().tm_wday 
              , int(date_timestruct.strftime('%W')) 
              , date_timestruct.month ] + [missingValue] * (numberOfFeatures - 3) 
      else : 
       featureSelection[ date ] = [ date_timestruct.timetuple().tm_wday 
              , int(date_timestruct.strftime('%W')) 
              , date_timestruct.month ] + [lowSampleValue] * (numberOfFeatures - 3) 
      else :  
       featureSelection[ date ] = [ date_timestruct.timetuple().tm_wday 
              , int(date_timestruct.strftime('%W')) 
              , date_timestruct.month 
              , len(timeSample) 
              # counts how many late night activities. 
              , sum(Pandas.Series(timeSample).apply(lambda x: (x>0) & (x <= 4)).tolist()) 
              , Pandas.Series(timeSample).mean() 
              , Pandas.Series(timeSample).median() 
              , Pandas.Series(timeSample).std() 
              , Pandas.Series(timeSample).min() 
              , Pandas.Series(timeSample).max() 
              , Pandas.Series(timeSample).mad() 
              , Pandas.Series(timeSample).quantile(0.75) - Pandas.Series(timeSample).quantile(0.25) 
              , Pandas.Series(timeSample).kurt() 
              , Pandas.Series(timeSample).skew() 
              ] 
    return Pandas.DataFrame(featureSelection, index = ['dayOfWeek', 'WeekOfYear', 'MonthOfYear', 
                 'Number of Calls', 'Number of Late Night Activities', 
                 'Average Time', 'Median of Time', 
                 'Standard Deviation', 'Earliest Call', 
                 'Latest Call', 'Mean Absolute Deviation', 
                 'Interquartile Range', 'Kurtosis', 
                 'Skewness']).T 

當我寫輸出數據幀以上Python函數並試圖在上述功能中的一個更多列添加到所述數據幀:Python的錯誤:numpy.bool_'對象不是可迭代

featureTime['Whether Staying Late'] = featureTime['Number of Late Night Activities'].apply(lambda x: x > 0).apply(lambda x: sum(x)) 

我得到了一個錯誤:

TypeError         Traceback (most recent call last) 
/home/aaa/Enthought/Canopy_64bit/System/lib/python2.7/site- packages/IPython/utils/py3compat.pyc in execfile(fname, *where) 
181    else: 
182     filename = fname 
--> 183    __builtin__.execfile(filename, *where) 

/home/aaa/pyRepo/feature_selection_v15.py in <module>() 
352 featureTime.to_csv('time.csv') 
353 
--> 354 featureTime['Whether Staying Late'] = featureTime['Number of Late Night Activities'].apply(lambda x: x > 0).apply(lambda x: sum(x)) 
355 
356 

/home/aaa/Enthought/Canopy_64bit/User/lib/python2.7/site- packages/pandas/core/series.pyc in apply(self, func, convert_dtype, args, **kwds) 
2445    values = lib.map_infer(values, lib.Timestamp) 
2446 
-> 2447   mapped = lib.map_infer(values, f, convert=convert_dtype) 
2448   if isinstance(mapped[0], Series): 
2449    from pandas.core.frame import DataFrame 

/home/aaa/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pandas/lib.so in pandas.lib.map_infer (pandas/lib.c:41822)() 

/home/aaa/pyRepo/feature_selection_v15.py in <lambda>(x) 
352 featureTime.to_csv('time.csv') 
353 
--> 354 featureTime['Whether Staying Late'] = featureTime['Number of Late Night Activities'].apply(lambda x: x > 0).apply(lambda x: sum(x)) 
355 
356 

TypeError: 'numpy.bool_' object is not iterable 

不存在的,如果我手動添加它通過談話控制檯。

我已經通過使用python內置數據類型和for循環來解決該問題。什麼讓我好奇是爲什麼我得到上面那種錯誤...想知道它來自哪裏...想知道它來自哪裏...

+0

你可以發佈完整的堆棧跟蹤嗎? –

回答

1

假設sequence.apply將lambda應用於序列中的每個元素,sequence.apply(lambda x: x > 0)產生一系列布爾值,並且sequence.apply(lambda x: x > 0).apply(lambda x: sum(x))嘗試累加每個布爾值,導致'bool' object is not iterable -kinda錯誤。你得到一個類似的錯誤:

>>> sum(True) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: 'bool' object is not iterable 
+0

嗯非常奇怪,總結(真)實際上適用於我(不是總和([真實]),但都在我的電腦上工作)。還有更進一步的問題是,使用完全相同的代碼,在一種情況下,我把它放到.py文件中,它不起作用。在另一種情況下,我將它輸入到控制檯,它運行良好...也許這是我的Python/Linux版本。 – user2551507

+0

我用另一個IDE,sum(True)現在返回一個錯誤。我認爲冠層對我來說是「矯正」。也許其他奇怪的東西也來自這個IDE ...非常感謝您的幫助! – user2551507

相關問題