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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
import numpy as np
a1 = np.array([1, 2, 3, 4, 5]) --> array([1, 2, 3, 4, 5])
type(a1) --> numpy.ndarray
np.size(a1) --> 5
a2 = np.array([1, 2, 3, 4.0, 5.0]) --> array([1., 2., 3., 4., 5.])
a2.dtype --> dtype('float64')
np.zeros(5) --> array([0., 0., 0., 0., 0.])
np.arange(5) --> array([0, 1, 2, 3, 4])
np.linspace(0, 5, 6) --> array([0., 1., 2., 3., 4., 5.])
a1 * 2 = array([2, 4, 6, 8, 10])
a1 + a2 = array([2.0, 4.0, 6.0, 8.0, 10.0])
np.array([[1, 2], [3, 4]]) --> array([[1, 2],
[3, 4]])
m = np.arange(0, 6).reshape(2, 3) --> array([[0, 1, 2],
[3, 4, 5]])
np.size(m) --> 6
m.shape --> (2, 3)
np.size(m, 0) --> 2
np.size(m, 1) --> 3
m[1] --> array([3, 4, 5])
m[1, ] --> array([3, 4, 5])
m[1, 2] --> 5
m[:, 1] --> array([1, 4])
m[::-1, ::-1] --> array([[5, 4, 3],
[2, 1, 0]])
a = np.arange(5)
a < 2 --> array([True, True, False, False, False], dtype=bool)
(a < 2) | (a > 3) --> array([True, True, False, False, True], dtype=bool)
np.sum(a < 2) --> 2
def func(x):
return x < 2 or x > 3
np.vectorize(func)(a) --> array([True, True, False, False, True], dtype=bool)
r = a < 2
a[r] --> array([0, 1])
a1 = np.arange(1, 10) -- > array([1, 2, 3, 4, 5, 6, 7, 8, 9])
a1[3:8] --> array([4, 5, 6, 7, 8])
a1[8:3:-1] --> array([9, 8, 7, 6, 5])
a1[::-2] --> array([9, 7, 5, 3, 1])
a = np.arange(0, 9)
m = a.reshape(3, 3) --> array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
m.ravel() --> array([0, 1, 2, 3, 4, 5, 6, 7, 8]) (return a new array)
m.flatten() --> array([0, 1, 2, 3, 4, 5, 6, 7, 8]) (return a copy view)
m.flatten().shape --> (9, ) (1 dimension array)
m.transpose() --> array([[0, 3, 6],
[1, 4, 7],
[2, 5, 8]]) (identical: m.T)
m.resize(1, 9) --> array([[0, 1, 2, 3, 4, 5, 6, 7, 8]]) (1 * 9 array, inplace transform)
|