I have an array and want to change elements based on a classification using another array.
That is, I import one array, and then if the value of cell[i,j] is within a certain limit (say between 1 and 8) then multiply secondArray[i,j] by 0.3 and put result into an ouput array at place [i,j]
I have some code that does this (and probably explains what I mean a bit clearer) but it takes a 'very' long time (as my arrays are about 1000*1000 elements) and wondered if there is a more efficient solution.
Currently I have:
..
import numpy as np
def Factor_AM(cell):
if cell < 1 return 0.2;
elif cell < 8: return 0.3;
else: return 0.5;
mat = np.array(..some code to get an array from an external source..) //size 1000*1000
mat_out_1 = np.zeros_like(mat)
mat_out_1 = np.zeros_like(mat)
mat_ClassifyMe = np.array(..some code to import another 1000*1000 array with values in)
for index, x in np.ndenumerate(mat):
mat_out_1[index] = Factor_AM(x) * mat_ClassifyMe[index]
mat_out_2[index] = (1-Factor_AM(x)) * mat_ClassifyMe[index]
..some code to output mat_out_1 and mat_out_2
I saw some documentation on the np.where and np.argwhere functions that look hopeful, but given I have more than 1 test (in the above code I have 3 but in reality I have 12) I can't think of a way without making very ugly nested statements.
Is there another way to do this, or is this as efficient as Python gets?