使用numpy数组作为输入的函数的多处理

时间:2019-02-17 16:09:26

标签: python-3.x numpy multiprocessing

我有一个函数调用data_transform。它以二维numpy数组作为输入。我如何才能使此数据转换功能并行化?

df = {'col2': ['a','a','b','a','b','a','a'],'col3': ['a1', 'a2','a2','a1','a1','a2','a1']}
df = pd.DataFrame(data=df)
L1 = df.values  
L1 = np.transpose(L1) 
print(L1)

def data_transform(L1):
    global L3
    L3 = []
    RV = []
    for i in range(len(L1)):
       Qual_RV = list(set(L1[i]))
       Qual_RV = np.array(Qual_RV)
       RV.append(Qual_RV)
       index = np.array([],dtype=int)
       for j in range(0,len(L1[i])):
           x = np.where( Qual_RV == L1[i][j] ) 
           index = np.append(index, x[0][0])

       mask_array = np.zeros((len(L1[i]), len(Qual_RV))) 
       index1  = np.array([np.arange(len(L1[i])),index])  

       flat_index_array = np.ravel_multi_index(index1,mask_array.shape)
       np.ravel(mask_array)[flat_index_array] = 1
       mm = np.array(mask_array).tolist()  
       L3.append(mm) 
    return L3

data_transform(L1)
print(L3)

0 个答案:

没有答案