首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >嵌套np.where和布尔数组索引问题

嵌套np.where和布尔数组索引问题
EN

Stack Overflow用户
提问于 2016-10-20 15:32:31
回答 1查看 445关注 0票数 0

概述:--我很难理解使用np.where的嵌套掩码的错误所在。我希望的是-如果雪为真,则分配1,如果为假,则分配第二个np.where,这将测试no_snow是否为真,如果no_snow为true,则分配0 (如果no_snow为false,则为false),则分配2。

代码语言:javascript
运行
复制
# 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, :])

错误:

代码语言:javascript
运行
复制
    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

帮帮我,堆积如山。你是我唯一的希望。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-20 17:39:15

来自帮助(np.where):

代码语言:javascript
运行
复制
where(condition, [x, y])

Return elements, either from `x` or `y`, depending on `condition`.

If only `condition` is given, return ``condition.nonzero()``.

Parameters
----------
condition : array_like, bool
    When True, yield `x`, otherwise yield `y`.
x, y : array_like, optional
    Values from which to choose. `x` and `y` need to have the same
    shape as `condition`.

我怀疑np.where(no_snow...)是否具有与snow == True相同的形状。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40158720

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档