/ 写在前面 – 我热爱技术、热爱开源。我也相信开源能使技术变得更好、共享能使知识传播得更远。但是开源并不意味着某些商业机构/个人可以为了自身的利益而一味地索取,甚至直接剽窃大家曾为之辛勤付出的知识成果,所以本文未经允许,不得转载,谢谢。/
The concept of axis is essential for understanding NumPy.
NumPy’s main object is the homogeneous multidimensional array. It is a table of elements (usually numbers), all of the same type, indexed by a tuple of non-negative integers. In NumPy dimensions are called axes.
NumPy’s array class is called
ndarray
. It is also known by the aliasarray
. Note thatnumpy.array
is not the same as the Standard Python Library classarray.array
, which only handles one-dimensional arrays and offers less functionality.
Important attributes of an ndarray
object are:
ndarray.ndim
ndarray.shape
ndarray.size
ndarray.dtype
ndarray.itemsize
ndarray.data
ndarray.flat
ndarray.T
Commonly used methods:
ravel()
,flatten()
- returns the array, flattenedreshape()
resize()
dot()
sum()
cumsum()
min()
max()
Commonly used functions:
np.zeros()
np.ones()
np.eye()
np.empty()
np.arange()
- similar but different with built-inrange
np.linspace()
np.fromfunction()
Below are key points of NumPy to understand.
Creation of array - tuple or list
_
Keyword arguments - dtype=complex
, etc.
How does NumPy organize axes? Which axis is the last axis?
About upcasting, how is it applied in operations? Like +=
, *=
, etc.
What’s the dafault dtype
of the created array? - float64
Arithmetic operators on arrays apply elementwise.
Product operator *
operates elementwise in NumPy arrays.
The matrix product can be performed using the @
operator (in python >=3.5) or the dot
function or method.
Creation of the instance of default random number generator:
rg = np.random.default_rng(1)
a = rg.random((2,3))
Unary operations with specifying the keyword argument axis
“universal functions”( ufunc
) - elementwise:
np.sin()
np.cos()
np.ext()
np.sqrt()
np.add()
- …
Understand indexing, slicing and iterating:
- How to reverse an one-dimensional array?
- Access multidimensional arrays in multiple ways - complete indices, fewer indeces and dots
...
- Iterator -
flat
attribute
Commands that can return different shapes of an array:
ndarray.ravel()
-ndarray.flatten()
ndarray.T
ndarray.reshape()
Difference between reshape()
and resize()
Meaning of passing -1
to a reshaping operation
Stacking together different arrays:
np.vstack()
np.hstack()
np.column_stack()
- different withnp.hstack()
np.row_stack()
- an alias fornp.vstack()
np.concatenate()
np.r_
andnp.c_
- difficult to understand…- …
Splitting an array:
np.hsplit()
np.vsplit()
np.array_split()
Is it a copy of an array?
id()
is
.view()
.base