python中reshape函数_Python numpy.reshape函数方法的使用

numpy.reshape

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

在不更改数据的情况下为数组赋予新的shape。参数 :a :array_like

要重塑的数组。

newshape :int 或 int类型的tuple

新形状应与原始形状兼容。 如果是整数,则结果将是该长度的一维数组。

一个形状尺寸可以为-1。 在这种情况下,该值是根据数组的长度和其余维来推断的。

或der :{‘C’, ‘F’, ‘A’}, 可选

使用此索引顺序读取a的元素,然后使用此索引顺序将元素放入重新排列的数组中。

“C”表示使用类似C的索引顺序读取/写入元素,最后一个轴索引更改最快,

回到第一个轴索引更改最慢。 “ F”表示使用类似于Fortran的索引顺序读取/写入元素,

第一个索引更改最快,最后一个索引更改最慢。

请注意,“C”和“F”选项不考虑基础数组的内存布局,

仅参考索引的顺序。 “a”表示如果a在内存中是连续的,

则以类似于Fortran的索引顺序读取/写入元素,否则为类似于C的顺序。

返回值 :reshaped_array :ndarray

如果可能的话,这将是一个新的视图对象; 否则,它将是副本。

注意,不能保证返回数组的内存布局(C或Fortran连续)。

Notes

在不复制数据的情况下,并非总是可以更改数组的形状。 如果希望在复制数据时引发错误,则应将新形状分配给数组的shape属性:>>> a = np.zeros((10, 2))

# A transpose makes the array non-contiguous

>>> b = a.T

# Taking a view makes it possible to modify the shape without modifying

# the initial object.

>>> c = b.view()

>>> c.shape = (20)

Traceback (most recent call last):

...

AttributeError: Incompatible shape for in-place modification. Use

`.reshape()` to make a copy with the desired shape.

order关键字提供索引排序,既可用于从a中获取值,又可将值放入输出数组中。 例如,假设您有一个数组:>>> a = np.arange(6).reshape((3, 2))

>>> a

array([[0, 1],

[2, 3],

[4, 5]])

您可以将重塑看作是首先对数组进行碎片处理(使用给定的索引顺序),然后使用与碎片处理相同的索引顺序将碎片数组中的元素插入到新数组中。>>> np.reshape(a, (2, 3)) # C-like index ordering

array([[0, 1, 2],

[3, 4, 5]])

>>> np.reshape(np.ravel(a), (2, 3)) # equivalent to C ravel then C reshape

array([[0, 1, 2],

[3, 4, 5]])

>>> np.reshape(a, (2, 3), order='F') # Fortran-like index ordering

array([[0, 4, 3],

[2, 1, 5]])

>>> np.reshape(np.ravel(a, order='F'), (2, 3), order='F')

array([[0, 4, 3],

[2, 1, 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])>>> np.reshape(a, (3,-1)) # the unspecified value is inferred to be 2

array([[1, 2],

[3, 4],

[5, 6]])

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Pythonreshape函数可以用于改变数组的形状。引用提到,reshape函数的功能是改变数组或矩阵的形状。具体地说,reshape函数可以将一个二维数组转换成一个三维数组。下面是一个示例代码: ```python import numpy as np # 创建一个二维数组,形状为(4, 6) a = np.array([[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24]]) # 将二维数组a转换成一个三维数组,形状为(2, 3, 4) b = np.reshape(a, (2, 3, 4)) print(b) ``` 运行以上代码,可以看到输出的数组b形状为(2, 3, 4),表示它是一个由2个3行4列的二维数组组成的三维数组。这就是使用reshape函数二维数组转换成三维数组的方法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Pythonreshape()函数](https://blog.csdn.net/weixin_46506757/article/details/109545799)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [pythonreshape函数用法详解](https://blog.csdn.net/change_xzt/article/details/129999062)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值