Skip to main content
 首页 » 编程设计

python之使用 sframe.apply() 导致运行时错误

2024年10月24日27thcjp

我正在尝试对充满数据的 s 帧使用简单的应用。这是针对其中一列的简单数据转换,应用一个接受文本输入并将其拆分为列表的函数。这是函数及其调用/输出:

    In [1]: def count_words(txt): 
           count = Counter() 
           for word in txt.split(): 
               count[word]+=1 
           return count 
 
    In [2]: products.apply(lambda x: count_words(x['review'])) 
 
    --------------------------------------------------------------------------- 
RuntimeError                              Traceback (most recent call last) 
<ipython-input-8-85338326302c> in <module>() 
----> 1 products.apply(lambda x: count_words(x['review'])) 
 
C:\Anaconda3\envs\dato-env\lib\site-packages\graphlab\data_structures\sframe.pyc in apply(self, fn, dtype, seed) 
   2607  
   2608         with cython_context(): 
-> 2609             return SArray(_proxy=self.__proxy__.transform(fn, dtype, seed)) 
   2610  
   2611     def flat_map(self, column_names, fn, column_types='auto', seed=None): 
 
C:\Anaconda3\envs\dato-env\lib\site-packages\graphlab\cython\context.pyc in __exit__(self, exc_type, exc_value, traceback) 
     47             if not self.show_cython_trace: 
     48                 # To hide cython trace, we re-raise from here 
---> 49                 raise exc_type(exc_value) 
     50             else: 
     51                 # To show the full trace, we do nothing and let exception propagate 
 
RuntimeError: Runtime Exception. Unable to evaluate lambdas. Lambda workers did not start. 

当我运行我的代码时,我得到了那个错误。 s 帧 (df) 只有 10 x 2,所以应该没有来自那里的过载。我不知道如何解决这个问题。

请您参考如下方法:

如果您使用的是 GraphLab Create,那么在“文本分析”工具包中实际上有一个内置工具可以执行此操作。假设我有这样的数据:

import graphlab 
products = graphlab.SFrame({'review': ['a portrait of the artist as a young man', 
                                       'the sound and the fury']}) 

计算每个条目中单词的最简单方法是

products['counts'] = graphlab.text_analytics.count_words(products['review']) 

如果您单独使用 sframe 包,或者如果您想执行您描述的自定义函数,我认为您的代码中缺少的关键部分是需要将 Counter 转换为字典SFrame 处理输出的顺序。

from collections import Counter 
 
def count_words(txt): 
    count = Counter() 
    for word in txt.split(): 
        count[word] += 1 
    return dict(count) 
 
products['counts'] = products.apply(lambda x: count_words(x['review']))