python——shape 与reshape

转载自:https://blog.csdn.net/u010916338/article/details/84066369
shape()和reshape()都是数组array中的方法

numpy中reshape函数的三种常见相关用法

  • numpy.arange(n).reshape(a, b)    依次生成n个自然数,并且以a行b列的数组形式显示
 
np.arange(16).reshape(2,8) #生成16个自然数,以2行8列的形式显示

# Out:

# array([[ 0, 1, 2, 3, 4, 5, 6, 7],

# [ 8, 9, 10, 11, 12, 13, 14, 15]])
  • mat (or array).reshape(c, -1)     必须是矩阵格式或者数组格式,才能使用 .reshape(c, -1) 函数, 表示将此矩阵或者数组重组,以 c行d列的形式表示
reshape(m,-1) #改变维度为m行、1列

reshape(-1,m) #改变维度为1行、m列

-1的作用就在此: 自动计算d:d=数组或者矩阵里面所有的元素个数/c, d必须是整数,不然报错)

(reshape(-1, m)即列数固定,行数需要计算)

arr=np.arange(16).reshape(2,8)

arr

'''

out:

array([[ 0, 1, 2, 3, 4, 5, 6, 7],

[ 8, 9, 10, 11, 12, 13, 14, 15]])

'''

arr.reshape(4,-1) #将arr变成4行的格式,列数自动计算的(c=4, d=16/4=4)

'''

out:

array([[ 0, 1, 2, 3],

[ 4, 5, 6, 7],

[ 8, 9, 10, 11],

[12, 13, 14, 15]])

'''

arr.reshape(8,-1) #将arr变成8行的格式,列数自动计算的(c=8, d=16/8=2)

'''

out:

array([[ 0, 1],

[ 2, 3],

[ 4, 5],

[ 6, 7],

[ 8, 9],

[10, 11],

[12, 13],

[14, 15]])

'''

arr.reshape(10,-1) #将arr变成10行的格式,列数自动计算的(c=10, d=16/10=1.6 != Int)

'''

out:

ValueError: cannot reshape array of size 16 into shape (10,newaxis)

'''
  • numpy.arange(a,b,c)    从 数字a起, 步长为c, 到b结束,生成array
  • numpy.arange(a,b,c).reshape(m,n)  :将array的维度变为m 行 n列。
 
np.arange(1,12,2)#间隔2生成数组,范围在1到12之间

# Out: array([ 1, 3, 5, 7, 9, 11])

np.arange(1,12,2).reshape(3,2)

'''

Out:

array([[ 1, 3],

[ 5, 7],

[ 9, 11]])

'''

reshape(1,-1)转化成1行:

reshape(2,-1)转换成两行:


reshape(-1,1)转换成1列:

 

reshape(-1,2)转化成两列

 

    shape()

 import numpy as np
        a = np.array([1,2,3,4,5,6,7,8])  #一维数组
        print(a.shape[0])  #值为8,因为有8个数据
        print(a.shape[1])  #IndexError: tuple index out of range
         
        a = np.array([[1,2,3,4],[5,6,7,8]])  #二维数组
        print(a.shape[0])  #值为2,最外层矩阵有2个元素,2个元素还是矩阵。
        print(a.shape[1])  #值为4,内层矩阵有4个元素。
        print(a.shape[2])  #IndexError: tuple index out of range

     
    reshape()


 
    reshape新生成数组和原数组公用一个内存,不管改变哪个都会互相影响。

 
 

shapereshape都是用来改变张量的形状的方法。 shape是一个属性,用来返回一个张量的维度信息,即它有多少行和多少列。它可以帮助我们了解一个张量的形状信息。 reshape是一个函数,用来改变一个张量的形状,即重新组织张量中的数据。它接受一个新形状作为参数,可以是一个整数或整数构成的元组。通过reshape,我们可以将一个张量从一种形状转换为另一种形状。 view和reshape都可以用来改变张量的形状,但是它们有一些区别。view只适用于满足连续性条件(contiguous)的张量,而reshape可以用于满足和不满足连续性条件的张量,具有更好的鲁棒性。如果一个张量无法通过view改变形状,可以尝试使用reshape来处理。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [深度学习(PyTorch)——shape、view、reshape用法及其区别](https://blog.csdn.net/qq_42233059/article/details/126656133)[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: 33.333333333333336%"] - *2* [Python | numpy库 | shape函数与reshape函数](https://blog.csdn.net/qq_52555480/article/details/127757791)[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: 33.333333333333336%"] - *3* [详解numpy.reshape中参数newshape出现-1的含义](https://download.csdn.net/download/weixin_38526650/13753950)[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: 33.333333333333336%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有情怀的机械男

你的鼓励将是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值