numpy的基本用法(一)——基本运算

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

本文主要是关于numpy的一些基本运算的用法。

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python
# _*_ coding: utf-8 _*_

import numpy as np

# Test 1
# 定义矩阵
arr = np.array([[1, 2, 3],
[4, 5, 6]])
print arr

# Test 1 Result
[[1 2 3]
[4 5 6]]

# Test 2
# 矩阵的维度
print 'number of dim: ', arr.ndim
# 矩阵的shape,即每一维度上的元素个数
print 'shape: ', arr.shape
# 矩阵的元素总数
print 'size: ', arr.size
# 矩阵的元素类型
print 'dtype: ', arr.dtype

# Test 2 Result
number of dim: 2
shape: (2, 3)
size: 6
dtype: int64

# Test 3
# 定义矩阵及矩阵的元素类型——int32, int64, float32, float64
a = np.array([1, 2, 3], dtype = np.int32)
print a
print a.ndim
print a.shape
print a.size
print a.dtype


# Test 3 Result
[1 2 3]
1
(3,)
3
int32

# Test 4
# 定义零矩阵
z = np.zeros((3, 4), dtype = np.int16)
print z
print z.dtype

# 定义空矩阵
n = np.empty((3, 4))
print n

# Test 4 Result
[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]
int16

[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]

# Test 5
# 定义向量, 10-20之间, 元素间隔为2, 左闭右开
a = np.arange(10, 20, 2)
print a

# 定义向量并转为矩阵
b = np.arange(12).reshape((3, 4))
print b

# 定义向量, 类型是线性间隔
a = np.linspace(1, 10, 6).reshape((2, 3))
print a

# Test 5 Result
[10 12 14 16 18]

[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]

[[ 1. 2.8 4.6]
[ 6.4 8.2 10. ]]

# Test 6
# 矩阵的加、减、点乘、平方
a = np.array([10, 20, 30, 40])
b = np.arange(4)
c = a - b
d = a + b
print a, b
print c, d
e = a * b
print e
f = e ** 2
print f

# Test 6 Result
[10 20 30 40] [0 1 2 3]
[10 19 28 37] [10 21 32 43]
[ 0 20 60 120]
[ 0 400 3600 14400]

# Test 7
# 矩阵的三角运算——sin, cos, tan
sin = 10 * np.sin(a)
print sin

# 矩阵的判断
print b < 3
print b == 3

# Test 7 Result
[-5.44021111 9.12945251 -9.88031624 7.4511316 ]

[ True True True False]
[False False False True]

# Test 8
# 矩阵的点乘及乘法
a = [ [1, 1], [0, 1]]
b = np.arange(4).reshape((2, 2))
c = a * b
d = np.dot(a, b)
print c
print d

# Test 8 Result
[[0 1]
[0 3]]
[[2 4]
[2 3]]

# Test 9
# np.random返回随机的浮点数,在半开区间 [0.0, 1.0)
# 定义随机矩阵
a = np.random.random((2, 4))
print a

# Test 9 Result
[[ 0.93213483 0.58102186 0.98259187 0.27387014]
[ 0.43796835 0.98195976 0.29343791 0.94752226]]

# Test 10
# 矩阵的求和, 最小值, 最大值
print np.sum(a)
print np.min(a)
print np.max(a)

# 矩阵某一维度的求和, 最小值, 最大值, 0是列, 1是行
print np.sum(a, axis = 1)
print np.max(a, axis = 1)
print np.min(a, axis = 0)

# Test 10 Result
5.43050697485
0.273870140282
0.982591870104

[ 2.7696187 2.66088828]
[ 0.98259187 0.98195976]
[ 0.43796835 0.58102186 0.29343791 0.27387014]

参考资料

  1. https://www.youtube.com/user/MorvanZhou
如果有收获,可以请我喝杯咖啡!