【问题标题】:graphlab SFrame sum all values in a columngraphlab SFrame 对列中的所有值求和
【发布时间】:2016-09-05 02:44:02
【问题描述】:

如何对 SFrame graphlab 的一列中的所有值求和。我尝试查看官方文档,它仅适用于 SaArray(doc) 没有任何例子。

【问题讨论】:

    标签: python graphlab


    【解决方案1】:
    >>> import graphlab as gl
    >>> sf = gl.SFrame({'foo':[1,2,3], 'bar':[4,5,6]})
    >>> sf
    Columns:
            bar     int
            foo     int
    
    Rows: 3
    
    Data:
    +-----+-----+
    | bar | foo |
    +-----+-----+
    |  4  |  1  |
    |  5  |  2  |
    |  6  |  3  |
    +-----+-----+
    [3 rows x 2 columns]
    >>> sf['foo'].sum()
    6
    

    【讨论】:

      【解决方案2】:

      我认为来自操作的问题更多是关于如何一次跨所有(或一列)列执行此操作。这是 pandas 和 graphlab 的比较。

      # imports
      import graphlab as gl    
      import pandas as pd
      import numpy as np
      
      # generate data
      data = np.random.randint(0,10,size=100).reshape(10,10)
      col_names = list('ABCDEFGHIJ')
      
      # make dataframe and sframe
      df = pd.DataFrame(data, columns=names)
      sf = graphlab.SFrame(df)
      
      # get sum for all columns (pandas).  Returns a series.
      df.sum().sort_values(ascending=False)
      
      D    65
      A    61
      J    59
      B    50
      H    46
      G    46
      I    45
      F    43
      C    37
      E    36
      
      # sf.sum() does not work
      # get sum for each of the columns (graphlab)
      for col in col_names:
          print col, sf[col].sum()
      
      A 61
      B 50
      C 37
      D 65
      E 36
      F 43
      G 46
      H 46
      I 45
      J 59
      

      我也有同样的问题。 Pandas 提供了一个简单的界面来跨数据帧的行或列应用聚合函数。找不到相同的 SFrame?我能想到的唯一方法是迭代列列表。

      有没有更好的办法?

      【讨论】:

        猜你喜欢
        • 2017-04-11
        • 2016-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-30
        相关资源
        最近更新 更多