11  Basics

NumPy is commonly imported as np.

import numpy as np

11.1 .array(): create an ndarray from a list

a = np.array([1, 3, 5, 9])
a
array([1, 3, 5, 9])

11.2 .size: get length of array

a.size
4

11.3 .shape: get dimensions of array

a.shape
(4,)

11.4 np.arange(): create an ndarray using a range

c = np.arange(1, 10)
c
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
c = np.arange(1, 10, 2)
c
array([1, 3, 5, 7, 9])
c = np.arange(10, 0, -3)
c
array([10,  7,  4,  1])

11.5 ND arrays

You can seperate rows of vectors by commas to build a multidimensional array:

b = np.array([[1, 3, 5], [9, 11, 21]])
b
b.shape
b.size
6

11.6 .reshape() array to N-dimensions

Using reshape() is an easy way to form ND arrays

c = np.arange(1, 21).reshape(5, 4)
c
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12],
       [13, 14, 15, 16],
       [17, 18, 19, 20]])
d = np.arange(1, 61).reshape(3, 5, 4)
d
array([[[ 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]]])

11.7 np.linspace(): ndarray to span a linear space

c = np.linspace(11, 100, num=20)
c
array([ 11.        ,  15.68421053,  20.36842105,  25.05263158,
        29.73684211,  34.42105263,  39.10526316,  43.78947368,
        48.47368421,  53.15789474,  57.84210526,  62.52631579,
        67.21052632,  71.89473684,  76.57894737,  81.26315789,
        85.94736842,  90.63157895,  95.31578947, 100.        ])

11.8 Initialize an ndarray

11.9 np.zeros() initialize array with zeros

d = np.zeros([5, 3])
d
array([[0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.]])

11.10 np.ones(): initialize array with ones

d = np.ones([3, 5, 7])
d
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.]]])

11.11 np.empty(): initialize “empty” array

np.empty() Initializes an array with random contents of memory is fastest - therefore it is not at all empty.

d = np.empty([4, 12])
d
array([[2.31584178e+077, 2.31584178e+077, 1.18575755e-322,
        0.00000000e+000, 0.00000000e+000, 0.00000000e+000,
        0.00000000e+000, 0.00000000e+000, 0.00000000e+000,
        0.00000000e+000, 0.00000000e+000, 0.00000000e+000],
       [0.00000000e+000, 0.00000000e+000, 0.00000000e+000,
        1.13001397e-308, 0.00000000e+000, 0.00000000e+000,
        0.00000000e+000, 0.00000000e+000, 0.00000000e+000,
        0.00000000e+000, 0.00000000e+000, 0.00000000e+000],
       [0.00000000e+000, 0.00000000e+000, 0.00000000e+000,
        0.00000000e+000, 0.00000000e+000, 0.00000000e+000,
        0.00000000e+000, 2.22507490e-308, 2.31584178e+077,
        2.31584178e+077, 3.95252517e-323, 0.00000000e+000],
       [0.00000000e+000, 1.50008929e+248, 4.31174539e-096,
        9.80058441e+252, 1.23971686e+224, 1.05162486e-153,
        9.03292329e+271, 9.08366793e+223, 1.06244660e-153,
        3.44981369e+175, 2.38672185e+077, 3.33761094e-308]])

11.12 .argmax(): get position of max value

a
a.argmax()
3

11.13 Indexing

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

Indexing is 0-based and excludes the last index.

So, 0:3 means “from 1 to 4 but forget about 4”.

a[0:3]
array([1, 2, 3])

: on the right means “to the end”

a[2:]
array([ 3,  4,  5,  6,  7,  8,  9, 10])

: on the left means “from beginning to”

The last element is again excluded

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

11.13.1 Negative indexing gives the last n elements

a[-3:]
array([ 8,  9, 10])

11.14 Resources