2.3. Array Generate

2.3.1. SetUp

>>> import numpy as np

2.3.2. Zeros

>>> np.zeros((2, 3))
array([[0., 0., 0.],
       [0., 0., 0.]])
>>>
>>> np.zeros(shape=(2, 3))
array([[0., 0., 0.],
       [0., 0., 0.]])
>>> a = np.array([[1, 2, 3],
...               [4, 5, 6]])
>>>
>>> np.zeros_like(a)
array([[0, 0, 0],
       [0, 0, 0]])
>>> a = np.array([[1, 2, 3],
...               [4, 5, 6]], float)
>>>
>>> np.zeros_like(a)
array([[0., 0., 0.],
       [0., 0., 0.]])

2.3.3. Ones

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

2.3.4. Empty

  • Garbage from memory

  • Will reuse previous if given shape was already created

>>> 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]])
>>> a = np.array([[1, 2, 3],
...               [4, 5, 6]])
>>>
>>> np.empty((2,3))
array([[1., 2., 3.],
       [4., 5., 6.]])
>>> a = np.array([[1, 2, 3],
...               [4, 5, 6]])
>>>
>>> np.empty_like(a)
array([[1, 2, 3],
       [4, 5, 6]])

2.3.5. Full

>>> np.full((2, 2), np.inf)
array([[inf, inf],
       [inf, inf]])
>>>
>>> np.full((2, 2), 10)
array([[10, 10],
       [10, 10]])

2.3.6. Identity

>>> 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.3.7. Recap

>>> a = np.zeros(shape=(2,3))
>>> b = np.zeros_like(a)
>>> c = np.ones(shape=(2,3))
>>> d = np.ones_like(a)
>>> e = np.empty(shape=(2,3))
>>> f = np.empty_like(a)
>>> g = np.full(shape=(2, 2), fill_value=np.nan)
>>> h = np.full_like(a, np.nan)
>>> i = np.identity(4)

2.3.8. References

1

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

2.3.9. Assignments

Code 2.33. 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 = ...