np.array()
import numpy as np
arr = np.array([[0, 1, 2], [3, 4, 5]],
dtype=np.float32)
print(repr(arr))
output:
array([[0., 1., 2.], [3., 4., 5.]], dtype=float32)
.copy()
a = np.array([0, 1])
b = np.array([9, 8])
c = a
print('Array a: {}'.format(repr(a)))
c[0] = 5
print('Array a: {}'.format(repr(a)))
d = b.copy()
d[0] = 6
print('Array b: {}'.format(repr(b)))
output:
Array a: array([0, 1]) Array a: array([5, 1]) Array b: array([9, 8])
.dtype()
Description: It returns the array cast to the new type.
arr = np.array([0, 1, 2])
print(arr.dtype)
arr = arr.astype(np.float32)
print(arr.dtype)
np.nan
Description: Act as a placeholder
arr = np.array([np.nan, 1, 2])
print(repr(arr))
arr = np.array([np.nan, 'abc'])
print(repr(arr))
# Will result in a ValueError
#Note that np.nan cannot take on an integer type
np.array([np.nan, 1, 2], dtype=np.int32)
np.inf
Description: To represent infinity
print(np.inf > 1000000)
arr = np.array([np.inf, 5])
print(repr(arr))
arr = np.array([-np.inf, 1])
print(repr(arr))
# Will result in an OverflowError
#No integer type
np.array([np.inf, 3], dtype=np.int32)