2014年11月30日日曜日

141130(4)

NumPy


多重ランダムウォーク

>>> nwalks = 5
>>> nsteps = 50
>>> draws = np.random.randint(0, 2, size=(nwalks, nsteps))
>>> steps = np.where(draws > 0, 1, -1)
>>> steps
array([[-1, -1, -1,  1,  1,  1,  1, -1, -1, -1, -1, -1,  1, -1,  1, -1, -1,
         1, -1,  1,  1,  1,  1,  1, -1,  1,  1,  1,  1, -1, -1,  1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  1,  1, -1,  1,  1,  1],
       [-1,  1,  1, -1,  1, -1,  1, -1,  1, -1, -1,  1, -1, -1, -1, -1,  1,
         1,  1,  1,  1,  1, -1, -1,  1,  1, -1, -1, -1,  1, -1,  1, -1,  1,
        -1,  1, -1, -1,  1,  1,  1,  1, -1, -1, -1,  1,  1,  1, -1,  1],
       [ 1,  1, -1, -1, -1, -1,  1,  1, -1, -1, -1, -1,  1, -1,  1,  1,  1,
         1, -1, -1,  1, -1, -1, -1,  1, -1, -1,  1, -1,  1, -1, -1, -1, -1,
         1, -1,  1, -1,  1,  1, -1, -1, -1,  1, -1,  1,  1,  1,  1,  1],
       [-1, -1, -1, -1, -1,  1,  1, -1, -1, -1,  1,  1,  1, -1, -1, -1, -1,
         1,  1, -1, -1, -1,  1, -1,  1, -1, -1,  1, -1, -1,  1, -1, -1, -1,
        -1,  1,  1, -1, -1,  1, -1,  1, -1,  1, -1, -1, -1,  1,  1,  1],
       [-1, -1,  1,  1,  1, -1,  1, -1,  1, -1,  1, -1, -1, -1,  1, -1, -1,
         1,  1, -1, -1,  1,  1, -1,  1, -1,  1,  1, -1, -1, -1,  1,  1, -1,
         1, -1, -1, -1,  1,  1,  1,  1,  1,  1,  1, -1,  1,  1, -1,  1]])
>>> walk = steps.cumsum(1)
>>> walk
array([[ -1,  -2,  -3,  -2,  -1,   0,   1,   0,  -1,  -2,  -3,  -4,  -3,
         -4,  -3,  -4,  -5,  -4,  -5,  -4,  -3,  -2,  -1,   0,  -1,   0,
          1,   2,   3,   2,   1,   2,   1,   0,  -1,  -2,  -3,  -4,  -5,
         -6,  -7,  -8,  -9, -10,  -9,  -8,  -9,  -8,  -7,  -6],
       [ -1,   0,   1,   0,   1,   0,   1,   0,   1,   0,  -1,   0,  -1,
         -2,  -3,  -4,  -3,  -2,  -1,   0,   1,   2,   1,   0,   1,   2,
          1,   0,  -1,   0,  -1,   0,  -1,   0,  -1,   0,  -1,  -2,  -1,
          0,   1,   2,   1,   0,  -1,   0,   1,   2,   1,   2],
       [  1,   2,   1,   0,  -1,  -2,  -1,   0,  -1,  -2,  -3,  -4,  -3,
         -4,  -3,  -2,  -1,   0,  -1,  -2,  -1,  -2,  -3,  -4,  -3,  -4,
         -5,  -4,  -5,  -4,  -5,  -6,  -7,  -8,  -7,  -8,  -7,  -8,  -7,
         -6,  -7,  -8,  -9,  -8,  -9,  -8,  -7,  -6,  -5,  -4],
       [ -1,  -2,  -3,  -4,  -5,  -4,  -3,  -4,  -5,  -6,  -5,  -4,  -3,
         -4,  -5,  -6,  -7,  -6,  -5,  -6,  -7,  -8,  -7,  -8,  -7,  -8,
         -9,  -8,  -9, -10,  -9, -10, -11, -12, -13, -12, -11, -12, -13,
        -12, -13, -12, -13, -12, -13, -14, -15, -14, -13, -12],
       [ -1,  -2,  -1,   0,   1,   0,   1,   0,   1,   0,   1,   0,  -1,
         -2,  -1,  -2,  -3,  -2,  -1,  -2,  -3,  -2,  -1,  -2,  -1,  -2,
         -1,   0,  -1,  -2,  -3,  -2,  -1,  -2,  -1,  -2,  -3,  -4,  -3,
         -2,  -1,   0,   1,   2,   3,   2,   3,   4,   3,   4]])

141130(3)

NumPy


行列計算

>>> from numpy.linalg import inv, qr
>>> mat = np.array([[1, 2, 3], [2, 4, 5], [3, 5, 7]])
>>> mat.dot(inv(mat))
array([[  1.00000000e+00,   0.00000000e+00,   2.22044605e-16],
       [  0.00000000e+00,   1.00000000e+00,   4.44089210e-16],
       [  0.00000000e+00,   0.00000000e+00,   1.00000000e+00]])
>>> q,r = qr(mat)
>>> q
array([[ -2.67261242e-01,  -3.58568583e-01,  -8.94427191e-01],
       [ -5.34522484e-01,  -7.17137166e-01,   4.47213595e-01],
       [ -8.01783726e-01,   5.97614305e-01,  -1.22124533e-15]])
>>> r
array([[-3.74165739, -6.68153105, -9.08688223],
       [ 0.        , -0.5976143 , -0.47809144],
       [ 0.        ,  0.        , -0.4472136 ]])
