Numpyにおけるreshape関数、reshape(1,-1)の意味(わかりやすく、ソースコードの例)
1543 ワード
この論文ではnumpyにおけるreshape関数の3つの一般的な相関用法を詳細に説明する.
一般的な使い方:numpy.arange(n).reshape(a, b); n個の自然数を順次生成し、a行b列の配列形式で表示する.
特殊用法:mat(or array).reshape(c, -1);マトリクスフォーマットまたは配列フォーマットでなければ使用できません.reshape(c,-1)関数は、この行列または配列を再編成することを表し、c行d列の形式で表す(-1の役割はここで、d:d=配列または行列の中のすべての要素の個数/cを自動的に計算し、dは整数でなければならない.そうでなければ、エラーを報告しない)(reshape(-1,e)は列数が固定され、行数が計算される):
その他の使い方:numpy.arange(a,b,c)/ numpy.arange(a,b,c).reshape(m,n);数字aからステップ長cからbまでarrayを生成します.
一般的な使い方:numpy.arange(n).reshape(a, b); n個の自然数を順次生成し、a行b列の配列形式で表示する.
In [1]:
np.arange(16).reshape(2,8) # 16 , 2 8
Out[1]:
array([[ 0, 1, 2, 3, 4, 5, 6, 7],
[ 8, 9, 10, 11, 12, 13, 14, 15]])
特殊用法:mat(or array).reshape(c, -1);マトリクスフォーマットまたは配列フォーマットでなければ使用できません.reshape(c,-1)関数は、この行列または配列を再編成することを表し、c行d列の形式で表す(-1の役割はここで、d:d=配列または行列の中のすべての要素の個数/cを自動的に計算し、dは整数でなければならない.そうでなければ、エラーを報告しない)(reshape(-1,e)は列数が固定され、行数が計算される):
In [2]: arr=np.arange(16).reshape(2,8)
out[2]:
In [3]: arr
out[3]:
array([[ 0, 1, 2, 3, 4, 5, 6, 7],
[ 8, 9, 10, 11, 12, 13, 14, 15]])
In [4]: arr.reshape(4,-1) # arr 4 , (c=4, d=16/4=4)
out[4]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
In [5]: arr.reshape(8,-1) # arr 8 , (c=8, d=16/8=2)
out[5]:
array([[ 0, 1],
[ 2, 3],
[ 4, 5],
[ 6, 7],
[ 8, 9],
[10, 11],
[12, 13],
[14, 15]])
In [6]: arr.reshape(10,-1) # arr 10 , (c=10, d=16/10=1.6 != Int)
out[6]:
ValueError: cannot reshape array of size 16 into shape (10,newaxis)
その他の使い方:numpy.arange(a,b,c)/ numpy.arange(a,b,c).reshape(m,n);数字aからステップ長cからbまでarrayを生成します.
In [7]: np.arange(1,12,2)# 2 , 1 12
Out[7]: array([ 1, 3, 5, 7, 9, 11])
In [8]: np.arange(1,12,2).reshape(3,2)
Out[39]:
array([[ 1, 3],
[ 5, 7],
[ 9, 11]])