2018年5月23日水曜日

開発環境

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

コード(Emacs)

Python 3

#!/usr/bin/env python3


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

    return nums


def create_classes(nums, n):
    low = min(nums)
    high = max(nums)
    width = (high - low) / n
    classes = []
    a = low
    b = low + width
    classes = []
    while a < (high - width):
        classes.append((a, b))
        a = b
        b = a + width
    classes.append((a, high + 1))
    return classes


def frequency(nums, classes):
    result = [0] * len(classes)
    for n in nums:
        for i, (a, b) in enumerate(classes):
            if a <= n < b:
                result[i] += 1
                break
    return result


def p(classes, freq):
    for (a, b), t in zip(classes, freq):
        print(f'{a}-{b}: {t}')


if __name__ == '__main__':
    nums = [7, 8, 9, 2, 10, 9, 9, 9, 9, 4, 5, 6, 1, 5, 6, 7, 8, 6, 1, 10]
    classes = create_classes(nums, 2)
    freq = frequency(nums, classes)
    p(classes, freq)
    print()

    filename = 'marks.txt'
    nums = read_data(filename)
    for n in range(1, 11):
        classes = create_classes(nums, n)
        freq = frequency(nums, classes)
        p(classes, freq)
        print()

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

$ ./sample5.py
1-5.5: 6
5.5-11: 14

10.5-21.0: 25

10.5-15.25: 8
15.25-21.0: 17

10.5-13.666666666666666: 6
13.666666666666666-16.833333333333332: 8
16.833333333333332-21.0: 11

10.5-12.875: 4
12.875-15.25: 4
15.25-17.625: 7
17.625-21.0: 10

10.5-12.4: 3
12.4-14.3: 3
14.3-16.2: 5
16.2-18.099999999999998: 9
18.099999999999998-19.999999999999996: 3
19.999999999999996-21.0: 2

10.5-12.083333333333334: 3
12.083333333333334-13.666666666666668: 3
13.666666666666668-15.250000000000002: 2
15.250000000000002-16.833333333333336: 6
16.833333333333336-18.416666666666668: 6
18.416666666666668-21.0: 5

10.5-11.857142857142858: 3
11.857142857142858-13.214285714285715: 1
13.214285714285715-14.571428571428573: 3
14.571428571428573-15.92857142857143: 2
15.92857142857143-17.28571428571429: 6
17.28571428571429-18.642857142857146: 6
18.642857142857146-21.0: 4

10.5-11.6875: 3
11.6875-12.875: 1
12.875-14.0625: 2
14.0625-15.25: 2
15.25-16.4375: 3
16.4375-17.625: 4
17.625-18.8125: 6
18.8125-21.0: 4

10.5-11.555555555555555: 3
11.555555555555555-12.61111111111111: 1
12.61111111111111-13.666666666666666: 2
13.666666666666666-14.722222222222221: 1
14.722222222222221-15.777777777777777: 2
15.777777777777777-16.833333333333332: 5
16.833333333333332-17.88888888888889: 1
17.88888888888889-18.944444444444446: 6
18.944444444444446-21.0: 4

10.5-11.45: 3
11.45-12.399999999999999: 0
12.399999999999999-13.349999999999998: 1
13.349999999999998-14.299999999999997: 2
14.299999999999997-15.249999999999996: 2
15.249999999999996-16.199999999999996: 3
16.199999999999996-17.149999999999995: 4
17.149999999999995-18.099999999999994: 5
18.099999999999994-19.049999999999994: 1
19.049999999999994-19.999999999999993: 2
19.999999999999993-21.0: 2

$

0 コメント:

コメントを投稿

関連コンテンツ