NumPy¶
NumPy: Numerical Python
Doc: https://numpy.org/doc/stable/reference/index.html
ndarray: At the core of the NumPy package, is the ndarray object. This encapsulates n-dimensional arrays of homogeneous data types, with many operations being performed in compiled code for performance.
NaN (Not a Number)
In [ ]:
Copied!
import numpy as np
import numpy as np
In [21]:
Copied!
arr = np.array([3.7, -1.2, -2.6, 0.5, 12.9, 10.1])
print("ndim:", arr.ndim)
print("shape:", arr.shape)
print("dtype:", arr.dtype)
arr
arr = np.array([3.7, -1.2, -2.6, 0.5, 12.9, 10.1])
print("ndim:", arr.ndim)
print("shape:", arr.shape)
print("dtype:", arr.dtype)
arr
ndim: 1 shape: (6,) dtype: float64
Out[21]:
array([ 3.7, -1.2, -2.6, 0.5, 12.9, 10.1])
In [22]:
Copied!
arr.astype(np.int32)
arr.astype(np.int32)
Out[22]:
array([ 3, -1, -2, 0, 12, 10], dtype=int32)
In [50]:
Copied!
arr > 0
arr > 0
Out[50]:
array([ True, False, False, True, True, True])
In [51]:
Copied!
arr[arr > 0]
arr[arr > 0]
Out[51]:
array([ 3.7, 0.5, 12.9, 10.1])
In [52]:
Copied!
np.where(arr > 0, 2, -2)
np.where(arr > 0, 2, -2)
Out[52]:
array([ 2, -2, -2, 2, 2, 2])
random¶
In [45]:
Copied!
np.random.seed(42)
rng = np.random.default_rng(seed=42)
type(rng)
np.random.seed(42)
rng = np.random.default_rng(seed=42)
type(rng)
Out[45]:
numpy.random._generator.Generator
In [46]:
Copied!
rng.integers(low=0, high=10, size=(3, 4))
rng.integers(low=0, high=10, size=(3, 4))
Out[46]:
array([[0, 7, 6, 4],
[4, 8, 0, 6],
[2, 0, 5, 9]])
In [48]:
Copied!
rng.standard_normal(size=(3, 4))
rng.standard_normal(size=(3, 4))
Out[48]:
array([[-0.31624259, -0.01680116, -0.85304393, 0.87939797],
[ 0.77779194, 0.0660307 , 1.12724121, 0.46750934],
[-0.85929246, 0.36875078, -0.9588826 , 0.8784503 ]])
In [ ]:
Copied!



















