-
Notifications
You must be signed in to change notification settings - Fork 419
Expand file tree
/
Copy pathindex.html
More file actions
305 lines (229 loc) · 11.6 KB
/
Copy pathindex.html
File metadata and controls
305 lines (229 loc) · 11.6 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>9.7. NumPy</title><link rel="stylesheet" type="text/css" href="../docbook.css" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><meta name="keywords" content="php,pear,pecl,phar, python, , " /><link rel="home" href="../index.html" title="Netkiller Python 手札" /><link rel="up" href="../ai.html" title="第 9 章 AI 相关" /><link rel="prev" href="../ch09s06.html" title="9.6. GPU" /><link rel="next" href="../tensorflow.html" title="9.8. tensorflow" /></head><body><a xmlns="" href="//www.netkiller.cn/">Home</a> | <a xmlns="" href="//netkiller.github.io/">简体中文</a> | <a xmlns="" href="http://netkiller.sourceforge.net/">繁体中文</a> | <a xmlns="" href="/journal/index.html">杂文</a>
| <a xmlns="" href="https://github.com/netkiller">Github</a> | <a xmlns="" href="https://zhuanlan.zhihu.com/netkiller">知乎专栏</a> | <a xmlns="" href="https://www.facebook.com/bg7nyt">Facebook</a> | <a xmlns="" href="http://cn.linkedin.com/in/netkiller/">Linkedin</a> | <a xmlns="" href="https://www.youtube.com/user/bg7nyt/videos">Youtube</a> | <a xmlns="" href="//www.netkiller.cn/home/donations.html">打赏(Donations)</a> | <a xmlns="" href="//www.netkiller.cn/home/about.html">About</a><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">9.7. NumPy</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="../ch09s06.html">上一页</a> </td><th width="60%" align="center">第 9 章 AI 相关</th><td width="20%" align="right"> <a accesskey="n" href="../tensorflow.html">下一页</a></td></tr></table><hr /></div><table xmlns=""><tr><td><iframe src="//ghbtns.com/github-btn.html?user=netkiller&repo=netkiller.github.io&type=watch&count=true&size=large" height="30" width="170" frameborder="0" scrolling="0" style="width:170px; height: 30px;" allowTransparency="true"></iframe></td><td><iframe src="//ghbtns.com/github-btn.html?user=netkiller&repo=netkiller.github.io&type=fork&count=true&size=large" height="30" width="170" frameborder="0" scrolling="0" style="width:170px; height: 30px;" allowTransparency="true"></iframe></td><td><iframe src="//ghbtns.com/github-btn.html?user=netkiller&type=follow&count=true&size=large" height="30" width="240" frameborder="0" scrolling="0" style="width:240px; height: 30px;" allowTransparency="true"></iframe></td><td></td><td><a href="https://zhuanlan.zhihu.com/netkiller"><img src="/images/logo/zhihu-card-default.svg" height="25" /></a></td><td valign="middle"><a href="https://zhuanlan.zhihu.com/netkiller">知乎专栏</a></td><td></td><td></td><td></td><td></td></tr></table><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="index"></a>9.7. NumPy</h2></div></div></div>
<p>NumPy is the fundamental package for scientific computing with Python.</p>
<div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="numpy.random"></a>9.7.1. 随机数</h3></div></div></div>
<div class="section"><div class="titlepage"><div><div><h4 class="title"><a id="id1063"></a>9.7.1.1. 随机种子</h4></div></div></div>
<p>np.random.seed(1) 两次产生的随机数相同</p>
<pre class="programlisting">
import numpy as np
np.random.seed(1)
L1 = np.random.randn(3, 3)
L2 = np.random.randn(3, 3)
print(L1)
print(L2)
</pre>
<pre class="screen">
[[-1.32959475 -0.35726593 -1.01824748]
[-0.16875459 -1.40799966 -0.42432159]
[-2.90363742 0.77847352 -0.03868502]]
[[ 0.8140817 0.47350462 -0.44832424]
[-0.21463067 0.18678995 -1.56375306]
[ 0.39748247 -0.74620674 -0.97122838]]
</pre>
</div>
<div class="section"><div class="titlepage"><div><div><h4 class="title"><a id="id1064"></a>9.7.1.2. 随机矩阵数组</h4></div></div></div>
<p>生成随机浮点矩阵数组</p>
<pre class="programlisting">
import numpy as np
number = np.random.rand(10, 5)
print(number)
</pre>
<pre class="screen">
neo@MacBook-Pro-Neo ~/workspace/python % python3.9 /Users/neo/workspace/python/numpy/test.py
[[0.07210811 0.89871612 0.31670349 0.88870892 0.38252093]
[0.08210199 0.37878429 0.09693934 0.53084051 0.81222326]
[0.99527501 0.39815405 0.02937093 0.21271075 0.09775669]
[0.97038382 0.10373132 0.60815363 0.00740848 0.51247618]
[0.77290466 0.7961732 0.21776523 0.27498686 0.84316289]
[0.11457979 0.98606765 0.36357378 0.00754072 0.62702464]
[0.19330684 0.60832298 0.57052479 0.81215836 0.04167786]
[0.71456373 0.9203253 0.27650414 0.6247527 0.28517774]
[0.85126634 0.06420073 0.92123025 0.84654969 0.11828913]
[0.38481704 0.95317434 0.62498057 0.5297113 0.22969415]]
</pre>
<pre class="programlisting">
pd.DataFrame(np.random.rand(10,2))
</pre>
</div>
<div class="section"><div class="titlepage"><div><div><h4 class="title"><a id="id1065"></a>9.7.1.3. 生成随机整数矩阵数组</h4></div></div></div>
<p>np.random.randint(从 0 ,到 255,(5,6,3))</p>
<pre class="programlisting">
import numpy as np
np.random.seed(1)
x = np.random.randint(0,255,(5,6,3))
print(x)
</pre>
<pre class="screen">
[[[ 37 235 140]
[ 72 137 203]
[133 79 192]
[144 129 204]
[ 71 237 252]
[134 25 178]]
[[ 20 254 101]
[146 212 139]
[252 234 156]
[157 142 50]
[ 68 215 215]
[233 241 247]]
[[222 96 86]
[141 233 137]
[ 7 63 61]
[ 22 57 1]
[128 60 209]
[ 8 216 141]]
[[115 175 234]
[121 200 30]
[ 71 131 198]
[149 49 57]
[ 3 196 24]
[241 43 76]]
[[ 26 52 80]
[109 115 41]
[210 15 64]
[196 25 111]
[226 215 135]
[ 26 153 104]]]
</pre>
</div>
</div>
<div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="numpy.linspace"></a>9.7.2. 生成数列</h3></div></div></div>
<p>从0 到 100,间隔为10的数值序列</p>
<pre class="programlisting">
n = np.linspace(start = 0, stop = 100, num = 11)
x = np.linspace(-3, 3, 50)
</pre>
</div>
<div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="numpy.shape"></a>9.7.3. 查看矩阵或者数组的维度</h3></div></div></div>
<pre class="programlisting">
import numpy as np
np.random.seed(1)
matrix = np.random.randint(0,255,(5,6,3))
print(matrix.shape)
</pre>
<pre class="screen">
(5, 6, 3)
</pre>
<div class="section"><div class="titlepage"><div><div><h4 class="title"><a id="id1066"></a>9.7.3.1. 数组维度</h4></div></div></div>
<pre class="programlisting">
from numpy import array
a = array([[1,1],[1,2],[1,3],[1,4]])
print(a.shape)
</pre>
<pre class="screen">
(4, 2)
</pre>
</div>
</div>
<div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="numpy.dtype"></a>9.7.4. 数据类型</h3></div></div></div>
<div class="section"><div class="titlepage"><div><div><h4 class="title"><a id="id1067"></a>9.7.4.1. 显示数据类型</h4></div></div></div>
<pre class="programlisting">
import numpy
numpy.random.seed(1)
matrix = numpy.random.randint(0,255,(5,6,3))
print(matrix.dtype)
</pre>
</div>
<div class="section"><div class="titlepage"><div><div><h4 class="title"><a id="id1068"></a>9.7.4.2. 转换数据类型</h4></div></div></div>
<pre class="programlisting">
import numpy
numpy.random.seed(1)
matrix = numpy.random.randint(0,255,(5,6,3))
print(matrix.dtype)
matrix1 = matrix.astype(np.int8)
print(matrix1.dtype)
</pre>
</div>
</div>
<div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="numpy.max"></a>9.7.5. max</h3></div></div></div>
<pre class="programlisting">
# 创建一个包含学生成绩的二维数组
data = np.array([
['Alice', 85, 92, 78],
['Bob', 89, 90, 95],
['Charlie', 91, 85, 88],
['David', 78, 80, 82]
])
# 提取成绩列(忽略姓名列)
grades = data[:, 1:].astype(int)
print(grades)
# 计算每位学生的最高成绩
max_grades = np.max(grades, axis=1)
print(max_grades)
# 打印结果
for i, student in enumerate(data[:, 0]):
print(f"{student} 的最好成绩是: {max_grades[i]}")
</pre>
</div>
<div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="numpy.amax"></a>9.7.6. max</h3></div></div></div>
<pre class="programlisting">
import numpy as np
help(np.amax)
a = np.arange(9).reshape((3, 3))
max_all = np.amax(a)
max_dimension1 = np.amax(a, axis=0)
max_dimension2 = np.amax(a, axis=1)
print('a:\n', a)
print('max_all:', max_all)
print('max_dimension1:', max_dimension1)
print('max_dimension2:', max_dimension2)
</pre>
<pre class="screen">
D:\workspace\netkiller\.venv\Scripts\python.exe D:\workspace\netkiller\test\test1.py
Help on _ArrayFunctionDispatcher in module numpy:
amax(a, axis=None, out=None, keepdims=<no value>, initial=<no value>, where=<no value>)
Return the maximum of an array or maximum along an axis.
`amax` is an alias of `~numpy.max`.
See Also
--------
max : alias of this function
ndarray.max : equivalent method
a:
[[0 1 2]
[3 4 5]
[6 7 8]]
max_all: 8
max_dimension1: [6 7 8]
max_dimension2: [2 5 8]
</pre>
</div>
<div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="numpy.imshow"></a>9.7.7. 显示矩阵</h3></div></div></div>
<pre class="programlisting">
import numpy
from matplotlib import pyplot as plt
numpy.random.seed(1)
matrix = numpy.random.randint(0,255,(5,6,3))
print(matrix.shape)
plt.imshow(matrix)
</pre>
</div>
</div><script xmlns="" type="text/javascript" id="clustrmaps" src="//cdn.clustrmaps.com/map_v2.js?u=r5HG&d=9mi5r_kkDC8uxG8HuY3p4-2qgeeVypAK9vMD-2P6BYM"></script><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="../ch09s06.html">上一页</a> </td><td width="20%" align="center"><a accesskey="u" href="../ai.html">上一级</a></td><td width="40%" align="right"> <a accesskey="n" href="../tensorflow.html">下一页</a></td></tr><tr><td width="40%" align="left" valign="top">9.6. GPU </td><td width="20%" align="center"><a accesskey="h" href="../index.html">起始页</a></td><td width="40%" align="right" valign="top"> 9.8. tensorflow</td></tr></table></div><script xmlns="">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-11694057-1', 'auto');
ga('send', 'pageview');
</script><script xmlns="" async="async">
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?93967759a51cda79e49bf4e34d0b0f2c";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script><script xmlns="" async="async">
(function(){
var bp = document.createElement('script');
var curProtocol = window.location.protocol.split(':')[0];
if (curProtocol === 'https') {
bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
}
else {
bp.src = 'http://push.zhanzhang.baidu.com/push.js';
}
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(bp, s);
})();
</script></body></html>