python - 使用单例维度查看 numpy View

标签 python numpy

设置

我有一个 Fortran 有序的 numpy 数组,第一维是单例

In [1]: import numpy as np

In [2]: x = np.array([[1, 2, 3]], order='F')

In [3]: x.strides
Out[3]: (8, 8)

我用更小尺寸的 dtype 查看这个数组

In [4]: y = x.view('i2')

In [5]: y
Out[5]: array([[1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0]], dtype=int16)

In [6]: y.strides
Out[6]: (8, 2)

问题

首先,我应该担心这种进步的结果吗?下一行有超过八个字节。

其次,我真的希望这个数组的形状是(4, 3),而不是(12, 3)。通常使用 Fortran 布局的数组 .view 将扩展第一个维度,而不是第二个维度。

第三,如何将上面的数组(或类似过程生成的任何数组)转换为 (4, 3) 数组,这样,在本例中,四行中的三行是全为零。我是不是错过了一个巧妙的换位技巧?

版本

In [3]: sys.version_info
Out[3]: sys.version_info(major=3, minor=4, micro=3, releaselevel='final', serial=0)

In [4]: numpy.__version__
Out[4]: '1.10.1'

最佳答案

奇怪的是,当我重复你的步骤时,我立即得到 (4,3):

In [637]: x=np.array([[1,2,3]],'int64',order='F')
In [638]: x
Out[638]: array([[1, 2, 3]], dtype=int64)
In [639]: y=x.view('i2')
In [640]: y
Out[640]: 
array([[1, 2, 3],
       [0, 0, 0],
       [0, 0, 0],
       [0, 0, 0]], dtype=int16)
In [641]: x.shape
Out[641]: (1, 3)
In [642]: y.shape
Out[642]: (4, 3)
In [643]: x.strides
Out[643]: (8, 8)
In [644]: y.strides
Out[644]: (2, 8)

我在 Py3(旧 32b 机器)上的 numpy 1.8.2

订单C

In [655]: x=np.array([[1,2,3]],'int64')
In [656]: y=x.view('i2')
In [657]: y
Out[657]: array([[1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0]], dtype=int16)
In [658]: y.strides
Out[658]: (24, 2)
In [659]: x.strides
Out[659]: (24, 8)

关于python - 使用单例维度查看 numpy View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33816582/

相关文章:

python - 我能对 "ImportError: Cannot import name X"或 "AttributeError: ... (most likely due to a circular import)"做些什么?

python - 无缓冲的Python覆盖率

python - 使用 Numpy 数组,出现错误

python - 非线性函数的 numpy 爱因斯坦求和

python - 包含大型矩阵运算的优化函数

python - 使用 pyinstaller 创建 Python 可执行文件时未导入的库

python - Fast API Python 每个请求的特殊日志参数

python - Heroku:如何在部署时自动启动 Python 应用程序?

python - 在用 python 生成的图形中添加一些数字

python - 数据类型的 tensorflow 警告