2.13. Array Concatenation

2.13.1. 1-dimensional Array

import numpy as np


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

np.concatenate(a, b)
# Traceback (most recent call last):
# TypeError: only integer scalar arrays can be converted to a scalar index

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


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

np.concatenate((a, b, c))
# array([1, 2, 3, 4, 5, 6, 7, 8, 9])
import numpy as np


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

np.concatenate((a, b))
# array([1, 2, 3, 2, 3, 4])

2.13.2. 2-dimensional Array

import numpy as np


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

np.concatenate((a, b))
# array([[1, 2],
#        [3, 4],
#        [5, 6],
#        [7, 8]])

np.concatenate((a, b), axis=0)
# array([[1, 2],
#        [3, 4],
#        [5, 6],
#        [7, 8]])

np.concatenate((a, b), axis=1)
# array([[1, 2, 5, 6],
#        [3, 4, 7, 8]])

2.13.3. Assignments

Code 2.49. Solution
"""
* Assignment: Numpy Concatenation
* Complexity: easy
* Lines of code: 1 lines
* Time: 3 min

English:
    1. Given are one-dimensional: `a: np.ndarray`, `b: np.ndarray`
    2. Concatenate them as `result: np.ndarray`
    3. Reshape `result` into two rows and three columns
    4. Run doctests - all must succeed

Polish:
    1. Dane są jednowymiarowe: `a: np.ndarray`, `b: np.ndarray`
    2. Połącz je ze sobą jako `result: np.ndarray`
    3. Przekształć `result` w dwa wiersze na trzy kolumny
    4. Uruchom doctesty - wszystkie muszą się powieść

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

    >>> type(result) is np.ndarray
    True
    >>> result
    array([[1, 2, 3],
           [4, 5, 6]])
"""

import numpy as np

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

result = ...