LoginSignup
0
1

More than 5 years have passed since last update.

Jupyter / matplotlib / numpy > 2次元の乱数を取得し、2D画像にする実装 > v0.2, v0.3

Last updated at Posted at 2017-03-18
動作環境
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 14.04 LTS desktop amd64
TensorFlow v0.11
cuDNN v5.1 for Linux
CUDA v8.0
Python 2.7.6
IPython 5.1.0 -- An enhanced Interactive Python.
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
GNU bash, version 4.3.8(1)-release (x86_64-pc-linux-gnu)

タイトルのことをしたい。

v0.2 > np.random.randint()使用

Jupyter code.

map_2d_randomNums.ipynb
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

'''
on Python 2.7.6

v0.2 Mar. 18, 2017
  - show in 2D map
v0.1 Mar. 18, 2017
  - add get_2d_random_nums()
'''

def get_2d_random_nums(xsize, ysize):
    MAXVAL_PLUS_ONE = 65536
    ints = np.random.randint(MAXVAL_PLUS_ONE, size=(xsize,ysize))
    # print(ints)

    flts = ints / float(MAXVAL_PLUS_ONE)
    # print(flts)
    return flts

NUM_SAMPLES = 200

# 1. get random numbers in 2D
arr_2d = get_2d_random_nums(NUM_SAMPLES,2) # [0,1)

# 2. show in 2D map 
SIZE_MAP_X = 50
SIZE_MAP_Y = 50
rmap = [[0.0 for yi in range(SIZE_MAP_Y)] for xi in range(SIZE_MAP_X)]
for xval,yval in arr_2d:
    xidx = (SIZE_MAP_X * xval).astype(int)
    yidx = (SIZE_MAP_Y * yval).astype(int)
    rmap[xidx][yidx] = 100.0 # not adding

wrkarr = np.array(rmap)
figmap = np.reshape(wrkarr, (SIZE_MAP_X, SIZE_MAP_Y))
plt.imshow(figmap, extent=(0, SIZE_MAP_X, 0, SIZE_MAP_Y), cmap=cm.jet)
plt.show()

38行目において、2D化する時に100という数字を代入しているが、これは任意の値。
jetというカラーで分布が分かる値として使っているだけ。

色は以下からjetを適当に選択した。とりあえず分布が分かる色にはなった。
http://matplotlib.org/examples/color/colormaps_reference.html

N=200
qiita.png

N=10000
qiita.png

分布が偏っている。
Halton Sequenceを使うか。http://qiita.com/7of9/items/7a8920531fd715d0b75a

v0.3 > Halton Sequence 使用

http://qiita.com/7of9/items/7a8920531fd715d0b75a
のv0.3の実装を使用して、Halton Sequenceを使用するようにしてみた。

map_2d_randomNums.ipynb
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from UtilHaltonSequence import CHaltonSequence

'''
on Python 2.7.6

v0.4 Mar. 20, 2017
  - add get_2d_QMC()
  - use [Halton Sequence] for (xval, yval)
v0.3 Mar. 18, 2017
  - increase [SIZE_MAP_X], [SIZE_MAP_Y]
  - increase [NUM_SAMPLES]
v0.2 Mar. 18, 2017
  - show in 2D map
v0.1 Mar. 18, 2017
  - add get_2d_random_nums()
'''
# codingrule:PEP8


def get_2d_random_nums(xsize, ysize):
    MAXVAL_PLUS_ONE = 65536
    ints = np.random.randint(MAXVAL_PLUS_ONE, size=(xsize, ysize))
    # print(ints)

    flts = ints / float(MAXVAL_PLUS_ONE)
    # print(flts)
    return flts


def get_2d_QMC(xsize):
    alist = []
    for idx in range(0, xsize):
        xwrk, ywrk = CHaltonSequence.calc_Halton_sequence(index=idx)
        alist.append([xwrk, ywrk])
    # print(alist)
    return np.array(alist)

NUM_SAMPLES = 10000

# 1. get random numbers in 2D
#
# a. np.random.randint()
# arr_2d = get_2d_random_nums(NUM_SAMPLES,2) # [0,1)
# b. QMC using Halton Sequence
arr_2d = get_2d_QMC(NUM_SAMPLES)  # [0, 1)

# 2. show in 2D map
SIZE_MAP_X = 130
SIZE_MAP_Y = 130
rmap = [[0.0 for yi in range(SIZE_MAP_Y)] for xi in range(SIZE_MAP_X)]
for xval, yval in arr_2d:
    xidx = (SIZE_MAP_X * xval).astype(int)
    yidx = (SIZE_MAP_Y * yval).astype(int)
    rmap[xidx][yidx] = 100.0  # not adding

wrkarr = np.array(rmap)
figmap = np.reshape(wrkarr, (SIZE_MAP_X, SIZE_MAP_Y))
plt.imshow(figmap, extent=(0, SIZE_MAP_X, 0, SIZE_MAP_Y), cmap=cm.jet)
plt.show()

N=10000(v0.2) > np.random.randint()使用
qiita.png

N=10000(v0.3) > Halton Sequence使用
qiita.png

分布は均一化した。

問題はtraining dataset, validation dataset, test datasetをどのように分けるのか。
http://qiita.com/7of9/items/d970baf3322b93efb02b

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