Python numpy.reshape用法

numpy.reshape(a, newshape, order='C')

这个函数的作用就是把数据原来的尺寸更改为我们想要的尺寸。
参数:

  • a: array_like
    我们想要的变更尺寸的数组。
  • newshape: int or int of ints
    想变成什么样的尺寸,这时要注意这个尺寸产生的数值数目要等于原数组数值数组。如:原始数组尺寸为1 × 10,我们要分成2 × 5的数组,此时1 × 10 = 2 × 5。若中一个值设置为-1,python会自动帮我们计算这个-1应为多少。
  • order: {‘C’, ‘F’, ‘A’}, 可选
    重新分配的顺序,很少用。

返回:

  • reshaped_array : ndarray
    一个重新调整好尺寸的数组

例子

>>> a = np.arange(6).reshape((3, 2))
>>> a
array([[0, 1],
       [2, 3],
       [4, 5]])
>>> a = np.array([[1,2,3], [4,5,6]])
>>> np.reshape(a, 6)
array([1, 2, 3, 4, 5, 6])
>>> np.reshape(a, 6, order='F')
array([1, 4, 2, 5, 3, 6])

-1的用法:

>>> np.reshape(a, (3,-1))       # the unspecified value is inferred to be 2
array([[1, 2],
       [3, 4],
       [5, 6]])

这里顺便说一下如何看输出的矩阵或自己设置的矩阵是几维的:
数一下[的个数,几个就是几维。

猜你喜欢

转载自blog.csdn.net/u011851421/article/details/83545563