In [1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from pandas import DataFrame, Series

In [2]:
arr  = np.arange(10)
np.add.reduce(arr)


Out[2]:
45

In [3]:
arr.sum()


Out[3]:
45

In [5]:
from numpy.random import randn
arr = randn(5, 5)
arr[::2]


Out[5]:
array([[ 0.02187458, -0.62777294,  0.16734544,  1.43807009, -0.22187227],
       [-0.33820375,  0.13544869, -0.19165592, -0.94656592,  0.18745003],
       [ 1.39229807, -2.32765928,  0.81650944,  1.42274168,  0.46909254]])

In [6]:
arr[::2].sort(1) # 对部分行进行排序
arr[:, :-1] < arr[:, 1:]


Out[6]:
array([[ True,  True,  True,  True],
       [ True, False,  True, False],
       [ True,  True,  True,  True],
       [False,  True,  True,  True],
       [ True,  True,  True,  True]], dtype=bool)

In [7]:
np.logical_and.reduce(arr[:, :-1] < arr[:, 1:], axis=1)


Out[7]:
array([ True, False,  True, False,  True], dtype=bool)

In [8]:
np.all(arr[:, :-1] < arr[:, 1:], axis=1)


Out[8]:
array([ True, False,  True, False,  True], dtype=bool)

In [9]:
arr = np.arange(15).reshape((3, 5))
np.add.accumulate(arr, axis=1)


Out[9]:
array([[ 0,  1,  3,  6, 10],
       [ 5, 11, 18, 26, 35],
       [10, 21, 33, 46, 60]])

In [10]:
arr = np.arange(3).repeat([1, 2, 2])
arr


Out[10]:
array([0, 1, 1, 2, 2])

In [11]:
np.multiply.outer(arr, np.arange(5))


Out[11]:
array([[0, 0, 0, 0, 0],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 2, 4, 6, 8],
       [0, 2, 4, 6, 8]])

In [13]:
result = np.subtract.outer(randn(3, 4), randn(5))
result.shape


Out[13]:
(3L, 4L, 5L)

In [15]:
np.subtract.outer(np.arange(5), np.arange(3))


Out[15]:
array([[ 0, -1, -2],
       [ 1,  0, -1],
       [ 2,  1,  0],
       [ 3,  2,  1],
       [ 4,  3,  2]])

In [16]:
arr = np.arange(10)
np.add.reduceat(arr, [0, 5, 8])


Out[16]:
array([10, 18, 17])

In [17]:
arr = np.multiply.outer(np.arange(4), np.arange(5))
np.add.reduceat(arr, [0, 2, 4], axis=1)


Out[17]:
array([[ 0,  0,  0],
       [ 1,  5,  4],
       [ 2, 10,  8],
       [ 3, 15, 12]])

In [18]:
def add_elements(x, y):
    return x + y

add_them = np.frompyfunc(add_elements, 2, 1)
add_them(np.arange(8), np.arange(8))


Out[18]:
array([0, 2, 4, 6, 8, 10, 12, 14], dtype=object)

In [19]:
add_them = np.vectorize(add_elements, otypes=[np.float64])
add_them(np.arange(8), np.arange(8))


Out[19]:
array([  0.,   2.,   4.,   6.,   8.,  10.,  12.,  14.])

In [20]:
arr = randn(10000)
%timeit add_them(arr, arr)


100 loops, best of 3: 2.36 ms per loop

In [21]:
%timeit np.add(arr, arr)


The slowest run took 5.30 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 6.62 µs per loop

In [22]:
dtype = [('x', np.float64), ('y', np.int32)]
sarr = np.array([(1.5, 6), (np.pi, -2)], dtype=dtype)
sarr


Out[22]:
array([(1.5, 6), (3.141592653589793, -2)], 
      dtype=[('x', '<f8'), ('y', '<i4')])

In [23]:
sarr[0]['y']


Out[23]:
6

In [24]:
sarr['x']


Out[24]:
array([ 1.5       ,  3.14159265])

In [25]:
dtype = [('x', np.int64, 3), ('y', np.int32)]
arr = np.zeros(4, dtype=dtype)
arr


Out[25]:
array([([0L, 0L, 0L], 0), ([0L, 0L, 0L], 0), ([0L, 0L, 0L], 0),
       ([0L, 0L, 0L], 0)], 
      dtype=[('x', '<i8', (3,)), ('y', '<i4')])

In [26]:
arr[0]['x']


Out[26]:
array([0, 0, 0], dtype=int64)

In [27]:
arr['x']


Out[27]:
array([[0, 0, 0],
       [0, 0, 0],
       [0, 0, 0],
       [0, 0, 0]], dtype=int64)

In [28]:
dtype = [('x', [('a', 'f8'), ('b', 'f4')]), ('y', np.int32)]
data = np.array([((1, 2), 5), ((3, 4), 6)], dtype=dtype)
data['x']


Out[28]:
array([(1.0, 2.0), (3.0, 4.0)], 
      dtype=[('a', '<f8'), ('b', '<f4')])

In [29]:
data['y']


Out[29]:
array([5, 6])