Python Practice › NumPy Programs › Program 60
Change the data type
Write a NumPy program to change the data type of an array.
Input
[[4, 8, 7], [2, 3, 6]]
Output
int32 float64 [[4. 8. 7.] [2. 3. 6.]]
Approach
astype()returns a converted copy.
Solution
import numpy as npx = np.array([[4, 8, 7], [2, 3, 6]], dtype=np.int32)print(x.dtype)y = x.astype(np.float64)print(y.dtype)print(y)Output
int32 float64 [[4. 8. 7.] [2. 3. 6.]]
Learn the concept
New to this topic? Read the lesson, then come back.