In [1]:
%pylab inline --no-import-all
import pandas as pd


Populating the interactive namespace from numpy and matplotlib

教科書P240の図10.11のようなグラフを生成する

まず、0から150まで、0.1刻みで1500個の数字を生成する
前半30個をx, 後半120個をyとする。


In [2]:
x = np.arange(0, 10, 0.1)
y = np.arange(10, 150, 0.1)
lenx = len(x)
leny = len(y)

xyはxとyを結合させたあとのもの


In [3]:
xy = np.zeros(len(x) + len(y))

前半部分はそのまま、後半部分yは配列の中身をシャッフルする。


In [4]:
for i in range(0, lenx):
    xy [i] = x[i]
np.random.shuffle(y)
for i in range(0, leny):
    xy[lenx + i] = y[i]

このままだと、綺麗すぎるので、少しずらす。


In [5]:
xy = xy + np.random.randn(len(xy))

In [6]:
plt.plot(xy, "wo")


Out[6]:
[<matplotlib.lines.Line2D at 0x108dd44a8>]

In [7]:
xy.mean()


Out[7]:
75.011400496980926

In [8]:
xy[:lenx].mean()


Out[8]:
5.094186326229087

In [9]:
xy[lenx:leny].mean()


Out[9]:
80.076426513470935

In [10]:
plt.plot(xy[:lenx], "wo")


Out[10]:
[<matplotlib.lines.Line2D at 0x10903aac8>]

In [11]:
plt.plot(xy[lenx:leny], "wo")


Out[11]:
[<matplotlib.lines.Line2D at 0x1090f1ef0>]