重塑数组:我们可以使用reshape方法重塑数组。考虑一个具有形状的数组(a1,a2,a3,...,aN)。我们可以重塑并将其转换为另一个具有形状的数组(b1,b2,b3,...,bM)。
唯一需要的条件是:a1 x a2 x a3 ... x aN = b1 x b2 x b3 ... x bM。(即阵列的原始大小保持不变。)
numpy.reshape(array,shape,order ='C'):在不更改数组数据的情况下对数组进行整形。
# Python Program illustrating# numpy.reshape() method
import numpy as geek
array = geek.arange(8)print("Original array : \n", array)
# shape array with 2 rows and 4 columnsarray = geek.arange(8).reshape(2, 4)print("\narray reshaped with 2 rows and 4 columns : \n", array)
# shape array with 2 rows and 4 columnsarray = geek.arange(8).reshape(4 ,2)print("\narray reshaped with 2 rows and 4 columns : \n", array)
# Constructs 3D arrayarray = geek.arange(8).reshape(2, 2, 2)print("\nOriginal array reshaped to 3D : \n", array)
0.0000
0
3
关注作者
收藏
发表评论
暂无数据

