【一文读懂】python 中的 numpy.reshape(a, newshape, order=‘C‘) 详细说明及实例讲解

numpy.reshape(a, newshape, order=‘C’)

在不改变数据的情况下给数组一个新的形状。

就是先将数组按给定索引顺序一维展开,然后按与展开时相同的索引顺序将展开的元素填充到新数组中;

即等价于 np.reshape(np.revel(array), newshape, order) .

1. 参数

a : 数组类,要被重塑的数组。


newshape : 整数或整数元组,用于指示新数组的形状,格式(行row,列col,… )。

  • 新的形状应该与原来的形状兼容,即元素个数必须相同。如果是一个整数,那么结果将是一个该长度的一维数组。
  • 形状的其中一个维度可以是 -1,在这种情况下,新形状的该维度值是由数组的长度和其余维度自动推断出来的。

order: {‘C’, ‘F’, ‘A’}, 可选参数。

  • 使用a的索引顺序读取其元素,并使用这个索引顺序将元素放入重塑的数组中。

  • ‘C’ , 行优先顺序 (表示使用类似 C 语言的索引顺序来读/写元素,最后一个轴的索引变化最快,回到第一个轴的索引变化最慢)。

  • ‘F’ , 列优先顺序 (表示使用类似 Fortran 语言的索引顺序来读/写元素,第一个索引变化最快,最后一个索引变化最慢)。

  • ‘A’ , 按存储顺序 (表示如果数组 ‘a’ 在内存中是类似 Fortran 语言的数组分布方式,则按照类似 Fortran 语言的索引顺序读/写元素,否则按照类似 C 语言的顺序)。

  • 注意 ‘C’ 和 ‘F’ 选项没有考虑底层数组的内存布局,只是参考了索引的顺序。

2. 返回值

reshaped_array : ndarray (n维数组)
如果可能的话,这将是一个新的视图对象;否则,它将是一个副本。 注意不能保证返回数组的内存布局(C-或Fortran-连续的)。


3. 如何理解 “新形状newshape,与原形状兼容,元素个数相同” ?

"""newshape 只能是整数或整数元组

若原形状是shape, shape的长度len(shape)=N, 那新形状newshape=(row, col, ...),
其中的row, col, ...必须是N的因式分解值,即维度值必须是N的因数(约数, 因子);

如: 
shape=5, newshape可取5,(1,5),(5,1),(1,1,5),(1,5,1),...
- 即不论newshape有几个维度, 其连乘积必须和shape相等;
shape=(2,4), 则len(shape)=8, 那么newshape=(3,-1)就是非法的
- 因为-1所在的第二维度值为8/3, 不是8的因数, 非整数;

注意: 
1. newshape为整数n时, 表示 1 维数组, n只能取len(shape)或-1, 其他数都是非法的;
2. newshape为整数元组(n[,m[,...]])时, 表示 2 维以上数组;
3. 特别地, newshape=(n,)表示 1 维数组, n 取len(shape)或-1;
4. newshape的任一维度值都可以是-1, 表示自动推断, 但最多只能有一个-1,
   这样numpy才能自动计算该维度值(因数);
5. -1通常用于不知道len(shape)的情况下, 生成限定维度数组, 如想让array变成只有一列,
   行数由numpy自动计算:(-1,1);
6. "1 维数组" 就是1行n列的一维数组, 纵向一列(n行1列)数组是二维数组, 
   基础概念不要搞错了:当数组中每个元素都只带有一个下标时,称这样的数组为一维数组;
7. np.reshape(array, newshape, order='C') 与 array.reshape(newshape,order='C') 等价, 
   但newshape为元组时, np.reshape(array,(n,m))合法, array.reshape((n,m))合法, 
   array.reshape(n,m)也合法, np.reshape(array,n,m)非法!
"""

import numpy as np

# 定义数组 b
>>> b = np.arange(5)
array([0, 1, 2, 3, 4])		# 此时 b 是 1 维数组, shape = 5, len(shape) = 5

# ---------- newshape取整数 -----------------
# 返回 1 维数组(以下8种表达方式等价)
>>> np.reshape(b,5)
>>> np.reshape(b,-1)
>>> np.reshape(b,(5,))
>>> np.reshape(b,(-1,))

