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

Using sframe.apply() causing runtime error

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

    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,"text analytics" 工具包中实际上有一个内置工具可以执行此操作。假设我有这样的数据:

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']))

对于在使用 graphlab 时遇到此问题的任何人,这里是关于 dato 支持问题的讨论线程:

http://forum.dato.com/discussion/1499/graphlab-create-using-anaconda-ipython-notebook-lambda-workers-did-not-start

这里是代码,可以运行为这个问题提供个案基础。

在Dato/Graphlab环境下启动ipython或ipythonnotebook后,导入graphlab前,复制并运行以下代码

import ctypes, inspect, os, graphlab
from ctypes import wintypes
kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
kernel32.SetDllDirectoryW.argtypes = (wintypes.LPCWSTR,)
src_dir = os.path.split(inspect.getfile(graphlab))[0]
kernel32.SetDllDirectoryW(src_dir)

# Should work
graphlab.SArray(range(1000)).apply(lambda x: x)

如果这是 运行,应用函数应该可以与 sframe 一起正常工作。