Reference

Install from source

tar zxvf numpy-1.6.1.tar.gz
cd numpy-1.6.1
python setup.py build --fcompiler=gnu95
python setup.py install -n
python setup.py install

Install from source w/ BLAS/LAPACK

See here(scipy official page for install).

Install from source w/ MKL

See here(Intel official page w/ benchmark).

Install (CentOS)

sudo yum install python-devel python-nose python-setuptools gcc gcc-gfortran gcc-c++ blas-devel lapack-devel atlas-devel
sudo easy_install pip
sudo pip install numpy==1.6.2
sudo pip install scipy==0.10.1

You should replace version as you want.

Tips

numpy abstract

arr = np.linspace(0, 9, 10)
array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.])
ndarray.ndim
the number of axes (dimensions) of the array. In the Python world, the number of dimensions is referred to as rank.
arr.ndim
1
ndarray.shape
the dimensions of the array. This is a tuple of integers indicating the size of the array in each dimension. For a matrix with n rows and m columns, shape will be (n,m). The length of the shape tuple is therefore the rank, or number of dimensions, ndim.
arr.shape
(10,)
ndarray.size
the total number of elements of the array. This is equal to the product of the elements of shape.
arr.size
10
ndarray.dtype
an object describing the type of the elements in the array. One can create or specify dtype's using standard Python types. Additionally numpy provides types of its own. numpy.int32, numpy.int16, and numpy.float64 are some examples.
arr.dtype
dtype('float64')

Some examples,

arr.reshape(5,2)
array([[ 0.,  1.],
       [ 2.,  3.],
       [ 4.,  5.],
       [ 6.,  7.],
       [ 8.,  9.]])
arr.reshape(5,2).ndim
2
arr.reshape(5,2).shape
(5, 2)
arr.reshape(5,2).size
10
arr.reshape(5,2).mean()
4.5
arr.reshape(5,2).mean(axis=0) # each column
array([ 4.,  5.])
arr.reshape(5,2).mean(axis=1) # each row
array([ 0.5,  2.5,  4.5,  6.5,  8.5])

ndarray indexing

idx0.png

idx1.png

axis option in numpy

For example, np.mean has "axis" option. Description is

axis : int, optional
Axis along which the means are computed. The default is to compute the mean of the flattened array.

1D x num

axis0.png

If you don't set "axis" option, the result is

In [29]: np.mean(np.random.normal(size=(100,5)))
Out[29]: -0.04388412096074646

With "axis=0", we get,

In [30]: np.mean(np.random.normal(size=(100,5)), axis=0)
Out[30]: array([-0.18631572, -0.02797562,  0.06115698,  0.0944815 , -0.09065672])

With "axis=1", we get,

In [31]: np.mean(np.random.normal(size=(100,5)), axis=1)
Out[31]: 
array([ 0.23989158, -0.83028796,  0.41580478, -0.54811181, -0.48644471,
       -0.0884988 ,  0.22454286, -0.34178575, -0.27064079,  0.13472989,
       -0.14602116, -0.32226302, -0.08628286, -0.69182885, -0.722962  ,
       -0.15622467, -0.11341364, -0.03785862, -0.15445551,  0.13579349,
       -0.28397846,  0.40823796,  0.35722158, -0.09501177,  0.09665369,
       -0.23336876, -0.26747344, -0.21008002,  0.38506011,  0.54030783,
       -1.15930983,  0.09483322,  0.60679675,  0.0565787 ,  0.13805277,
        0.28404466,  0.3309727 ,  0.29398526,  0.83664765,  0.31356201,
       -0.5197411 ,  0.18567519,  0.41980207,  0.42518296,  0.77130017,
        0.49451644, -0.20197456, -0.00315012, -0.05933962,  0.27613463,
       -0.02477431,  0.22490268,  0.09920435, -0.77392157, -0.17589871,
        0.37612978, -0.41126453,  0.20757621,  0.03432338, -0.00367415,
        0.06232633, -0.35579492,  0.20604875, -0.24111743, -0.08301523,
       -0.6444954 ,  0.57611401,  0.53965993,  0.1306546 ,  0.26171248,
        0.1934195 ,  0.03858484,  0.05657696,  0.57205038, -0.1566154 ,
        0.10802271,  0.19214612,  1.26120427,  0.91170774, -0.68189064,
       -0.82885913, -0.16077481,  0.41909049,  0.41888669,  0.21791339,
       -0.64383128,  0.9156997 , -0.18593184, -0.63707648, -0.69209635,
        0.32036258, -0.67841764,  0.59967566, -0.21158908, -0.20115576,
       -0.1268791 ,  0.51380453, -0.09292057, -0.04587118,  0.08232339])

2D x num

axis2.png

If you don't set "axis" option, the result is

In [52]: np.mean(np.arange(100).reshape(10,2,5))
Out[52]: 49.5

With "axis=0", we get,

In [53]: np.mean(np.arange(100).reshape(10,2,5), axis=0)
Out[53]: 
array([[ 45.,  46.,  47.,  48.,  49.],
       [ 50.,  51.,  52.,  53.,  54.]])

With "axis=1", we get,

In [54]: np.mean(np.arange(100).reshape(10,2,5), axis=1)
Out[54]: 
array([[  2.5,   3.5,   4.5,   5.5,   6.5],
       [ 12.5,  13.5,  14.5,  15.5,  16.5],
       [ 22.5,  23.5,  24.5,  25.5,  26.5],
       [ 32.5,  33.5,  34.5,  35.5,  36.5],
       [ 42.5,  43.5,  44.5,  45.5,  46.5],
       [ 52.5,  53.5,  54.5,  55.5,  56.5],
       [ 62.5,  63.5,  64.5,  65.5,  66.5],
       [ 72.5,  73.5,  74.5,  75.5,  76.5],
       [ 82.5,  83.5,  84.5,  85.5,  86.5],
       [ 92.5,  93.5,  94.5,  95.5,  96.5]])

