Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

NumPy: Create a three-dimension array, fill the array elements with values using unsigned integer (0 to 255)

NumPy: Random Exercise-17 with Solution

Write a NumPy program to create a three-dimension array with shape (300,400,5) and set to a variable. Fill the array elements with values using unsigned integer (0 to 255).

Sample Solution:

Python Code:

import numpy as np   
np.random.seed(32) 
nums = np.random.randint(low=0, high=256, size=(300, 400, 5), dtype=np.uint8)
print(nums)

Sample Output:

[[[215  42 224 219  43]
  [166  69  15 133 255]
  [105  95  54  37 201]
  ...
  [240  22  66 232 132]
  [ 13  85  53 220 170]
  [249  62 221 146  69]]

 [[ 73  79 148 132 164]
  [  3  93  98 138 200]
  [174  34  31 208 130]
  ...
  [ 15 252  41  64  39]
  [188 216 223 124  27]
  [ 85 112 240 116 231]]

 [[227  50 243  20 171]
  [ 12  66 108 102  63]
  [107  54   0   0 173]
  ...
  [215  46  57  99 151]
  [243 199  31  28 179]
  [143   7  30 175 190]]

 ...

 [[214  64  64 212  62]
  [140  44 217  17 164]
  [226 146 247  53 199]
  ...
  [189  68  49 117  63]
  [ 14  17 109  82  92]
  [155 221 135 184 231]]

 [[132 194 160 136 102]
  [132 244 230 117 181]
  [146 245  21 164  29]
  ...
  [125 240 243 190 240]
  [137  41 157 117 155]
  [ 20  92  72 182  41]]

 [[120  45 198 218 190]
  [ 42 150 190 103 106]
  [164  71 220 114  59]
  ...
  [143  20 219 154  85]
  [219 190 170 227 246]
  [ 39  14 127 230 158]]]

Python-Numpy Code Editor:

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

Previous: Write a NumPy program to get the n largest values of an array.
Next: NumPy Math Exercises Home.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Python: Tips of the Day

Find current directory and file's directory:

To get the full path to the directory a Python file is contained in, write this in that file:

import os 
dir_path = os.path.dirname(os.path.realpath(__file__))

(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)

To get the current working directory use

import os
cwd = os.getcwd()

Documentation references for the modules, constants and functions used above:

  • The os and os.path modules.
  • The __file__ constant
  • os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
  • os.path.dirname(path) (returns "the directory name of pathname path")
  • os.getcwd() (returns "a string representing the current working directory")
  • os.chdir(path) ("change the current working directory to path")

Ref: https://bit.ly/3fy0R6m