Advertisements
Advertisements
Question
Using the arrays created in Question 4 above, write NumPy commands for the following:
- Divide all elements of array ones by 3.
- Add the arrays myarray1 and myarray2.
- Subtract myarray1 from myarray2 and store the result in a new array.
- Multiply myarray1 and myarray2 elementwise.
- Do the matrix multiplication of myarray1 and myarray2 and store the result in a new array myarray3.
- Divide myarray1 by myarray2.
- Find the cube of all elements of myarray1 and divide the resulting array by 2.
- 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?
