저는 Pandas Dataframe에 저장된 370k 레코드의 데이터 세트를 통합해야합니다. 멀티 프로세싱, 스레딩, Cpython 및 루프 언 롤링을 시도했습니다. 그러나 나는 성공하지 못했고 계산 시간은 22 시간이었다.루프를 통해 파이썬 속도를 높이려면 어떻게해야합니까?
%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
- 윈도우 10
는 구현 멀티 나에게 더 높은 계산 시간을 주었다. 스레딩이 순차적으로 실행되었습니다 (GIL 때문에 생각합니다). 루프 언 롤링은 동일한 계산 속도를주었습니다. Cpython에서 라이브러리를 가져 오는 중에 오류가 발생했습니다.
"그래서 다중 처리, 스레딩, Cpython 및 루프 언 롤링을 시도했습니다." 무엇이 효과가 없었습니까? 질문에 게시 할 수 있습니까? – Boggartfly
[MCVE]를 제공해야합니다. – IanS
@ Boggartfly 고마워요, 작동하지 않는 것들을 추가했습니다. – ASD