In [1]:
import numpy as np

In [2]:
np.array([1, 2, 3, 4])


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

In [3]:
np.array([1, 2, 3, 4], dtype='float32')


Out[3]:
array([ 1.,  2.,  3.,  4.], dtype=float32)

In [4]:
np.array([range(i, i + 3) for i in [2, 4, 6]])


Out[4]:
array([[2, 3, 4],
       [4, 5, 6],
       [6, 7, 8]])

In [5]:
np.zeros(10, dtype=int)


Out[5]:
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

In [6]:
np.ones((3, 5), dtype=float)


Out[6]:
array([[ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.]])

In [7]:
np.full((3, 5), 3.14)


Out[7]:
array([[ 3.14,  3.14,  3.14,  3.14,  3.14],
       [ 3.14,  3.14,  3.14,  3.14,  3.14],
       [ 3.14,  3.14,  3.14,  3.14,  3.14]])

In [8]:
np.arange(0, 20, 2)


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

In [9]:
np.linspace(0, 1, 5)


Out[9]:
array([ 0.  ,  0.25,  0.5 ,  0.75,  1.  ])

In [10]:
np.random.random((3, 3))


Out[10]:
array([[ 0.81512999,  0.96010159,  0.07371983],
       [ 0.5365491 ,  0.93667171,  0.7892256 ],
       [ 0.07670136,  0.37505519,  0.95233023]])

In [11]:
np.random.normal(0, 1, (3, 3))  # mean=0, std=1


Out[11]:
array([[-0.61546927,  1.26299182,  2.06935162],
       [-0.44338651,  0.30878961,  0.7213505 ],
       [ 0.46318187,  0.03793384,  1.26512778]])

In [12]:
np.random.randint(0, 10, (3, 3))  # [0,10)


Out[12]:
array([[8, 0, 8],
       [4, 7, 9],
       [9, 7, 9]])

In [13]:
np.eye(3)


Out[13]:
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])

In [14]:
np.empty(3)  # no value init, values will be whatever happens to already exist at that memory location


Out[14]:
array([ 1.,  1.,  1.])

In [15]:
np.random.seed(0) # seed for reproducibility
x1 = np.random.randint(10, size=6) # One-dimensional array
x2 = np.random.randint(10, size=(3, 4)) # Two-dimensional array
x3 = np.random.randint(10, size=(3, 4, 5)) # Three-dimensional array
print("x3 ndim: ", x3.ndim)
print("x3 shape:", x3.shape)
print("x3 size: ", x3.size)
print("dtype:", x3.dtype)
print("itemsize:", x3.itemsize, "bytes")
print("nbytes:", x3.nbytes, "bytes")


('x3 ndim: ', 3)
('x3 shape:', (3, 4, 5))
('x3 size: ', 60)
('dtype:', dtype('int64'))
('itemsize:', 8, 'bytes')
('nbytes:', 480, 'bytes')

In [16]:
x1


Out[16]:
array([5, 0, 3, 3, 7, 9])

In [17]:
x1[0], x1[3], x1[-1], x1[-2]


Out[17]:
(5, 3, 9, 7)

In [18]:
x2


Out[18]:
array([[3, 5, 2, 4],
       [7, 6, 8, 8],
       [1, 6, 7, 7]])

In [19]:
x2[0, 0], x2[2, 0], x2[2, -1]


Out[19]:
(3, 1, 7)

In [20]:
x2[0, 0] = 12
x2


Out[20]:
array([[12,  5,  2,  4],
       [ 7,  6,  8,  8],
       [ 1,  6,  7,  7]])

In [21]:
x = np.arange(10)
x


Out[21]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [22]:
x[:5]


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

In [23]:
x[5:]


Out[23]:
array([5, 6, 7, 8, 9])

In [24]:
x[2:5]


Out[24]:
array([2, 3, 4])

In [25]:
x[::2]


Out[25]:
array([0, 2, 4, 6, 8])

In [26]:
x[1::2]


Out[26]:
array([1, 3, 5, 7, 9])

In [27]:
x[::-1]


Out[27]:
array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])

In [28]:
x[5::-2]


Out[28]:
array([5, 3, 1])

In [29]:
x2


Out[29]:
array([[12,  5,  2,  4],
       [ 7,  6,  8,  8],
       [ 1,  6,  7,  7]])

