python reshape 和 shape用法

1. reshape用法

语法:numpy.reshape(a,b,order='c/F') 将数组numpy重新排列生成a行b列的数组,a,b可以选择值为-1,系统会自动计算

# reshape快速创建数组 
a = np.arange(16).reshape(4,4)
#[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]
b = np.array([0,1,2,3,4,5,6,7,8,9])

# 默认数值按行排列
print(b.reshape(2,5)) 
#[[0 1 2 3 4]
 [5 6 7 8 9]]

# 值按列排列
print(b.reshape(2,5,order='F'))
#[[0 2 4 6 8]
 [1 3 5 7 9]]

# -1系统自动计算
print(b.reshape(-1,2))
#[[0 1]
 [2 3]
 [4 5]
 [6 7]
 [8 9]]

2.shape用法 描述数组形式

a = np.array([1,2,3,4])
print(a.shape)
# (4,)

b= np.array([[1,2,3,4],[1,2,3,4]])
print(b.shape)
# (2,4) 2行4列

c = np.array([
    [[1,2],
     [2,2]],
    [[1,2],
     [3,3]]])
print(c.shape)

#(2,2,2) 2层2行2列
posted @ 2022-08-25 14:31  凡凡的宝贝  阅读(225)  评论(0)    收藏  举报