python – 在SFrame中分组而不安装graphlab
作者:互联网
如何在SFrame中使用groupby操作,无需安装graphlab.
我想做一些聚合,但在互联网的所有例子中,我看到聚合函数来自Graphlab.
喜欢:
import graphlab.aggregate as agg
user_rating_stats = sf.groupby(key_columns='user_id',
operations={
'mean_rating': agg.MEAN('rating'),
'std_rating': agg.STD('rating')
})
在上面的例子中,我如何使用numpy.mean而不是agg.MEAN?
解决方法:
sframe包包含与graphlab包相同的聚合模块,因此您不需要求助于numpy.
import sframe
import sframe.aggregate as agg
sf = sframe.SFrame({'user_id': [1, 1, 2],
'rating': [3.3, 3.6, 4.1]})
grp = sf.groupby('user_id', {'mean_rating': agg.MEAN('rating'),
'std_rating': agg.STD('rating')})
print(grp)
+---------+---------------------+-------------+
| user_id | std_rating | mean_rating |
+---------+---------------------+-------------+
| 2 | 0.0 | 4.1 |
| 1 | 0.15000000000000024 | 3.45 |
+---------+---------------------+-------------+
[2 rows x 3 columns]
标签:python,group-by,numpy,graphlab,sframe 来源: https://codeday.me/bug/20190628/1310636.html