# 2.1. Array Create¶

## 2.1.1. SetUp¶

>>> import numpy as np


## 2.1.2. Example¶

• ndarray - n-dimensional array

>>> a = np.array([1, 2, 3])
>>>
>>> type(a)
<class 'numpy.ndarray'>


## 2.1.3. From List¶

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


## 2.1.4. From Range¶

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

data range(0, 10) list(data) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

## 2.1.5. Declare¶

1-dimensional Array:

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

>>> 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]]])


## 2.1.6. Stringify¶

>>> 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.7. Performance¶

• Python 3.11.4

Pure Python:

>>>
... %%timeit -n 1000 -r 1000
... data = range(0, 10)
... result = list(data)
279 ns ± 102 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)

>>>
... %%timeit -n 1000 -r 1000
... result = [x for x in range(0, 10)]
520 ns ± 201 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)


Python + Numpy:

>>>
... %%timeit -n 1000 -r 1000
... data = range(0, 10)
... result = np.array(data)
2.34 µs ± 249 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)

>>>
... %%timeit -n 1000 -r 1000
... result = np.array(range(0, 10))
2.46 µs ± 359 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)


Pure Numpy:

>>>
... %%timeit -n 1000 -r 1000
... result = np.arange(0, 10)
559 ns ± 189 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)


## 2.1.8. Recap¶

>>> a = np.array([1, 2, 3])
>>> b = np.array(range(0, 10))


## 2.1.10. Assignments¶

"""
* Assignment: Numpy Create Array
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min

English:
1. Define result: np.ndarray:
a. dtype: do not change, leave default
b. values: from 0 to 10 (without 10)
c. use: np.array()
2. Run doctests - all must succeed

Polish:
1. Zdefiniuj result: np.ndarray:
a. dtype: nie zmieniaj, pozostaw domyślny
b. wartości: od 0 do 10 (bez 10)
c. użyj: np.array()
2. 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, 1, 2, 3, 4, 5, 6, 7, 8, 9])
"""

import numpy as np

# dtype: do not change, leave default
# values: from 0 to 10 (without 10)
# use: np.array()
# type: np.ndarray
result = ...


"""
* Assignment: Numpy Create ArrayDtype
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min

English:
1. Define result: np.ndarray:
a. dtype: float
b. values: from 0 to 10 (without 10)
c. use: np.array()
2. Run doctests - all must succeed

Polish:
1. Zdefiniuj result: np.ndarray:
a. dtype: float
b. wartości: od 0 do 10 (bez 10)
c. użyj: np.array()
2. 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., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
"""

import numpy as np

# dtype: float
# values: from 0 to 10 (without 10)
# use: np.array()
# type: np.ndarray
result = ...


"""
* Assignment: Numpy Create Arange
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min

English:
1. Define result: np.ndarray:
a. dtype: do not change, leave default
b. values: from 0 to 10 (without 10)
c. use: np.arange()
2. Run doctests - all must succeed

Polish:
1. Zdefiniuj result: np.ndarray:
a. dtype: nie zmieniaj, pozostaw domyślny
b. wartości: od 0 do 10 (bez 10)
c. użyj: np.arange()
2. 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, 1, 2, 3, 4, 5, 6, 7, 8, 9])
"""

import numpy as np

# dtype: do not change, leave default
# values: from 0 to 10 (without 10)
# use: np.arange()
# type: np.ndarray
result = ...


"""
* Assignment: Numpy Create ArangeStep
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min

English:
1. Define result: np.ndarray:
a. dtype: do not change, leave default
b. values: from 0 to 10 step 2 (even numbers)
c. use: np.arange()
2. Run doctests - all must succeed

Polish:
1. Zdefiniuj result: np.ndarray:
a. dtype: nie zmieniaj, pozostaw domyślny
b. wartości: od 0 do 10 krok 2 (liczby parzyste)
c. użyj: np.arange()
2. 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])
"""

import numpy as np

# dtype: do not change, leave default
# values: from 0 to 10 step 2 (even numbers)
# use: np.arange()
# type: np.ndarray
result = ...


"""
* Assignment: Numpy Create ArangeDtype
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min

English:
1. Define result: np.ndarray:
a. dtype: float
b. values: from 0 to 10 step 2 (even numbers)
c. use: np.arange()
2. Run doctests - all must succeed

Polish:
1. Zdefiniuj result: np.ndarray:
a. dtype: float
b. wartości: od 0 do 10 krok 2 (liczby parzyste)
c. użyj: np.arange()
2. 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.])
"""

import numpy as np

# dtype: float
# values: from 0 to 10 step 2 (even numbers)
# use: np.arange()
# type: np.ndarray
result = ...


"""
* Assignment: Numpy Create Linspace
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min

English:
1. Define result: np.ndarray:
a. dtype: do not change, leave default
a. values: from 0 to 10 (without 10)
b. use: np.linspace()
2. Run doctests - all must succeed

Polish:
1. Zdefiniuj result: np.ndarray:
a. dtype: nie zmieniaj, pozostaw domyślny
a. wartości: od 0 do 10 (bez 10)
b. użyj: np.linspace()
2. 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.        ,  0.20408163,  0.40816327,  0.6122449 ,  0.81632653,
1.02040816,  1.2244898 ,  1.42857143,  1.63265306,  1.83673469,
2.04081633,  2.24489796,  2.44897959,  2.65306122,  2.85714286,
3.06122449,  3.26530612,  3.46938776,  3.67346939,  3.87755102,
4.08163265,  4.28571429,  4.48979592,  4.69387755,  4.89795918,
5.10204082,  5.30612245,  5.51020408,  5.71428571,  5.91836735,
6.12244898,  6.32653061,  6.53061224,  6.73469388,  6.93877551,
7.14285714,  7.34693878,  7.55102041,  7.75510204,  7.95918367,
8.16326531,  8.36734694,  8.57142857,  8.7755102 ,  8.97959184,
9.18367347,  9.3877551 ,  9.59183673,  9.79591837, 10.        ])
"""

import numpy as np

# dtype: do not change, leave default
# values: from 0 to 10 (without 10)
# use: np.linspace()
# type: np.ndarray
result = ...


"""
* Assignment: Numpy Create LinspaceNum
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min

English:
1. Define result: np.ndarray:
a. dtype: do not change, leave default
b. values: from 0 to 10 steps 11
c. use: np.linspace()
2. Run doctests - all must succeed

Polish:
1. Zdefiniuj result: np.ndarray:
a. dtype: nie zmieniaj, pozostaw domyślny
b. wartości: od 0 do 10 krków 11
c. użyj: np.linspace()
2. 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.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])
"""

import numpy as np

# dtype: do not change, leave default
# values: from 0 to 10 steps 11
# use: np.linspace()
# type: np.ndarray
result = ...