With "axis=2", we get,

In [55]: np.mean(np.arange(100).reshape(10,2,5), axis=2)
Out[55]: 
array([[  2.,   7.],
       [ 12.,  17.],
       [ 22.,  27.],
       [ 32.,  37.],
       [ 42.,  47.],
       [ 52.,  57.],
       [ 62.,  67.],
       [ 72.,  77.],
       [ 82.,  87.],
       [ 92.,  97.]])

mean every m sample

use reshape:

N = 2**15 -5
val = numpy.random.normal(size=N)
m = 25
n = N / m
l = N % m
Val = val[-n*m:].reshape(n,m).mean(axis=1)
#Val = numpy.append( val[:l].mean(axis=0), val[-n*m:].reshape(n,m).mean(axis=1))

fileave.py

ave.png

numerical differentiation

def calc_1st_diff(x, y):
    dy = np.append(y[1:], y[-1]) - np.append(y[:1], y[:-1])
    dx = np.append(x[1:], x[-1]) - np.append(x[:1], x[:-1])
    dydx = dy / dx
    dydx[0] = (-y[2] + 4 * y[1] - 3 * y[0]) / dx[0] / 2
    dydx[-1] = (y[-3] - 4 * y[-2] + 3 * y[-1]) / dx[-1] / 2
    return dydx

Here Lagrange polynomial interpolation are used at both edges of the array.

diff.png

dot & cross

numpy.dot calculates dot product of two vectors like:

In [2]: numpy.dot([1,2,3],[4,5,6])
Out[2]: 32

If you want to calculate list of dot product of list of two vectors, numpy.dot does not work:

In [27]: a
Out[27]: [[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6]]
In [29]: b
Out[29]: [[4, 5, 6, 7], [5, 6, 7, 8], [6, 7, 8, 9]]
In [32]: numpy.dot(a,b)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/Users/chinoney/<ipython-input-32-74d125d6fc11> in <module>()
----> 1 numpy.dot(a,b)

ValueError: objects are not aligned

For this purpose, you should just use * and numpy.sum like

In [38]: numpy.sum(numpy.array(a) * numpy.array(b), axis=0)
Out[38]: array([ 32,  56,  86, 122])

On the other hand, to calculate list of cross product of list of two vectors, you just use numpy.cross like

In [96]: numpy.cross([1,2,3],[4,5,6])
Out[96]: array([-3,  6, -3])
In [90]: numpy.cross(a,b, axis=0)
Out[90]: 
array([[-3, -3, -3, -3],
       [ 6,  6,  6,  6],
       [-3, -3, -3, -3]])

Benchmark is below:

def dot_loop(nrows, p, s):
    for i in range(nrows):
        tmp = np.dot(p[:,i], s[:,i])

def diag(nrows, p, s):
    tmp = np.diag(np.dot(p.transpose(), s))

def kake(nrwos, p, s):
    tmp = np.sum(p * s, axis=0)

def cross_loop(nrows, p, s):
    for i in range(nrows):
        tmp = np.cross(p[:,i], s[:,i])

def cross(nrows, p, s):
    tmp = np.cross(p, s, axis=0)
nrows 10000
dot_loop: 0.0353200435638 sec
dot_diag: 1.30841994286 sec
dot_kake: 0.000382900238037 sec
cross_loop: 0.198802947998 sec
cross: 0.00249004364014 sec

covariance

We measure N values for 100 MCs like

a = np.random.normal(size=(100, 5))
array([[ -6.68715296e-02,   6.27521634e-02,   2.10094523e+00,  -1.22186264e+00,  -1.49644127e+00],
        [ 1.39868487e+00,  -1.44875853e+00,  -3.89646359e-01,   9.66875258e-01,  -7.74172305e-01],
        [  1.00060026e+00,   2.51215897e+00,   5.73874041e-01, -7.03982022e-01,  -3.20763375e-03],
        [ -5.76342540e-01,   3.90327117e-01,  -1.66224470e+00,  2.41878700e-01,   6.16275799e-01],
        [ -8.21927316e-01,   4.14258477e-01,  -1.35722331e+00,  4.97118781e-01,  -7.35588030e-01],
        ...................]])

To calculate covariance, there are two ways to implement,

   a = np.random.normal(size=(num_MCs, num_values))
   print np.cov(a.T)

   A = a - np.mean(a, axis=0)
   print np.dot(A.T, A) / (num_MCs - 1)

The output is like below,

[[ 0.91662737  0.05715182  0.03848676 -0.10531667 -0.05929907]
 [ 0.05715182  0.99439128  0.16304138 -0.0243608  -0.10737514]
 [ 0.03848676  0.16304138  0.93650551  0.00257979  0.02813236]
 [-0.10531667 -0.0243608   0.00257979  0.8651745   0.11397871]
 [-0.05929907 -0.10737514  0.02813236  0.11397871  0.94792185]]

添付ファイル: fileidx1.png 485件 [詳細] fileidx0.png 469件 [詳細] fileaxis2.png 675件 [詳細] fileaxis0.png 551件 [詳細] filecov1d.png 249件 [詳細] filediff.png 500件 [詳細] fileave.py 324件 [詳細] fileave.png 597件 [詳細]