Python Numpy Tutorial based on http://cs231n.github.io/python-numpy-tutorial/.

主要内容：Arrays,Array Indexing,Datatypes,Array Math,Broadcasting

## Arrays

A numpy array is a grid of values, all of the same type, and is indexed by a tuple of nonnegative integers. The number of dimensions is the rank of the array; the shape of an array is a tuple of integers giving the size of the array along each dimension.

我们可以使用嵌套Python列表来初始化一个numpy数组，使用方括号来访问元素。

1 | import numpy as np |

Numpy还提供很多方法来创建array

1 | import numpy as np |

_More details about numpy.random in https://docs.scipy.org/doc/numpy/reference/routines.random.html_

_More details about numpy array creation in https://docs.scipy.org/doc/numpy/user/basics.creation.html#arrays-creation_

## Array Indexing

Numpy提供了多种方法来索引数组。

#### Slicing

和Python的列表一样Numpy数组也是可以被切片的。因为数组可能是多维的，所以对数组的每一个元素都要指定slice。

1 | import numpy as np |

你也可以混合整型索引和slice索引。然而这样做将会得到一个相比原始数组rank更低的数组。

注意这和MATLAB处理数组slicing的方法有很大的不同。

1 | import numpy as np |

#### 整型矩阵索引

When you index into numpy arrays using slicing, the resulting array view will always be a subarray of the original array. In contrast, integer array indexing allows you to construct arbitrary arrays using the data from another array. Here is an example:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17 import numpy as np

a = np.array([[1,2], [3, 4], [5, 6]])

# An example of integer array indexing.

# The returned array will have shape (3,) and

print a[[0, 1, 2], [0, 1, 0]] # Prints "[1 4 5]",可以看成[a[0, 0], a[1, 1], a[2, 0]]

# The above example of integer array indexing is equivalent to this:

print np.array([a[0, 0], a[1, 1], a[2, 0]]) # Prints "[1 4 5]"

# When using integer array indexing, you can reuse the same

# element from the source array:

print a[[0, 0], [1, 1]] # Prints "[2 2]"

# Equivalent to the previous integer array indexing example

print np.array([a[0, 1], a[0, 1]]) # Prints "[2 2]"

整型数组索引的一个实用技巧是用来选择或变换矩阵的每一行的一个元素。

1 | import numpy as np |

#### Boolean Array Indexing

Boolean array indexing可以取得一个矩阵中的任意元素。通常这种索引方式用来选择符合一定条件的元素。例如：

1 | import numpy as np |

_More details in https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html_

#### Datatypes

Every numpy array is a grid of elements of the same type. Numpy provides a large set of numeric datatypes that you can use to construct arrays. Numpy tries to guess a datatype when you create an array, but functions that construct arrays usually also include an optional argument to explicitly specify the datatype.

1

2

3

4

5

6

7

8

9

10 import numpy as np

x = np.array([1, 2]) # Let numpy choose the datatype

print(x.dtype) # Prints "int32"

x = np.array([1.0, 2.0]) # Let numpy choose the datatype

print(x.dtype) # Prints "float64"

x = np.array([1, 2], dtype=np.int64) # Force a particular datatype

print(x.dtype) # Prints "int64"

_More detailsi in https://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html_

#### Array Math

基本数学计算函数会对数组中元素逐个进行计算，既可以利用操作符重载，也可以使用函数方式

1 | import numpy as np |

Note that unlike MATLAB, * is elementwise multiplication, not matrix multiplication. We instead use the dot function to compute inner products of vectors, to multiply a vector by a matrix, and to multiply matrices. dot is available both as a function in the numpy module and as an instance method of array objects.More details in https://docs.scipy.org/doc/numpy/reference/generated/numpy.dot.html

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21 import numpy as np

x = np.array([[1,2],[3,4]])

y = np.array([[5,6],[7,8]])

v = np.array([9,10])

w = np.array([11, 12])

# Inner product of vectors; both produce 219;向量内积

print(v.dot(w))

print(np.dot(v, w))

# Matrix / vector product; both produce the rank 1 array [29 67]

print(x.dot(v))

print(np.dot(x, v))

# Matrix / matrix product; both produce the rank 2 array；矩阵乘法

# [[19 22]

# [43 50]]

print(x.dot(y))

print(np.dot(x, y))

Numpy provides many useful functions for performing computations on arrays; one of the most useful is sum:

1 | import numpy as np |

_More details in https://docs.scipy.org/doc/numpy/reference/routines.math.html_

#### Broadcasting

Broadcasting is a powerful mechanism that allows numpy to work with arrays of different shapes when performing arithmetic operations. Frequently we have a smaller array and a larger array, and we want to use the smaller array multiple times to perform some operation on the larger array.

For example, suppose that we want to add a constant vector to each row of a matrix. We could do it like this:

1 | import numpy as np |

This works; however when the matrix x is very large, computing an explicit loop in Python could be slow. Note that adding the vector v to each row of the matrix x is equivalent to forming a matrix vv by stacking multiple copies of v vertically, then performing elementwise summation of x and vv. We could implement this approach like this:

1 | import numpy as np |

Numpy broadcasting allows us to perform this computation without actually creating multiple copies of v. Consider this version, using broadcasting:

1 | import numpy as np |

The line y = x + v works even though x has shape (4, 3) and v has shape (3,) due to broadcasting; this line works as if v actually had shape (4, 3), where each row was a copy of v, and the sum was performed elementwise.