>>> b.reshape(5)
>>> b.reshape(5,)
>>> b.reshape(-1)
>>> b.reshape(-1,)
array([0, 1, 2, 3, 4])		# 一维数组

# newshape 单值整数非法
>>> b.reshape(3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: cannot reshape array of size 5 into shape (3,) 
(无法将大小为5的数组重塑为(3,)的形状)

# --------- newshape取整数元组 -----------------
# 单行数组, 一维, 特殊情况
>>> b.reshape((-1,))	# 等价于 b.reshape(-1,)
array([0, 1, 2, 3, 4])

# 单行数组, 二维
>>> b.reshape(1,5)
>>> b.reshape(1,-1)
array([[0, 1, 2, 3, 4]])

# newshape参数未带括号错误
>>> np.reshape(b,1,5)
Traceback (most recent call last):
  File "..numpy\core\fromnumeric.py", line 58, in _wrapfunc
    return bound(*args, **kwds)
TypeError: order must be str, not int (order 必须是字符串, 不能是整数)

# 单列数组, 二维
>>> b.reshape(-1,1)
array([[0],
       [1],
       [2],
       [3],
       [4]])

# 三维数组
>>> b.reshape(1,5,1)
array([[[0],
        [1],
        [2],
        [3],
        [4]]])
>>> b.reshape(1,1,5)
array([[[0, 1, 2, 3, 4]]])
>>> b.reshape(5,1,1)
array([[[0]],
       [[1]],
       [[2]],
       [[3]],
       [[4]]])

# -1太多错误
>>> b.reshape(-1,-1,5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: can only specify one unknown dimension (只能指定一个未知维度)

4. 如何理解 order = {‘C’, ‘F’, ‘A’} ?

"""数组索引和顺序

C: C语言
F: Fortran语言
A: 

数组索引和顺序在Fortran和C之间有所不同:
1. 索引
   C数组始终从零开始,但是默认情况下,Fortran数组从1开始

2. 顺序
   Fortran数组以列优先顺序存储:A(3,2)
   A(1,1) A(2,1) A(3,1) A(1,2) A(2,2) A(3,2) A(1,3) A(2,3) A(3,3)

   C数组以行优先顺序存储: A[3][2]
   A[0][0] A[0][1] A[0][2] A[1][0] A[1][1] A[1][2] A[2][0] A[2][1] A[2][2]

注意: 
3. C 和 F 都只考虑索引顺序, 与数组元素在内存中的顺序无关;
4. A 基于数组元素在内存中的存储顺序, 行序存储用行序优先(等价C)读/写,
   列序存储用列序优先(等价F)读/写;
5. 若使用 array.reshape(newshape, order='C'), order 不可省略;
"""

# 定义数组 a
>>> a = np.arange(6).reshape((3, 2))
array([[0, 1],
       [2, 3],
       [4, 5]])

# order='C', 行优先读/写顺序,默认
>>> np.reshape(a, 6)
array([0, 1, 2, 3, 4, 5])
>>> np.reshape(a, (2, 3))
array([[0, 1, 2],
       [3, 4, 5]])

# order='F', 列优先读/写顺序
>>> np.reshape(a, 6, order='F')
array([0, 2, 4, 1, 3, 5])
>>> np.reshape(a, (2, 3), order='F')
array([[0, 4, 3],
       [2, 1, 5]])

# order='A', 基于存储视图读/写顺序
>>> np.reshape(a,6,order='A')
>>> np.reshape(a,6,'A')
array([0, 1, 2, 3, 4, 5])
>>> np.reshape(a,(2,3),order='A')	# 结果说明a在内存中是行优先存储的
array([[0, 1, 2],
       [3, 4, 5]])


>>> a.reshape(6,'A')				# array.reshape方式下, 'order='不可缺省
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object cannot be interpreted as an integer 
(字符串对象不能被解释为整数)

© 2021 Cherry_ChenNan
错漏之处不吝指正讨论.
点赞支持,收藏学习,关注不迷路~

猜你喜欢

转载自blog.csdn.net/qq_27677599/article/details/116597966
今日推荐