首页 > 解决方案 > 使用 joblib 共享 pandas 数据框以进行不同的分类过程

问题描述

我正在使用引用此链接joblib.memory的缓存数据框。因此,其他进程可以使用相同的 shared 。我必须训练多个分类器,对于所有分类器都是通用的,但对于每个分类器来说都是不同的。我的想法是与所有分类过程共享,这样就不需要为每个分类加载到内存中。dataframedfXdfydataframe dfXdfX

def costly_compute():
    df_v = pd.read_parquet(file, columns=var, engine="fastparquet")    
    return df_v


location = './cachedir'
memory = Memory(location, verbose=0)
costly_compute_cached = memory.cache(costly_compute)

def data_processing_using_cache(col):
    start = time.time()
    dfX = costly_compute_cached()
    stop= time.time()
    print("reading dfx",stop-start)

    dfy = pd.read_parquet(file, columns=[col]) 
    ##Training with (dfX,dfy) and generate accuracy for col
    return accuracy

start = time.time()
results = data_processing_using_cache(y0)
stop = time.time()
print('\nFirst round - caching the data')
print('Elapsed time for the entire processing: {:.2f} s'
      .format(stop - start))


start = time.time()
results = data_processing_using_cache(y0)
stop = time.time()
print('\Second round - serial- using caching data')
print('Elapsed time for the entire processing: {:.2f} s'
      .format(stop - start))

start = time.time()
results = Parallel(n_jobs=2)(
    delayed(data_processing_mean_using_cache)(col)
    for col in all_y_cols)
stop = time.time()
print('\Third round - parallel- using caching data')
   

所有三个回合都需要相似的时间。这是否意味着第二轮和第三轮没有使用缓存数据帧?

标签: pythonpandasmachine-learningmultiprocessingjoblib

解决方案


推荐阅读