w3resource

NumPy: Insert a new axis at the beginning in two arrays and combine the two into one

NumPy: Array Object Exercise-194 with Solution

Write a NumPy program to create two arrays with shape (300,400, 5), fill values using unsigned integer (0 to 255). Insert a new axis that will appear at the beginning in the expanded array shape. Now combine the said two arrays into one.

Sample Solution:

Python Code:

import numpy as np 
nums1 = np.random.randint(low=0, high=256, size=(200, 300, 3), dtype=np.uint8)
nums2 = np.random.randint(low=0, high=256, size=(200, 300, 3), dtype=np.uint8)
print("Array1:") 
print(nums1) 
print("\nArray2:") 
print(nums2) 
nums1 = np.expand_dims(nums1, axis=0)
nums2 = np.expand_dims(nums2, axis=0)
nums = np.append(nums1, nums2, axis=0)
print("\nCombined array:")
print(nums)

Sample Output:

Array1:
[[[ 46 117  73]
  [215  90  86]
  [ 80  89 220]
  ...
  [ 47  94 234]
  [ 95  72  61]
  [154  91 175]]

 [[232 194  26]
  [219 116 116]
  [126 179 177]
  ...
  [247 216  60]
  [ 21  38  31]
  [187 117  92]]

 [[250 162 194]
  [111 157 112]
  [120  38  90]
  ...
  [195  45  31]
  [108  73  76]
  [176 189  76]]

 ...

 [[  5  98 255]
  [155  90 230]
  [231  74   8]
  ...
  [144  87  53]
  [242 204 189]
  [ 89  18  75]]

 [[ 18 169  99]
  [183   0 215]
  [ 10 141 208]
  ...
  [ 58 130 180]
  [225  47 173]
  [  6  64 215]]

 [[199 139  25]
  [207  63  18]
  [  0 163 176]
  ...
  [ 91  25 210]
  [ 10  31 201]
  [  2 116  82]]]

Array2:
[[[ 37  10 228]
  [241  52  82]
  [196  47 189]
  ...
  [127  70 246]
  [158  46  12]
  [ 56 129 162]]

 [[224 215  47]
  [139  72  13]
  [218  64  78]
  ...
  [171 108  55]
  [197  49  94]
  [ 24 108  48]]

 [[200 136  23]
  [160 248 212]
  [217 150 183]
  ...
  [216 217 159]
  [253 190  27]
  [130  79 140]]

 ...

 [[ 61  96 157]
  [ 19 201  53]
  [ 62 105 156]
  ...
  [ 24  13  37]
  [ 92  27 157]
  [106 109 164]]

 [[241  75  33]
  [210 118  79]
  [ 59 227 193]
  ...
  [208 187  55]
  [109 115 254]
  [115 134  68]]

 [[158 106 165]
  [189  48  54]
  [  5  84  26]
  ...
  [131 235  25]
  [101 146  90]
  [216  15  92]]]

Combined array:
[[[[ 46 117  73]
   [215  90  86]
   [ 80  89 220]
   ...
   [ 47  94 234]
   [ 95  72  61]
   [154  91 175]]

  [[232 194  26]
   [219 116 116]
   [126 179 177]
   ...
   [247 216  60]
   [ 21  38  31]
   [187 117  92]]

  [[250 162 194]
   [111 157 112]
   [120  38  90]
   ...
   [195  45  31]
   [108  73  76]
   [176 189  76]]

  ...

  [[  5  98 255]
   [155  90 230]
   [231  74   8]
   ...
   [144  87  53]
   [242 204 189]
   [ 89  18  75]]

  [[ 18 169  99]
   [183   0 215]
   [ 10 141 208]
   ...
   [ 58 130 180]
   [225  47 173]
   [  6  64 215]]

  [[199 139  25]
   [207  63  18]
   [  0 163 176]
   ...
   [ 91  25 210]
   [ 10  31 201]
   [  2 116  82]]]


 [[[ 37  10 228]
   [241  52  82]
   [196  47 189]
   ...
   [127  70 246]
   [158  46  12]
   [ 56 129 162]]

  [[224 215  47]
   [139  72  13]
   [218  64  78]
   ...
   [171 108  55]
   [197  49  94]
   [ 24 108  48]]

  [[200 136  23]
   [160 248 212]
   [217 150 183]
   ...
   [216 217 159]
   [253 190  27]
   [130  79 140]]

  ...

  [[ 61  96 157]
   [ 19 201  53]
   [ 62 105 156]
   ...
   [ 24  13  37]
   [ 92  27 157]
   [106 109 164]]

  [[241  75  33]
   [210 118  79]
   [ 59 227 193]
   ...
   [208 187  55]
   [109 115 254]
   [115 134  68]]

  [[158 106 165]
   [189  48  54]
   [  5  84  26]
   ...
   [131 235  25]
   [101 146  90]
   [216  15  92]]]]

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Numpy program to test whether numpy array is faster than Python list or not.
Next: Write a NumPy program to remove the first dimension from a given array of shape (1,3,4).

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

List comprehension:

>>> m = [x ** 2 for x in range(5)]
>>> m
[0, 1, 4, 9, 16]