详解numpy.reshape中参数newshape出现-1的含义

Lamaara ·
更新时间:2024-11-10
· 991 次阅读

前言

reshape函数是Numpy中用来修改数组形状的函数,其函数原型为:

numpy.reshape(a, newshape, order='C')

在该函数中有一个用来指定数组新形状的参数newshape,该参数的取值可以为整数或整数构成的元组,在众多的取值情况中,存在一种特殊的情况:newshape取-1或(dim1,dim2,...,−1,...,dimn)(dim1,dim2,...,-1,...,dimn)(dim1,dim2,...,−1,...,dimn),那这里的-1有什么含义呢?请看下文。

详细解释

在Numpy官方文档中对参数newshape的解释中存在这样几句话:One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions.
其意思是:当新形状的某个维度可以取值为-1时,-1代表的维度的取值需要根据数组的长度和其他维度来共同确认,这里我描述为一个公式,首先定义数组arrayarrayarray的新形状(dim1,dim2,...,dimi,...,dimn)(dim1,dim2,...,dimi,...,dimn)(dim1,dim2,...,dimi,...,dimn),其中dimidimidimi取-1,则dimidimidimi的取值为:
dimi=len(array)dim1×dim⁡2×...×dimndimi=\frac{len(array)}{dim1\times\dim2\times...\times dimn}dimi=dim1×dim2×...×dimnlen(array)​
上述公式就代表了-1的具体含义,下面通过一些具体的例子来体会一下:

例一:下面代码中原数组为(3,4,5)(3,4,5)(3,4,5),而newshape等于一个整数-1,即表示将原数组的多个维度展成一个维度,即数组经过Reshape后的形状为(60,)(60,)(60,)

arr = np.random.rand(3,4,5) print(arr) arr_new = np.reshape(arr,-1) print(arr_new) ''' 原数组: [[[0.18908953 0.45632035 0.60829989 0.88535181 0.06004628] [0.77856873 0.97748809 0.87179748 0.61720263 0.54540931] [0.13997071 0.30347439 0.14380962 0.10624067 0.2854957 ] [0.77781811 0.20591323 0.75677237 0.61603541 0.02127296]] [[0.70360073 0.87541966 0.91603954 0.31743298 0.33283986] [0.93529192 0.78824197 0.26518159 0.71232784 0.69927396] [0.1252056 0.90056948 0.30974894 0.3263685 0.65985064] [0.42565193 0.26282195 0.92978112 0.18145506 0.1575511 ]] [[0.88638663 0.14568464 0.53084672 0.30027998 0.17035437] [0.3763821 0.6118317 0.79393619 0.16702035 0.69873627] [0.55124798 0.52077403 0.59928311 0.62740863 0.72015323] [0.65840375 0.18908399 0.09844688 0.64066476 0.59419928]]] Reshape后的数组: [0.18908953 0.45632035 0.60829989 0.88535181 0.06004628 0.77856873 0.97748809 0.87179748 0.61720263 0.54540931 0.13997071 0.30347439 0.14380962 0.10624067 0.2854957 0.77781811 0.20591323 0.75677237 0.61603541 0.02127296 0.70360073 0.87541966 0.91603954 0.31743298 0.33283986 0.93529192 0.78824197 0.26518159 0.71232784 0.69927396 0.1252056 0.90056948 0.30974894 0.3263685 0.65985064 0.42565193 0.26282195 0.92978112 0.18145506 0.1575511 0.88638663 0.14568464 0.53084672 0.30027998 0.17035437 0.3763821 0.6118317 0.79393619 0.16702035 0.69873627 0.55124798 0.52077403 0.59928311 0.62740863 0.72015323 0.65840375 0.18908399 0.09844688 0.64066476 0.59419928] '''

例二:下面代码中原数组为(3,4,5)(3,4,5)(3,4,5),而newshape为(−1,6)(-1,6)(−1,6),即表示数组的新形状是二维的,其中第二个维度值为6,根据公式计算可以得出第一个维度为10,即数组经过Reshape后的形状为(10,6)(10,6)(10,6)

