NumPy: Insert a new axis at the beginning in two arrays and combine the two into one
NumPy: Array Object Exercise-194 with Solution
Combine two arrays into one after inserting an axis.
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:
# Importing NumPy library
import numpy as np
# Generating random arrays of integers from 0 to 255 with dimensions (200, 300, 3)
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)
# Printing the original arrays
print("Array1:")
print(nums1)
print("\nArray2:")
print(nums2)
# Adding a new axis to the arrays along the 0th dimension
nums1 = np.expand_dims(nums1, axis=0)
nums2 = np.expand_dims(nums2, axis=0)
# Appending the arrays along the 0th axis
nums = np.append(nums1, nums2, axis=0)
# Printing the combined array
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]]]]
Explanation:
In the above code -
nums1 and nums2: Create two random (200, 300, 3) arrays with integer values between 0 and 255, using the np.random.randint function. The dtype is set to np.uint8 to represent 8-bit color channels.
np.expand_dims(nums1, axis=0) and np.expand_dims(nums2, axis=0): Add an extra dimension to each of the arrays, nums1 and nums2, along the 0th axis. This converts their shapes from (200, 300, 3) to (1, 200, 300, 3).
np.append(nums1, nums2, axis=0): Concatenate the two arrays with expanded dimensions along the 0th axis. This creates a new array “nums” with the shape (2, 200, 300, 3), containing both images.
print(nums): Finally print() function prints the resulting array "nums".
Python-Numpy Code Editor:
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 Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/python-exercises/numpy/python-numpy-exercise-194.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics