2.1. Array Create

2.1.1. Declare

1-dimensional Array:

import numpy as np


np.array([1, 2, 3])
# array([1, 2, 3])

np.array([1.0, 2.0, 3.0])
# array([1., 2., 3.])

np.array([1.1, 2.2, 3.3])
# array([1.1, 2.2, 3.3])

np.array([1, 2, 3], float)
# array([ 1., 2., 3.])

np.array([1, 2, 3], dtype=float)
# array([ 1., 2., 3.])

2-dimensional Array:

import numpy as np


np.array([[1, 2, 3],
          [4, 5, 6],
          [7, 8, 9]])

# array([[1, 2, 3],
#        [4, 5, 6],
#        [7, 8, 9]])

3-dimensional Array:

np.array([[[1, 2, 3],
           [4, 5, 6],
           [7, 8, 9]],

          [[1, 2, 3],
           [4, 5, 6],
           [7, 8, 9]]])

# array([[[1, 2, 3],
#         [4, 5, 6],
#         [7, 8, 9]],
#
#        [[1, 2, 3],
#         [4, 5, 6],
#         [7, 8, 9]]])
../_images/numpy-create-cake.png

Figure 2.2. Multi layer cake as an analog for n-dim array 1

2.1.2. Range

Array from Python range():

import numpy as np


np.array(range(5))
# array([0, 1, 2, 3, 4])

np.array(range(5), float)
# array([ 0., 1., 2., 3., 4.])

np.array(range(5, 10))
# array([5, 6, 7, 8, 9])

np.array(range(5, 10), float)
# array([5., 6., 7., 8., 9.])

np.array(range(5, 10, 2))
# array([5, 7, 9])

np.array(range(5, 10, 2), float)
# array([5., 7., 9.])

Array from Python comprehension:

import numpy as np


np.array([x for x in range(5)])
# array([0, 1, 2, 3, 4])

np.array([x for x in range(5)], float)
# array([ 0., 1., 2., 3., 4.])

np.array([x for x in range(5, 10)])
# array([5, 6, 7, 8, 9])

np.array([x for x in range(5, 10)], float)
# array([5., 6., 7., 8., 9.])

np.array([x for x in range(5, 10, 2)])
# array([5, 7, 9])

np.array([x for x in range(5, 10, 2)], float)
# array([5., 7., 9.])

Array from np.arange():

import numpy as np


np.arange(5)
# array([0, 1, 2, 3, 4])

np.arange(5, dtype=float)
# array([0., 1., 2., 3., 4.])

np.arange(5.0)
# array([0., 1., 2., 3., 4.])

np.arange(5, 10)
# array([5, 6, 7, 8, 9])

np.arange(5, 10, step=2)
# array([5, 7, 9])

np.arange(start=5, stop=10, step=2)
# array([5, 7, 9])

np.arange(start=5, stop=10, step=2, dtype=float)
# array([5., 7., 9.])

np.arange(0.0, 1.0, 0.1)
# array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])

np.arange(0.0, 1.0, 0.2)
# array([0. , 0.2, 0.4, 0.6, 0.8])

np.arange(0.0, 1.0, 0.3)
# array([0. , 0.3, 0.6, 0.9])

2.1.3. Linspace

  • np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)

  • Return evenly spaced numbers over a specified interval.

np.linspace(2.0, 3.0, num=5)
# array([2.  , 2.25, 2.5 , 2.75, 3.  ])

np.linspace(2.0, 3.0, num=5, endpoint=False)
# array([2. ,  2.2,  2.4,  2.6,  2.8])

np.linspace(2.0, 3.0, num=5, retstep=True)
# (array([2.  ,  2.25,  2.5 ,  2.75,  3.  ]), 0.25)

2.1.4. Zeros

import numpy as np


np.zeros((2, 3))
# array([[0., 0., 0.],
#       [0., 0., 0.]])

np.zeros(shape=(2, 3))
# array([[0., 0., 0.],
#        [0., 0., 0.]])
import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]])

np.zeros_like(a)
# array([[0, 0, 0],
#        [0, 0, 0]])
import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]], float)

np.zeros_like(a)
# array([[0., 0., 0.],
#        [0., 0., 0.]])

2.1.5. Ones

import numpy as np


np.ones((3, 2))
# array([[1., 1.],
#        [1., 1.],
#        [1., 1.]])

np.ones(shape=(3, 2))
# array([[1., 1.],
#        [1., 1.],
#        [1., 1.]])
import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]])

np.ones_like(a)
# array([[1, 1, 1],
#        [1, 1, 1]])
import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]], float)

np.ones_like(a)
# array([[1., 1., 1.],
#        [1., 1., 1.]])

2.1.6. Empty

  • Garbage from memory

  • Will reuse previous if given shape was already created

import numpy as np


np.empty((3,4))
# array([[ 2.31584178e+077,  1.29073692e-231,  2.96439388e-323, 0.00000000e+000],
#       [-2.32034891e+077,  2.68678047e+154,  2.18018101e-314, 2.18022275e-314],
#       [ 0.00000000e+000,  2.18023445e-314,  1.38338381e-322, 9.03690495e-309]])
import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]])

np.empty((2,3))
# array([[1., 2., 3.],
#        [4., 5., 6.]])
import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]])

np.empty_like(a)
# array([[1, 2, 3],
#        [4, 5, 6]])

2.1.7. Full

import numpy as np


np.full((2, 2), np.inf)
# array([[inf, inf],
#        [inf, inf]])

np.full((2, 2), 10)
# array([[10, 10],
#        [10, 10]])

2.1.8. Identity

import numpy as np


np.identity(2)
# array([[1., 0.],
#        [0., 1.]])

np.identity(3)
# array([[1., 0., 0.],
#        [0., 1., 0.],
#        [0., 0., 1.]])

np.identity(4, int)
# array([[1, 0, 0, 0],
#        [0, 1, 0, 0],
#        [0, 0, 1, 0],
#        [0, 0, 0, 1]])

2.1.9. Stringify

import numpy as np


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

str(a)
# '[[1 2 3]\n [4 5 6]\n [7 8 9]]'

print(a)
# [[1 2 3]
#  [4 5 6]
#  [7 8 9]]

repr(a)
# 'array([[1, 2, 3],\n       [4, 5, 6],\n       [7, 8, 9]])'

a
# array([[1, 2, 3],
#        [4, 5, 6],
#        [7, 8, 9]])

print(repr(a))
# array([[1, 2, 3],
#        [4, 5, 6],
#        [7, 8, 9]])

2.1.10. Performance

  • Results with %%timeit -n 1_000_000 -r 10

import numpy as np

result = np.arange(0, 100, step=2, dtype=float)
# 756 ns ± 10.3 ns per loop (mean ± std. dev. of 10 runs, 1000000 loops each)

result = np.array(range(0, 100, 2), dtype=float)
# 8.28 µs ± 364 ns per loop (mean ± std. dev. of 10 runs, 1000000 loops each)

result = np.array([x for x in range(0, 100) if x % 2 == 0], dtype=float)
# 9.76 µs ± 324 ns per loop (mean ± std. dev. of 10 runs, 1000000 loops each)

result = np.array([float(x) for x in range(0, 100) if x % 2 == 0])
# 12.7 µs ± 195 ns per loop (mean ± std. dev. of 10 runs, 1000000 loops each)

result = np.array([float(x) for x in range(0, 100, 2)])
# 8.35 µs ± 196 ns per loop (mean ± std. dev. of 10 runs, 1000000 loops each)

result = np.array([x for x in range(0, 100, 2)], dtype=float)
# 5.89 µs ± 77 ns per loop (mean ± std. dev. of 10 runs, 1000000 loops each)

2.1.11. References

1

https://i.ytimg.com/vi/iCOhz07Ng6g/maxresdefault.jpg

2.1.12. Assignments

Code 2.29. Solution
"""
* Assignment: Numpy Create Arange
* Complexity: easy
* Lines of code: 1 lines
* Time: 3 min

English:
    1. Create `result: np.ndarray` with even numbers from 0 to 100 (without 100)
    2. Numbers must be `float` type
    3. Run doctests - all must succeed

Polish:
    1. Stwórz `result: np.ndarray` z liczbami parzystymi od 0 do 100 (bez 100)
    2. Liczby muszą być typu `float`
    3. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert result is not Ellipsis, \
    'Assign result to variable: `result`'
    >>> assert type(result) is np.ndarray, \
    'Variable `result` has invalid type, expected: np.ndarray'

    >>> result
    array([ 0.,  2.,  4.,  6.,  8., 10., 12., 14., 16., 18., 20., 22., 24.,
           26., 28., 30., 32., 34., 36., 38., 40., 42., 44., 46., 48., 50.,
           52., 54., 56., 58., 60., 62., 64., 66., 68., 70., 72., 74., 76.,
           78., 80., 82., 84., 86., 88., 90., 92., 94., 96., 98.])
"""

import numpy as np


result = ...