【问题标题】:SFrame Column of type: DictionarySFrame 列类型:字典
【发布时间】:2016-12-24 02:15:31
【问题描述】:

当我跑步时:

my_sframe['col_1'] = ''

我得到一个空白列,这正是我想要的。

但是当我跑步时:

my_sframe['col_1'] = {}

我收到一条提示意外数据类型的错误。

SFrame API 没有解决这个问题,如下所示:

https://turi.com/products/create/docs/generated/graphlab.SFrame.html

此时我的理解是,SFrame 列不能是字典。

但是,出于好奇,我尝试了这个:

my_sframe['col_1'] = graphlab.text_analytics.count_words('my_text')

type(my_sframe['col_1'][1])

out: dict

这个结果与我之前的理解完全相反。

我想要的是一个字典列,每一行都有自己的字典,很像.count_words,只是我使用的是从头开始制作的 word_count 字典,通过import string

这是一条双向街道,还是.count_words 是一个例外,我不应该期望能够重现这种数据转换?

请指教,

谢谢

更新

这里似乎是 GitHub 上的一些相关信息:

https://github.com/turi-code/how-to/blob/master/sframe_pack.py

我不确定这种技术是否可以创造出我所追求的东西,我仍在尝试。如果有人对此有任何想法,请告诉我。

【问题讨论】:

    标签: dataframe graphlab sframe


    【解决方案1】:

    我仍然愿意接受更有效的答案,但与此同时,如果其他人遇到此问题,这里是创建 SFrame 字典列的一种方法。我刚刚想通了:

    def count_words(text):
        words = text.split()
        wordfreq = {}
        for x in words:
            if x not in wordfreq:
                wordfreq[x] = 0
            wordfreq[x] += 1
        return wordfreq
    
    sframe['word_count'] = sframe['text'].apply(count_words)
    

    您会注意到 dtype 是 dict。好像有点复杂。我仍然很想知道为什么我们不能只对新列使用强制转换方法,而不是说错误:意外数据类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 2016-01-11
      • 2011-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多