arr = np.random.rand(3,4,5) print(arr) arr_new = np.reshape(arr,(-1,6)) print(arr_new) ''' 原数组为: [[[0.42174227 0.92375083 0.52812403 0.82412386 0.78091172] [0.07786206 0.56499814 0.26742312 0.64240348 0.80924781] [0.69369929 0.79091898 0.65191738 0.12793063 0.57171385] [0.27210565 0.53206115 0.81496854 0.87634764 0.1485443 ]] [[0.9283396 0.19602051 0.55526619 0.9475084 0.81077826] [0.62648994 0.96349421 0.61969999 0.47571235 0.87352483] [0.85180494 0.16980368 0.47837769 0.16756491 0.75983611] [0.26146992 0.84731684 0.39144647 0.476159 0.48653134]] [[0.21647798 0.54077104 0.11888931 0.91606392 0.6479488 ] [0.08034227 0.34108976 0.97762559 0.65757993 0.85826893] [0.77326409 0.62963526 0.85296218 0.02411685 0.39000965] [0.80359876 0.09920141 0.19210841 0.14694273 0.65519886]]] Reshape后的数组为: [[0.42174227 0.92375083 0.52812403 0.82412386 0.78091172 0.07786206] [0.56499814 0.26742312 0.64240348 0.80924781 0.69369929 0.79091898] [0.65191738 0.12793063 0.57171385 0.27210565 0.53206115 0.81496854] [0.87634764 0.1485443 0.9283396 0.19602051 0.55526619 0.9475084 ] [0.81077826 0.62648994 0.96349421 0.61969999 0.47571235 0.87352483] [0.85180494 0.16980368 0.47837769 0.16756491 0.75983611 0.26146992] [0.84731684 0.39144647 0.476159 0.48653134 0.21647798 0.54077104] [0.11888931 0.91606392 0.6479488 0.08034227 0.34108976 0.97762559] [0.65757993 0.85826893 0.77326409 0.62963526 0.85296218 0.02411685] [0.39000965 0.80359876 0.09920141 0.19210841 0.14694273 0.65519886]] '''

例三:下面代码中原数组为(3,4,5)(3,4,5)(3,4,5),而newshape为(6,−1,2)(6,-1,2)(6,−1,2),即表示数组的新形状是三维的,其中第一个维度值为6,第三个维度值为2,根据公式计算可以得出第二个维度为5,即数组经过Reshape后的形状为(6,5,2)(6,5,2)(6,5,2)

import numpy as np arr = np.random.rand(3,4,5) print(arr) arr_new = np.reshape(arr,(6,-1,2)) print(arr_new) ''' 原数组为: [[[0.12738545 0.15815041 0.66368663 0.04768804 0.12169861] [0.48287379 0.03449696 0.13272964 0.24843391 0.81665647] [0.59356826 0.28746627 0.24271899 0.97108517 0.1012294 ] [0.34509959 0.86057818 0.21819026 0.974423 0.56771299]] [[0.66465411 0.22011894 0.54320012 0.90525881 0.2210757 ] [0.04361296 0.59965157 0.39893431 0.86689352 0.5011301 ] [0.46391293 0.07084823 0.62207472 0.11096569 0.73346639] [0.51612205 0.29943895 0.49880607 0.83156271 0.58016689]] [[0.33143182 0.11186348 0.21977766 0.72495055 0.80526985] [0.38284111 0.3730357 0.66760575 0.75794368 0.28820835] [0.82925733 0.55213101 0.7392374 0.08494175 0.699597 ] [0.57090719 0.20802179 0.75575705 0.16512089 0.02169632]]] Reshape后的数组为: [[[0.12738545 0.15815041] [0.66368663 0.04768804] [0.12169861 0.48287379] [0.03449696 0.13272964] [0.24843391 0.81665647]] [[0.59356826 0.28746627] [0.24271899 0.97108517] [0.1012294 0.34509959] [0.86057818 0.21819026] [0.974423 0.56771299]] [[0.66465411 0.22011894] [0.54320012 0.90525881] [0.2210757 0.04361296] [0.59965157 0.39893431] [0.86689352 0.5011301 ]] [[0.46391293 0.07084823] [0.62207472 0.11096569] [0.73346639 0.51612205] [0.29943895 0.49880607] [0.83156271 0.58016689]] [[0.33143182 0.11186348] [0.21977766 0.72495055] [0.80526985 0.38284111] [0.3730357 0.66760575] [0.75794368 0.28820835]] [[0.82925733 0.55213101] [0.7392374 0.08494175] [0.699597 0.57090719] [0.20802179 0.75575705] [0.16512089 0.02169632]]] '''

例四:下面代码中原数组为(4,1,2)(4,1,2)(4,1,2),而newshape为(−1,2)(-1,2)(−1,2),即表示数组的新形状是二维的,其中第二个维度值为2根据公式计算可以得出第二个维度为4,即数组经过Reshape后的形状为(4,2)(4,2)(4,2)

arr = np.random.rand(4,1,2) print(arr) arr_new = np.reshape(arr,(-1,2)) print(arr_new) ''' 原数组为: [[[0.47017561 0.9737738 ]] [[0.46228013 0.1755705 ]] [[0.32037248 0.88863236]] [[0.07027458 0.92926978]]] Reshape后的数组为: [[0.47017561 0.9737738 ] [0.46228013 0.1755705 ] [0.32037248 0.88863236] [0.07027458 0.92926978]] '''
作者:斯曦巍峨



NumPy reshape

需要 登录 后方可回复, 如果你还没有账号请 注册新账号