0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Python】NumPyのmean関数やsum関数などの統計関数は配列全体もしくは指定の方向に対して集計する

Posted at

arr = np.array([[1, 1, 1],
[3, 3, 3]])

NumPyのmean関数やsum関数などの統計関数は、配列全体、もしくは指定の方向に対して集計します。

arr.mean()
▶︎ 2.0

mean関数で引数を指定しなかった場合は、配列のすべての要素を合計して算術平均を出力します。「(1 + 1 + 1 + 3 + 3 + 3) / 6 = 2」です。

arr.mean(axis=0)
▶︎ array([2., 2., 2.])

引数で「axis=0」を指定した場合は、行方向(縦方向)で合計して、列毎の平均を算出します。

arr.mean(axis=1)
▶︎ array([1., 3.])

引数で「axis=1」を指定した場合は、列方向(横方向)で合計して、行毎の平均を算出します。

arr - arr.mean(axis=0)
▶︎ array([[-1., -1., -1.],
[ 1., 1., 1.]])

arrの配列から、arr.mean(axis=0)の配列を引いています。ブロードキャストにより、arr.mean(axis=0)の配列が以下に拡張され、arrの配列の各要素からマイナスされます。

[2., 2., 2.]
[2., 2., 2.]

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?