2017/07/31 ~ 2017/08/03
Python matplotlibとpandasで信頼区間つきのグラフを描画する
fill_betweenをつかうとよい.
これはpandas.DataFrameを使っているときにも利用できて便利.
pandasとmatplotlibの仲の良さはとてもありがたい.
以下にコード例を示す.
import math import random import pandas x = [i for i in range(100)] y = [math.sin(i) for i in range(100)] stdy = [random.random() for i in range(100)] data = zip(x, y, stdy) df = pandas.DataFrame(list(data), columns=['x', 'y', 'ystd']) plt = df.plot(x='x', y='y') plt.fill_between(df.x, df.y-df.ystd, df.y+df.ystd, alpha=0.3)
実行するとこんな感じになる.
Pythonのコンテキストマネージャは1つで複数のコンテキストを管理できる
<|python|
with open('input1.txt') as input1_fp, open('input2.txt') as input2_fp:
for line1, line2 in zip(input1_fp, input2_fp):
print(line1, line2)
|
8. Compound statements — Python 3.6.2 documentation
pickleで大きなデータをシリアライズ
普通だとpickle.dumpに4GB以上のオブジェクトを渡すと
OverflowError: cannot serialize a bytes object larger than 4 GiB
と言われてしまう.これはpickle.dumpの中でint32が使われているせいらしい.
調べて見たらprotocol4というのがあって,このプロトコルではサイズをint64で管理していそう.
C++の非決定的な乱数生成
標準で使えるrand()は決定的な乱数を生成する.
//#define _GRIBCXX_DEBUG #include <bits/stdc++.h> # define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; int main() { for (int i=0; i<10; i++) { cout << rand() << endl; } return 0; }
これをコンパイルして何回実行しても結果は以下のようになった.
16807 282475249 1622650073 984943658 1144108930 470211272 101027544 1457850878 1458777923 2007237709
一方,ハードウェアの状態などを用いて非決定的な乱数を生成する生成器がC++11にはある.
以下のように使える.
//#define _GRIBCXX_DEBUG #include <bits/stdc++.h> # define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; int main() { random_device rnd; for (int i=0; i<10; i++) { cout << rnd() << endl; } return 0; }