LoginSignup
0
1

More than 5 years have passed since last update.

PythonでU分布を生成する

Last updated at Posted at 2017-02-08

書籍:「データの見えざる手 ウエアラブルセンサが明かす人間・組織・社会の法則」に記載のあったU分布を生成してみたいと考え、Pythonでプログラミングを行ってみました。
2017.2.12コードの誤りを修正しました。

書籍についての詳細は以下リンクの私のブログにも記載しています。
[リンク]http://kansetsulight.seesaa.net

コード

import numpy as np
import matplotlib.pyplot as plt

nindex = []

n = 0
nxxx = 0
nyyy = 0
x = np.random.rand(1000)*100
y = np.random.rand(1000)*100

while n < 1000:
if x[n] < 10:
nxxx = 1
elif x[n] < 20:
nxxx = 2
elif x[n] < 30:
nxxx = 3
elif x[n] < 40:
nxxx = 4
elif x[n] < 50:
nxxx = 5
elif x[n] < 60:
nxxx = 6
elif x[n] < 70:
nxxx = 7
elif x[n] < 80:
nxxx = 8
elif x[n] < 90:
nxxx = 9
elif x[n] <= 100:
nxxx = 10

if y[n] < 10:
nyyy = 1
elif y[n] < 20:
nyyy = 2
elif y[n] < 30:
nyyy = 3
elif y[n] < 40:
nyyy = 4
elif y[n] < 50:
nyyy = 5
elif y[n] < 60:
nyyy = 6
elif y[n] < 70:
nyyy = 7
elif y[n] < 80:
nyyy = 8
elif y[n] < 90:
nyyy = 9
elif y[n] <= 100:
nyyy = 10

nindex.append([nxxx, nyyy])
n = n + 1
nxxx = 0
nyyy = 0
ite = 0
chk = 0
while ite < 1e4:

chk1 = 0
while chk1 == 0:
x_strt = np.random.randint(1,11)
y_strt = np.random.randint(1,11)

chk1 = nindex.count([x_strt,y_strt])

strt = nindex.index([x_strt,y_strt])
print 'index of start: ',strt
print 'index of start: ',nindex[strt]
print 'x_strt, y_strt: ',x_strt, y_strt

chk2 = 0
while chk2 == 0:
x_dest = np.random.randint(1,11)
y_dest = np.random.randint(1,11)
chk2 = nindex.count([x_dest,y_dest])
print chk2

dest = nindex.index([x_dest,y_dest])

x[strt] = float(x_dest*10)-np.random.uniform(0,10)
y[strt] = float(y_dest*10)-np.random.uniform(0,10)
print 'dest x,y index: ', x_dest,' ', y_dest
print 'dest crd: ',x[strt],y[strt]
print

Update for nindex

nindex[strt] = nindex[dest]
ite = ite + 1

fig = plt.figure()

ax = fig.add_subplot(1,1,1)

ax.scatter(x,y,s=1)
ax.set_title('random plot')
ax.set_xlabel('x')
ax.set_ylabel('y')

ax.grid(True)

plt.savefig('testfig')
plt.show()

コードはGitHubにもUpしています。
[リンク]https://github.com/yama89/U-distribution1.0

結果

< 1step後 >
up1.png

まだまだ均等ですね。

<10step後>
up10.png

あまり変わりません

<100step後>
up100.png

これまたあまり(全く?)変わりません。

<1000step後>
up1000.png

いよいよ偏りが出てきた気がする。

<2000step後>
up2000.png

これはきました。
偏りが明らかに現れています。

<10000step後>
up10000.png

これぞU分布といったものが出ました。

これは何かに活用したいなあ。
と思います。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1