- More efficient
- Use for large scale arrays / matrices
- Convert between
lists
tolist()
andnp.array()
.shape
check the dimensions.dtype
check the data type of the elementsnp.zeros()
creates an array filled with0
s of the set shapenp.arange(n)
creates an array from0
ton-1
np.linspace(s, e, num=g)
creates an array with elements, froms
toe
linearly separated with gap sizeg
+
,-
,+=
, and-=
work elementwise- Boolean indexing
arr > n
Maps the array to booleans, compares them elementwisearr[arr > n]
keeps all elements that satisfy the condition
- Array stacking
np.vstack()
- stack on vertical stacknp.hstack()
- stack on horizontal stacknp.dstack()
- stack on depth stack
- Array concatenation
- Appends on the same axis, unlike stacking
- Array splitting
- Can specify splitting at what positions
np.vsplit()
- splits on vertical axisnp.hsplit()
- splits on horizontal axisnp.dsplit()
- splits on depth axis
- Repeating arrays
np.repeat()
repeats items elementwise- Flattens array if no axis is specified
- Nonzero searching
np.nonzero()
returns the indices in arrays
- Unique filtering
np.unique()
returns the unique elements- Makes a set
- Sorting
arr.sort()
is a mutatornp.argsort
returns the sorted indices
- Saving and loading
- Will save as
.npz
- Can also save as txt by
.savetxt()
and.loadtxt()
- Will save as
- Random
- Seeding
rg = default_rng(seed)
- Use
rg
for functions.random()
,.integers()
- Seeding
- Print options
np.set_printoptions(threshold=x)
- Prints more items up to x