python - How to print values of a 3 dimensional array that are less than a specific value? -
so have dataset named data_low looks this:
(array([ 0, 0, 0, ..., 30, 30, 30]), array([ 2, 2, 5, ..., 199, 199, 199]), array([113, 114, 64, ..., 93, 94, 96])) and shape: (84243,3).
i can unique value precipitation dataset this:
in [63]: print(data_low[0, 2, 113]) out [63]: 1.74 what trying print values in dataset have value of less 3.86667. i'm pretty new python, , don't know loop use in order this. appreciated. thanks.
edit: here program have. context, used ncecat combine 31 datasets, why have 3 1d arrays: first array day, , 2nd , 3rd represent longitude , latitude.
data_path = r"c:\users\matth\downloads\trmm_3b42rt\3b42rt_daily.201001.7.nc4" f = dataset(data_path) latbounds = [ -38 , -20 ] lonbounds = [ 115 , 145 ] # degrees east ? lats = f.variables['lat'][:] lons = f.variables['lon'][:] # latitude lower , upper index latli = np.argmin( np.abs( lats - latbounds[0] ) ) latui = np.argmin( np.abs( lats - latbounds[1] ) ) # longitude lower , upper index lonli = np.argmin( np.abs( lons - lonbounds[0] ) ) lonui = np.argmin( np.abs( lons - lonbounds[1] ) ) precip_subset = f.variables['precipitation'][ : , lonli:lonui , latli:latui ] print(precip_subset.shape) print(precip_subset.size) print(np.mean(precip_subset)) data_low = np.nonzero((precip_subset > 0) & (precip_subset < 3.86667)) print(data_low) x = list(zip(*data_low))[:] xx = np.array(x) print(xx.shape) print(xx.size) in range(0,84243,1): print(data_low[i, i, i]) out:
in [136]: %run "c:\users\matth\precip_anomalies.py" (31, 120, 72) 267840 1.51398 (array([ 0, 0, 0, ..., 30, 30, 30]), array([ 7, 7, 7, ..., 119, 119, 119]), array([ 9, 10, 11, ..., 23, 53, 54])) (13982, 3) 41946 [ 0 0 0 ..., 30 30 30] typeerrortraceback (most recent call last) c:\users\matth\precip_anomalies.py in <module>() 53 54 in range(0,84243,1): ---> 55 print(data_low[i, i, i]) typeerror: tuple indices must integers, not tuple
given data_low numpy matrix (based on question not, 3-tuple 3 arrays), can use masking:
data_low[data_low < 3.86667] this return 1d numpy array contains values less 3.86667.
if want these vanilla python list, can use:
list(data_low[data_low < 3.86667]) but if want further processing (in numpy) better use numpy array anyway.
Comments
Post a Comment