Skip to content

Numpy

In [1]:
import numpy as np
In [2]:
x = np.arange(10)
In [3]:
x
Out[3]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [4]:
x + 1
Out[4]:
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
In [5]:
(x + 1)**2
Out[5]:
array([  1,   4,   9,  16,  25,  36,  49,  64,  81, 100])
In [6]:
np.sin(x)
Out[6]:
array([ 0.        ,  0.84147098,  0.90929743,  0.14112001, -0.7568025 ,
       -0.95892427, -0.2794155 ,  0.6569866 ,  0.98935825,  0.41211849])
In [7]:
np.sin(x).dtype
Out[7]:
dtype('float64')
In [8]:
x > 3
Out[8]:
array([False, False, False, False,  True,  True,  True,  True,  True,
        True])
In [9]:
x[:4]
Out[9]:
array([0, 1, 2, 3])
In [10]:
x[7:]
Out[10]:
array([7, 8, 9])
In [11]:
x[x > 3]
Out[11]:
array([4, 5, 6, 7, 8, 9])
In [12]:
x[(x > 3) & (x < 7)]
Out[12]:
array([4, 5, 6])
In [13]:
x[(x > 7) | (x < 3)]
Out[13]:
array([0, 1, 2, 8, 9])
In [14]:
np.arange(50)
Out[14]:
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])
In [15]:
np.arange(50).reshape(10,5)
Out[15]:
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]])
In [16]:
table = _
In [17]:
table
Out[17]:
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]])
In [18]:
table**2
Out[18]:
array([[   0,    1,    4,    9,   16],
       [  25,   36,   49,   64,   81],
       [ 100,  121,  144,  169,  196],
       [ 225,  256,  289,  324,  361],
       [ 400,  441,  484,  529,  576],
       [ 625,  676,  729,  784,  841],
       [ 900,  961, 1024, 1089, 1156],
       [1225, 1296, 1369, 1444, 1521],
       [1600, 1681, 1764, 1849, 1936],
       [2025, 2116, 2209, 2304, 2401]])
In [19]:
(table**2)[2:4]
Out[19]:
array([[100, 121, 144, 169, 196],
       [225, 256, 289, 324, 361]])
In [20]:
(table**2)[:,2:4]
Out[20]:
array([[   4,    9],
       [  49,   64],
       [ 144,  169],
       [ 289,  324],
       [ 484,  529],
       [ 729,  784],
       [1024, 1089],
       [1369, 1444],
       [1764, 1849],
       [2209, 2304]])
In [21]:
(table**2)[5:7, 2:4]
Out[21]:
array([[ 729,  784],
       [1024, 1089]])
In [22]:
table ** 2 == 1089
Out[22]:
array([[False, False, False, False, False],
       [False, False, False, False, False],
       [False, False, False, False, False],
       [False, False, False, False, False],
       [False, False, False, False, False],
       [False, False, False, False, False],
       [False, False, False,  True, False],
       [False, False, False, False, False],
       [False, False, False, False, False],
       [False, False, False, False, False]])
In [23]:
[index for (index, value) in np.ndenumerate(table**2) if value == 1089]
Out[23]:
[(6, 3)]
In [24]:
l = list(range(10))
In [25]:
l
Out[25]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [26]:
[i * 2 for i in l if i < 5]
Out[26]:
[0, 2, 4, 6, 8]
In [27]:
table**100
Out[27]:
array([[                   0,                    1,                    0,
        -2984622845537545263,                    0],
       [-3842938066129721103,                    0,  3728452490685454945,
                           0,  6627890308811632801],
       [                   0, -5485011738861510223,                    0,
        -8267457965844590319,                    0],
       [ 6813754833676406721,                    0, -1895149777787118527,
                           0,  8207815121025376913],
       [                   0,  1178643571979107377,                    0,
         5958518545374539809,                    0],
       [-7817535966050405663,                    0,  4157753088978724465,
                           0,  3054346751387081297],
       [                   0,  6365139678740040577,                    0,
        -8877898876394183551,                    0],
       [ 8170176069297290577,                    0, -1901415581956121743,
                           0,  2024094702548431329],
       [                   0,  6476859561917718817,                    0,
         5633018509028505393,                    0],
       [ 3548161959473065873,                    0, -2387541571489615039,
                           0,  1459632558914132161]])
In [28]:
a = np.mat('4 3; 2 1')
In [29]:
b = np.mat('1 2; 3 4')
In [30]:
a**2
Out[30]:
matrix([[22, 15],
        [10,  7]])
In [31]:
a
Out[31]:
matrix([[4, 3],
        [2, 1]])
In [32]:
a*b
Out[32]:
matrix([[13, 20],
        [ 5,  8]])
In [33]:
(np.arange(4)+1).reshape(2,2)
Out[33]:
array([[1, 2],
       [3, 4]])
In [34]:
np.mat(_)
Out[34]:
matrix([[1, 2],
        [3, 4]])