-
Notifications
You must be signed in to change notification settings - Fork 2
/
scientific_computing_with_numpy.py
70 lines (48 loc) · 1.42 KB
/
scientific_computing_with_numpy.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
import numpy as np
mylist_2d = [[87, 84, 91],
[19, 64, 25],
[24, 95, 9]]
mymatrix_2d = np.array(mylist_2d)
mymatrix_2d.shape
myvector_1d = np.array([87, 84, 91, 19, 64])
# Slicing
print(myvector_1d[1:4])
print(mymatrix_2d[:, 1])
print(mymatrix_2d[0:2, 1:3])
print(mymatrix_2d[[0, 2]])
mymatrix_3d = np.array([[[1, 2], [1, 2]], [[1, 2], [1, 2]]])
random_vec = np.random.uniform(low=0, high=10, size=50)
print(random_vec < 5)
print(random_vec[random_vec < 5])
print(random_vec[np.logical_not(random_vec < 5)])
vector_sum = 0
for row in random_vec:
vector_sum += row
print(random_vec.sum())
vector1 = np.random.uniform(low=-100, high=100, size=3)
vector2 = np.random.uniform(low=-100, high=100, size=3)
print(vector1 * vector2)
print(vector1 + vector2)
print(vector1 ** 3)
# dot product
print(np.dot(vector1, vector2))
print(np.sum(vector1 * vector2))
print(vector1.dot(vector2))
# Matrix dot vector
print(mymatrix_2d.dot(vector1))
print(mymatrix_2d.dot(vector1).dot(vector2))
matrix2 = np.random.uniform(low=-100, high=100, size=(3, 3))
# Matrix matrix dot product
print(matrix2.dot(mymatrix_2d))
print(mymatrix_2d.dot(matrix2))
# Useful methods
print(random_vec.cumsum())
print(random_vec.mean())
print(random_vec.max())
print(random_vec.std())
print(random_vec.round(2))
# Transpose two-dimensional matrix
print(matrix2.T)
# Convert a numpy array to a Python list
random_vec.tolist()