NumPy v1.13

NumPy v1.13 - numpy.concatenate

numpy.concatenate
numpy.concatenate((a1, a2, ...), axis=0)
        Join a sequence of arrays along an existing axis.

Parameters:
a1, a2, ... : sequence of array_like
        The arrays must have the same shape, except in the dimension corresponding to axis (the first, by default).
axis : int, optional
        The axis along which the arrays will be joined. Default is 0.

Returns:
res : ndarray
        The concatenated array.

Notes

        When one or more of the arrays to be concatenated is a MaskedArray, this function will return a MaskedArray object instead of an ndarray, but the input masks are not preserved. In cases where a MaskedArray is expected as input, use the ma.concatenate function from the masked array module instead.


example 1

# example 1
import numpy as np

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

concate_ab0 = np.concatenate((a, b), axis=0)
concate_abt1 = np.concatenate((a, b.T), axis=1)

print "a.shape: ", a.shape
print a
print "b.shape: ", b.shape
print b
print "concate_ab0.shape: ", concate_ab0.shape
print concate_ab0
print "concate_abt1.shape: ", concate_abt1.shape
print concate_abt1

=>

/usr/bin/python2.7 /home/strong/PycharmProjects/crash_course/numpy_function/numpy_concatenate.py
a.shape:  (2, 2)
[[1 2]
 [3 4]]
b.shape:  (1, 2)
[[5 6]]
concate_ab0.shape:  (3, 2)
[[1 2]
 [3 4]
 [5 6]]
concate_abt1.shape:  (2, 3)
[[1 2 5]
 [3 4 6]]

Process finished with exit code 0


example 2

import numpy as np

a = np.array([[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]]])
b = np.array([[[13, 14], [15, 16], [17, 18], [19, 20], [21, 22], [23, 24]]])

concate_ab0 = np.concatenate((a, b), axis=0)
concate_ab1 = np.concatenate((a, b), axis=1)
concate_ab2 = np.concatenate((a, b), axis=2)

print "a.shape: ", a.shape
print a
print "b.shape: ", b.shape
print b
print "concate_ab0.shape: ", concate_ab0.shape
print concate_ab0
print "concate_ab1.shape: ", concate_ab1.shape
print concate_ab1
print "concate_ab2.shape: ", concate_ab2.shape
print concate_ab2

=>

/usr/bin/python2.7 /home/strong/PycharmProjects/crash_course/numpy_function/numpy_concatenate.py
a.shape:  (1, 6, 2)
[[[ 1  2]
  [ 3  4]
  [ 5  6]
  [ 7  8]
  [ 9 10]
  [11 12]]]
b.shape:  (1, 6, 2)
[[[13 14]
  [15 16]
  [17 18]
  [19 20]
  [21 22]
  [23 24]]]
concate_ab0.shape:  (2, 6, 2)
[[[ 1  2]
  [ 3  4]
  [ 5  6]
  [ 7  8]
  [ 9 10]
  [11 12]]

 [[13 14]
  [15 16]
  [17 18]
  [19 20]
  [21 22]
  [23 24]]]
concate_ab1.shape:  (1, 12, 2)
[[[ 1  2]
  [ 3  4]
  [ 5  6]
  [ 7  8]
  [ 9 10]
  [11 12]
  [13 14]
  [15 16]
  [17 18]
  [19 20]
  [21 22]
  [23 24]]]
concate_ab2.shape:  (1, 6, 4)
[[[ 1  2 13 14]
  [ 3  4 15 16]
  [ 5  6 17 18]
  [ 7  8 19 20]
  [ 9 10 21 22]
  [11 12 23 24]]]

Process finished with exit code 0

example 3

[email protected]:~$ python
Python 2.7.12 (default, Nov 20 2017, 18:23:56) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> a = np.ma.arange(3)
>>> a
masked_array(data = [0 1 2],
             mask = False,
       fill_value = 999999)

>>> a[1] = np.ma.masked
>>> a
masked_array(data = [0 -- 2],
             mask = [False  True False],
       fill_value = 999999)

>>> b = np.arange(2, 5)
>>> b
array([2, 3, 4])
>>> np.concatenate([a, b])
masked_array(data = [0 1 2 2 3 4],
             mask = False,
       fill_value = 999999)

>>> np.ma.concatenate([a, b])
masked_array(data = [0 -- 2 2 3 4],
             mask = [False  True False False False False],
       fill_value = 999999)

>>> exit()
[email protected]:~$ 




;