2018年5月18日金曜日

開発環境

Pythonからはじめる数学入門 (Amit Saha (著)、黒川 利明 (翻訳)、オライリージャパン)の2章(データを統計量で記述する)、3.9(プログラミングチャレンジ)、問題3-2(統計電卓)を取り組んでみる。

コード(Emacs)

Python 3

#!/usr/bin/env python3

from collections import Counter


def read_nums(filename):
    with open(filename) as f:
        nums = [float(line) for line in f]

    return nums


def mean(nums):
    s = sum(nums)
    n = len(nums)
    return s / n


def median(nums):
    n = len(nums)
    nums.sort()

    if n % 2 == 0:
        m1 = n / 2
        m2 = n / 2 + 1
        m1 = int(m1) - 1
        m2 = int(m2) - 1
        return (nums[m1] + nums[m2]) / 2
    m = (n + 1) / 2
    m = int(m) - 1
    return nums[m]


def mode(nums):
    c = Counter(nums)
    nums_freq = c.most_common()
    max_count = nums_freq[0][1]
    modes = [num[0] for num in nums_freq if num[1] == max_count]
    return modes


def differences(nums):
    m = mean(nums)
    diff = [n - m for n in nums]
    return diff


def variance(nums):
    diff = differences(nums)
    squared_diff = [d ** 2 for d in diff]
    sum_sqaured_diff = sum(squared_diff)
    return sum_sqaured_diff / len(nums)


def standard_deviation(nums):
    return variance(nums) ** (1 / 2)


if __name__ == '__main__':
    filename = 'mydata.txt'
    nums = read_nums(filename)
    funcs = [mean, median, mode, variance, standard_deviation]
    for func in funcs:
        print(f'{func.__name__.replace("_", " ")}: {func(nums)}')

入出力結果(Terminal, Jupyter(IPython))

$ ./sample2.py
mean: 477.75
median: 500.0
mode: [100.0, 500.0]
variance: 141047.35416666666
standard deviation: 375.5627166887931
$

0 コメント:

コメントを投稿

関連コンテンツ