Q. Using the arrays created in Question 4 above, 

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) Divide all elements of array ones by 3.

(b) Add the arrays myarray1 and myarray2.

(c) Subtract myarray1 from myarray2 and store the result in a new array.

(d) Multiply myarray1 and myarray2 element wise.

(e) Do the matrix multiplication of myarray1 and myarray2 and store the result in a new array myarray3.

(f) Divide myarray1 by myarray2.

(g) Find the cube of all elements of myarray1 and divide the resulting array by 2.

(h) Find the square root of all elements of myarray2 and divide the resulting array by 2. The result should be rounded to two places of decimals.


Answer :


(A)

ones = np.ones((2, 5), dtype=int)
divided_ones = ones / 3

To divide all elements of the ones array by 3, we simply use the division operator /. NumPy automatically performs element-wise division, resulting in a new array divided_ones with the same shape as ones, where each element is divided by 3.


(B) (C) (D)

When performing element-wise operations, the dimensions of the arrays should either match or be compatible according to broadcasting rules. Incompatible shapes, such as (3, 5) and (3, 3) in This case, cannot be directly added, divided, or multiplied element-wise.


(E)

To perform matrix multiplication between myarray1 and myarray2, you can use the np.dot() function or the @ operator.

Using np.dot():

import numpy as np

myarray1 = np.array([[2.7, -2, -19], [0, 3.4, 99.9], [10.6, 0, 13]])
myarray2 = np.arange(4, 4 + 3 * 5 * 4, 4, dtype=float).reshape(3, 5)
myarray3 = np.dot(myarray1, myarray2)
print(myarray3)
[[ -873.2  -946.4 -1019.6 -1092.8 -1166. ]
 [ 4477.2  4890.4  5303.6  5716.8  6130. ]
 [  614.4   708.8   803.2   897.6   992. ]]
 

Using @ operator:

 import numpy as np

myarray1 = np.array([[2.7, -2, -19], [0, 3.4, 99.9], [10.6, 0, 13]])
myarray2 = np.arange(4, 4 + 3 * 5 * 4, 4, dtype=float).reshape(3, 5)
myarray3 = myarray1 @ myarray2
print(myarray3)
[[ -873.2  -946.4 -1019.6 -1092.8 -1166. ]
 [ 4477.2  4890.4  5303.6  5716.8  6130. ]
 [  614.4   708.8   803.2   897.6   992. ]]
 

(F)

When performing element-wise operations, the dimensions of the arrays should either match or be compatible according to broadcasting rules. Incompatible shapes, such as (3, 5) and (3, 3) in This case, cannot be directly divided element-wise.


(G)

import numpy as np
myarray1 = np.array([[2.7, -2, -19], [0, 3.4, 99.9], [10.6, 0, 13]])
cube_result = myarray1 ** 3
# Divide the cube_result array by 2
final_result = cube_result / 2
print(final_result)

(H)

myarray2 = np.arange(4, 4 + 3 * 5 * 4, 4, dtype=float).reshape(3, 5)
# Compute the square root of all elements in myarray2
sqrt_result = np.sqrt(myarray2)
# Divide the sqrt_result array by 2
divided_result = sqrt_result / 2
# Round the values to two decimal places
rounded_result = np.round(divided_result, decimals=2)
print(rounded_result)

Post a Comment

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

Previous Post Next Post