python - Efficient way to iterate through numpy arrays in parallel and create a new resultant array -
i have 3 numpy arrays dm_w, dm_s
, dm_p
. in need of iterating through these arrays in parallel, computation based on check condition shown in code below.
my code works smaller arrays, takes long larger arrays. need efficient , faster method achieve this. need expert opinion.
my code:
prox_mat = [] w_dist, s_dist, pi in zip(np.nditer(dm_w), np.nditer(dm_s), np.nditer(dm_p)): if pi == 0.0: proximity_score = ((w_dist + len(np.unique(dm_s) * s_dist)) / (dm_w.shape[0] * len(np.unique(dm_s)))) prox_mat.append(proximity_score) else: proximity_score = ((w_dist + len(np.unique(dm_s) * s_dist)) / (dm_w.shape[0] * len(np.unique(dm_s)))) * log10(10 * pi) prox_mat.append(proximity_score) ps = np.array(prox_mat) ps = np.reshape(ps, dm_w.shape)
several things. one, computation of np.unique(dm_s)
should pulled outside of loop. further, looks like:
len(np.unique(dm_s) * s_dist) == len(np.unique(dm_s))
which should either pulled out of loop or mistake. in case..
we should vectorize forloop/append construct:
dm_s_uniques = len(np.unique(dm_s)) logs = np.log10(10 * dm_p) logs[logs == -np.inf] = 1 prox_mat = ((dm_w + dm_s_uniques) / (dm_w.shape[0] * dm_s_uniques)) * logs ps = np.reshape(ps, dm_w.shape)
it looks map
Comments
Post a Comment