pytutorial/numpy/concatenate
David Rotermund 85c0c01268
Update README.md
Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com>
2023-12-29 18:23:17 +01:00
..
README.md Update README.md 2023-12-29 18:23:17 +01:00

Concatenate

{:.no_toc}

* TOC {:toc}

The goal

Questions to David Rotermund

numpy.concatenate

numpy.concatenate((a1, a2, ...), axis=0, out=None, dtype=None, casting="same_kind")

Join a sequence of arrays along an existing axis.

import numpy as np

a = np.arange(0, 5)
print(a)  # -> [0 1 2 3 4]
print(a.shape)  # -> (5,)
b = np.arange(0, 8)
print(b)  # -> [0 1 2 3 4 5 6 7]
print(b.shape)  # -> (8,)

c = np.concatenate((a, b))
print(c)  # -> [0 1 2 3 4 0 1 2 3 4 5 6 7]
print(c.shape)  # -> (13,)
print(np.may_share_memory(a, c))  # -> False (Copy)

c = np.concatenate((a, b), axis=0)
print(c)  # -> [0 1 2 3 4 0 1 2 3 4 5 6 7]
print(c.shape)  # -> (13,)
print(np.may_share_memory(a, c))  # -> False (Copy)

c = np.concatenate(
    (a, b), axis=1
)  # AxisError: axis 1 is out of bounds for array of dimension 1

concatenate does not add necessary dimensions, you have to do that yourself:

import numpy as np

a = np.arange(0, 10)
print(a.shape)  # -> (10,)
b = np.arange(0, 10)
print(b.shape)  # -> (10,)

c = np.concatenate((a, b), axis=0)
print(c.shape)  # -> (20,)

c = np.concatenate((a, b), axis=1)  # AxisError: axis 1 is out of bounds for array of dimension 1
import numpy as np

a = np.arange(0, 10)[:, np.newaxis]
print(a.shape)  # -> (10,1)
b = np.arange(0, 10)[:, np.newaxis]
print(b.shape)  # -> (10,1)

c = np.concatenate((a, b), axis=0)
print(c.shape)  # -> (20,1)

c = np.concatenate((a, b), axis=1)
print(c)
print(c.shape)  # -> (10,2)

Output:

[[0 0]
 [1 1]
 [2 2]
 [3 3]
 [4 4]
 [5 5]
 [6 6]
 [7 7]
 [8 8]
 [9 9]]

numpy.r_

{: .topic-optional} This is an optional topic!

numpy.r_ = <numpy.lib.index_tricks.RClass object>

Translates slice objects to concatenation along the first axis.

import numpy as np

a = np.arange(0, 10)
print(a.shape)  # -> (10,)
b = np.arange(0, 10)
print(b.shape)  # -> (10,)

c = np.r_[a, b]
print(c.shape)  # -> (20,)

numpy.c_

{: .topic-optional} This is an optional topic!

numpy.c_ = <numpy.lib.index_tricks.CClass object>