2、Numpy-Quickstart tutorial(快速入门教程)

1. Prerequisites(先决条件)


  • know a bit of Python
  • have some software installed on your computer

2. The Basics(基础)


  • numpy的首要目标是处理齐次多维数组,他是一个元素的表格,所有的元素具有相同的数据类型,由非负整数的元组索引,在numpy维度中称为轴

  • 例如,在三维空间中点的坐标[1,2,1],他有一个轴,这个轴上面有三个元素,因此我们说他有一个长度是3,再比如[ [1,0,0], [0,1,2] ],这个数组中含有两个轴,第一个轴的长度是2,第二个轴的长度是3.

  • numpy的数组类叫做:ndarray,注意:numpy.array跟标准Python库中的类array.array不同,后者只能处理一维的数组,提供的功能也较少,ndarray的重要属性有:
  • ndarray.ndim(数组的轴(维度)数)
  • ndarray.shape(数组的维度,这是一个整数元组,指示数组在每个维度中的大小)
  • ndarray.size(数组的元素总数)
  • ndarray.dtype(描述数组中元素类型的对象)
  • ndarray.itemsize(数组中每个元素的字节大小)
  • ndarray.data(包含数组的实际元素的缓冲区)

An example


Array Creation


create an array from a regular Python list or tuple using the array function. The type of the resulting array is deduced from the type of the elements in the sequences.


A frequent error consists in calling array with multiple numeric arguments, rather than providing a single list of numbers as an argument.

a = np.array(1,2,3,4)       # WRONG
a = np.array([1,2,3,4])    # RIGHT

array transforms sequences of sequences into two-dimensional arrays, sequences of sequences of sequences into three-dimensional arrays, and so on.

b = np.array([(1.5,2,3),(4,5,6)])
b

>>>output:

array( [ [1.5, 2.,3. ] , [4. , 5. , 6. ]])

The type of the array can also be explicitly specified at creation time

c = np.array( [ [1,2], [3,4] ], dtype=complex )
c

>>>output:

array([[ 1.+0.j,  2.+0.j], [ 3.+0.j,  4.+0.j]])

Often, the elements of an array are originally unknown, but its size is known. Hence, NumPy offers several functions to create arrays with initial placeholder content.

  • function zeros : creates an array full of zeros
  • function ones : creates an array full of ones
  • function empty : creates an array whose initial content is random and depends on the state of the memory


To create sequences of numbers, NumPy provides a function analogous to range that returns arrays instead of lists.


When arange is used with floating point arguments, it is generally not possible to predict the number of elements obtained, due to the finite floating point precision. For this reason, it is usually better to use the function linspace that receives as an argument the number of elements that we want, instead of the step:


Printing Arrays


When you print an array, NumPy displays it in a similar way to nested lists, but with the following layout:

  • the last axis is printed from left to right
  • the second-to-last is printed from top to bottom
    -the rest are also printed from top to bottom, with each slice separated from the next by an empty line
    One-dimensional arrays are then printed as rows, bidimensionals as matrices and tridimensionals as lists of matrices

    reshape
    If an array is too large to be printed, NumPy automatically skips the central part of the array and only prints the corners:

    To disable this behaviour and force NumPy to print the entire array, you can change the printing options using set_printoptions.

Basic Operations


Arithmetic operators on arrays apply elementwise. A new array is created and filled with the result.


Unlike in many matrix languages, the 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:

Some operations, such as += and *=, act in place to modify an existing array rather than create a new one.

When operating with arrays of different types, the type of the resulting array corresponds to the more general or precise one (a behavior known as upcasting).

Many unary operations, such as computing the sum of all the elements in the array, are implemented as methods of the ndarray class.

By default, these operations apply to the array as though it were a list of numbers, regardless of its shape. However, by specifying the axis parameter you can apply an operation along the specified axis of an array:

Universal Function(通用函数)


NumPy provides familiar mathematical functions such as sin, cos, and exp. In NumPy, these are called “universal functions”(ufunc). Within NumPy, these functions operate elementwise on an array, producing an array as output.


Indexing,Slicing and Iterating(索引、切片和迭代)


One-dimensional arrays can be indexed, sliced and iterated over, much like lists and other Python sequences.


Multidimensional arrays can have one index per axis. These indices are given in a tuple separated by commas:

When fewer indices are provided than the number of axes, the missing indices are considered complete slices:

The expression within brackets in b[i] is treated as an i followed by as many instances of : as needed to represent the remaining axes. NumPy also allows you to write this using dots as b[i,...].
The dots (...) represent as many colons as needed to produce a complete indexing tuple. For example, if x is an array with 5 axes, then

  • x[1,2,...] is equivalent to x[1,2,:,:,:]
  • x[...,3] to x[:,:,:,:,3]
  • x[4,...,5,:] to x[4,:,:,5,:]

    Iterating over multidimensional arrays is done with respect to the first axis:

    However, if one wants to perform an operation on each element in the array, one can use the flat attribute which is an iterator over all the elements of the array:

3. Shape Manipulation(形状操作)


Changing the shape of an array


An array has a shape given by the number of elements along each axis:



The shape of an array can be changed with various commands. Note that the following three commands all return a modified array, but do not change the original array:


