Specifies the data-type of the array elements. If not provided, it’s inferred from the input.
import numpy as np
array = np.array(object=[1, 2, 3], dtype=np.float32)
print(array)
#Output: [1., 2., 3.]
np.int8: 8-bit signed integer (range: -128 to 127).
np.int16: 16-bit signed integer (range: -32,768 to 32,767).
np.int32: 32-bit signed integer (range: -2,147,483,648 to 2,147,483,647).
np.int64: 64-bit signed integer (large integer range).
np.uint8: 8-bit unsigned integer (range: 0 to 255).
np.uint16: 16-bit unsigned integer (range: 0 to 65,535).
np.uint32: 32-bit unsigned integer (range: 0 to 4,294,967,295).
np.uint64: 64-bit unsigned integer (large positive integer range).
np.float16: Half precision floating-point (16-bit, for low-precision computations).
np.float32: Single precision floating-point (32-bit).
np.float64: Double precision floating-point (64-bit, the default float in NumPy).
np.float128: Extended precision floating-point (128-bit, availability depends on system).
np.complex64: Complex number represented by two 32-bit floats (for real and imaginary parts).
np.complex128: Complex number represented by two 64-bit floats (default complex dtype).
np.complex256: Complex number represented by two 128-bit floats (system-dependent).
np.bool_: Boolean type, can be either True or False (stored as 1-bit but takes up a full byte).
np.str_: Fixed-length Unicode string, specified by S + length (e.g., S10 for a 10-character string).
np.unicode_: Fixed-length Unicode string with support for multiple characters (uses U).
np.object_: Allows storing any Python object, including mixed types, strings, or other arrays. Useful for heterogeneous data but slower than native NumPy types.
np.datetime64: Stores dates and times with varying precisions (e.g., Y, M, D, h, m, s, ms, us, ns, ps, fs, as). Example: np.datetime64('2003-10-02')
np.timedelta64: Represents time durations with units (same units as datetime64).
The copy parameter specifies whether to create a new array (copy) or if NumPy can reuse the existing memory from the input array (i.e., return a view ).
A new NumPy array is created, and the data is copied.
import numpy as np
# Original array
original_array = np.array([1, 2, 3])
# Creating a new array with copy=True (default behavior)
new_array = np.array(original_array, copy=True)
# Modify the new array
new_array[0] = 99
# Print both arrays
print("Original Array:", original_array) # Output: [1 2 3]
print("New Array:", new_array) # Output: [99 2 3]
NumPy avoids copying and returns a view of the original array, meaning changes to the new array will also affect the original array.
import numpy as np
# Original array
original_array = np.array([1, 2, 3])
# Creating a new array with copy=False (view)
new_array = np.array(original_array, copy=False)
# Modify the new array
new_array[0] = 99
# Print both arrays
print("Original Array:", original_array) # Output: [99 2 3]
print("New Array:", new_array) # Output: [99 2 3]
Modification to the original_array or new_array will affect both the arrays.
Specifies the memory layout order for multi-dimensional arrays.
Preserves the original memory layout (C-order or F-order) as much as possible.
Row-major order (C-style), where rows are flattened first.
Column-major order (Fortran-style), where columns are flattened first.
.ravel() is a NumPy function used to flatten a multi-dimensional array into a 1D array.
It controls whether the output array will retain the subclass type of the input array (if it’s a subclass of np.ndarray) or be cast to a base np.ndarray.
A base np.ndarray is returned, even if the input is a subclass.
import numpy as np
class MyArray(np.ndarray):
pass # Just a subclass for demonstration
# Create an instance of MyArray
my_array = np.array([[1, 2, 3], [4, 5, 6]]).view(MyArray)
print("Original type:", type(my_array)) # Output: <class '__main__.MyArray'>
# Using np.array with subok=False
result_subok_false = np.array(my_array, subok=False)
print("With subok=False:", type(result_subok_false)) # Output: <class 'numpy.ndarray'>
If you pass an instance of a subclass of np.ndarray directly, you can control whether the subclass is preserved
import numpy as np
class MyArray(np.ndarray):
pass # Just a subclass for demonstration
# Create an instance of MyArray
my_array = np.array([[1, 2, 3], [4, 5, 6]]).view(MyArray)
print("Original type:", type(my_array)) # Output: <class '__main__.MyArray'>
# Using np.array with subok=True
result_subok_true = np.array(my_array, subok=True)
print("With subok=True:", type(result_subok_true)) # Output: <class '__main__.MyArray'>
In NumPy, the .view() method creates a new "view" of an existing array with a different data type or subclass without copying the data.
This means the new array points to the same data as the original array but can be accessed or interpreted differently.
Ensures that the created array has at least a specified number of dimensions. If the input does not meet the minimum number of dimensions, it will be automatically padded with extra dimensions (of size 1) at the beginning to meet the requirement.
import numpy as np
# A 1D array
arr_1d = np.array([1, 2, 3])
print("Original array:", arr_1d) # Output: [1 2 3]
print("Original array dimensions:", arr_1d.ndim) # Output: 1
# Using ndmin=2 to make sure the array has at least 2 dimensions
arr_2d = np.array([1, 2, 3], ndmin=2)
print("
Array with ndmin=2:", arr_2d) # Output: [[1 2 3]]
print("Array with ndmin=2 dimensions:", arr_2d.ndim) # Output: 2
# Using ndmin=3 to make sure the array has at least 3 dimensions
arr_3d = np.array([1, 2, 3], ndmin=3)
print("
Array with ndmin=3:", arr_3d) # Output: [[[1 2 3]]]
print("Array with ndmin=3 dimensions:", arr_3d.ndim) # Output: 3
If the actual_dimension > ndmin then the output will have actual_dimension.
The like parameter in np.array() was introduced to make array creation more flexible, particularly for compatibility with libraries that extend NumPy, like Dask, CuPy, or other libraries that create arrays compatible with np.ndarray but optimized for different backends (e.g., parallel processing or GPU-based arrays).