2

我正在尝试对充满数据的 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,所以应该不会有过载。我不知道如何解决这个问题。

4

2 回答 2

1

如果您使用的是 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']))
于 2015-12-10T21:25:00.220 回答
1

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

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

这是可以运行的代码,以针对此问题提供个案基础。

在 Dato/Graphlab 环境中启动 ipython 或 ipython notebook 后,但在导入 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)

如果运行,apply 函数应该可以在 sframe 上正常工作。

于 2015-12-15T23:49:35.543 回答