The reshape function returns its argument with a modified shape, whereas the ndarray.resize method modifies the array itself:


If a dimension is given as -1 in a reshaping operation, the other dimensions are automatically calculated:
reshape-1.png

Stacking together different arrays(将不同的阵列堆叠在一起)


Several arrays can be stacked together along different axes:


The function column_stack stacks 1D arrays as columns into a 2D array. It is equivalent tohstack** only for 2D arrays:

Note
In complex cases, r_ and c_ are useful for creating arrays by stacking numbers along one axis. They allow the use of range literals (“:”)

r_.png

Splitting one array into several smaller ones(将一个数组拆分为几个较小的数组)


Using hsplit, you can split an array along its horizontal axis, either by specifying the number of equally shaped arrays to return, or by specifying the columns after which the division should occur:


vsplit splits along the vertical axis, and array_split allows one to specify along which axis to split.

4. Copies and Views(副本和视图)


When operating and manipulating arrays, their data is sometimes copied into a new array and sometimes not. This is often a source of confusion for beginners. There are three cases:

No Copy at All


Simple assignments make no copy of array objects or of their data.



Python passes mutable objects as references, so function calls make no copy.


View or Shallow Copy(浅复制)


Different array objects can share the same data. The view method creates a new array object that looks at the same data.


Slicing an array returns a view of it:

Deep Copy


The copy method makes a complete copy of the array and its data.



Sometimes copy should be called after slicing if the original array is not required anymore. For example, suppose a is a huge intermediate result and the final result b only contains a small fraction of a, a deep copy should be made when constructing b with slicing:



If b = a[:100] is used instead, a is referenced by b and will persist in memory even if del a is executed.

Functions and Methods Overview


Here is a list of some useful NumPy functions and methods names ordered in categories.

5. Less Basic


Broadcasting rules


6. Fancy indexing and index tricks(复杂的索引和索引技巧)


NumPy offers more indexing facilities than regular Python sequences. In addition to indexing by integers and slices, as we saw before, arrays can be indexed by arrays of integers and arrays of booleans.

Indexing with Arrays of Indices(用索引数组索引)



When the indexed array a is multidimensional, a single array of indices refers to the first dimension of a. The following example shows this behavior by converting an image of labels into a color image using a palette.



We can also give indexes for more than one dimension. The arrays of indices for each dimension must have the same shape.



Naturally, we can put i and j in a sequence (say a list) and then do the indexing with the list.

However, we can not do this by putting i and j into an array, because this array will be interpreted as indexing the first dimension of a.

Another common use of indexing with arrays is the search of the maximum value of time-dependent series:



You can also use indexing with arrays as a target to assign to:

However, when the list of indices contains repetitions, the assignment is done several times, leaving behind the last value:

This is reasonable enough, but watch out if you want to use Python’s += construct, as it may not do what you expect:

Indexing with Boolean Arrays


When we index arrays with arrays of (integer) indices we are providing the list of indices to pick. With boolean indices the approach is different; we explicitly choose which items in the array we want and which ones we don’t.
The most natural way one can think of for boolean indexing is to use boolean arrays that have the same shape as the original array:


This property can be very useful in assignments:

You can look at the following example to see how to use boolean indexing to generate an image of the Mandelbrot set:


Note that the length of the 1D boolean array must coincide with the length of the dimension (or axis) you want to slice. In the previous example, b1 has length 3 (the number of rows in a), and b2 (of length 4) is suitable to index the 2nd axis (columns) of a.

The ix_() function


The ix_ function can be used to combine different vectors so as to obtain the result for each n-uplet. For example, if you want to compute all the a+b*c for all the triplets taken from each of the vectors a, b and c:


You could also implement the reduce as follows:

The advantage of this version of reduce compared to the normal ufunc.reduce is that it makes use of the Broadcasting Rules in order to avoid creating an argument array the size of the output times the number of vectors.

Indexing with strings


See Structured arrays.

7. Linear Algebra(线性代数)


Work in progress. Basic linear algebra to be included here.

Simple Array Operations


See linalg.py in numpy folder for more.


8. Tricks and Tips(技巧和提示)


“Automatic” Reshaping(自动整形)


To change the dimensions of an array, you can omit one of the sizes which will then be deduced automatically:


Vector Stacking(矢量叠加)


How do we construct a 2D array from a list of equally-sized row vectors? In MATLAB this is quite easy: if x and y are two vectors of the same length you only need do m=[x;y]. In NumPy this works via the functions column_stack, dstack, hstack and vstack, depending on the dimension in which the stacking is to be done. For example:


Histograms(直方图)


The NumPy histogram function applied to an array returns a pair of vectors: the histogram of the array and the vector of bins. Beware: matplotlib also has a function to build histograms (called hist, as in Matlab) that differs from the one in NumPy. The main difference is that pylab.hist plots the histogram automatically, while numpy.histogram only generates the data.



9. Further reading


  • The Python tutorial
  • NumPy Reference
  • SciPy Tutorial
  • SciPy Lecture Notes
  • A matlab, R, IDL, NumPy/SciPy dictionary

你可能感兴趣的:(2、Numpy-Quickstart tutorial(快速入门教程))