Tutorials

  • 强烈建议通过教程中提供的 colab 来进行学习

    Notes

    Arrays

  • 可以通过 python 的 list 来创建一个 array,并且通过方括号来访问其中的元素。

  • 在指定 shape 时,靠后的参数对应的是更靠近单一元素的,表述为语言可以这样理解:

    • 假设x = np.ones((2,3,4))
      • 首先其中 234=24 个元素中,四个四个打包成一组,形成六组,再三个三个打包成一组,形成两组,再两个两个一组,形成x
      • 反过来说,则表示x是一个具有 2 个元素的数组(1),而这个数组的 element 的类型也是一个数组(2);这个数组(2)有 3 个元素,这 3 个 elements 的类型也是一个数组(3);这个数组(3)有 4 个元素,它们都是一个数。
    • 此外,需要说明的是,这些索引的最左是axis=0,即在该例子中,axis=2才指代4的维度。
      1. [[[1 1 1 1]
      2. [1 1 1 1]
      3. [1 1 1 1]]
      4. [[1 1 1 1]
      5. [1 1 1 1]
      6. [1 1 1 1]]]
  • Arrays 可以像 python 的 list 一样使用切片语法,你必须为每个维度指定好切片。 ```python import numpy as np

Create the following rank 2 array with shape (3, 4)

[[ 1 2 3 4]

[ 5 6 7 8]

[ 9 10 11 12]]

a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])

Use slicing to pull out the subarray consisting of the first 2 rows

and columns 1 and 2; b is the following array of shape (2, 2):

[[2 3]

[6 7]]

b = a[:2, 1:3] print(b)

Output:

[[2 3]

[6 7]]

  1. - 这里需要注意一点,在某一维度中,倘若切片的效果都是选取索引中的某一个整数,例如`1``1:2`,其效果是有区别的:
  2. - 具体来说,如果使用`1`,则会直接**降维**,将那一维度的内容直接取出来;而使用`1:2`,则**不会降维**。
  3. ```python
  4. import numpy as np
  5. # Create the following rank 2 array with shape (3, 4)
  6. # [[ 1 2 3 4]
  7. # [ 5 6 7 8]
  8. # [ 9 10 11 12]]
  9. a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
  10. # Two ways of accessing the data in the middle row of the array.
  11. # Mixing integer indexing with slices yields an array of lower rank,
  12. # while using only slices yields an array of the same rank as the
  13. # original array:
  14. row_r1 = a[1, :] # Rank 1 view of the second row of a
  15. row_r2 = a[1:2, :] # Rank 2 view of the second row of a
  16. print row_r1, row_r1.shape # Prints "[5 6 7 8] (4,)"
  17. print row_r2, row_r2.shape # Prints "[[5 6 7 8]] (1, 4)"
  18. # We can make the same distinction when accessing columns of an array:
  19. col_r1 = a[:, 1]
  20. col_r2 = a[:, 1:2]
  21. print col_r1, col_r1.shape # Prints "[ 2 6 10] (3,)"
  22. print col_r2, col_r2.shape # Prints "[[ 2]
  23. # [ 6]
  24. # [10]] (3, 1)"
  • 还可以通过数组来作为 index 访问一个 array,其方法如下:
    • 简单来说就是:
      • 例如直接访问一个二维数组a,我们使用a[x_idx, y_idx],那现在只需要把x_idxy_idx换做x_idx[]y_idx[],即两个n个元素的 list ,分别表示得到的n个元素的结果list中每一个元素的x_idxy_idx。 ```python 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]”

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. - 以及其他的整数数组访问方法,详细参考 colab 的交互内容
  2. - 你还可以通过布尔数组来访问,即满足特定要求的元素被筛选出来:
  3. ```python
  4. import numpy as np
  5. a = np.array([[1,2], [3, 4], [5, 6]])
  6. bool_idx = (a > 2) # Find the elements of a that are bigger than 2;
  7. # this returns a numpy array of Booleans of the same
  8. # shape as a, where each slot of bool_idx tells
  9. # whether that element of a is > 2.
  10. print bool_idx # Prints "[[False False]
  11. # [ True True]
  12. # [ True True]]"
  13. # We use boolean array indexing to construct a rank 1 array
  14. # consisting of the elements of a corresponding to the True values
  15. # of bool_idx
  16. print a[bool_idx] # Prints "[3 4 5 6]"
  17. # We can do all of the above in a single concise statement:
  18. print a[a > 2] # Prints "[3 4 5 6]"

计算

  • 首先,+``-``*``/都被重载为逐项运算,也可以使用np.add()``np.subtract()``np.multiply()``np.divide()(np**.**sqrt())实现。
  • 对于矩阵乘法,应使用.dot()实现: ```python 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)

  1. 其他操作详见 colab
  2. <a name="JXdIw"></a>
  3. ## 广播机制
  4. 建议结合例子分析
  5. ```python
  6. import numpy as np
  7. # We will add the vector v to each row of the matrix x,
  8. # storing the result in the matrix y
  9. x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
  10. v = np.array([1, 0, 1])
  11. y = x + v # Add v to each row of x using broadcasting
  12. print y # Prints "[[ 2 2 4]
  13. # [ 5 5 7]
  14. # [ 8 8 10]
  15. # [11 11 13]]"