Lesson 7 of 60 · python
NumPy Arrays and Data Types
Duration: 15 minutes
NumPy Arrays and Data Types
NumPy arrays (ndarray) are homogeneous – every element shares the same data type (dtype).
Creating arrays of different dims
import numpy as np
# 1‑D array (vector)
vec = np.arange(5) # [0 1 2 3 4]
# 2‑D array (matrix)
mat = np.arange(12).reshape(3,4)
print(mat)
# 3‑D array (tensor)
# shape: (2, 3, 4)
tensor = np.arange(24).reshape(2,3,4)
print(tensor.shape)
Specifying data types
float_arr = np.array([1, 2, 3], dtype=np.float64)
int_arr = np.array([1.2, 2.5, 3.7], dtype=np.int32) # truncates
Inspecting dtype
print(float_arr.dtype) # float64
print(int_arr.dtype) # int32
Common dtypes
np.int8,np.int16,np.int32,np.int64np.uint8,np.uint16, …np.float16,np.float32,np.float64np.bool_