0
概述:我很努力去理解這個嵌套mask使用np.where時出錯的地方。我所希望的是 - 如果積雪是真的,則分配1,如果爲假,則評估第二個np.where,如果no_snow爲真,則測試0,如果爲false(意思是雪是假,如果是假,則不分配),則分配2 。嵌套np.where和布爾型數組索引問題
# open IMS & pull necessary keys.
hf = h5py.File(ims_dir + 'ims_daily_snow_cover.h5', 'r')
ims = hf['snow_cover'][...]
# create an empty parameter to be later written to new hdf file as gap_fill_flag.
dataset_fill = np.zeros(ims.shape)
# loop through fill - branch based on temporal fill or merra fill.
for day in range(len(fill)):
# print len(day)
print day
fill[day] == 2
year = days[day][:4]
# merra fill - more than one consecutive day missing.
if (fill[day-1] == 2) | (fill[day+1] == 2):
# run merra_fill function
# fill with a 2 to signify data are filled from merra.
ims[day, :] = merra_fill(days[day], ims[day, :])
dataset_fill[day, :] = 2
else:
# temporal_fill - less than one consecutive day missing.
snow = ((ims[day - 1:day+2, :] == 1).sum(axis=0)) == 2
no_snow = ((ims[day - 1:day+2, :] == 0).sum(axis=0)) == 2
# nested np.where.
ims[day, :] = np.where(snow == True, 1, np.where(no_snow == True, 0, 2))
dataset_fill[day, :][ims[day, :] < 2] = 1
dataset_fill[day, :][ims[day, :] == 2] = 2
ims[day, :][ims[day, :] == 2] = merra_fill(days[day], ims[day, :])
錯誤:
ims[day, :] = np.where(snow == True, 1, np.where(no_snow == True, 0, 2))
ValueError: NumPy boolean array indexing assignment cannot assign 2005409 input values to the 0 output values where the mask is true
幫助我,計算器。你是我唯一的希望。
什麼是ims? ... –
@Jérômeims是hdf文件中的一個屬性,形狀爲(6574,2005409),代表積雪。它被稱爲全局變量。 – Nikolai
PyCharm建議我使用'如果cond爲真:'或'如果cond:'這將如何應用於這行代碼中並且結果是否相同? – Nikolai