In [30]:
x2[:2, :3]


Out[30]:
array([[12,  5,  2],
       [ 7,  6,  8]])

In [31]:
x2[:2, ::2]


Out[31]:
array([[12,  2],
       [ 7,  8]])

In [32]:
x2[::-1, ::-1]


Out[32]:
array([[ 7,  7,  6,  1],
       [ 8,  8,  6,  7],
       [ 4,  2,  5, 12]])

In [33]:
x2[:, 0]


Out[33]:
array([12,  7,  1])

In [34]:
x2[0]  # == x2[0, :]


Out[34]:
array([12,  5,  2,  4])

In [35]:
x2_sub = x2[:2, :2]  # view
x2_sub


Out[35]:
array([[12,  5],
       [ 7,  6]])

In [36]:
x2_sub[0, 0] = 99
x2_sub


Out[36]:
array([[99,  5],
       [ 7,  6]])

In [37]:
x2


Out[37]:
array([[99,  5,  2,  4],
       [ 7,  6,  8,  8],
       [ 1,  6,  7,  7]])

In [38]:
x2_sub_copy = x2[:2, :2].copy()  # copy
x2_sub_copy[0, 0] = 42
print x2_sub_copy
print x2


[[42  5]
 [ 7  6]]
[[99  5  2  4]
 [ 7  6  8  8]
 [ 1  6  7  7]]

In [39]:
np.arange(1, 10).reshape((3, 3))


Out[39]:
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

In [40]:
x = np.array([1, 2, 3])
print x.shape
print x[:, np.newaxis].shape
print x[np.newaxis, :].shape
print x.reshape(-1, 1).shape
print x.reshape(1, -1).shape


(3,)
(3, 1)
(1, 3)
(3, 1)
(1, 3)

In [41]:
x = np.array([1, 2, 3])
y = np.array([3, 2, 1])
print np.concatenate([x, y])
z = [9, 9, 9]
print np.concatenate([x, y, z])


[1 2 3 3 2 1]
[1 2 3 3 2 1 9 9 9]

In [42]:
grid = np.array([[1, 2, 3],
                 [4, 5, 6]])
print np.concatenate([grid, grid])
print np.concatenate([grid, grid], axis=1)


[[1 2 3]
 [4 5 6]
 [1 2 3]
 [4 5 6]]
[[1 2 3 1 2 3]
 [4 5 6 4 5 6]]

In [43]:
x = np.array([1, 2, 3])
grid = np.array([[9, 8, 7],
                [6, 5, 4]])
# vertically stack the arrays
np.vstack([x, grid])


Out[43]:
array([[1, 2, 3],
       [9, 8, 7],
       [6, 5, 4]])

In [44]:
y = np.array([[99],
              [99]])
# horizontally stack the arrays
np.hstack([grid, y])


Out[44]:
array([[ 9,  8,  7, 99],
       [ 6,  5,  4, 99]])

In [45]:
x=[1,2,3,99,99,3,2,1]
x1, x2, x3 = np.split(x, [3, 5])
print(x1, x2, x3)


(array([1, 2, 3]), array([99, 99]), array([3, 2, 1]))

In [46]:
a, b = np.vsplit(np.arange(16).reshape((4, 4)), [2])
print a
print b


[[0 1 2 3]
 [4 5 6 7]]
[[ 8  9 10 11]
 [12 13 14 15]]

In [47]:
a, b = np.hsplit(np.arange(16).reshape((4, 4)), [2])
print a
print b


[[ 0  1]
 [ 4  5]
 [ 8  9]
 [12 13]]
[[ 2  3]
 [ 6  7]
 [10 11]
 [14 15]]

In [48]:
def compute_reciprocals(values):
    output = np.empty(len(values))
    for i in range(len(values)):
        output[i] = 1.0 / values[i]
    return output

big_array = np.random.randint(1, 100, size=1000000)
%timeit compute_reciprocals(big_array)


1 loop, best of 3: 384 ms per loop

In [49]:
%timeit (1.0 / big_array)


100 loops, best of 3: 4.04 ms per loop

Operator Equivalent ufunc

  • np.add
  • np.subtract
  • np.negative
  • np.multiply / np.divide // np.floor_divide ** np.power % np.mod

