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.
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]]]]
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
need an explanation for this answer? contact us directly to get an explanation for this answer