最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python利用多進程將大量數(shù)據(jù)放入有限內存的教程

 更新時間:2015年04月01日 10:54:58   作者:James Tobin  
這篇文章主要介紹了Python利用多進程將大量數(shù)據(jù)放入有限內存的教程,使用了multiprocessing和pandas來加速內存中的操作,需要的朋友可以參考下

簡介

這是一篇有關如何將大量的數(shù)據(jù)放入有限的內存中的簡略教程。

與客戶工作時,有時會發(fā)現(xiàn)他們的數(shù)據(jù)庫實際上只是一個csv或Excel文件倉庫,你只能將就著用,經(jīng)常需要在不更新他們的數(shù)據(jù)倉庫的情況下完成工作。大部分情況下,如果將這些文件存儲在一個簡單的數(shù)據(jù)庫框架中或許更好,但時間可能不允許。這種方法對時間、機器硬件和所處環(huán)境都有要求。

下面介紹一個很好的例子:假設有一堆表格(沒有使用Neo4j、MongoDB或其他類型的數(shù)據(jù)庫,僅僅使用csvs、tsvs等格式存儲的表格),如果將所有表格組合在一起,得到的數(shù)據(jù)幀太大,無法放入內存。所以第一個想法是:將其拆分成不同的部分,逐個存儲。這個方案看起來不錯,但處理起來很慢。除非我們使用多核處理器。
目標

這里的目標是從所有職位中(大約1萬個),找出相關的的職位。將這些職位與政府給的職位代碼組合起來。接著將組合的結果與對應的州(行政單位)信息組合起來。然后用通過word2vec生成的屬性信息在我們的客戶的管道中增強已有的屬性。

這個任務要求在短時間內完成,誰也不愿意等待。想象一下,這就像在不使用標準的關系型數(shù)據(jù)庫的情況下進行多個表的連接。
數(shù)據(jù)

201541105411439.jpg (1274×406)

示例腳本

下面的是一個示例腳本,展示了如何使用multiprocessing來在有限的內存空間中加速操作過程。腳本的第一部分是和特定任務相關的,可以自由跳過。請著重關注第二部分,這里側重的是multiprocessing引擎。

#import the necessary packages
import pandas as pd
import us
import numpy as np
from multiprocessing import Pool,cpu_count,Queue,Manager
 
# the data in one particular column was number in the form that horrible excel version
# of a number where '12000' is '12,000' with that beautiful useless comma in there.
# did I mention I excel bothers me?
# instead of converting the number right away, we only convert them when we need to
def median_maker(column):
  return np.median([int(x.replace(',','')) for x in column])
 
# dictionary_of_dataframes contains a dataframe with information for each title; e.g title is 'Data Scientist'
# related_title_score_df is the dataframe of information for the title; columns = ['title','score']
### where title is a similar_title and score is how closely the two are related, e.g. 'Data Analyst', 0.871
# code_title_df contains columns ['code','title']
# oes_data_df is a HUGE dataframe with all of the Bureau of Labor Statistics(BLS) data for a given time period (YAY FREE DATA, BOO BAD CENSUS DATA!)
 
def job_title_location_matcher(title,location):
  try:
    related_title_score_df = dictionary_of_dataframes[title]
    # we limit dataframe1 to only those related_titles that are above
    # a previously established threshold
    related_title_score_df = related_title_score_df[title_score_df['score']>80]
 
    #we merge the related titles with another table and its codes
    codes_relTitles_scores = pd.merge(code_title_df,related_title_score_df)
    codes_relTitles_scores = codes_relTitles_scores.drop_duplicates()
 
    # merge the two dataframes by the codes
    merged_df = pd.merge(codes_relTitles_scores, oes_data_df)
    #limit the BLS data to the state we want
    all_merged = merged_df[merged_df['area_title']==str(us.states.lookup(location).name)]
 
    #calculate some summary statistics for the time we want
    group_med_emp,group_mean,group_pct10,group_pct25,group_median,group_pct75,group_pct90 = all_merged[['tot_emp','a_mean','a_pct10','a_pct25','a_median','a_pct75','a_pct90']].apply(median_maker)
    row = [title,location,group_med_emp,group_mean,group_pct10,group_pct25, group_median, group_pct75, group_pct90]
    #convert it all to strings so we can combine them all when writing to file
    row_string = [str(x) for x in row]
    return row_string
  except:
    # if it doesnt work for a particular title/state just throw it out, there are enough to make this insignificant
    'do nothing'

這里發(fā)生了神奇的事情:

#runs the function and puts the answers in the queue
def worker(row, q):
    ans = job_title_location_matcher(row[0],row[1])
    q.put(ans)
 
# this writes to the file while there are still things that could be in the queue
# this allows for multiple processes to write to the same file without blocking eachother
def listener(q):
  f = open(filename,'wb')
  while 1:
    m = q.get()
    if m =='kill':
        break
    f.write(','.join(m) + 'n')
    f.flush()
  f.close()
 
def main():
  #load all your data, then throw out all unnecessary tables/columns
  filename = 'skill_TEST_POOL.txt'
 
  #sets up the necessary multiprocessing tasks
  manager = Manager()
  q = manager.Queue()
  pool = Pool(cpu_count() + 2)
  watcher = pool.map_async(listener,(q,))
 
  jobs = []
  #titles_states is a dataframe of millions of job titles and states they were found in
  for i in titles_states.iloc:
    job = pool.map_async(worker, (i, q))
    jobs.append(job)
 
  for job in jobs:
    job.get()
  q.put('kill')
  pool.close()
  pool.join()
 
if __name__ == "__main__":
  main()

由于每個數(shù)據(jù)幀的大小都不同(總共約有100Gb),所以將所有數(shù)據(jù)都放入內存是不可能的。通過將最終的數(shù)據(jù)幀逐行寫入內存,但從來不在內存中存儲完整的數(shù)據(jù)幀。我們可以完成所有的計算和組合任務。這里的“標準方法”是,我們可以僅僅在“job_title_location_matcher”的末尾編寫一個“write_line”方法,但這樣每次只會處理一個實例。根據(jù)我們需要處理的職位/州的數(shù)量,這大概需要2天的時間。而通過multiprocessing,只需2個小時。

雖然讀者可能接觸不到本教程處理的任務環(huán)境,但通過multiprocessing,可以突破許多計算機硬件的限制。本例的工作環(huán)境是c3.8xl ubuntu ec2,硬件為32核60Gb內存(雖然這個內存很大,但還是無法一次性放入所有數(shù)據(jù))。這里的關鍵之處是我們在60Gb的內存的機器上有效的處理了約100Gb的數(shù)據(jù),同時速度提升了約25倍。通過multiprocessing在多核機器上自動處理大規(guī)模的進程,可以有效提高機器的利用率。也許有些讀者已經(jīng)知道了這個方法,但對于其他人,可以通過multiprocessing能帶來非常大的收益。順便說一句,這部分是skill assets in the job-market這篇博文的延續(xù)。

相關文章

  • Python加pyGame實現(xiàn)的簡單拼圖游戲實例

    Python加pyGame實現(xiàn)的簡單拼圖游戲實例

    這篇文章主要介紹了Python加pyGame實現(xiàn)的簡單拼圖游戲,以一個完整實例形式分析了pyGame模塊操作圖片的相關技巧,需要的朋友可以參考下
    2015-05-05
  • python和bash統(tǒng)計CPU利用率的方法

    python和bash統(tǒng)計CPU利用率的方法

    這篇文章主要介紹了python和bash統(tǒng)計CPU利用率的方法,涉及Python針對系統(tǒng)硬件信息的讀取技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-07-07
  • Python?中的異步?for?循環(huán)示例詳解

    Python?中的異步?for?循環(huán)示例詳解

    這篇文章主要介紹了Python中的異步for循環(huán),我們將討論 Python 庫 asyncio 和運行異步代碼所需的函數(shù),需要的朋友可以參考下
    2023-05-05
  • Python如何使用paramiko模塊連接linux

    Python如何使用paramiko模塊連接linux

    這篇文章主要介紹了Python如何使用paramiko模塊連接linux,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-03-03
  • 解決Tensorboard 不顯示計算圖graph的問題

    解決Tensorboard 不顯示計算圖graph的問題

    今天小編就為大家分享一篇解決Tensorboard 不顯示計算圖graph的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • PyCharm 無法 import pandas 程序卡住的解決方式

    PyCharm 無法 import pandas 程序卡住的解決方式

    這篇文章主要介紹了PyCharm 無法 import pandas 程序卡住的解決方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • python 爬取京東指定商品評論并進行情感分析

    python 爬取京東指定商品評論并進行情感分析

    本文主要講述了利用Python網(wǎng)絡爬蟲對指定京東商城中指定商品下的用戶評論進行爬取,對數(shù)據(jù)預處理操作后進行文本情感分析,感興趣的朋友可以了解下
    2021-05-05
  • python實現(xiàn)趣味圖片字符化

    python實現(xiàn)趣味圖片字符化

    這篇文章主要為大家詳細介紹了python實現(xiàn)趣味圖片字符化,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • Python使用GeoIP2實現(xiàn)地圖定位

    Python使用GeoIP2實現(xiàn)地圖定位

    GeoIP2是一種IP地址定位庫,它允許開發(fā)人員根據(jù)IP地址查找有關位置和地理位置的信息,這篇文章主要為大家介紹了python如何使用GeoIP2實現(xiàn)地圖定位,感興趣的可以了解下
    2023-10-10
  • python使用pika庫調用rabbitmq交換機模式詳解

    python使用pika庫調用rabbitmq交換機模式詳解

    這篇文章主要介紹了python使用pika庫調用rabbitmq交換機模式詳解,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下
    2022-08-08

最新評論

宁陕县| 加查县| 丹棱县| 扶余县| 罗江县| 白河县| 花莲县| 万荣县| 长子县| 磐石市| 南安市| 哈尔滨市| 凤冈县| 黄平县| 游戏| 天镇县| 岱山县| 扶余县| 新宾| 泾阳县| 通辽市| 临江市| 景东| 莎车县| 黎平县| 建始县| 高要市| 邳州市| 银川市| 汝州市| 石家庄市| 紫阳县| 临朐县| 仙游县| 中卫市| 南华县| 溆浦县| 鹤岗市| 黎平县| 额尔古纳市| 阳泉市|