当前位置:   article > 正文

NumPy v1.13 - numpy.concatenate_numpy 1.13

numpy 1.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

  1. # example 1
  2. import numpy as np
  3. a = np.array([[1, 2], [3, 4]])
  4. b = np.array([[5, 6]])
  5. concate_ab0 = np.concatenate((a, b), axis=0)
  6. concate_abt1 = np.concatenate((a, b.T), axis=1)
  7. print "a.shape: ", a.shape
  8. print a
  9. print "b.shape: ", b.shape
  10. print b
  11. print "concate_ab0.shape: ", concate_ab0.shape
  12. print concate_ab0
  13. print "concate_abt1.shape: ", concate_abt1.shape
  14. print concate_abt1

=>

  1. /usr/bin/python2.7 /home/strong/PycharmProjects/crash_course/numpy_function/numpy_concatenate.py
  2. a.shape: (2, 2)
  3. [[1 2]
  4. [3 4]]
  5. b.shape: (1, 2)
  6. [[5 6]]
  7. concate_ab0.shape: (3, 2)
  8. [[1 2]
  9. [3 4]
  10. [5 6]]
  11. concate_abt1.shape: (2, 3)
  12. [[1 2 5]
  13. [3 4 6]]
  14. Process finished with exit code 0


example 2

  1. import numpy as np
  2. a = np.array([[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]]])
  3. b = np.array([[[13, 14], [15, 16], [17, 18], [19, 20], [21, 22], [23, 24]]])
  4. concate_ab0 = np.concatenate((a, b), axis=0)
  5. concate_ab1 = np.concatenate((a, b), axis=1)
  6. concate_ab2 = np.concatenate((a, b), axis=2)
  7. print "a.shape: ", a.shape
  8. print a
  9. print "b.shape: ", b.shape
  10. print b
  11. print "concate_ab0.shape: ", concate_ab0.shape
  12. print concate_ab0
  13. print "concate_ab1.shape: ", concate_ab1.shape
  14. print concate_ab1
  15. print "concate_ab2.shape: ", concate_ab2.shape
  16. print concate_ab2

=>

  1. /usr/bin/python2.7 /home/strong/PycharmProjects/crash_course/numpy_function/numpy_concatenate.py
  2. a.shape: (1, 6, 2)
  3. [[[ 1 2]
  4. [ 3 4]
  5. [ 5 6]
  6. [ 7 8]
  7. [ 9 10]
  8. [11 12]]]
  9. b.shape: (1, 6, 2)
  10. [[[13 14]
  11. [15 16]
  12. [17 18]
  13. [19 20]
  14. [21 22]
  15. [23 24]]]
  16. concate_ab0.shape: (2, 6, 2)
  17. [[[ 1 2]
  18. [ 3 4]
  19. [ 5 6]
  20. [ 7 8]
  21. [ 9 10]
  22. [11 12]]
  23. [[13 14]
  24. [15 16]
  25. [17 18]
  26. [19 20]
  27. [21 22]
  28. [23 24]]]
  29. concate_ab1.shape: (1, 12, 2)
  30. [[[ 1 2]
  31. [ 3 4]
  32. [ 5 6]
  33. [ 7 8]
  34. [ 9 10]
  35. [11 12]
  36. [13 14]
  37. [15 16]
  38. [17 18]
  39. [19 20]
  40. [21 22]
  41. [23 24]]]
  42. concate_ab2.shape: (1, 6, 4)
  43. [[[ 1 2 13 14]
  44. [ 3 4 15 16]
  45. [ 5 6 17 18]
  46. [ 7 8 19 20]
  47. [ 9 10 21 22]
  48. [11 12 23 24]]]
  49. Process finished with exit code 0

example 3

  1. strong@foreverstrong:~$ python
  2. Python 2.7.12 (default, Nov 20 2017, 18:23:56)
  3. [GCC 5.4.0 20160609] on linux2
  4. Type "help", "copyright", "credits" or "license" for more information.
  5. >>> import numpy as np
  6. >>> a = np.ma.arange(3)
  7. >>> a
  8. masked_array(data = [0 1 2],
  9. mask = False,
  10. fill_value = 999999)
  11. >>> a[1] = np.ma.masked
  12. >>> a
  13. masked_array(data = [0 -- 2],
  14. mask = [False True False],
  15. fill_value = 999999)
  16. >>> b = np.arange(2, 5)
  17. >>> b
  18. array([2, 3, 4])
  19. >>> np.concatenate([a, b])
  20. masked_array(data = [0 1 2 2 3 4],
  21. mask = False,
  22. fill_value = 999999)
  23. >>> np.ma.concatenate([a, b])
  24. masked_array(data = [0 -- 2 2 3 4],
  25. mask = [False True False False False False],
  26. fill_value = 999999)
  27. >>> exit()
  28. strong@foreverstrong:~$




声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/700937
推荐阅读
相关标签
  

闽ICP备14008679号