
上QQ阅读APP看书,第一时间看更新
3-3-9 使用reshape( )函数更改数组形式
reshape( )函数可以更改数组形式,语法如下:
np.reshape(a, newshape)
参数a是要更改的数组;参数newshape是新数组的外形,可以是整数或元组。
程序实例ch3_7_2.py:将1×16数组改为2×8数组。

执行结果

有时候reshape( )函数的newshape元组的其中一个元素是-1,这表示将依照另一个元素安排元素内容。
程序实例ch3_7_3.py:重新设计ch3_7_2.py,但是newshape元组的其中一个元素是-1,整个newshape内容是(4, -1)。

执行结果

程序实例ch3_7_4.py:重新设计ch3_7_2.py,但是newshape元组的其中一个元素是-1,整个newshape内容是(-1, 8)。

执行结果
