Q. Using the arrays created in Question

Q. Create the following NumPy arrays:

(a) A 1-D array called zeros having 10 elements and all the elements are set to zero.

(b) A 1-D array called vowels having the elements ‘a’, ‘e’, ‘i’, ‘o’ and ‘u’.

(c) A 2-D array called ones having 2 rows and 5 columns and all the elements are set to 1 and dtype as int.

(d) Use nested Python lists to create a 2-D array called myarray1 having 3 rows and 3 columns and store the following data: 2.7, -2, -190, 3.4, 99.910.6, 0, 13

(e) A 2-D array called myarray2 using arange() having 3 rows and 5 columns with start value = 4, step size 4 and dtype as float.

Write NumPy commands for the following:

(a) Find the dimensions, shape, size, and data type of the items and itemsize of arrays zeros, vowels, ones, myarray1 and myarray2.

(b) Reshape the array ones to have all the 10 elements in a single row.

(c) Display the 2nd and 3rd element of the array vowels.

(d) Display all elements in the 2nd and 3rd row of the array myarray1.

(e) Display the elements in the 1st and 2nd column of the array myarray1.

(f) Display the elements in the 1st column of the 2ndand 3rd row of the array myarray1.

(g) Reverse the array of vowels.


Answer: -

(a)

import numpy as np

# Finding dimensions
dimensions = my_array.ndim
print("Dimensions:", dimensions)

# Finding shape
shape = my_array.shape
print("Shape:", shape)

# Finding size
size = my_array.size
print("Size:", size)

# Finding data type
data_type = my_array.dtype
print("Data Type:", data_type)

# Finding item size
item_size = my_array.itemsize
print("Item Size:", item_size)

• my_array.ndim returns the number of dimensions (2 in this case).

• my_array.shape returns a tuple representing the shape of the array (2 rows, 3 columns).

• my_array.size returns the total number of elements in the array (6 in this case).

• my_array.dtype returns the data type of the array's elements.

• my_array.itemsize returns the size (in bytes) of each individual element in the array.


(b)

# Reshape the array into a single row
reshaped = my_array.reshape(1, -1)

The reshape() function is used to reshape the array into a single row by specifying the target shape as (1, -1).

The -1 in the reshape function acts as a placeholder and automatically determines the appropriate number of columns based on the given number of elements.

The resulting reshaped array, reshaped, will have a shape of (1, 10), indicating a single row with 10 columns.


(c)

print(vowels[1:3])

To display the 2nd and 3rd elements of the vowels array, we use indexing with the square brackets notation.

The index values 1:3 indicate a range from the 2nd element (index 1) to the 3rd element (index 2).

Note that the ending index is exclusive, so it doesn't include the element at index 3.


(d)

print(myarray1[1:3, :])

To display all elements in the 2nd and 3rd rows of myarray1, we use indexing with the square brackets notation.

The index values 1:3 indicate a range from the 2nd row (index 1) to the 3rd row (index 2).

The : after the comma specifies that we want to include all columns.


(e)

print(myarray1[:, 0:2])

To display the elements in the 1st and 2nd columns of myarray1, we use indexing with the square brackets notation.

The : before the comma indicates that we want to include all rows. The index values 0:2 after the comma indicate a range from the 1st column (index 0) to the 2nd column (index 1).


(f)

print(myarray1[1:3, 0])

To display the elements in the 1st column of the 2nd and 3rd rows of myarray1, we use indexing with the square brackets notation.

The index values 1:3 indicate a range from the 2nd row (index 1) to the 3rd row (index 2). The index 0 after the comma specifies that we want to access the 1st column.


(g)

vowels = np.array(list('aeiou'))
reversed_vowels = vowels[::-1]
print(reversed_vowels)

5 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post