2015-12-10 83 views
2

我想使用一個簡單的適用於s幀充滿數據。這是爲了在其中一個列上進行簡單的數據轉換,它應用了一個接受文本輸入並將其分割成列表的函數。這裏是函數和它的調用/輸出:使用sframe.apply()導致運行時錯誤

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乘以2,所以應該沒有來自那裏的過載。我不知道如何解決這個問題。

+0

你不想要'產品['review']。apply(count_words)'? – EdChum

+0

我得到了與Graphlab 2.1相同的運行時錯誤。然後我重新編寫我的代碼塊並執行時沒有錯誤。不知道爲什麼,但對於遇到同樣錯誤的任何人來說,這可能值得嘗試。 – garbo999

回答

2

對於任何人誰也碰到過這樣的問題,而在這裏使用graphlab是上拿督支持問題的話題:

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

這裏是一個可以運行的代碼提供以個案爲基礎解決這個問題。

開始IPython中或IPython中的拿督/ Graphlab環境筆記本電腦,但在導入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。

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包本身,或者如果你想要做一個自定義函數就像你描述的那樣,我認爲你的代碼中的關鍵缺失部分是計數器需要被轉換成字典,以便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'])) 
+0

我在返回'dict(count)'時仍然出現運行時錯誤,並且感謝我瞭解'graphlab.text_analytics',但想使用我自己的解決方案,這個解決方案效果不佳。 – rgalbo