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?

累乗平均の実装方法

Posted at

累乗平均の実装

累積平均(またはインクリメンタル平均)を実装する。この方法では、データを一つずつ受け取るたびに平均を更新するため、全てのデータをメモリに保持する必要がなく、オーバーフローのリスクを軽減できる。

incremental_mean.py

def incremental_mean(data):
    mean = 0
    count = 0
    for value in data:
        count += 1
        mean += (value - mean) / count
    return mean

# 使用例
data = [1, 2, 3, 4, 5]
print(incremental_mean(data))  # 出力: 3.0

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?