python - Efficient way to loop through numpy array -
hi have array number of nan values. looking way estimate values nan values through plane defined other finite data. plane covering full dimension more 1000*1000 exaggeration of data. idea loop through each nan value location window of 20*20 , find plane defined data avialable in window , estimate value @ center of window. process takes quite time process. looking if suggest me efficient way it. thankful.
dim = np.shape(data) row, col = np.where(np.isnan(data)) = row > 10 b = row < dim[0] - 10 c = col > 10 d = col < dim[1] - 10 row = row[a & b & c & d] col = col[a & b & c & d] interdata = np.zeros(np.shape(data)) interdata[np.isfinite(data)] = data[np.isfinite(data)] ii,jj in zip(row,col): block = data[ii - 10:ii + 10, jj - 10:jj + 10] # data in 11 11 window if not np.all(np.isnan(block)): block[block > 2 * np.median( block[np.isfinite(block)])] = np.nan # replace outliers greater twice median nan. pointvalue = block[np.isfinite(block)] loc = np.ones((pointvalue.shape[0], 3)) loc[:, 0:2] = np.transpose(np.where(np.isfinite(block))) c, _, _, _ = sp.linalg.lstsq(loc, pointvalue) # plane fitting interdata[ii, jj] = c[0] * 10 + c[1] * 10 + c[2] # estimation of value coefficients defining plane
it might somehow duplicate question looked lot of similar questions have been asked before. of them dealing continuous data avoiding loop worked them.
you starting, far see, wrong premise: row , col independent! need rows , columns [a & b & c & d]. otherwise accepting rows of points , columns of other points. loop on each element of rows , within it, each elements of columns. doing nxn operations n nan points! if assume wrong points (250, 430) , (160, 470) "fixing": (250, 430), (250, 470), (160, 470) , (160, 430). recommend:
whr = numpy.where( (col < ...) & (col > ...) & (row < ...) & (row > ...)) rr,cc in zip(row[whr],col[whr]):
Comments
Post a Comment