>>> q.dot(r)
array([[ 1.,  2.,  3.],
       [ 2.,  4.,  5.],
       [ 3.,  5.,  7.]])

141130(2)

NumPy


sum, meanの使い方

>>> a = np.arange(24).reshape((2,3,4))
>>> a
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])
>>> a.sum()
276
>>> a.mean()
11.5
>>> a.sum(0)
array([[12, 14, 16, 18],
       [20, 22, 24, 26],
       [28, 30, 32, 34]])
>>> a.mean(axis=0)
array([[  6.,   7.,   8.,   9.],
       [ 10.,  11.,  12.,  13.],
       [ 14.,  15.,  16.,  17.]])
>>> a.sum(1)
array([[12, 15, 18, 21],
       [48, 51, 54, 57]])
>>> a.mean(axis=1)
array([[  4.,   5.,   6.,   7.],
       [ 16.,  17.,  18.,  19.]])
>>> a.sum(2)
array([[ 6, 22, 38],
       [54, 70, 86]])
>>> a.mean(axis=2)
array([[  1.5,   5.5,   9.5],
       [ 13.5,  17.5,  21.5]])

load, savez, loadtxtの使い方

>>> ary1 = np.arange(10)
>>> ary2 = np.arange(3)
>>> np.savez('data', a = ary1, b = ary2)
>>> npz = np.load('data.npz')
>>> npz['a']
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> npz['b']
array([0, 1, 2])

(data.txt
 1, 2, 3
 10,20,30
 が保存している状況で)
>>> ary = np.loadtxt('data.txt', delimiter=',')
>>> ary
array([[  1.,   2.,   3.],
       [ 10.,  20.,  30.]])

141130

NumPy, Matplotlib


山の描画

>>> import pylab as pl
>>> import numpy as np
>>> points = np.arange(-5,5,0.1)
>>> xs, ys = np.meshgrid(points, points)
>>> z = np.sqrt(xs**2 + ys**2)
>>> pl.imshow(z, cmap=pl.cm.gray); pl.colorbar()
<matplotlib.image.AxesImage object at 0x034EBA90>
<matplotlib.colorbar.Colorbar instance at 0x03550850>
>>> pl.title("Image plot of sqrt(x**2 + y**2) for a grid of values")
<matplotlib.text.Text object at 0x034DE730>
>>> pl.show()

2014年11月29日土曜日

141129

NumPy


reshapeの使い方

>>> a = np.arange(16).reshape((2,8))
>>> a
array([[ 0,  1,  2,  3,  4,  5,  6,  7],
       [ 8,  9, 10, 11, 12, 13, 14, 15]])
>>> a.reshape((4,4))
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])

transposeの使い方

>>> a = np.arange(120).reshape((2,3,4,5))
>>> a
array([[[[  0,   1,   2,   3,   4],
         [  5,   6,   7,   8,   9],
         [ 10,  11,  12,  13,  14],
         [ 15,  16,  17,  18,  19]],

        [[ 20,  21,  22,  23,  24],
         [ 25,  26,  27,  28,  29],
         [ 30,  31,  32,  33,  34],
         [ 35,  36,  37,  38,  39]],

        [[ 40,  41,  42,  43,  44],
         [ 45,  46,  47,  48,  49],
         [ 50,  51,  52,  53,  54],
         [ 55,  56,  57,  58,  59]]],


       [[[ 60,  61,  62,  63,  64],
         [ 65,  66,  67,  68,  69],
         [ 70,  71,  72,  73,  74],
         [ 75,  76,  77,  78,  79]],

        [[ 80,  81,  82,  83,  84],
         [ 85,  86,  87,  88,  89],
         [ 90,  91,  92,  93,  94],
         [ 95,  96,  97,  98,  99]],

        [[100, 101, 102, 103, 104],
         [105, 106, 107, 108, 109],
         [110, 111, 112, 113, 114],
         [115, 116, 117, 118, 119]]]])
>>> a.transpose((0,1,2,3)).shape
(2, 3, 4, 5)
>>> a.transpose((0,1,3,2)).shape
(2, 3, 5, 4)
>>> a.transpose((0,2,1,3)).shape
(2, 4, 3, 5)
>>> a.transpose((0,2,3,1)).shape
(2, 4, 5, 3)

2014年11月24日月曜日

141124

クローラー(開発言語:Ruby)


今日「Rubyによるクローラー開発技法」という本を買ってきた。

第1章に「10分クローラーの作成」と書いてあるし、
amazonのレビューを見ても、親切で初心者向けと書いてあったので、
「よし!クローラーが作ってみよう!!」と意気込んでいたのですが、
結構つまづいている感じ…。

今日の進捗を大まかに記してみる。

1.wgetのインストール
2.wgetの簡単な使い方はわかっていないがすっとばす。
3.Rubyの基礎はわかっているのですっとばす。
4.webrick0.rb 実行
5.webrick-template.rb 実行
6.test-webserver0.rb 実行
7.再帰ダウンロードの検証には、
  2.がわかっていないといけないことに気付く。

  本書ではwgetは
  C:\wget\bin
  の直下にあるので、wgetを使用するときは
  cd /d 〜
  を使って移動。

  また、検証の際、立ち上げたページは閉じてしまわない。
8.10分クローラーの作成
  WebサイトのエンコードはUTF-8。
  本書では、
  そのまま処理できる
  と書いてあるが、
  Windowsではどのように標準出力してよいかわからない。
  ↑
  挫折気味。