The term broadcasting describes how numpy treats arrays with different shapes during arithmetic operations. Subject to certain constraints, the smaller array is “broadcast” across the larger array so that they have compatible shapes. Broadcasting provides a means of vectorizing array operations so that looping occurs in C instead of Python. It does this without making needless copies of data and usually leads to efficient algorithm implementations. There are, however, cases where broadcasting is a bad idea because it leads to inefficient use of memory that slows computation.

Numpy对数组进行运算时，最基本的情况是两个数组的维度相同，例如：

1 | 1.0, 2.0, 3.0]) a = np.array([ |

当两个数组的维度不同时，但又满足一定的约束，亦可以进行运算，即为广播。

1 | 1.0, 2.0, 3.0]) a = np.array([ |

_Note: The result is equivalent to the previous example where b was an array. We can think of the scalar b being stretched during the arithmetic operation into an array with the same shape as a. The new elements in b are simply copies of the original scalar. The stretching analogy is only conceptual. NumPy is smart enough to use the original scalar value without actually making copies, so that broadcasting operations are as memory and computationally efficient as possible._**Rules:**

When operating on two arrays, NumPy compares their shapes element-wise.Two dimensions are compatible when

- they are equal, or(某个维度上的shape相等)
- one of them is 1(某个维度上的shape=1)
- 所有的维度都应该满足以上条件之一

If these conditions are not met, a ValueError: frames are not aligned exception is thrown, indicating that the arrays have incompatible shapes.

The size of the resulting array is the maximum size along each dimension of the input arrays.1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29A (4d array): 8 x 1 x 6 x 1

B (3d array): 7 x 1 x 5

Result (4d array): 8 x 7 x 6 x 5

A (2d array): 5 x 4

B (1d array): 1

Result (2d array): 5 x 4

A (2d array): 5 x 4

B (1d array): 4

Result (2d array): 5 x 4

A (3d array): 15 x 3 x 5

B (3d array): 15 x 1 x 5

Result (3d array): 15 x 3 x 5

A (3d array): 15 x 3 x 5

B (2d array): 3 x 5

Result (3d array): 15 x 3 x 5

A (3d array): 15 x 3 x 5

B (2d array): 3 x 1

Result (3d array): 15 x 3 x 5

A (1d array): 3

B (1d array): 4 # trailing dimensions do not match

A (2d array): 2 x 1

B (3d array): 8 x 4 x 3 # second from last dimensions mismatched

An example of broadcasting in practice:

1 | 4) # [0,1,2,3] x = np.arange( |

Broadcasting provides a convenient way of taking the outer product (or any other outer operation) of two arrays. The following example shows an outer addition operation of two 1-d arrays:

1 | 0.0, 10.0, 20.0, 30.0]) a = np.array([ |

Here the **newaxis** index operator inserts a new axis into a, making it a two-dimensional 4x1 array. Combining the 4x1 array with b, which has shape (3,), yields a 4x3 array.

_Functions that support broadcasting are known as universal functions. You can find the list of all universal functions in https://docs.scipy.org/doc/numpy/reference/ufuncs.html#available-ufuncs._

#### Other

1.numpy.argmax(a, axis=None, out=None) : 返回沿轴axis最大值的索引

1 | 6).reshape(2,3) a = np.arange( |

2.保存为npy文件

以NumPy专用的二进制类型保存数据，这两个函数会自动处理元素类型和shape等信息，使用它们读写数组就方便多了，但是numpy.save输出的文件很难和其它语言编写的程序读入：

1 | p = np.array( |

**numpy.savez函数**

如果你想将多个数组保存到一个文件中的话，可以使用numpy.savez函数。savez函数的第一个参数是文件名，其后的参数都是需要保存的数组，也可以使用关键字参数为数组起一个名字，非关键字参数传递的数组会自动起名为arr_0, arr_1, …。savez函数输出的是一个压缩文件(扩展名为npz)，其中每个文件都是一个save函数保存的npy文件，文件名对应于数组名。load函数自动识别npz文件，并且返回一个类似于字典的对象，可以通过数组名作为关键字获取数组的内容：

1 | 1,2,3],[4,5,6]]) a = np.array([[ |

如果你用解压软件打开result.npz文件的话，会发现其中有三个文件：arr_0.npy， arr_1.npy， sin_array.npy，其中分别保存着数组a, b, c的内容。

3.reshape

It is not always possible to change the shape of an array without copying the data. If you want an error to be raise if the data is copied, you should assign the new shape to the shape attribute of the array:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18 6).reshape((3, 2)) a = np.arange(

a

array([[0, 1],

[2, 3],

[4, 5]])

2, 3)) # C-like index ordering np.reshape(a, (

array([[0, 1, 2],

[3, 4, 5]])

2, 3)) # equivalent to C ravel then C reshape np.reshape(np.ravel(a), (

array([[0, 1, 2],

[3, 4, 5]])

2, 3), order='F') # Fortran-like index ordering np.reshape(a, (

array([[0, 4, 3],

[2, 1, 5]])

'F'), (2, 3), order='F') np.reshape(np.ravel(a, order=

array([[0, 4, 3],

[2, 1, 5]])

_More details in https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html_

4.hstack and vstack

函数原型：hstack(tup) ，参数tup可以是元组，列表，或者numpy数组，返回结果为numpy的数组.它其实就是水平(按列顺序)把数组给堆叠起来，vstack()函数正好和它相反。

1 | import numpy as np |

_More details in http://blog.csdn.net/csdn15698845876/article/details/73380803_