forked from liuyubobobo/Play-with-Linear-Algebra
-
Notifications
You must be signed in to change notification settings - Fork 11
/
main_numpy_vector.py
45 lines (36 loc) · 1.07 KB
/
main_numpy_vector.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
import numpy as np
if __name__ == "__main__":
print(np.__version__)
# np.array 基础
lst = [1, 2, 3]
lst[0] = "Linear Algebra"
print(lst)
vec = np.array([1, 2, 3])
print(vec)
# vec[0] = "Linear Algebra"
# vec[0] = 666
# print(vec)
# np.array的创建
print(np.zeros(5))
print(np.ones(5))
print(np.full(5, 666))
# np.array的基本属性
print(vec)
print("size =", vec.size)
print("size =", len(vec))
print(vec[0])
print(vec[-1])
print(vec[0: 2])
print(type(vec[0: 2]))
# np.array的基本运算
vec2 = np.array([4, 5, 6])
print("{} + {} = {}".format(vec, vec2, vec + vec2))
print("{} - {} = {}".format(vec, vec2, vec - vec2))
print("{} * {} = {}".format(2, vec, 2 * vec))
print("{} * {} = {}".format(vec, vec2, vec * vec2))
print("{}.dot({}) = {}".format(vec, vec2, vec.dot(vec2)))
print(np.linalg.norm(vec))
print(vec / np.linalg.norm(vec))
print(np.linalg.norm(vec / np.linalg.norm(vec)))
# zero3 = np.zeros(3)
# print(zero3 / np.linalg.norm(zero3))