import numpy as npimport numpy as np
print(np.__version__)
x = range(10)
xbar = np.mean(x) # implicit conversion to ndarray
xbar
np.nannp.Inf or -np.Infnp.pindarray object is a flexible N-dimensional array.ndarray's are atomic or homogenous, containing data of a single type.ndarray has attributes / meta-data:ndim - the dimensionality,shape - a tuple giving the size of each dimension,dtype - the data type of the array's values. x = [0, 1, 2]; y = [3, 4, 5]
x_a = np.array(x)
y_a = np.array([x, y])
[(x_a.ndim, y_a.ndim), x_a.shape, y_a.shape, y_a]
np.array() converts sequence objects to arrays.np.asarray() is similar, but creates an alias when passed an ndarray.np.ones(), np.zeros(), np.empty(), *_like(),np.arange(), np.identity().z_x = np.asarray(x) # x is a list
z_y = np.asarray(y_a) # y_a is an ndarray
print((z_x is x, z_y is y_a))
z_y.shape = (3, 2)
y_a
np.arange() - like built-in range() but returns an array; [start, stop).np.linspace() - num evenly spaced values in [start, stop]. print(np.linspace(0, 2 * np.pi, 5) / np.pi)
np.linspace([0, 0], [3, 6], 3)
dtype attribute tells NumPy what type to interpret the primary data
(values) of the array as.dtype.dtype's are:int8, int16, int32 and int64, uint8-uint64,float16-float128, complex64-complex256,bool, object, string_, unicode_. (z_x.dtype, z_y.dtype)
ndarray to another type using the .astype() method.print((z_x.dtype, z_y.dtype))
z_f = z_x.astype(np.float64)
[(z_y[:, 1] + z_f).dtype, (z_f + z_y[:, 1]).dtype]
.astype() always creates a copy.np.dtype(). z_f2 = z_f.astype('d') # 'd' is shorthand for np.float64
print(z_f2 is z_f)
if z_f.dtype == np.dtype('float64'):
pass
else:
z_f = z_f.astype(np.float64)
if z_f.dtype != np.float64:
z_f = z_f.astype(np.float64)
np.mean(), np.std().ndarray objects. x = np.arange(9).reshape(3, 3)
y = np.array([-1, 0, 1])
x[:, 0] * y, x[1, :] > y, x * y
ndarray is similar to slicing a list.ndarray is a view of the original array referencing
the original data. z = np.ones((4, 3))
z1 = z[:, 1].copy()
z2 = z[:, 2]
z1[:] = 0
z2[:] = 7 # 7 is broadcast to the entire slice
z
ndarray can be indexed using the bool type, often created from
the array itself.and and or are not vectorized, use np.logical_and() or
np.logical_or() instead.print(z > 1)
z[z > 1]
col_sums = np.sum(z, axis=0)
col_sums = np.sum(z, axis=0)
z[:, np.logical_and(col_sums > 4, col_sums < 30)]
np.array([-1, 2]) * np.ones((2, 2, 2))
z below? z = np.ones((2, 3, 2))
x = np.array([i % 3 - 1 for i in range(6)])
x = x.reshape(2, 3)
try:
z = x * z
except:
x = x.reshape(3, 2)
try:
z = x * z
except:
pass
x = x.reshape(1, 1, 3, 2)
try:
z = x * z
except:
z[:] = 0
[z.shape, np.sum(z), x.shape]
random API provides a random number generator and routines to
sample from a large number of distributions. np.random.choice() can be used to sample a sequence object. print(np.random.uniform(0, 1, 3))
print(np.random.normal(63.5, 5.55, 2))
np.random.choice(range(3), 4)
rng = np.random.default_rng(seed=42)
rng.uniform()
.shuffle() method permutes an array in-place..permutation() method creates a copy..permuted() method shuffles along an axis, use out to reassign
in-place.rng = np.random.default_rng(91421)
a = np.arange(4)
rng.shuffle(a)
b = rng.permutation(a)
[a, b, b is a]
b.shape = (2, 2)
c = b
_ = rng.permuted(b, axis=0, out=b)
[c is b, c, b]