English

Using the arrays created in Question 4 above, write NumPy commands for the following: a) Divide all elements of array ones by 3. b) Add the arrays myarray1 and myarray2.

Advertisements
Advertisements

Question

Using the arrays created in Question 4 above, write NumPy commands for the following:

  1. Divide all elements of array ones by 3.
  2. Add the arrays myarray1 and myarray2.
  3. Subtract myarray1 from myarray2 and store the result in a new array.
  4. Multiply myarray1 and myarray2 elementwise.
  5. Do the matrix multiplication of myarray1 and myarray2 and store the result in a new array myarray3.
  6. Divide myarray1 by myarray2.
  7. Find the cube of all elements of myarray1 and divide the resulting array by 2.
  8. 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.
Code Writing
Advertisements

Solution

a) 

result = ones / 3
print(result)

Output:

[0.33333333 0.33333333 0.33333333 0.33333333 0.33333333 0.33333333 0.33333333 0.33333333 0.33333333 0.33333333]

b) 

result = myarray1 + myarray2
print(result)

Output:

[11 22 33]
[44 55 66]
[77 88 99]]

c) 

result = myarray2 - myarray1
print(result)

Output:

[[9 18 27]
[36 45 54]
[63 72 81]]

d) 

result = myarray1 * myarray2
print(result)

Output:

[[10 40 90]
[160 250 360]
[490 640 810]]

e) 

myarray3 = myarray1 @ myarray2
print(myarray3)

Output:

[[300 360 420]
[660 810 960]
[1020 1260 1500]]

f) 

result = myarray1 / myarray2
print(result)

Output:

[[0.1 0.1 0.1]
[0.1 0.1 0.1]
[0.1 0.1 0.1]]

g) 

result = (myarray1 ** 3) / 2
print(result)

Output:

[[0.5 4. 13.5]
[32 62.5 108.]
[171.5 256. 364.5]]

h) 

result = np.round(np.sqrt(myarray2) / 2, 2)
print(result)

Output:

[[1.58 2.24 2.74]
[3.16 3.54 3.87]
[4.18 4.14 4.74]
shaalaa.com
  Is there an error in this question or solution?
Chapter 6: Introduction to NumPy - Exercise [Page 113]

APPEARS IN

NCERT Informatics Practices [English] Class 11
Chapter 6 Introduction to NumPy
Exercise | Q 6. | Page 113
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×