Flash Sale: Flat 70% + Addl. 25% Off on all Courses | Use Coupon DS25 in Checkout | Offer Expires In

  Enroll Now

NumPy - Arrays - Special Types of Arrays - Array filled with specific value

In addition to the normal NumPy arrays that we created in the previous chapter, there are few special NumPy arrays that you can create.

For example:

  1. NumPy array with all its elements values as zero (using zeros() function of NumPy)
  2. NumPy array with all its elements values as one (using ones() function of NumPy)
  3. NumPy array with all its elements values as a specific given value (using full() function of NumPy)
  4. Identity matrix or array
INSTRUCTIONS

Note: Please import numpy as np

(1) Creating NumPy array with all its elements values as zero (using zeros() function of NumPy)

  • Create a tuple indicating dimensions of the desired array

    tup_dim = (3, 4)
    
  • Now, create your NumPy array by passing the above tuple (tup_dim) to zeros() function below

    my_zeros_array = np.zeros(<<your code comes here>>, dtype=np.int16)
    

(2) Creating NumPy array with all its elements values as one (using ones() function of NumPy)

  • Create a tuple indicating dimensions of the desired array

    tup_dim = (3, 4)
    
  • Now, create your NumPy array by passing the above tuple (tup_dim) to ones() function below

    my_ones_array = np.ones( <<your code comes here>>, dtype=np.int16)
    

(3) Creating NumPy array with all its elements values as a specific given value (using full() function of NumPy)

  • Create a tuple indicating dimensions of the desired array

    tup_dim = (3, 4)
    
  • Now, create your NumPy array by passing the above tuple (tup_dim) to full() function below, along with the desired value that you want the array to be filled with (e.g. say value 7)

    my_seven_array = np.full( <<your code comes here>>, 7, dtype=np.int16)
    

The above NumPy array my_seven_array will be filled with value 7.

(4) Creating Identity matrix or array

Identity matrix (array) is a square matrix with all its elements as zero (0) except for the diagonal elements whose value is one (1). That is, all the main diagonal values are 1s (one).

You can create an Identity matrix by using numpy's identity() function by passing the desired dimension of the matrix (array) and the data type required.

e.g. np.identity(2, dtype=float) will create a 2x2 matrix with all its values as 0.0 except for the diagonal values which will be 1.0

  • Please create an identity matrix my_identity_array of size 4x4 with float values.

    my_identity_array = np.identity(<<your code comes here>>, dtype=np.float64)
    

You can use the print() function to view the above-created arrays.


No hints are availble for this assesment

Answer is not availble for this assesment


Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...