pandas apply 函数 实现多进程的示例讲解
技术  /  管理员 发布于 7年前   369
前言: 在进行数据处理的时候,我们经常会用到 pandas 。但是 pandas 本身好像并没有提供多进程的机制。本文将介绍如何来自己实现 pandas (apply 函数)的多进程执行。其中,我们主要借助 joblib 库,这个库为python 提供了一个非常简洁方便的多进程实现方法。
所以,本文将按照下面的安排展开,前面可能比较拢糁皇窍胫涝趺从每芍苯涌吹谌糠郑
- 首先简单介绍 pandas 中的分组聚合操作 groupby。
- 然后简单介绍 joblib 的使用方法。
- 最后,通过一个去停用词的实验详细介绍如何实现 pandas 中 apply 函数多进程执行。
注意:本文说的都是多进程而不是多线程。
1. DataFrame.groupby 分组聚合操作
# groupby 操作df1 = pd.DataFrame({'a':[1,2,1,2,1,2], 'b':[3,3,3,4,4,4], 'data':[12,13,11,8,10,3]})df1
按照某列分组
grouped = df1.groupby('b')# 按照 'b' 这列分组了,name 为 'b' 的 key 值,group 为对应的df_groupfor name, group in grouped: print name, '->' print group
3 -> a b data0 1 3 121 2 3 132 1 3 114 -> a b data3 2 4 84 1 4 105 2 4 3
按照多列分组
grouped = df1.groupby(['a','b'])# 按照 'b' 这列分组了,name 为 'b' 的 key 值,group 为对应的df_groupfor name, group in grouped: print name, '->' print group
(1, 3) -> a b data0 1 3 122 1 3 11(1, 4) -> a b data4 1 4 10(2, 3) -> a b data1 2 3 13(2, 4) -> a b data3 2 4 85 2 4 3
若 df.index 为[1,2,3…]这样一个 list, 那么按照 df.index分组,其实就是每组就是一行,在后面去停用词实验中,我们就用这个方法把 df_all 处理成每行为一个元素的 list, 再用多进程处理这个 list。
grouped = df1.groupby(df1.index)# 按照 index 分组,其实每行就是一个组了print len(grouped), type(grouped)for name, group in grouped: print name, '->' print group
6 <class 'pandas.core.groupby.DataFrameGroupBy'>0 -> a b data0 1 3 121 -> a b data1 2 3 132 -> a b data2 1 3 113 -> a b data3 2 4 84 -> a b data4 1 4 105 -> a b data5 2 4 3
2. joblib 用法
refer: https://pypi.python.org/pypi/joblib
# 1. Embarrassingly parallel helper: to make it easy to write readable parallel code and debug it quickly:from joblib import Parallel, delayedfrom math import sqrt
处理小任务的时候,多进程并没有体现出优势。
%time result1 = Parallel(n_jobs=1)(delayed(sqrt)(i**2) for i in range(10000))%time result2 = Parallel(n_jobs=8)(delayed(sqrt)(i**2) for i in range(10000))
CPU times: user 316 ms, sys: 0 ns, total: 316 msWall time: 309 msCPU times: user 692 ms, sys: 384 ms, total: 1.08 sWall time: 1.03 s
当需要处理大量数据的时候,并行处理就体现出了它的优势
%time result = Parallel(n_jobs=1)(delayed(sqrt)(i**2) for i in range(1000000))
CPU times: user 3min 43s, sys: 5.66 s, total: 3min 49sWall time: 3min 33s
%time result = Parallel(n_jobs=8)(delayed(sqrt)(i**2) for i in range(1000000))
CPU times: user 50.9 s, sys: 12.6 s, total: 1min 3sWall time: 52 s
3. apply 函数的多进程执行(去停用词)
多进程的实现主要参考了 stack overflow 的解答: Parallelize apply after pandas groupby
上图中,我们要把 AbstractText 去停用词, 处理成 AbstractText1 那样。首先,导入停用词表。
# 读入所有停用词with open('stopwords.txt', 'rb') as inp: lines = inp.read()stopwords = re.findall('"(.*?)"', lines)print len(stopwords)print stopwords[:10]
692['a', "a's", 'able', 'about', 'above', 'according', 'accordingly', 'across', 'actually', 'after']
# 对 AbstractText 去停用词# 方法一:暴力法,对每个词进行判断def remove_stopwords1(text): words = text.split(' ') new_words = list() for word in words: if word not in stopwords: new_words.append(word) return new_words# 方法二:先构建停用词的映射for word in stopwords: if word in words_count.index: words_count[word] = -1def remove_stopwords2(text): words = text.split(' ') new_words = list() for word in words: if words_count[word] != -1: new_words.append(word) return new_words%time df_all['AbstractText1'] = df_all['AbstractText'].apply(remove_stopwords1)%time df_all['AbstractText2'] = df_all['AbstractText'].apply(remove_stopwords2)
CPU times: user 8min 56s, sys: 2.72 s, total: 8min 59sWall time: 8min 48sCPU times: user 1min 2s, sys: 4.12 s, total: 1min 6sWall time: 1min 2s
上面我尝试了两种不同的方法来去停用词:
方法一中使用了比较粗暴的方法:首先用一个 list 存储所有的 stopwords,然后对于每一个 text 中的每一个 word,我们判断它是否出现在 stopwords 的list中(复杂度 O(n)O(n) ), 若为 stopword 则去掉。
方法二中我用 一个Series(words_count) 对所有的词进行映射,如果该词为 stopword, 则把它的值修改为 -1。这样,对于 text 中的每个词 ww, 我们只需要判断它的值是否为 -1 即可判定是否为 stopword (复杂度 O(1)O(1))。
所以,在这两个方法中,我们都是采用单进程来执行,方法二的速度(1min 2s)明显高于方法一(8min 48s)。
from joblib import Parallel, delayedimport multiprocessing# 方法三:对方法一使用多进程def tmp_func(df): df['AbstractText3'] = df['AbstractText'].apply(remove_stopwords1) return dfdef apply_parallel(df_grouped, func): """利用 Parallel 和 delayed 函数实现并行运算""" results = Parallel(n_jobs=-1)(delayed(func)(group) for name, group in df_grouped) return pd.concat(results)if __name__ == '__main__': time0 = time.time() df_grouped = df_all.groupby(df_all.index) df_all =applyParallel(df_grouped, tmp_func) print 'time costed {0:.2f}'.format(time.time() - time0)
time costed 150.81
# 方法四:对方法二使用多进程def tmp_func(df): df['AbstractText3'] = df['AbstractText'].apply(remove_stopwords2) return dfdef apply_parallel(df_grouped, func): """利用 Parallel 和 delayed 函数实现并行运算""" results = Parallel(n_jobs=-1)(delayed(func)(group) for name, group in df_grouped) return pd.concat(results)if __name__ == '__main__': time0 = time.time() df_grouped = df_all.groupby(df_all.index) df_all =applyParallel(df_grouped, tmp_func) print 'time costed {0:.2f}'.format(time.time() - time0)
time costed 123.80
上面方法三和方法四分别对应于前面方法一和方法二,但是都是用了多进程操作。结果是方法一使用多进程以后,速度一下子提高了好几倍,但是方法二的多进程速度不升反降。这是不是有问题?的确,但是首先可以肯定,我们的代码没有问题。下图显示了我用 top 命令看到各个方法的进程执行情况。可以看出,在方法三和方法四中,的的确确是 12 个CPU核都跑起来了。只是在方法四中,每个核占用的比例都是比较低的。
fig1. 单进程 cpu 使用情况
fig2. 方法三 cpu 使用情况
fig3. 方法四 cpu 使用情况
一个直观的解释就是,当我们开启多进程的时候,进程开启和最后结果合并,进程结束,这些操作都是要消耗时间的。如果我们执行的任务比较小,那么进程开启等操作所消耗的时间可能就要比执行任务本身消耗的时间还多。这样就会出现多进程的方法四比单进程的方法二耗时更多的情况了。
所以总结来说,在处理小任务的时候没有必要开启多进程。借助joblib (Parallel, delayed 两个函数) ,我们能够很方便地实现 python 多进程。
以上这篇pandas apply 函数 实现多进程的示例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号