Take a numpy view with a singleton size

Customization

I have a Fortran sorted numpy array with one dimensional size

In [1]: import numpy as np

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

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

I am considering this array with a smaller 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)

Questions

First, should I be concerned about the outcome of these steps? The next line contains more than eight bytes.

Secondly, I really wanted this array form to be (4, 3), not (12, 3). Typically, with arrays installed, Fortran .viewwill expand the first dimension, not the second.

Thirdly, how can I rotate the array above (or any array created using a similar process into an array (4, 3)) so that in this case three of the four lines are zeros. Am I missing a smart transposition trick?

Version

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
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)

numpy 1.8.2 Py3 ( 32b)

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)
+1

Source: https://habr.com/ru/post/1616647/


All Articles