Numpy size() 関数 |パイソン

Python では、numpy.size() 関数は、指定された軸に沿った要素の数をカウントします。

構文: numpy.size(arr, axis=なし)
パラメーター:
到着: [array_like] 入力データ。
軸: [int、オプション] 要素 (行または列) がカウントされる軸 (x,y,z)。デフォルトでは、配列内の要素の合計数を指定します。
戻り値: [int] 指定された軸に沿った要素の数を返します。

コード #1 :

Python3




# Python program explaining> # numpy.size() method> # importing numpy> import> numpy as np> # Making a random array> arr> => np.array([[> 1> ,> 2> ,> 3> ,> 4> ], [> 5> ,> 6> ,> 7> ,> 8> ]])> # By default, give the total number of elements.> print> (np.size(arr))>

出力:

8 

コード #2 :

Python3




# Python program explaining> # numpy.size() method> # importing numpy> import> numpy as np> # Making a random array> arr> => np.array([[> 1> ,> 2> ,> 3> ,> 4> ], [> 5> ,> 6> ,> 7> ,> 8> ]])> # count the number of elements along the axis.> # Here rows and columns are being treated> # as elements> #gives no. of rows along x-axis> print> (np.size(arr,> 0> ))> #gives no. of columns along y-axis> print> (np.size(arr,> 1> ))>

出力:

2 4