Python Numpy Reference 阅读
np.array, np.dtype 和 array scalar type 概念与关系
NumPy provides an N-dimensional array type, the ndarray, which describes a collection of items of the same type.
All ndarrays are homogenous: every item takes up the same size block of memory, and all blocks are interpreted in exactly the same way. How each item in the array is to be interpreted is specified by a separate data-type object
An item extracted from an array, e.g., by indexing, is represented by a Python object whose type is one of the array scalar types built in Numpy.
np.array
Base array and View array
View is An array that does not own its data, but refers to another array’s data instead.
Different ndarrays can share the same data, so that changes made in one ndarray may be visible in another. That is, an ndarray can be a view to another ndarray, and the data it is referring to is taken care of by the base ndarray.
任何对于np.array的操作,先明确其操作对象,是array本身? array的一个view? 还是array的一个copy?
Creation
- Using ndarray constructor
- Using array creation routines
Function XXX_like(an_array) Return a new array with the same shape and type as a given array
https://docs.scipy.org/doc/numpy-dev/reference/routines.array-creation.html#routines-array-creation
Indexing and Slicing
熟练地应用indexing 和 slicing 非常重要,可以对 data set 进行初步的处理
Basic indexing/slicing
x[selection object]
All arrays generated by basic slicing are always views of the original array.
You may use slicing to set values in the array, but (unlike lists) you can never grow the array.
integers: postivie index, negative index
slice object
tuple for multi-dimensions
ellipsis
np.newaxis = np.NoneField access
Indexing x[‘field-name’] returns a new view to the array (same shape with x)Advanced indexing
Advanced indexing always returns a copy of the data contrast with basic slicing that returns a view
integer array indexing
auto-broadcast
Boolean array indexing
If obj.ndim == x.ndim, x[obj] returns a 1-dimensional array filled with the elements of x corresponding to the True values of obj.np.ix_() function
Flat Iterator indexing
ndarray.flat : A 1-D iterator over the array
This iterator object can also be indexed using basic slicing or advanced indexing as long as the selection object is not a tuple. This should be clear from the fact that x.flat is a 1-dimensional view. The shape of any returned array is therefore the shape of the integer indexing object.
https://docs.scipy.org/doc/numpy-dev/reference/arrays.indexing.html#flat-iterator-indexing