2.2. Array Range

2.2.1. SetUp

>>> import numpy as np

2.2.2. Array range

Array from Python range():

>>> 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:

>>> 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():

>>> 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.2.3. Linspace

>>> 
... def linspace(self,
...              start=...,
...              stop=...,
...              num=50,
...              endpoint=True,
...              retstep=False,
...              dtype=None
...              axis=0
... ) -> np.ndarray: ...

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])
>>> data, step = np.linspace(2.0, 3.0, num=5, retstep=True)
>>>
>>> data
array([2.  , 2.25, 2.5 , 2.75, 3.  ])
>>>
>>> step
0.25

2.2.4. Recap

>>> a = np.array(range(0, 10))
>>> b = np.arange(0, 10, 2)
>>> c = np.linspace(0, 10, 100)

2.2.5. Assignments

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