Tuple to array
Write a NumPy program to convert a tuple into an array.
(5, 7, 0)
[5 7 0]
Approach
np.array(tuple).
Solution
import numpy as npprint(np.array((5, 7, 0)))[5 7 0]
Learn the concept
New to this topic? Read the lesson, then come back.
Online Learning Platform
Write a NumPy program to convert a tuple into an array.
(5, 7, 0)
[5 7 0]
np.array(tuple).import numpy as npprint(np.array((5, 7, 0)))[5 7 0]
New to this topic? Read the lesson, then come back.
Write a NumPy program to round elements up with ceil.
[4.8757, 2.4578]
[5. 3.]
np.ceil.import numpy as npprint(np.ceil([4.8757, 2.4578]))[5. 3.]
New to this topic? Read the lesson, then come back.
Write a NumPy program to round elements down with floor.
[4.8757, 2.4578]
[4. 2.]
np.floor.import numpy as npprint(np.floor([4.8757, 2.4578]))[4. 2.]
New to this topic? Read the lesson, then come back.
Write a NumPy program to round elements to two decimal places.
[4.8757, 2.4578]
[4.88 2.46]
np.round(a, 2).import numpy as npprint(np.round(np.array([4.8757, 2.4578]), 2))[4.88 2.46]
Rounding gives 4.88 and 2.46; the original sample (4.86, 2.45) was not rounded correctly.
New to this topic? Read the lesson, then come back.
Write a NumPy program to calculate the tangent of angles.
0, 45 degrees
[0. 1.]
np.tan(np.radians(...)).import numpy as npprint(np.tan(np.radians([0, 45])).round(3))[0. 1.]
New to this topic? Read the lesson, then come back.
Write a NumPy program to calculate the cosine of angles.
0, 60, 90 degrees
[1. 0.5 0. ]
np.cos(np.radians(...)).import numpy as npprint(np.cos(np.radians([0, 60, 90])).round(3))[1. 0.5 0. ]
New to this topic? Read the lesson, then come back.
Write a NumPy program to calculate the variance of an array.
[2, 4, 4, 4, 5, 5, 7, 9]
4.0
np.var.import numpy as npprint(np.var([2, 4, 4, 4, 5, 5, 7, 9]))4.0
New to this topic? Read the lesson, then come back.
Write a NumPy program to calculate the standard deviation of an array.
[2, 4, 4, 4, 5, 5, 7, 9]
2.0 2.138
np.std (population); ddof=1 for a sample.import numpy as npa = np.array([2, 4, 4, 4, 5, 5, 7, 9])print(np.std(a), round(float(np.std(a, ddof=1)), 3))2.0 2.138
New to this topic? Read the lesson, then come back.
Write a NumPy program to calculate the sine of angles.
0, 30, 90 degrees
[0. 0.5 1. ]
np.sin.import numpy as npprint(np.sin(np.radians([0, 30, 90])).round(3))[0. 0.5 1. ]
New to this topic? Read the lesson, then come back.
Write a NumPy program to check whether two arrays have the same dimensions.
True False
.shape.import numpy as npa, b, c = np.zeros((2, 3)), np.ones((2, 3)), np.ones((3, 2))print(a.shape == b.shape, a.shape == c.shape)True False
New to this topic? Read the lesson, then come back.