首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为不同的ndarray索引分配相同的值- Python

为不同的ndarray索引分配相同的值- Python
EN

Stack Overflow用户
提问于 2014-03-16 01:33:01
回答 2查看 404关注 0票数 3

这个问题可能不清楚,所以我用代码来解释:

代码语言:javascript
复制
@staticmethod
def generate_data_uncertainty_heat_map(data_property, data_uncertainty, file_path):
    plt.figure()
    uncertainty = numpy.zeros((data_property.rows, data_property.cols, 4), 'uint8')
    uncertainty[..., 0] = uncertainty[..., 1] = uncertainty[..., 2] = numpy.uint8(data_property * 255)
    uncertainty[..., 3] = numpy.uint8(data_uncertainty)
    fig = plt.imshow(uncertainty, extent=[0, data_property.cols, data_property.rows, 0])
    plt.colorbar(fig)
    plt.savefig(file_path + '.png')
    plt.close()

这是用两个(n,n)个ndarray来形成一个带有matploblib的RGBA图像。为此,我使用data_property参数作为RGB,使用data_property参数作为不透明度。

我基本上想知道我能不能

代码语言:javascript
复制
uncertainty[..., 0] = uncertainty[..., 1] = uncertainty[..., 2]

用另一种方式来表示不确定性的数据.0或1或2值应该是numpy.uint8(data_property * 255)

提前谢谢你。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-03-16 01:44:30

您可以使用data_property[..., np.newaxis]在右边添加一个新的轴。然后就可以这样完成任务:

代码语言:javascript
复制
uncertainty[..., :3] = numpy.uint8(data_property * 255)[..., np.newaxis]

例如,

代码语言:javascript
复制
In [49]: x = np.arange(6).reshape(2,3)

In [50]: x
Out[50]: 
array([[0, 1, 2],
       [3, 4, 5]])

In [51]: y = np.zeros((2,3,4))

In [52]: y[...,:3] = x[...,np.newaxis]

In [53]: y
Out[53]: 
array([[[ 0.,  0.,  0.,  0.],
        [ 1.,  1.,  1.,  0.],
        [ 2.,  2.,  2.,  0.]],

       [[ 3.,  3.,  3.,  0.],
        [ 4.,  4.,  4.,  0.],
        [ 5.,  5.,  5.,  0.]]])

In [54]: np.allclose(y[...,0], x)
Out[54]: True

In [55]: np.allclose(y[...,1], x)
Out[55]: True

In [56]: np.allclose(y[...,2], x)
Out[56]: True
票数 0
EN

Stack Overflow用户

发布于 2014-03-16 01:44:14

是的,只要data_propertyuncertainty[..., 0]有相同的shape,你就可以。

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

https://stackoverflow.com/questions/22431937

复制
相关文章

相似问题

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