- Tutorials
- Notes
- Create the following rank 2 array with shape (3, 4)
- [[ 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]]
- Output:
- [[2 3]
- [6 7]]
- An example of integer array indexing.
- The returned array will have shape (3,) and
- The above example of integer array indexing is equivalent to this:
- When using integer array indexing, you can reuse the same
- element from the source array:
- Equivalent to the previous integer array indexing example
- Inner product of vectors; both produce 219
- Matrix / vector product; both produce the rank 1 array [29 67]
- Matrix / matrix product; both produce the rank 2 array
- [[19 22]
- [43 50]]
Tutorials
-
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 个元素,它们都是一个数。
- 首先其中 234=24 个元素中,四个四个打包成一组,形成六组,再三个三个打包成一组,形成两组,再两个两个一组,形成
- 此外,需要说明的是,这些索引的最左是
axis=0,即在该例子中,axis=2才指代4的维度。[[[1 1 1 1][1 1 1 1][1 1 1 1]][[1 1 1 1][1 1 1 1][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:2`,其效果是有区别的:- 具体来说,如果使用`1`,则会直接**降维**,将那一维度的内容直接取出来;而使用`1:2`,则**不会降维**。```pythonimport 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]])# Two ways of accessing the data in the middle row of the array.# Mixing integer indexing with slices yields an array of lower rank,# while using only slices yields an array of the same rank as the# original array:row_r1 = a[1, :] # Rank 1 view of the second row of arow_r2 = a[1:2, :] # Rank 2 view of the second row of aprint row_r1, row_r1.shape # Prints "[5 6 7 8] (4,)"print row_r2, row_r2.shape # Prints "[[5 6 7 8]] (1, 4)"# We can make the same distinction when accessing columns of an array:col_r1 = a[:, 1]col_r2 = a[:, 1:2]print col_r1, col_r1.shape # Prints "[ 2 6 10] (3,)"print col_r2, col_r2.shape # Prints "[[ 2]# [ 6]# [10]] (3, 1)"
- 还可以通过数组来作为 index 访问一个 array,其方法如下:
- 简单来说就是:
- 例如直接访问一个二维数组
a,我们使用a[x_idx, y_idx],那现在只需要把x_idx和y_idx换做x_idx[]和y_idx[],即两个n个元素的 list ,分别表示得到的n个元素的结果list中每一个元素的x_idx和y_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]”
- 以及其他的整数数组访问方法,详细参考 colab 的交互内容- 你还可以通过布尔数组来访问,即满足特定要求的元素被筛选出来:```pythonimport numpy as npa = np.array([[1,2], [3, 4], [5, 6]])bool_idx = (a > 2) # Find the elements of a that are bigger than 2;# this returns a numpy array of Booleans of the same# shape as a, where each slot of bool_idx tells# whether that element of a is > 2.print bool_idx # Prints "[[False False]# [ True True]# [ True True]]"# We use boolean array indexing to construct a rank 1 array# consisting of the elements of a corresponding to the True values# of bool_idxprint a[bool_idx] # Prints "[3 4 5 6]"# We can do all of the above in a single concise statement: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)
其他操作详见 colab 。<a name="JXdIw"></a>## 广播机制建议结合例子分析```pythonimport numpy as np# We will add the vector v to each row of the matrix x,# storing the result in the matrix yx = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])v = np.array([1, 0, 1])y = x + v # Add v to each row of x using broadcastingprint y # Prints "[[ 2 2 4]# [ 5 5 7]# [ 8 8 10]# [11 11 13]]"
