python shape 和reshape 的使用方法

shape 其实就是维度,对于矩阵而言,二维矩阵就有两个维度,三维矩阵就有3个维度,n维矩阵就有n个维度。reshape就是重新改变数组的维度。

我们直接看代码,就什么都明白了。

>>> a=np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16])
>>> a
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16])
>>> a.shape
(16,)                #此类型情况表明 数组是只有一维,而且元素为16个
>>> print(a.shape[0])
16
>>> print(a.shape[1])
Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    print(a.shape[1])
IndexError: tuple index out of range
 >>> a.reshape(-1,1)
array([[ 1],
       [ 2],
       [ 3],
       [ 4],
       [ 5],
       [ 6],
       [ 7],
       [ 8],
       [ 9],
       [10],
       [11],
       [12],
       [13],
       [14],
       [15],
       [16]])
>>> a.reshape(2,8)
array([[ 1,  2,  3,  4,  5,  6,  7,  8],
       [ 9, 10, 11, 12, 13, 14, 15, 16]])
>>> a.reshape(4,4)
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12],
       [13, 14, 15, 16]])
>>> b=a.reshape(2,8)
>>> b
array([[ 1,  2,  3,  4,  5,  6,  7,  8],
       [ 9, 10, 11, 12, 13, 14, 15, 16]])
>>> print(b.shape[0])   #因此shape第一个参数是行
2
>>> print(b.shape[1])   #第二个参数是列
8
>>> print(b.shape[2])   
Traceback (most recent call last):
  File "<pyshell#22>", line 1, in <module>
    print(b.shape[2])
IndexError: tuple index out of range
b
array([[ 1,  2,  3,  4,  5,  6,  7,  8],
       [ 9, 10, 11, 12, 13, 14, 15, 16]])
>>> b.reshape(-1,1)
array([[ 1],
       [ 2],
       [ 3],
       [ 4],
       [ 5],
       [ 6],
       [ 7],
       [ 8],
       [ 9],
       [10],
       [11],
       [12],
       [13],
       [14],
       [15],
       [16]])
>>> b.reshape(-1,2)
array([[ 1,  2],
       [ 3,  4],
       [ 5,  6],
       [ 7,  8],
       [ 9, 10],
       [11, 12],
       [13, 14],
       [15, 16]])

总结

1:shape 和 reshape是改变数组维度的一种方式。通过输出数组的shape[0] 或者shape[1] 可以查询数组的行数 或者列数。

2: reshape有三种使用方法,

       方法1: c.reshape(a,b)   本方法要求c=a*b

        方法2: c.reshape(a,b,d)   生成的矩阵是  本方法要求 a个子矩阵,每个子矩阵包括b行d列  要求:c=a*b*d

         方法3:c.reshape(-1,a,b)  生成的矩阵为   子矩阵的a行b列

接下来我们进行验证:

>>> b= np.arange(12,24).reshape(2,2,3)
>>> b
array([[[12, 13, 14],
        [15, 16, 17]],

       [[18, 19, 20],
        [21, 22, 23]]])
>>> b.shape[0]
2
>>> b.shape[1]
2
>>> b.shape[2]
3

 

import numpy as np
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])
print(a.shape)
d=a.reshape(2,3,4) ### 代表包括两个矩阵,3行四列
print(d)
e=a.reshape(-1,2,4)    ##
print("e:",e)
print("e.shape:",e.shape)
f=a.reshape(-1,2,6)
print("f:",f)
print("f.shape:",f.shape)

g=a.reshape(4,6)
print("g:",g)

aa=a.reshape(4,6) ### 注意不是子矩阵
print("aa:",aa)
bb=a.reshape(-1,4,6)  ##如此则包含子矩阵
print("bb:",bb)
if aa.all==bb.all:   ### 因此二者不相等
    print("aaa")

运行结果:

(24,)
[[[ 1  2  3  4]
  [ 5  6  7  8]
  [ 9 10 11 12]]

 [[13 14 15 16]
  [17 18 19 20]
  [21 22 23 24]]]
e: [[[ 1  2  3  4]
  [ 5  6  7  8]]

 [[ 9 10 11 12]
  [13 14 15 16]]

 [[17 18 19 20]
  [21 22 23 24]]]
e.shape: (3, 2, 4)
f: [[[ 1  2  3  4  5  6]
  [ 7  8  9 10 11 12]]

 [[13 14 15 16 17 18]
  [19 20 21 22 23 24]]]
f.shape: (2, 2, 6)
g: [[ 1  2  3  4  5  6]
 [ 7  8  9 10 11 12]
 [13 14 15 16 17 18]
 [19 20 21 22 23 24]]
aa: [[ 1  2  3  4  5  6]
 [ 7  8  9 10 11 12]
 [13 14 15 16 17 18]
 [19 20 21 22 23 24]]
bb: [[[ 1  2  3  4  5  6]
  [ 7  8  9 10 11 12]
  [13 14 15 16 17 18]
  [19 20 21 22 23 24]]]

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值