宮崎大学 >>
工学部 >>
情報システム工学科 >>
伊達 >>
プログラミング >>
Last modified: Thu May 29 18:42:21 JST 2014
分散とか標準偏差とか,意味がわからないと, どうもしようがない. 例題で気分をつかもう. 正規分布については こちらも参照(C言語のコードあり).
#!/usr/bin/env python import random n = 200; sigma = 0.7 seed = 20140529 random.seed( seed ) for i in range(n): print (random.gauss(0,sigma))
% ./ss101.py > random200.txt % % less random200.txt -0.711290338749 -0.581799858042 ... 0.330380407382 -0.470903252733実はこんなことしなくても numpy を使えば np.random.normal(平均,標準偏差,個数) で生成できる.
#!/usr/bin/env python # -*- coding: utf-8 -*- import random import numpy as np n = 200; mu = 10.0 # 平均 sigma = 0.7 # 標準偏差 seed = 20140529 # 乱数の種 random.seed( seed ) x = np.random.normal(mu, sigma, n) print (x)
random.randn # 標準正規分布 random.randint(1,100) # 1 から 100 までの整数の一様乱数. random.random() # 0.0 <= < 1.0 の実数の一様乱数. random.uniform(1, 100) # 1 から 100 までの実数の一様乱数(100は含まない).
#!/usr/bin/env python # -*- coding: utf-8 -*- import numpy as np import matplotlib.pyplot as plt x = np.random.randn(10000) print ('サンプル(標本)の平均 mean =', np.mean(x)) print ('サンプルの標準偏差 s.d. =', np.std(x)) print ('サンプルの分散 variance =', np.var(x)) plt.figure() plt.hist(x, bins=100, alpha=0.3, histtype='stepfilled', color='r') # plt.hist(x+2, bins=100, alpha=0.3, histtype='stepfilled', color='b') plt.show() # plt.savefig("fig001.eps")
#!/usr/bin/python import sys y_vec = [] def read_data(filename): f = open(filename, 'r') line = f.readline() while line: y_vec.append(float(line)) line = f.readline() return len(y_vec) n = read_data("random200.txt") for i in y_vec: print (i) print ("#data =", n)
y_vec.sort()
#!/usr/bin/env python import numpy from matplotlib import pyplot def show(filename): data = numpy.loadtxt(filename) pyplot.hist(data) pyplot.show() show("random200.txt")
宮崎大学 >> 工学部 >> 情報システム工学科 >> 伊達 >>