Python Practice › NumPy Programs › Program 60

Change the data type

EasyNumPy Programs · 60 of 100

Write a NumPy program to change the data type of an array.

Advertisement
Input
[[4, 8, 7], [2, 3, 6]]
Output
int32
float64
[[4. 8. 7.]
 [2. 3. 6.]]

Approach

  1. astype() returns a converted copy.

Solution

Python
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.]]
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs