【问题标题】:How to insert value in numpy ndArray?如何在 numpy ndArray 中插入值?
【发布时间】:2020-04-21 04:44:00
【问题描述】:

我有两个 ndArray。

ex: 
x = np.array([110,200, 500,100])
y = np.array([50,150,30,70])

现在基于它们的价值,我创建了一个图像。

x_shape = np.max(x)   #x_shape=500
y_shape = np.max(y)   #y-shape=150
image = np.zeros((x_shape+1, y_shape+1))

根据我现在的数据,我的图像大小是 (501,151)

现在,如何将 (x, y) 中的数据作为 x,y 对插入?我的意思是像素值: (110,50), (200,150), (500,30), (100,70) 我希望图像是白色的,其余像素是暗的。我怎样才能做到这一点?

【问题讨论】:

    标签: python numpy numpy-ndarray


    【解决方案1】:

    基于OP's own answer,可以使用矢量化方法对其进行改进:

    import numpy as np
    import matplotlib.pyplot as plt
    
    x = np.array([110,200, 500,100])
    y = np.array([50,150,30,70])
    x = np.floor(x / 10).astype(int)
    y = np.floor(y / 10).astype(int)
    x_shape = np.max(x)   # x_shape = 500
    y_shape = np.max(y)   # y_shape = 150
    image = np.zeros((x_shape + 10, y_shape + 10))
    image[x, y] = 10
    
    plt.imshow(image)
    

    (说句公道话,我从问题中不明白这就是 OP 所追求的)。


    编辑

    在不从 cmets 调整大小的情况下解决“可视化问题”:

    import numpy as np
    import matplotlib.pyplot as plt
    
    x = np.array([110, 200, 500, 100])
    y = np.array([50, 150, 30, 70])
    
    x_shape = np.max(x)
    y_shape = np.max(y)
    image = np.zeros((x_shape + 1, y_shape + 1))
    image[x, y] = 10
    
    plt.figure(figsize=(20, 20))
    plt.imshow(image.transpose(), interpolation='nearest', aspect='equal')
    

    【讨论】:

    • 这就是所谓的花式索引
    • 感谢您的回答。但是,如果我们在 10 之前不进行设备,则它不可见。我猜单个像素太小而无法看到。你能建议任何修改而不除 10 吗? @norok2
    • 回答已被接受并支持。谢谢。如果你给我修改后的版本,我会很高兴。 @norok2
    • @Kazi 这是一个可视化,问题.. 看看here。我还包括了一个编辑来解决这个问题。
    【解决方案2】:

    不确定你到底需要什么,你可以试试

    a = np.array([110, 200, 500, 100])
    b = np.array([50, 150, 30, 70])
    
    np.array([zip(x,y) for x,y in zip(a,b)])
    pd.DataFrame(list(zipped))```
    ##or another representation
    np.dstack((x,y))
    
    
    both are taken from  https://stackoverflow.com/questions/49461605/why-do-we-need-to-convert-a-zipped-object-into-a-list
    
    
      [1]: https://stackoverflow.com/questions/49461605/why-do-we-need-to-convert-a-zipped-object-into-a-list
    

    【讨论】:

    • 感谢您的回答。它有助于找到解决方案。实际上我想从笛卡尔坐标中的一些值创建一个图像。无论如何都赞成你的帮助@olena
    • 这不是一个好办法。您正在放弃 numpy 的所有好处,并且不指定 ab 应该是什么。
    【解决方案3】:

    嗯,我得到了答案。这很容易,因为我是新手,这让我很困惑。

       import numpy as np
       import matplotlib.pyplot as plt
       x = np.array([110,200, 500,100])
       y = np.array([50,150,30,70])
    
       x = np.floor(x/10).astype(int)  #devided by 10 to reduce the img size
       y = np.floor(y/10).astype(int)  #devided by 10 to reduce the img size
       x_shape = np.max(x)   #x_shape=500
       y_shape = np.max(y)   #y-shape=150
       image = np.zeros((x_shape+10, y_shape+10))
       for x, y in zip(x,y):
    
            image[x,y]=200
    
       plt.imshow(image)
    

    【讨论】:

    • 这种方法效率不高。使用矢量化代码可以获得更好的性能。
    • 我对矢量化一无所知。你能创建一个答案吗?如果它有效,我可能会接受。谢谢。 @norok2
    • done...基本上你只是利用advanced indexing
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-24
    • 2012-08-31
    • 1970-01-01
    • 2018-10-10
    • 2018-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多