2017-05-08 8 views
2

私はPandas Dataframeに格納された370kレコードのデータセットを統合する必要があります。私はマルチプロセッシング、スレッディング、Cpython、ループアンローリングを試みました。しかし、私は成功しておらず、計算に表示された時間は22時間でした。次のようにタスクは次のとおりです。どのようにループ上のPythonの速度を上げるには?

%matplotlib inline 
from numba import jit, autojit 
import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt 

with open('data/full_text.txt', encoding = "ISO-8859-1") as f: 
strdata=f.readlines() 
data=[] 

for string in strdata: 
data.append(string.split('\t')) 

df=pd.DataFrame(data,columns=["uname","date","UT","lat","long","msg"]) 

df=df.drop('UT',axis=1) 

df[['lat','long']] = df[['lat','long']].apply(pd.to_numeric) 

from textblob import TextBlob 
from tqdm import tqdm 

df['polarity']=np.zeros(len(df)) 

スレッディング:ループ展開と

from queue import Queue 
from threading import Thread 
import logging 
logging.basicConfig(
level=logging.DEBUG, 
    format='(%(threadName)-10s) %(message)s', 
) 


class DownloadWorker(Thread): 
    def __init__(self, queue): 
     Thread.__init__(self) 
     self.queue = queue 

    def run(self): 
     while True: 
      # Get the work from the queue and expand the tuple 
     lowIndex, highIndex = self.queue.get() 
     a = range(lowIndex,highIndex-1) 
     for i in a: 
      df['polarity'][i]=TextBlob(df['msg'][i]).sentiment.polarity 
     self.queue.task_done() 

    def main(): 
    # Create a queue to communicate with the worker threads 
    queue = Queue() 
    # Create 8 worker threads 
    for x in range(8): 
    worker = DownloadWorker(queue) 
    worker.daemon = True 
    worker.start() 
    # Put the tasks into the queue as a tuple 
    for i in tqdm(range(0,len(df)-1,62936)): 
    logging.debug('Queueing') 
    queue.put((i,i+62936)) 
    queue.join() 
    print('Took {}'.format(time() - ts)) 

main() 

マルチプロセッシング:計算速度を向上させる方法

pool = multiprocessing.Pool(processes=2) 
r = pool.map(assign_polarity, df) 
pool.close() 

def assign_polarity(df): 
    a=range(0,len(df),5) 
    for i in tqdm(a): 
     df['polarity'][i]=TextBlob(df['msg'][i]).sentiment.polarity 
     df['polarity'][i+1]=TextBlob(df['msg'][i+1]).sentiment.polarity 
     df['polarity'][i+2]=TextBlob(df['msg'][i+2]).sentiment.polarity 
     df['polarity'][i+3]=TextBlob(df['msg'][i+3]).sentiment.polarity 
     df['polarity'][i+4]=TextBlob(df['msg'][i+4]).sentiment.polarity 

?計算をより速い方法でデータフレームに格納することができますか?私のラップトップの構成

  • ラム:8ギガバイト
  • 物理コア:2つの
  • 論理コア:8つの
  • のWindows 10

は実装マルチプロセッシングは私に高い計算時間を与えました。 スレッドが順次実行されていました(私はGILのためだと思います) ループアンロールでは同じ計算速度が得られました。 Cpythonはライブラリのインポート中にエラーを表示していました。

+2

"私はマルチプロセッシング、スレッディング、Cpython、ループアンローリングを試みました。"何がうまくいかなかったのですか?質問にそれを投稿できますか? – Boggartfly

+0

あなたは[MCVE]を提供する必要があります。 – IanS

+0

@Boggartflyありがとう、私は動作しなかったものを追加しました – ASD

答えて

1

ASD - dfに何かを繰り返し格納することが非常に遅いことに気付きました。あなたのTextBlobをリスト(または別の構造体)に格納し、そのリストをdfの列に変換しようとします。

関連する問題