# Numpy_Note

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

## 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.

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

_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提供了多种方法来索引数组。

#### 整型矩阵索引

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:

#### Boolean Array Indexing

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

_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.

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

#### Array Math

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

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

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

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:

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:

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

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对数组进行运算时，最基本的情况是两个数组的维度相同，例如：

_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.

An example of broadcasting in practice:

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:

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最大值的索引

2.保存为npy文件

numpy.savez函数

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:

4.hstack and vstack

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