python - 将 numpy 二维数组划分为更精细的分辨率

标签 python arrays numpy

我有一个 2d numpy 数组 (2 x 2) 元素,我想从中创建另一个 2D numpy 数组,这样:

二维数组:

import numpy as np
np.random.rand(2,2)
  array([[10.0,8.0],
              [6.0,4.0]])

我想从中得到一个 4x4 数组,以便与较粗糙数组的特定单元格对应的较精分割辨率数组的所有值与较粗糙数组相同:

  array([[10.0,10.0,8.0,8.0],
            [10.0,10.0,8.0,8.0]
            [6.0,6.0,4.0,4.0]
            [6.0,6.0,4.0,4.0]])

我可以使用 for 循环来做到这一点,但我真的很想知道是否存在更 pythonic 的方法。

最佳答案

您可以使用 repeat :

>>> a = np.random.rand(2,2)
>>> a
array([[ 0.66172561,  0.09262421],
       [ 0.40578266,  0.84510431]])
>>> a.repeat(2, 0).repeat(2, 1)
array([[ 0.66172561,  0.66172561,  0.09262421,  0.09262421],
       [ 0.66172561,  0.66172561,  0.09262421,  0.09262421],
       [ 0.40578266,  0.40578266,  0.84510431,  0.84510431],
       [ 0.40578266,  0.40578266,  0.84510431,  0.84510431]])

关于python - 将 numpy 二维数组划分为更精细的分辨率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32447588/

相关文章:

javascript - 如何在Django模板中刷新表格

php - 从表中选择不在 php 数组查询中不起作用

Java:当数组中什么都没有时,为什么数组的长度是1

python - pygobject 中的拖放文件示例

python - 如何确保 re.findall() 停在正确的位置?

python - 如何用 Python3 读写 INI 文件?

numpy - 使用 Numpy 在 python 中移动 HSV 像素值

arrays - dynamodB 将项目 : "UnexpectedParameter: Unexpected key ' L' found in params. 项目“

python - 从字符串中删除小写子字符串的快速方法?

python - 使用 Python 移动最小值