The difference between the storage method (n, 1) and (n,) of array array objects in numpy

  Information: https://stackoverflow.com/questions/22053050/difference-between-numpy-array-shape-r-1-and-r

  This article is a question on stackoverflow that I accidentally opened. It is about the array object in numpy. Needless to say, the important position of numpy in the world of python and machine learning. Translate this answer here to comprehend it for learning.

  Note: The translation is for learning purposes only. The author is Gareth Rees and may have my own modifications.

  For learning purposes only !!!

  The best way to look at NumPy arrays is to divide it into two parts, a data buffer contains a raw element (original element), and a view (I call it window) to describe and interpret the data buffer.

  For example, if we create an array  a  containing 12 integers :

>>> a = numpy.arange(12)
>>> a
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])

  Then a contains a data buffer, stored as follows:

┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐
│  0 │  1 │  2 │  3 │  4 │  5 │  6 │  7 │  8 │  9 │ 10 │ 11 │
└────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┘

  And a window that defines how to interpret the data:

>>> a.flags
  C_CONTIGUOUS : True
  F_CONTIGUOUS : True
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  UPDATEIFCOPY : False
>>> a.dtype
dtype('int64')
>>> a.itemsize
8
>>> a.strides
(8,)
>>> a.shape
(12,)

  Here, shape = (12,) means that the array is dominated by only one index: from 0 to 11. Conceptually, if we use this separate index to marked label, then  a  would look like this:

i= 0    1    2    3    4    5    6    7    8    9   10   11
┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐
│  0 │  1 │  2 │  3 │  4 │  5 │  6 │  7 │  8 │  9 │ 10 │ 11 │
└────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┘

  The operation of reshape an array does not change the data buffer, but creates a new window to interpret the data.

>>> b = a.reshape((3, 4))

  The above operation creates a  b that  has the same data buffer as  , but now it is dominated by two-dimensions indexed. One from 0 to 2, and one from 0 to 3. If we label the data, b  will look like this:

i= 0    0    0    0    1    1    1    1    2    2    2    2
j= 0    1    2    3    0    1    2    3    0    1    2    3
┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐
│  0 │  1 │  2 │  3 │  4 │  5 │  6 │  7 │  8 │  9 │ 10 │ 11 │
└────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┘

  This also means:

>>> b[2,1]
9

  The second index changes faster than the first index. If you want to reverse, you can use the following parameters to create an array  c 

>>> c = a.reshape((3, 4), order='F')

  Added: order = 'F' or order = 'C' means that the indexing method of the array is like C language or Fortran, respectively, 'C' is the default value

  This will produce an array with the following indexes:

i= 0    1    2    0    1    2    0    1    2    0    1    2
j= 0    0    0    1    1    1    2    2    2    3    3    3
┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐
│  0 │  1 │  2 │  3 │  4 │  5 │  6 │  7 │  8 │  9 │ 10 │ 11 │
└────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┘

  Meaning

>>> c[2,1]
5

  With the previous bedding, it is easy to understand the following example:

>>> d = a.reshape((12, 1))

  The array is dominated by two indexes, the first index is from 0 to 11, and the second index is always 0:

i= 0    1    2    3    4    5    6    7    8    9   10   11
j= 0    0    0    0    0    0    0    0    0    0    0    0
┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐
│  0 │  1 │  2 │  3 │  4 │  5 │  6 │  7 │  8 │  9 │ 10 │ 11 │
└────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┘

  and so:

>>> d[10,0]
10

  A flattened one-dimensional array is free in a sense, so we can define the size of each dimension ourselves:

>>> e = a.reshape((1, 2, 1, 6, 1))

  The above operation creates an array like this:

i= 0    0    0    0    0    0    0    0    0    0    0    0
j= 0    0    0    0    0    0    1    1    1    1    1    1
k= 0    0    0    0    0    0    0    0    0    0    0    0
l= 0    1    2    3    4    5    0    1    2    3    4    5
m= 0    0    0    0    0    0    0    0    0    0    0    0
┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐
│  0 │  1 │  2 │  3 │  4 │  5 │  6 │  7 │  8 │  9 │ 10 │ 11 │
└────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┘

  and so:

>>> e[0,1,0,0,0]
6

Guess you like

Origin www.cnblogs.com/chester-cs/p/12682612.html