In [50]:
x = np.arange(4)
print("x =", x)
print("x + 5 =", x + 5)
print("x - 5 =", x - 5)
print("x * 2 =", x * 2)
print("x / 2 =", x / 2)
print("x // 2 =", x // 2) # floor division
print("-x = ", -x)
print("x ** 2 = ", x ** 2)
print("x % 2 = ", x % 2)
print('-(0.5*x + 1) ** 2 =  ', -(0.5*x + 1) ** 2)


('x =', array([0, 1, 2, 3]))
('x + 5 =', array([5, 6, 7, 8]))
('x - 5 =', array([-5, -4, -3, -2]))
('x * 2 =', array([0, 2, 4, 6]))
('x / 2 =', array([0, 0, 1, 1]))
('x // 2 =', array([0, 0, 1, 1]))
('-x = ', array([ 0, -1, -2, -3]))
('x ** 2 = ', array([0, 1, 4, 9]))
('x % 2 = ', array([0, 1, 0, 1]))
('-(0.5*x + 1) ** 2 =  ', array([-1.  , -2.25, -4.  , -6.25]))

In [51]:
np.absolute is np.abs


Out[51]:
True

In [52]:
theta = np.linspace(0, np.pi, 3)
print("theta = ", theta)
print("sin(theta) = ", np.sin(theta))
print("cos(theta) = ", np.cos(theta))
print("tan(theta) = ", np.tan(theta))
x = [-1, 0, 1]
print("x = ", x)
print("arcsin(x) = ", np.arcsin(x))
print("arccos(x) = ", np.arccos(x))
print("arctan(x) = ", np.arctan(x))


('theta = ', array([ 0.        ,  1.57079633,  3.14159265]))
('sin(theta) = ', array([  0.00000000e+00,   1.00000000e+00,   1.22464680e-16]))
('cos(theta) = ', array([  1.00000000e+00,   6.12323400e-17,  -1.00000000e+00]))
('tan(theta) = ', array([  0.00000000e+00,   1.63312394e+16,  -1.22464680e-16]))
('x = ', [-1, 0, 1])
('arcsin(x) = ', array([-1.57079633,  0.        ,  1.57079633]))
('arccos(x) = ', array([ 3.14159265,  1.57079633,  0.        ]))
('arctan(x) = ', array([-0.78539816,  0.        ,  0.78539816]))

In [53]:
x = [1, 2, 3]
print("x =", x)
print("e^x =",np.exp(x))
print("2^x =",np.exp2(x))
print("3^x =",np.power(3,x))
x = [1, 2, 4, 10]
print("x =", x)
print("ln(x) =", np.log(x))
print("log2(x) =", np.log2(x))
print("log10(x) =", np.log10(x))
x = [0, 0.001, 0.01, 0.1]
print("exp(x) - 1 =", np.expm1(x))
print("log(1 + x) =", np.log1p(x))


('x =', [1, 2, 3])
('e^x =', array([  2.71828183,   7.3890561 ,  20.08553692]))
('2^x =', array([ 2.,  4.,  8.]))
('3^x =', array([ 3,  9, 27]))
('x =', [1, 2, 4, 10])
('ln(x) =', array([ 0.        ,  0.69314718,  1.38629436,  2.30258509]))
('log2(x) =', array([ 0.        ,  1.        ,  2.        ,  3.32192809]))
('log10(x) =', array([ 0.        ,  0.30103   ,  0.60205999,  1.        ]))
('exp(x) - 1 =', array([ 0.        ,  0.0010005 ,  0.01005017,  0.10517092]))
('log(1 + x) =', array([ 0.        ,  0.0009995 ,  0.00995033,  0.09531018]))

In [54]:
from scipy import special
# Gamma functions (generalized factorials) and related functions
x=[1,5,10]
print("gamma(x) =", special.gamma(x))
print("ln|gamma(x)| =", special.gammaln(x))
print("beta(x, 2) =", special.beta(x, 2))
# Error function (integral of Gaussian) # its complement, and its inverse
x = np.array([0, 0.3, 0.7, 1.0])
print("erf(x) =", special.erf(x))
print("erfc(x) =", special.erfc(x))
print("erfinv(x) =", special.erfinv(x))


('gamma(x) =', array([  1.00000000e+00,   2.40000000e+01,   3.62880000e+05]))
('ln|gamma(x)| =', array([  0.        ,   3.17805383,  12.80182748]))
('beta(x, 2) =', array([ 0.5       ,  0.03333333,  0.00909091]))
('erf(x) =', array([ 0.        ,  0.32862676,  0.67780119,  0.84270079]))
('erfc(x) =', array([ 1.        ,  0.67137324,  0.32219881,  0.15729921]))
('erfinv(x) =', array([ 0.        ,  0.27246271,  0.73286908,         inf]))

In [55]:
# For all ufuncs, you can do this using the out argument of the function:
x = np.arange(5)
y = np.empty(5)
np.multiply(x, 10, out=y)
print(y)


[  0.  10.  20.  30.  40.]

In [56]:
# for very large arrays the memory savings from careful use of the out argument can be significant
y = np.zeros(10)
np.power(2, x, out=y[::2])
print(y)


[  1.   0.   2.   0.   4.   0.   8.   0.  16.   0.]

In [57]:
big_array = np.random.rand(1000000)
%timeit sum(big_array)
%timeit np.sum(big_array)


10 loops, best of 3: 81.5 ms per loop
1000 loops, best of 3: 369 µs per loop

In [58]:
%timeit min(big_array)
%timeit np.min(big_array)


10 loops, best of 3: 65.6 ms per loop
The slowest run took 5.58 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 326 µs per loop

In [59]:
a = np.array([0, 1, 2])
b = np.array([5, 5, 5])
print a+b
print a+5


[5 6 7]
[5 6 7]

In [60]:
a = np.arange(3)
b = np.arange(3)[:, np.newaxis]
print(a.shape, b.shape)
print(a+b)


((3,), (3, 1))
[[0 1 2]
 [1 2 3]
 [2 3 4]]

In [61]:
a = np.arange(3).reshape((3, 1))
b = np.arange(3)
print(a+b)


[[0 1 2]
 [1 2 3]
 [2 3 4]]

In [62]:
M = np.ones((3, 2))
a = np.arange(3)
M+a


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-62-427b46fd0b1a> in <module>()
      1 M = np.ones((3, 2))
      2 a = np.arange(3)
----> 3 M+a

ValueError: operands could not be broadcast together with shapes (3,2) (3,) 

In [63]:
M + a[:, np.newaxis]


Out[63]:
array([[ 1.,  1.],
       [ 2.,  2.],
       [ 3.,  3.]])

In [64]:
X = np.random.random((10, 3))
Xmean = X.mean(0)
print Xmean
X_centered = X - Xmean
print X_centered.mean(0)


[ 0.37261435  0.45263781  0.39748812]
[ -2.22044605e-17   5.55111512e-18   0.00000000e+00]

In [65]:
%matplotlib inline
import matplotlib.pyplot as plt
x = np.linspace(0, 5, 50)
y = np.linspace(0, 5, 50)[:, np.newaxis]
z = np.sin(x) ** 10 + np.cos(10 + y * x) * np.cos(x)
plt.imshow(z, origin='lower', extent=[0, 5, 0, 5], cmap='viridis')
plt.colorbar()


Out[65]:
<matplotlib.colorbar.Colorbar at 0x112d68f50>

In [66]:
x = np.array([1, 2, 3, 4, 5])
print x < 3 # less than
print x > 3 # greater than
print x <= 3 # less than or equal
print x >= 3 # greater than or equal
print x != 3 # not equal
print x == 3 # equal


[ True  True False False False]
[False False False  True  True]
[ True  True  True False False]
[False False  True  True  True]
[ True  True False  True  True]
[False False  True False False]

In [67]:
rng = np.random.RandomState(0)
x = rng.randint(10, size=(3, 4))
print x
print x < 6


[[5 0 3 3]
 [7 9 3 5]
 [2 4 7 6]]
[[ True  True  True  True]
 [False False  True  True]
 [ True  True False False]]

In [68]:
print np.sum(x < 6)
# how many values less than 6 in each row
print np.sum(x < 6, axis=1)


8
[4 2 2]

In [69]:
print np.any(x > 8)
print np.all(x < 10)
print np.all(x < 8, axis=1)


True
True
[ True False  True]

In [70]:
# and and or perform a single Boolean evaluation on an entire object, 
# while & and | perform multiple Boolean evaluations on the content (the indi‐ vidual bits or bytes) of an object. 
# For Boolean NumPy arrays, the latter is nearly always the desired operation.
print np.sum((x > 4) & (x < 7))
print np.sum(~((x <= 5) | (x >= 1)))


3
0

In [71]:
x[x < 5]


Out[71]:
array([0, 3, 3, 3, 2, 4])

In [72]:
rand = np.random.RandomState(42)
x = rand.randint(100, size=10)
print(x)


[51 92 14 71 60 20 82 86 74 74]

In [73]:
ind = [3, 7, 4]
print x[ind]
ind = np.array([[3, 7],
                [4, 5]])
print x[ind]


[71 86 60]
[[71 86]
 [60 20]]

In [74]:
X = np.arange(12).reshape((3, 4))
row = np.array([0, 1, 2])
col = np.array([2, 1, 3])
X[row, col]


Out[74]:
array([ 2,  5, 11])

In [75]:
X[row[:, np.newaxis], col]


Out[75]:
array([[ 2,  1,  3],
       [ 6,  5,  7],
       [10,  9, 11]])

In [76]:
row[:, np.newaxis] * col


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

In [77]:
print X[2, [2, 0, 1]]
print X[1:, [2, 0, 1]]
mask = np.array([1, 0, 1, 0], dtype=bool)
print X[row[:, np.newaxis], mask]


[10  8  9]
[[ 6  4  5]
 [10  8  9]]
[[ 0  2]
 [ 4  6]
 [ 8 10]]

In [78]:
mean = [0, 0]
cov = [[1, 2],
       [2, 5]]
X = rand.multivariate_normal(mean, cov, 100)
X.shape


Out[78]:
(100, 2)

In [79]:
plt.scatter(X[:, 0], X[:, 1])


Out[79]:
<matplotlib.collections.PathCollection at 0x10e220b50>

In [81]:
indices = np.random.choice(X.shape[0], 20, replace=False)
selection = X[indices]
plt.scatter(X[:, 0], X[:, 1], alpha=0.3)
plt.scatter(selection[:, 0], selection[:, 1], facecolor='none', s=200, edgecolors='r')


Out[81]:
<matplotlib.collections.PathCollection at 0x1130e12d0>

In [82]:
x = np.zeros(10)
x[[0, 0]] = [4, 6]
print(x)  # careful
i=[2,3,3,4,4,4]
x[i] += 1
print(x)  # careful


[ 6.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
[ 6.  0.  1.  1.  1.  0.  0.  0.  0.  0.]

In [83]:
x = np.zeros(10)
np.add.at(x, i, 1)
print(x)


[ 0.  0.  1.  2.  3.  0.  0.  0.  0.  0.]

In [84]:
np.random.seed(42)
x = np.random.randn(100)
# compute a histogram by hand
bins = np.linspace(-5, 5, 20)
counts = np.zeros_like(bins)
# find the appropriate bin for each x
i = np.searchsorted(bins, x)
# add 1 to each of these bins
np.add.at(counts, i, 1)
plt.plot(bins, counts, linestyle='steps')


Out[84]:
[<matplotlib.lines.Line2D at 0x113199890>]

In [85]:
plt.hist(x, bins, histtype='step')


Out[85]:
(array([  0.,   0.,   0.,   0.,   1.,   3.,   7.,   9.,  23.,  22.,  17.,
         10.,   7.,   1.,   0.,   0.,   0.,   0.,   0.]),
 array([-5.        , -4.47368421, -3.94736842, -3.42105263, -2.89473684,
        -2.36842105, -1.84210526, -1.31578947, -0.78947368, -0.26315789,
         0.26315789,  0.78947368,  1.31578947,  1.84210526,  2.36842105,
         2.89473684,  3.42105263,  3.94736842,  4.47368421,  5.        ]),
 <a list of 1 Patch objects>)

In [86]:
x = np.array([2, 1, 4, 3, 5])
print np.sort(x)  # return a copy
x.sort()  # in-place
print x


[1 2 3 4 5]
[1 2 3 4 5]

In [87]:
x = np.array([2, 1, 4, 3, 5])
i = np.argsort(x)
print i
print x[i]


[1 0 3 2 4]
[1 2 3 4 5]

In [88]:
rand = np.random.RandomState(42)
X = rand.randint(0, 10, (4, 6))
print(X)
# sort each column of X
print(np.sort(X, axis=0))
# sort each row of X
print(np.sort(X, axis=1))


[[6 3 7 4 6 9]
 [2 6 7 4 3 7]
 [7 2 5 4 1 7]
 [5 1 4 0 9 5]]
[[2 1 4 0 1 5]
 [5 2 5 4 3 7]
 [6 3 7 4 6 7]
 [7 6 7 4 9 9]]
Out[88]:
array([[3, 4, 6, 6, 7, 9],
       [2, 3, 4, 6, 7, 7],
       [1, 2, 4, 5, 7, 7],
       [0, 1, 4, 5, 5, 9]])

In [89]:
# Sometimes we’re not interested in sorting the entire array, 
# but simply want to find the K smallest values in the array.
# NumPy provides this in the np.partition function.
x = np.array([7, 2, 3, 1, 6, 5, 4])
print np.partition(x, 3)
print np.partition(X, 2, axis=1)


[2 1 3 4 6 5 7]
[[3 4 6 7 6 9]
 [2 3 4 7 6 7]
 [1 2 4 5 7 7]
 [0 1 4 5 9 5]]

In [90]:
X = rand.rand(10, 2)
dist_sq = np.sum((X[:,np.newaxis,:] - X[np.newaxis,:,:]) ** 2, axis=-1)  # pari-wise distance
nearest = np.argsort(dist_sq, axis=1)
K = 2
nearest_partition = np.argpartition(dist_sq, K + 1, axis=1)  ##
plt.scatter(X[:, 0], X[:, 1], s=100)
for i in range(X.shape[0]):
    for j in nearest_partition[i, :K+1]:
        # plot a line from X[i] to X[j]
        # use some zip magic to make it happen: 
        plt.plot(*zip(X[j], X[i]), color='black')  #!!



In [3]:
# Use a compound data type for structured arrays
data = np.zeros(4, dtype={'names':('name', 'age', 'weight'),
                          'formats':('U10', 'i4', 'f8')})
print data, data.dtype


[(u'', 0, 0.0) (u'', 0, 0.0) (u'', 0, 0.0) (u'', 0, 0.0)] [('name', '<U10'), ('age', '<i4'), ('weight', '<f8')]

In [4]:
name = ['Alice', 'Bob', 'Cathy', 'Doug']
age = [25, 45, 37, 19]
weight = [55.0, 85.5, 68.0, 61.5]
data['name'] = name
data['age'] = age
data['weight'] = weight
print data


[(u'Alice', 25, 55.0) (u'Bob', 45, 85.5) (u'Cathy', 37, 68.0)
 (u'Doug', 19, 61.5)]

In [5]:
print data['name']
print data[0]
print data[-1]['name']
# Get names where age is under 30
print data[data['age'] < 30]['name']


[u'Alice' u'Bob' u'Cathy' u'Doug']
(u'Alice', 25, 55.0)
Doug
[u'Alice' u'Doug']

In [6]:
print np.dtype({'names':('name', 'age', 'weight'), 'formats':('U10', 'i4', 'f8')})
print np.dtype({'names':('name', 'age', 'weight'), 'formats':((np.str_, 10), int, np.float32)})
print np.dtype([('name', 'S10'), ('age', 'i4'), ('weight', 'f8')])
print np.dtype('S10,i4,f8')


[('name', '<U10'), ('age', '<i4'), ('weight', '<f8')]
[('name', 'S10'), ('age', '<i8'), ('weight', '<f4')]
[('name', 'S10'), ('age', '<i4'), ('weight', '<f8')]
[('f0', 'S10'), ('f1', '<i4'), ('f2', '<f8')]

In [9]:
tp = np.dtype([('id', 'i8'), ('mat', 'f8', (3, 3))])
X = np.zeros(1, dtype=tp)
print(X[0])
print(X['mat'][0])


(0, [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]])
[[ 0.  0.  0.]
 [ 0.  0.  0.]
 [ 0.  0.  0.]]

In [ ]: