2.3. Array Generate¶
2.3.1. SetUp¶
>>> import numpy as np
2.3.2. Zeros¶
>>> np.zeros(shape=(2, 3))
array([[0., 0., 0.],
[0., 0., 0.]])
>>> np.zeros(shape=(2, 3), dtype='int')
array([[0, 0, 0],
[0, 0, 0]])
2.3.3. Zeros Like¶
>>> 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]], dtype='float')
>>>
>>> np.zeros_like(a)
array([[0., 0., 0.],
[0., 0., 0.]])
2.3.4. Ones¶
>>> np.ones(shape=(3, 2))
array([[1., 1.],
[1., 1.],
[1., 1.]])
>>> np.ones(shape=(3, 2), dtype='int')
array([[1, 1],
[1, 1],
[1, 1]])
2.3.5. Ones Like¶
>>> 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]], dtype='float')
>>>
>>> np.ones_like(a)
array([[1., 1., 1.],
[1., 1., 1.]])
2.3.6. Empty¶
Garbage from memory
Will reuse previous if given shape was already created
>>> np.empty(shape=(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]])
Will reuse previous if given shape was already created:
>>> a = np.array([[1, 2, 3],
... [4, 5, 6]])
>>>
>>> np.empty(shape=(2,3))
array([[1., 2., 3.],
[4., 5., 6.]])
2.3.7. Empty Like¶
>>> a = np.array([[1, 2, 3],
... [4, 5, 6]])
>>>
>>> np.empty_like(a)
array([[1, 2, 3],
[4, 5, 6]])
2.3.8. Full¶
>>> np.full(shape=(2, 3), fill_value=2)
array([[2, 2, 2],
[2, 2, 2]])
2.3.9. Full Like¶
>>> a = np.array([[1, 2, 3],
... [4, 5, 6]])
>>>
>>> np.full_like(a, fill_value=2.0)
array([[2, 2, 2],
[2, 2, 2]])
2.3.10. 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, dtype='int')
array([[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]])
2.3.11. Identity Like¶
>>> a = np.array([[1, 2, 3],
... [4, 5, 6]])
...
>>> np.identity(3, like=a)
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
2.3.12. 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.13. References¶
2.3.14. 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 number of elements 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 liczba elementó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 number of elements 11
# use: `np.linspace()`
# type: np.ndarray
result = ...