1 import numpy as np
2
3 '''
4 数组:一维,秩为1
5 利用Numpy中random模块中的randn函数生成的一维数组,
6 既不是行向量,也不是列向量,而是秩为1的数组,
7 特点:
8 只有一个'[]'
9 是数组,不是向量或矩阵
10 '''
11 a = np.random.randn(5)
12 print(a)
13 #[ 2.50110276 1.16190518 0.2125841 -1.35464964 -1.6559847 ]
14 print(a.ndim, a.shape)
15 # 1, (5,)
16 print(a.T)
17 #[ 2.50110276 1.16190518 0.2125841 -1.35464964 -1.6559847 ]
18 print((a.T).ndim, (a.T).shape)
19 # 1, (5,)
20 print("--------\n")
21
22
23
24 '''
25 向量和矩阵都是为二维数组
26 '''
27
28 '''
29 行向量:
30 特点:两个方括号'[]'
31 '''
32 b = np.random.randn(1,5) #1行5列的行向量
33 print(b)
34 #[[ 0.16572125 0.61840102 -0.06370723 -0.56107341 1.04560651]]
35 print(b.ndim, b.shape)
36 # 2, (1, 5)
37 print(b.T)
38 '''
39 [[ 0.16572125]
40 [ 0.61840102]
41 [-0.06370723]
42 [-0.56107341]
43 [ 1.04560651]]
44 '''
45 print((b.T).ndim, (b.T).shape)
46 # 2, (5,1)
47 print("--------\n")
48
49
50 '''
51 列向量
52 '''
53 e = np.random.randn(5,1) #5行1列的列向量
54 print(e)
55 '''
56 [[-0.50398456]
57 [-1.12094662]
58 [-0.43209098]
59 [ 0.23721518]
60 [-0.58687296]]
61 '''
62 print(e.ndim, e.shape)
63 #2, (5,1)
64 print(e.T)
65 #[[-0.50398456 -1.12094662 -0.43209098 0.23721518 -0.58687296]]
66 print((e.T).ndim, (e.T).shape)
67 #2 (1, 5)
68 print("--------\n")
69
70
71 '''
72 矩阵:
73 既有行向量也有列向量,两个方括号'[]'
74 '''
75 c = np.random.randn(5,2)
76 print(c.ndim, c.shape)
77 #2 (5, 2)
78 print(c)
79 '''
80 [[-0.03726232 0.3003853 ]
81 [-0.52656126 1.77452533]
82 [ 1.13944323 0.53734788]
83 [-0.12426997 -0.78777449]
84 [-0.49564334 -0.22442305]]
85
86 1. 最外层的方括号表示矩阵
87 2. 内层的方括号是每行有一对:内层有5对方括号,表示5行
88 3. 内层每对方括号内有2个元素,表示2列
89 '''
90 print(c.T)
91 '''
92 [[-0.03726232 -0.52656126 1.13944323 -0.12426997 -0.49564334]
93 [ 0.3003853 1.77452533 0.53734788 -0.78777449 -0.22442305]]
94 '''
95 print((c.T).ndim, (c.T).shape)
96 #2 (2, 5)
97
98 '''
99 数组、向量、矩阵的区别:
100 可以以方括号的形式判断数组是否能够代表一个向量或者矩阵,
101 又或者通过转置看前后是否变化来判断。
102 '''
