Skip to content

Instantly share code, notes, and snippets.

@YimianDai
Last active September 4, 2019 23:15
Show Gist options
  • Save YimianDai/cb53e9eb690b752c797e3360cbf73483 to your computer and use it in GitHub Desktop.
Save YimianDai/cb53e9eb690b752c797e3360cbf73483 to your computer and use it in GitHub Desktop.
Notes on Numpy

在 Numpy 数组中,size 和 shape 都是属性,而非函数,所以调用的时候不要加括号,size 返回的是 里面的元素数,shape 返回的是真正的尺寸

RESHAPE and LINEAR INDEXING: Matlab always allows multi-dimensional arrays to be accessed using scalar or linear indices, NumPy does not. Linear indices are common in Matlab programs, e.g. find() on a matrix returns them, whereas NumPy’s find behaves differently.

When converting Matlab code it might be necessary to first reshape a matrix to a linear sequence, perform some indexing operations and then reshape back. As reshape (usually) produces views onto the same storage, it should be possible to do this fairly efficiently. Note that the scan order used by reshape in NumPy defaults to the ‘C’ order, whereas Matlab uses the Fortran order. If you are simply converting to a linear sequence and back this doesn’t matter. But if you are converting reshapes from Matlab code which relies on the scan order, then this Matlab code: z = reshape(x,3,4); should become z = x.reshape(3,4,order=’F’).copy() in NumPy.

NumPy 支持逻辑索引,而 MXNet 的 NDArray 不支持。

Numpy 的 == 符号,当左右两个变量相同大小的时候,会做 Element-wise 的比较,当两个变量大小不一样时,还是不会报错,只会说 False

即使是 dtype 为 uint8 的 numpy.ndarray,经过 nd.array() 后,dtype 会自动变成 numpy.float32,在可视化 Dataset 的输出时候要注意

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment