graphlab中.apply使用

1、官网解析

参考地址:

https://turi.com/products/create/docs/generated/graphlab.SFrame.apply.html?highlight=apply#graphlab.SFrame.apply

 

SFrame.apply(fndtype=Noneseed=None)

Transform each row to an SArray according to a specified function. Returns a new SArray of dtype where each element in this SArray is transformed by fn(x) where x is a single row in the sframe represented as a dictionary. The fn should return exactly one value which can be cast into type dtype. If dtype is not specified, the first 100 rows of the SFrame are used to make a guess of the target data type.

Parameters:

fn : function

The function to transform each row of the SFrame. The return type should be convertible to dtype if dtype is not None. This can also be a toolkit extension function which is compiled as a native shared library using SDK.

dtype : dtype, optional

The dtype of the new SArray. If None, the first 100 elements of the array are used to guess the target data type.

seed : int, optional

Used as the seed if a random number generator is included in fn.

Returns:

out : SArray

The SArray transformed by fn. Each element of the SArray is of type dtype

Examples

Concatenate strings from several columns:

>>> sf = graphlab.SFrame({'user_id': [1, 2, 3], 'movie_id': [3, 3, 6],
                          'rating': [4, 5, 1]})
>>> sf.apply(lambda x: str(x['user_id']) + str(x['movie_id']) + str(x['rating']))
dtype: str
Rows: 3
['134', '235', '361']

 

2、示例

def great_count(word_count_vector):
    if 'great' in word_count_vector:
        return word_count_vector['great']
    else:
        return 0
products['great'] = products['word_count'].apply(great_count, int)
products['great'].show()

products['word_count']是个字典数组,对products['word_count']的每一个元素都进行apply中的函数great_count。

加上Int类型,是为了不仅仅计算前100个数,而是整个数组。这一段见我的博文:

https://blog.csdn.net/weixin_41770169/article/details/80808393

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值