-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.xml
86 lines (41 loc) · 229 KB
/
search.xml
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
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title>Markdown学习笔记(一)</title>
<link href="2020/09/30/markdown-xue-xi-bi-ji-yi/"/>
<url>2020/09/30/markdown-xue-xi-bi-ji-yi/</url>
<content type="html"><![CDATA[<h3 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h3><p>学习书籍: 《了不起的markdown》</p><p>文中所有图片均摘自《了不起的markdown》原图</p><a id="more"></a><h3 id="一)基础语法"><a href="#一)基础语法" class="headerlink" title="一)基础语法"></a><strong>一)基础语法</strong></h3><ol><li><h4 id="标题"><a href="#标题" class="headerlink" title="标题"></a><strong>标题</strong></h4></li></ol><h5 id="1-1-使用底线-:-或者"><a href="#1-1-使用底线-:-或者" class="headerlink" title="1.1 使用底线 :- 或者="></a>1.1 使用底线 :- 或者=</h5><img src="/2020/09/30/markdown-xue-xi-bi-ji-yi/v2-393864b5bb24c77544713e77544c3811_1440w.jpg" class title="img"><p> 说明:底线符号最少两个 </p><h5 id="1-2-使用"><a href="#1-2-使用" class="headerlink" title="1.2 使用#"></a>1.2 使用#</h5><img src="/2020/09/30/markdown-xue-xi-bi-ji-yi/v2-fcaf95bf1d8a4de0c9cecdafc9f6bdf1_1440w.jpg" class title="img"><p>说明:最多使用前6级标题;建议再#后加一个空格。建议标题前后均空一行,便于阅读。</p><h4 id="2-粗体斜体"><a href="#2-粗体斜体" class="headerlink" title="2.粗体斜体"></a>2.<strong>粗体斜体</strong></h4><h5 id="2-1-粗体与斜体"><a href="#2-1-粗体与斜体" class="headerlink" title="2.1 粗体与斜体"></a>2.1 粗体与斜体</h5><img src="/2020/09/30/markdown-xue-xi-bi-ji-yi/v2-bbb38a043f929e7f5680fb6bf383ac99_b.png" class title="img"><h5 id="2-2-段落与换行"><a href="#2-2-段落与换行" class="headerlink" title="2.2 段落与换行"></a>2.2 段落与换行</h5><img src="/2020/09/30/markdown-xue-xi-bi-ji-yi/v2-d1c063b482928dc274161e1c41d0d6ac_b.png" class><h4 id="3-列表"><a href="#3-列表" class="headerlink" title="3.列表"></a>3.<strong>列表</strong></h4><h5 id="3-1-有序列表-:数字序号-英文句号-空格-列表内容"><a href="#3-1-有序列表-:数字序号-英文句号-空格-列表内容" class="headerlink" title="3.1 有序列表 :数字序号+英文句号+空格+列表内容"></a>3.1 有序列表 :数字序号+英文句号+空格+列表内容</h5><img src="/2020/09/30/markdown-xue-xi-bi-ji-yi/v2-c286c0ac7299daef023c3e204a59f337_b.png" class title="img"><h5 id="3-2-无序列表:-空格-列表内容"><a href="#3-2-无序列表:-空格-列表内容" class="headerlink" title="3.2 无序列表:* /+/-+空格+列表内容"></a>3.2 无序列表:* /+/-+空格+列表内容</h5><img src="/2020/09/30/markdown-xue-xi-bi-ji-yi/v2-7c6632454d5273c60467cd23e36d5335_b.png" class title="img"><h5 id="3-3-嵌套列表"><a href="#3-3-嵌套列表" class="headerlink" title="3.3 嵌套列表"></a>3.3 嵌套列表</h5><img src="/2020/09/30/markdown-xue-xi-bi-ji-yi/v2-cbdc7e8321cc082db4239360ba44b8bf_b.png" class title="img"><p> 说明,建议使用 <strong>-</strong> 标记无序列表;*容易与粗体和斜体弄混淆,+不常用。记得数字与字符列表之后要加空格</p><h4 id="4-分割线"><a href="#4-分割线" class="headerlink" title="4. 分割线"></a><strong>4. 分割线</strong></h4><img src="/2020/09/30/markdown-xue-xi-bi-ji-yi/v2-8526a085f93d7d9c7cf23fb28c2d40ee_b.png" class title="img"><p>说明:由3个以上的* /-/_来标记。</p><h4 id="5-图片"><a href="#5-图片" class="headerlink" title="5.图片"></a>5.<strong>图片</strong></h4><img src="/2020/09/30/markdown-xue-xi-bi-ji-yi/v2-9556fca262c0dc83cacfe7bd84d24f9c_b.png" class title="img"><p> 说明:语法:<code> ![图片替代文字][图片地址]</code></p><h4 id="6-链接"><a href="#6-链接" class="headerlink" title="6.链接"></a>6.<strong>链接</strong></h4><p><code> [链接文字][链接地址]</code></p><img src="/2020/09/30/markdown-xue-xi-bi-ji-yi/v2-ed7d008157f201edb0a425424dce5f1a_b.png" class title="img"><p>说明,第二种方法可读性好一些。不以Http/https开头。</p><p><strong>网址链接</strong>:将网络地址或邮箱地址使用<>包裹起来会被自动转换为超链接。这个尽量以http/https开头。</p><img src="/2020/09/30/markdown-xue-xi-bi-ji-yi/v2-c3c876371f24463920c139ef16cfc93e_b.png" class title="img"><h4 id="7-行内代码与代码块"><a href="#7-行内代码与代码块" class="headerlink" title="7.行内代码与代码块"></a>7.<strong>行内代码与代码块</strong></h4><img src="/2020/09/30/markdown-xue-xi-bi-ji-yi/v2-f7f7d5be188c3f7091f18a8deabdeccc_b.png" class title="img"><h4 id="8-引用"><a href="#8-引用" class="headerlink" title="8.引用"></a><strong>8.引用</strong></h4><img src="/2020/09/30/markdown-xue-xi-bi-ji-yi/v2-c9a9e80ac4d4c6bd749fb6f959d715df_b.png" class title="img"><p>说明:引用由<code> >+</code>引用内容来标记。建议再<code>></code>之后添加一个空格。</p><h4 id="9-转义"><a href="#9-转义" class="headerlink" title="9.转义"></a>9.<strong>转义</strong></h4><img src="/2020/09/30/markdown-xue-xi-bi-ji-yi/v2-32fedddf5c26e428345af5e2a0cce0eb_b.png" class title="img"><p>这些转义可以做到原符号输出。</p><h3 id="二)扩展语法GFM"><a href="#二)扩展语法GFM" class="headerlink" title="二)扩展语法GFM"></a><strong>二)扩展语法GFM</strong></h3><h4 id="1-删除线"><a href="#1-删除线" class="headerlink" title="1.删除线"></a><strong>1.删除线</strong></h4><img src="/2020/09/30/markdown-xue-xi-bi-ji-yi/v2-7acdd6a39bd847ed540a71786d8448f9_b.png" class title="img"><h4 id="2-表情符号"><a href="#2-表情符号" class="headerlink" title="2.表情符号"></a><strong>2.表情符号</strong></h4><img src="/2020/09/30/markdown-xue-xi-bi-ji-yi/v2-1b12dbcdea33d58de6f1586a1714a187_b.png" class title="img"><p>表情符号请参考:<a href="http://www.webpagefx.com/tools/emoji-cheat-sheet/">Emoji cheat sheet for GitHub, Basecamp and other services</a></p><h4 id="3-自动链接"><a href="#3-自动链接" class="headerlink" title="3.自动链接"></a><strong>3.自动链接</strong></h4><img src="/2020/09/30/markdown-xue-xi-bi-ji-yi/v2-20bde9e1048513f769cc0c67c251bb1b_b.png" class title="img"><p>说明:自动链接只识别以www或http://开头的URL地址;不使用时,请如下:<code>www.baidu.com</code></p><h4 id="4-表格"><a href="#4-表格" class="headerlink" title="4.表格"></a><strong>4.表格</strong></h4><img src="/2020/09/30/markdown-xue-xi-bi-ji-yi/v2-0076c737c4cbc99d60b339771f1ff84f_b.png" class title="img"><p>说明:建议表格前后均空一行。默认左对齐;<code> :----:</code>为中对齐;<code> ----:</code>为右对齐</p><h4 id="5-任务列表"><a href="#5-任务列表" class="headerlink" title="5.任务列表"></a><strong>5.任务列表</strong></h4><img src="/2020/09/30/markdown-xue-xi-bi-ji-yi/v2-9e30b9e98499b8cac8460a3aee9cc57a_b.png" class title="img"><p>说明:x尽量小写。</p><h4 id="6-围栏代码块"><a href="#6-围栏代码块" class="headerlink" title="6.围栏代码块"></a><strong>6.围栏代码块</strong></h4><img src="/2020/09/30/markdown-xue-xi-bi-ji-yi/v2-575dea0d17cb711529b5be4c78af865a_b.png" class title="img"><h4 id="7-锚点或者称书签"><a href="#7-锚点或者称书签" class="headerlink" title="7.锚点或者称书签"></a><strong>7.锚点或者称书签</strong></h4><img src="/2020/09/30/markdown-xue-xi-bi-ji-yi/v2-e51d60559b2ea920c321f5544720f056_b.png" class title="img"><img src="/2020/09/30/markdown-xue-xi-bi-ji-yi/v2-235696c7189e5f96887e732662725ec7_b.png" class title="img"><img src="/2020/09/30/markdown-xue-xi-bi-ji-yi/v2-7128d88eb98b4a3441524b8bb33ad5db_b.png" class title="img"><p>说明:锚点名建议使用字母,数字;区分英文大小写;不能有空格和特殊字符。在渲染过后的html文件中可以实现跳转。</p><img src="/2020/09/30/markdown-xue-xi-bi-ji-yi/v2-9e30b9e98499b8cac8460a3aee9cc57a_b.png" class title="v2-9e30b9e98499b8cac8460a3aee9cc57a_b"><h3 id="三)排版技巧"><a href="#三)排版技巧" class="headerlink" title="三)排版技巧"></a><strong>三)排版技巧</strong></h3><p>详细看书籍中的内容,不做过多笔记了。</p>]]></content>
<categories>
<category> 学习笔记 </category>
</categories>
<tags>
<tag> markdown </tag>
</tags>
</entry>
<entry>
<title>Numpy的基本使用</title>
<link href="2020/09/30/numpy-de-ji-ben-shi-yong/"/>
<url>2020/09/30/numpy-de-ji-ben-shi-yong/</url>
<content type="html"><![CDATA[<h3 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h3><p>学习书籍:<em>利用python进行数据分析</em></p><a id="more"></a><h3 id="NumPy"><a href="#NumPy" class="headerlink" title="NumPy"></a>NumPy</h3><h4 id="ndarray-一种多维数组对象"><a href="#ndarray-一种多维数组对象" class="headerlink" title="ndarray:一种多维数组对象"></a>ndarray:一种多维数组对象</h4><ol><li><p>创建ndarry</p><pre class=" language-python"><code class="language-python"> In <span class="token punctuation">[</span><span class="token number">23</span><span class="token punctuation">]</span><span class="token punctuation">:</span> data1 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">,</span><span class="token number">7.5</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span> In <span class="token punctuation">[</span><span class="token number">24</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr1 <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span>data1<span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">25</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr1 Out<span class="token punctuation">[</span><span class="token number">25</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">7.5</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span> <span class="token punctuation">]</span><span class="token punctuation">)</span></code></pre></li></ol><ol start="2"><li><p>嵌套列表会被转换为一个多维数组</p><pre class=" language-python"><code class="language-python"> In <span class="token punctuation">[</span><span class="token number">26</span><span class="token punctuation">]</span><span class="token punctuation">:</span> data2 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">]</span> In <span class="token punctuation">[</span><span class="token number">27</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr2 <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span>data2<span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">28</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr2 Out<span class="token punctuation">[</span><span class="token number">28</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">29</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr2<span class="token punctuation">.</span>ndim Out<span class="token punctuation">[</span><span class="token number">29</span><span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token number">2</span> In <span class="token punctuation">[</span><span class="token number">30</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr2<span class="token punctuation">.</span>shape Out<span class="token punctuation">[</span><span class="token number">30</span><span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span></code></pre></li></ol><ol start="3"><li><p>其他的创建数组的方法</p><pre class=" language-python"><code class="language-python"> In <span class="token punctuation">[</span><span class="token number">37</span><span class="token punctuation">]</span><span class="token punctuation">:</span> np<span class="token punctuation">.</span>zeros<span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">)</span> Out<span class="token punctuation">[</span><span class="token number">37</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">38</span><span class="token punctuation">]</span><span class="token punctuation">:</span> np<span class="token punctuation">.</span>empty<span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span> Out<span class="token punctuation">[</span><span class="token number">38</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">6.23042070e-307</span><span class="token punctuation">,</span> <span class="token number">1.69118108e-306</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">2.22520967e-306</span><span class="token punctuation">,</span> <span class="token number">1.37962117e-306</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">8.45590539e-307</span><span class="token punctuation">,</span> <span class="token number">6.23054972e-307</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1.06811422e-306</span><span class="token punctuation">,</span> <span class="token number">6.23037996e-307</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">6.23053954e-307</span><span class="token punctuation">,</span> <span class="token number">1.37961913e-306</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">1.69122046e-306</span><span class="token punctuation">,</span> <span class="token number">2.56765117e-312</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span></code></pre></li></ol><ol start="4"><li><pre class=" language-python"><code class="language-python"> In <span class="token punctuation">[</span><span class="token number">53</span><span class="token punctuation">]</span><span class="token punctuation">:</span> np<span class="token punctuation">.</span>arange<span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span> Out<span class="token punctuation">[</span><span class="token number">53</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">13</span><span class="token punctuation">,</span> <span class="token number">14</span><span class="token punctuation">]</span><span class="token punctuation">)</span></code></pre><table><thead><tr><th>函数</th><th>说明</th></tr></thead><tbody><tr><td>array</td><td>将输入数据转换为ndarray</td></tr><tr><td>asarray</td><td>将输入数据转换为ndarray,如果输入本身为已给ndarray就不转换。</td></tr><tr><td>arange</td><td>类似range,但是返回的为ndarray</td></tr><tr><td>noes, ones_like</td><td>均创建一个全1数组,后者根据另一个数组创建一个全一数组</td></tr><tr><td>zeros,zeros_like</td><td>和ones一样,不过产生的是全0数组</td></tr><tr><td>empty,empty_like</td><td>创建新数组,分配内存空间但不填充任何值</td></tr><tr><td>eye,identity</td><td>创建一个NxN的单位矩阵</td></tr></tbody></table> <img src="/2020/09/30/numpy-de-ji-ben-shi-yong/image-20200930165650538.png" class title="image-20200930165650538"> <img src="/2020/09/30/numpy-de-ji-ben-shi-yong/image-20200930165704493.png" class title="image-20200930165704493"></li></ol><p>转换数据类型:</p><pre class=" language-python"><code class="language-python"> In <span class="token punctuation">[</span><span class="token number">54</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">55</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr<span class="token punctuation">.</span>dtype Out<span class="token punctuation">[</span><span class="token number">55</span><span class="token punctuation">]</span><span class="token punctuation">:</span> dtype<span class="token punctuation">(</span><span class="token string">'int32'</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">56</span><span class="token punctuation">]</span><span class="token punctuation">:</span> float_arr <span class="token operator">=</span> arr<span class="token punctuation">.</span>astype<span class="token punctuation">(</span>np<span class="token punctuation">.</span>float64<span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">58</span><span class="token punctuation">]</span><span class="token punctuation">:</span> float_arr<span class="token punctuation">.</span>dtypeOut<span class="token punctuation">[</span><span class="token number">58</span><span class="token punctuation">]</span><span class="token punctuation">:</span> dtype<span class="token punctuation">(</span><span class="token string">'float64'</span><span class="token punctuation">)</span></code></pre><h4 id="数组和标量之间的运算"><a href="#数组和标量之间的运算" class="headerlink" title="数组和标量之间的运算"></a>数组和标量之间的运算</h4><pre class=" language-python"><code class="language-python"> In <span class="token punctuation">[</span><span class="token number">61</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">62</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr Out<span class="token punctuation">[</span><span class="token number">62</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">63</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr<span class="token operator">*</span>arr Out<span class="token punctuation">[</span><span class="token number">63</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">16</span><span class="token punctuation">,</span> <span class="token number">25</span><span class="token punctuation">,</span> <span class="token number">36</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">64</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr<span class="token operator">-</span>arr Out<span class="token punctuation">[</span><span class="token number">64</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">65</span><span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token number">1</span><span class="token operator">/</span>arr Out<span class="token punctuation">[</span><span class="token number">65</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">0.5</span> <span class="token punctuation">,</span> <span class="token number">0.33333333</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">0.25</span> <span class="token punctuation">,</span> <span class="token number">0.2</span> <span class="token punctuation">,</span> <span class="token number">0.16666667</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">66</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr<span class="token operator">**</span><span class="token number">0.5</span> Out<span class="token punctuation">[</span><span class="token number">66</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">1.41421356</span><span class="token punctuation">,</span> <span class="token number">1.73205081</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">2.23606798</span><span class="token punctuation">,</span> <span class="token number">2.44948974</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span></code></pre><h4 id="基本索引和切片"><a href="#基本索引和切片" class="headerlink" title="基本索引和切片"></a>基本索引和切片</h4><pre class=" language-python"><code class="language-python"> In <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr <span class="token operator">=</span> np<span class="token punctuation">.</span>arange<span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr Out<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span> Out<span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token number">5</span> In <span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> Out<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token number">1</span> In <span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> Out<span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token number">0</span> In <span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">:</span><span class="token number">8</span><span class="token punctuation">]</span> Out<span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">:</span><span class="token number">8</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">12</span> In <span class="token punctuation">[</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr Out<span class="token punctuation">[</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">)</span></code></pre><p>当将一个标量值赋值于一个切片时,如<code> arr[5:8] = 12</code> ,该值会自动传播到整个选区,但是数据不会复制,视图上的任何修改都会反映到源数组上。</p><pre class=" language-PYTHON"><code class="language-PYTHON"> In [10]: arr_slice = arr[5:8] In [11]: arr_slice[1] = 12345 In [12]: arr Out[12]: array([ 0, 1, 2, 3, 4, 12, 12345, 12, 8, 9]) In [14]: arr_slice[:] = 64 In [15]: arr Out[15]: array([ 0, 1, 2, 3, 4, 64, 64, 64, 8, 9])</code></pre><p>创建副本时需要使用<code> arr[5:8].copy()</code></p><p>对高维数组访问时:</p><pre class=" language-python"><code class="language-python"> In <span class="token punctuation">[</span><span class="token number">16</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr2d <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">17</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr2d<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> Out<span class="token punctuation">[</span><span class="token number">17</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">18</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr2d<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> Out<span class="token punctuation">[</span><span class="token number">18</span><span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token number">3</span> In <span class="token punctuation">[</span><span class="token number">19</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr2d<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">]</span> Out<span class="token punctuation">[</span><span class="token number">19</span><span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token number">3</span></code></pre><img src="/2020/09/30/numpy-de-ji-ben-shi-yong/image-20200930165729762.png" class title="image-20200930165729762"><pre class=" language-python"><code class="language-python"> In <span class="token punctuation">[</span><span class="token number">21</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr3d <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">11</span><span class="token punctuation">,</span><span class="token number">12</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">22</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr3d Out<span class="token punctuation">[</span><span class="token number">22</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">23</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr3d<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> Out<span class="token punctuation">[</span><span class="token number">23</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">24</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr3d<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">42</span> In <span class="token punctuation">[</span><span class="token number">25</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr3d Out<span class="token punctuation">[</span><span class="token number">25</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">42</span><span class="token punctuation">,</span> <span class="token number">42</span><span class="token punctuation">,</span> <span class="token number">42</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">42</span><span class="token punctuation">,</span> <span class="token number">42</span><span class="token punctuation">,</span> <span class="token number">42</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">26</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr3d<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">]</span> In <span class="token punctuation">[</span><span class="token number">27</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr3d Out<span class="token punctuation">[</span><span class="token number">27</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">28</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr3d<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span> Out<span class="token punctuation">[</span><span class="token number">28</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">29</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr3d<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span> Out<span class="token punctuation">[</span><span class="token number">29</span><span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token number">8</span></code></pre><h4 id="切片索引"><a href="#切片索引" class="headerlink" title="切片索引"></a>切片索引</h4><h5 id="一维"><a href="#一维" class="headerlink" title="一维"></a>一维</h5><pre class=" language-python"><code class="language-python"> In <span class="token punctuation">[</span><span class="token number">30</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token number">6</span><span class="token punctuation">]</span> Out<span class="token punctuation">[</span><span class="token number">30</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">64</span><span class="token punctuation">]</span><span class="token punctuation">)</span></code></pre><h5 id="多维"><a href="#多维" class="headerlink" title="多维"></a>多维</h5><pre class=" language-python"><code class="language-python"> In <span class="token punctuation">[</span><span class="token number">31</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr2d Out<span class="token punctuation">[</span><span class="token number">31</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">32</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr2d<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token number">2</span><span class="token punctuation">]</span> Out<span class="token punctuation">[</span><span class="token number">32</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">## 沿着列向量的方向进行切片,保留行向量,上一条命令的意思即为切片出前两行,即不包括索引2,包含索引0,1</span> In <span class="token punctuation">[</span><span class="token number">33</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr2d<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span> Out<span class="token punctuation">[</span><span class="token number">33</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">## 先切片出前两行,再从前两行中切片出从索引1之后的所有</span> In <span class="token punctuation">[</span><span class="token number">35</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr2d<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token punctuation">:</span><span class="token number">2</span><span class="token punctuation">]</span> Out<span class="token punctuation">[</span><span class="token number">35</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">36</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr2d<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token punctuation">:</span><span class="token number">1</span><span class="token punctuation">]</span> Out<span class="token punctuation">[</span><span class="token number">36</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">## 索引与切片混合</span> In <span class="token punctuation">[</span><span class="token number">37</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr2d<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span><span class="token punctuation">:</span><span class="token number">1</span><span class="token punctuation">]</span> Out<span class="token punctuation">[</span><span class="token number">37</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">## 索引与切片混合</span></code></pre><h4 id="布尔型索引"><a href="#布尔型索引" class="headerlink" title="布尔型索引"></a>布尔型索引</h4><pre class=" language-python"><code class="language-python"> In <span class="token punctuation">[</span><span class="token number">51</span><span class="token punctuation">]</span><span class="token punctuation">:</span> data <span class="token operator">=</span> np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>randn<span class="token punctuation">(</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">52</span><span class="token punctuation">]</span><span class="token punctuation">:</span> data Out<span class="token punctuation">[</span><span class="token number">52</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">0.93375663</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.08632033</span><span class="token punctuation">,</span> <span class="token number">1.53276672</span><span class="token punctuation">,</span> <span class="token number">0.54906358</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1.38693464</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.02109382</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.07763972</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.48239066</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1.94776729</span><span class="token punctuation">,</span> <span class="token number">0.63974838</span><span class="token punctuation">,</span> <span class="token number">0.90988197</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.82219167</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">0.18539583</span><span class="token punctuation">,</span> <span class="token number">1.38870465</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.07450723</span><span class="token punctuation">,</span> <span class="token number">0.64790535</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">0.53587732</span><span class="token punctuation">,</span> <span class="token number">1.11828572</span><span class="token punctuation">,</span> <span class="token number">1.19434653</span><span class="token punctuation">,</span> <span class="token number">0.22491293</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">0.72979041</span><span class="token punctuation">,</span> <span class="token number">0.69241737</span><span class="token punctuation">,</span> <span class="token number">0.29863796</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1.27876864</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">1.5306251</span> <span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.00550073</span><span class="token punctuation">,</span> <span class="token number">0.17981115</span><span class="token punctuation">,</span> <span class="token number">1.54753703</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">46</span><span class="token punctuation">]</span><span class="token punctuation">:</span> names <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'Bob'</span><span class="token punctuation">,</span><span class="token string">'Joe'</span><span class="token punctuation">,</span><span class="token string">'Will'</span><span class="token punctuation">,</span><span class="token string">'Bob'</span><span class="token punctuation">,</span><span class="token string">'Will'</span><span class="token punctuation">,</span><span class="token string">'Joe'</span><span class="token punctuation">,</span>'Joe <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">:</span> '<span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">47</span><span class="token punctuation">]</span><span class="token punctuation">:</span> names Out<span class="token punctuation">[</span><span class="token number">47</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'Bob'</span><span class="token punctuation">,</span> <span class="token string">'Joe'</span><span class="token punctuation">,</span> <span class="token string">'Will'</span><span class="token punctuation">,</span> <span class="token string">'Bob'</span><span class="token punctuation">,</span> <span class="token string">'Will'</span><span class="token punctuation">,</span> <span class="token string">'Joe'</span><span class="token punctuation">,</span> <span class="token string">'Joe'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> dtype<span class="token operator">=</span><span class="token string">'<U4'</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">53</span><span class="token punctuation">]</span><span class="token punctuation">:</span> names <span class="token operator">==</span> <span class="token string">'Bob'</span> Out<span class="token punctuation">[</span><span class="token number">53</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token boolean">False</span><span class="token punctuation">,</span> <span class="token boolean">False</span><span class="token punctuation">,</span> <span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token boolean">False</span><span class="token punctuation">,</span> <span class="token boolean">False</span><span class="token punctuation">,</span> <span class="token boolean">False</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">##通过boolean进行索引</span> In <span class="token punctuation">[</span><span class="token number">54</span><span class="token punctuation">]</span><span class="token punctuation">:</span> data<span class="token punctuation">[</span>names <span class="token operator">==</span> <span class="token string">'Bob'</span><span class="token punctuation">]</span> Out<span class="token punctuation">[</span><span class="token number">54</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">0.93375663</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.08632033</span><span class="token punctuation">,</span> <span class="token number">1.53276672</span><span class="token punctuation">,</span> <span class="token number">0.54906358</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">0.18539583</span><span class="token punctuation">,</span> <span class="token number">1.38870465</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.07450723</span><span class="token punctuation">,</span> <span class="token number">0.64790535</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">##两种对非'Bob'的索引方法 </span> In <span class="token punctuation">[</span><span class="token number">56</span><span class="token punctuation">]</span><span class="token punctuation">:</span> data<span class="token punctuation">[</span>names <span class="token operator">!=</span> <span class="token string">'Bob'</span><span class="token punctuation">]</span> Out<span class="token punctuation">[</span><span class="token number">56</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1.38693464</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.02109382</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.07763972</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.48239066</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1.94776729</span><span class="token punctuation">,</span> <span class="token number">0.63974838</span><span class="token punctuation">,</span> <span class="token number">0.90988197</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.82219167</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">0.53587732</span><span class="token punctuation">,</span> <span class="token number">1.11828572</span><span class="token punctuation">,</span> <span class="token number">1.19434653</span><span class="token punctuation">,</span> <span class="token number">0.22491293</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">0.72979041</span><span class="token punctuation">,</span> <span class="token number">0.69241737</span><span class="token punctuation">,</span> <span class="token number">0.29863796</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1.27876864</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">1.5306251</span> <span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.00550073</span><span class="token punctuation">,</span> <span class="token number">0.17981115</span><span class="token punctuation">,</span> <span class="token number">1.54753703</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">58</span><span class="token punctuation">]</span><span class="token punctuation">:</span> data<span class="token punctuation">[</span><span class="token operator">~</span><span class="token punctuation">(</span>names <span class="token operator">==</span> <span class="token string">'Bob'</span><span class="token punctuation">)</span><span class="token punctuation">]</span> Out<span class="token punctuation">[</span><span class="token number">58</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1.38693464</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.02109382</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.07763972</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.48239066</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1.94776729</span><span class="token punctuation">,</span> <span class="token number">0.63974838</span><span class="token punctuation">,</span> <span class="token number">0.90988197</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.82219167</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">0.53587732</span><span class="token punctuation">,</span> <span class="token number">1.11828572</span><span class="token punctuation">,</span> <span class="token number">1.19434653</span><span class="token punctuation">,</span> <span class="token number">0.22491293</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">0.72979041</span><span class="token punctuation">,</span> <span class="token number">0.69241737</span><span class="token punctuation">,</span> <span class="token number">0.29863796</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1.27876864</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">1.5306251</span> <span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.00550073</span><span class="token punctuation">,</span> <span class="token number">0.17981115</span><span class="token punctuation">,</span> <span class="token number">1.54753703</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">## 使用&,|,boolean运算符,and 和or再boolean数组中无效</span> In <span class="token punctuation">[</span><span class="token number">59</span><span class="token punctuation">]</span><span class="token punctuation">:</span> mask <span class="token operator">=</span> <span class="token punctuation">(</span>names <span class="token operator">==</span> <span class="token string">'Bob'</span><span class="token punctuation">)</span> <span class="token operator">|</span> <span class="token punctuation">(</span>names <span class="token operator">==</span> <span class="token string">'Will'</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">60</span><span class="token punctuation">]</span><span class="token punctuation">:</span> mask Out<span class="token punctuation">[</span><span class="token number">60</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token boolean">False</span><span class="token punctuation">,</span> <span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token boolean">False</span><span class="token punctuation">,</span> <span class="token boolean">False</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">61</span><span class="token punctuation">]</span><span class="token punctuation">:</span> data<span class="token punctuation">[</span>mask<span class="token punctuation">]</span> Out<span class="token punctuation">[</span><span class="token number">61</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">0.93375663</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.08632033</span><span class="token punctuation">,</span> <span class="token number">1.53276672</span><span class="token punctuation">,</span> <span class="token number">0.54906358</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1.94776729</span><span class="token punctuation">,</span> <span class="token number">0.63974838</span><span class="token punctuation">,</span> <span class="token number">0.90988197</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.82219167</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">0.18539583</span><span class="token punctuation">,</span> <span class="token number">1.38870465</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.07450723</span><span class="token punctuation">,</span> <span class="token number">0.64790535</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">0.53587732</span><span class="token punctuation">,</span> <span class="token number">1.11828572</span><span class="token punctuation">,</span> <span class="token number">1.19434653</span><span class="token punctuation">,</span> <span class="token number">0.22491293</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">## 将负值设为0</span> In <span class="token punctuation">[</span><span class="token number">62</span><span class="token punctuation">]</span><span class="token punctuation">:</span> data<span class="token punctuation">[</span>data<span class="token operator"><</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span> In <span class="token punctuation">[</span><span class="token number">63</span><span class="token punctuation">]</span><span class="token punctuation">:</span> data Out<span class="token punctuation">[</span><span class="token number">63</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">1.53276672</span><span class="token punctuation">,</span> <span class="token number">0.54906358</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">0.63974838</span><span class="token punctuation">,</span> <span class="token number">0.90988197</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">1.38870465</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">0.64790535</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">0.53587732</span><span class="token punctuation">,</span> <span class="token number">1.11828572</span><span class="token punctuation">,</span> <span class="token number">1.19434653</span><span class="token punctuation">,</span> <span class="token number">0.22491293</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">0.72979041</span><span class="token punctuation">,</span> <span class="token number">0.69241737</span><span class="token punctuation">,</span> <span class="token number">0.29863796</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">1.5306251</span> <span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">0.17981115</span><span class="token punctuation">,</span> <span class="token number">1.54753703</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">## 设置整行整列的值</span> In <span class="token punctuation">[</span><span class="token number">64</span><span class="token punctuation">]</span><span class="token punctuation">:</span> data<span class="token punctuation">[</span>names <span class="token operator">!=</span> <span class="token string">'Joe'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">7</span> In <span class="token punctuation">[</span><span class="token number">65</span><span class="token punctuation">]</span><span class="token punctuation">:</span> data Out<span class="token punctuation">[</span><span class="token number">65</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">.</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">.</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">.</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">.</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">0.72979041</span><span class="token punctuation">,</span> <span class="token number">0.69241737</span><span class="token punctuation">,</span> <span class="token number">0.29863796</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">1.5306251</span> <span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">0.17981115</span><span class="token punctuation">,</span> <span class="token number">1.54753703</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span></code></pre><h4 id="花式索引"><a href="#花式索引" class="headerlink" title="花式索引"></a>花式索引</h4><pre class=" language-python"><code class="language-python"> In <span class="token punctuation">[</span><span class="token number">68</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr Out<span class="token punctuation">[</span><span class="token number">68</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">## 随意索引</span> In <span class="token punctuation">[</span><span class="token number">69</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr<span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">]</span> Out<span class="token punctuation">[</span><span class="token number">69</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">## 通过负数索引</span> In <span class="token punctuation">[</span><span class="token number">70</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr<span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">]</span> Out<span class="token punctuation">[</span><span class="token number">70</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">73</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr <span class="token operator">=</span> np<span class="token punctuation">.</span>arange<span class="token punctuation">(</span><span class="token number">32</span><span class="token punctuation">)</span><span class="token punctuation">.</span>reshape<span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">##第一种方式返回为(1,0),(5,3),(7,1),(2,2)的元素</span> In <span class="token punctuation">[</span><span class="token number">74</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr Out<span class="token punctuation">[</span><span class="token number">74</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">13</span><span class="token punctuation">,</span> <span class="token number">14</span><span class="token punctuation">,</span> <span class="token number">15</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">16</span><span class="token punctuation">,</span> <span class="token number">17</span><span class="token punctuation">,</span> <span class="token number">18</span><span class="token punctuation">,</span> <span class="token number">19</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">20</span><span class="token punctuation">,</span> <span class="token number">21</span><span class="token punctuation">,</span> <span class="token number">22</span><span class="token punctuation">,</span> <span class="token number">23</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">24</span><span class="token punctuation">,</span> <span class="token number">25</span><span class="token punctuation">,</span> <span class="token number">26</span><span class="token punctuation">,</span> <span class="token number">27</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">28</span><span class="token punctuation">,</span> <span class="token number">29</span><span class="token punctuation">,</span> <span class="token number">30</span><span class="token punctuation">,</span> <span class="token number">31</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">76</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr<span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">]</span> Out<span class="token punctuation">[</span><span class="token number">76</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">23</span><span class="token punctuation">,</span> <span class="token number">29</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">## 第二种方式返回的是[1,5,7,2]这几行的元素,然后按着[0,3,1,2]这个排列方式来再次排列这些元素。</span> In <span class="token punctuation">[</span><span class="token number">77</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr<span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">]</span> Out<span class="token punctuation">[</span><span class="token number">77</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">20</span><span class="token punctuation">,</span> <span class="token number">23</span><span class="token punctuation">,</span> <span class="token number">21</span><span class="token punctuation">,</span> <span class="token number">22</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">28</span><span class="token punctuation">,</span> <span class="token number">31</span><span class="token punctuation">,</span> <span class="token number">29</span><span class="token punctuation">,</span> <span class="token number">30</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">##另一种方法</span> In <span class="token punctuation">[</span><span class="token number">78</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr<span class="token punctuation">[</span>np<span class="token punctuation">.</span>ix_<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">]</span> Out<span class="token punctuation">[</span><span class="token number">78</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">20</span><span class="token punctuation">,</span> <span class="token number">23</span><span class="token punctuation">,</span> <span class="token number">21</span><span class="token punctuation">,</span> <span class="token number">22</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">28</span><span class="token punctuation">,</span> <span class="token number">31</span><span class="token punctuation">,</span> <span class="token number">29</span><span class="token punctuation">,</span> <span class="token number">30</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span></code></pre><h4 id="数组转置和轴对换"><a href="#数组转置和轴对换" class="headerlink" title="数组转置和轴对换"></a>数组转置和轴对换</h4><pre class=" language-python"><code class="language-python"><span class="token comment" spellcheck="true">### 数组的转置</span> In <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr <span class="token operator">=</span> np<span class="token punctuation">.</span>arange<span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">.</span>reshape<span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr Out<span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">13</span><span class="token punctuation">,</span> <span class="token number">14</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr<span class="token punctuation">.</span>T Out<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">13</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">14</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">##用于计算内积</span> In <span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr Out<span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">0.61356787</span><span class="token punctuation">,</span> <span class="token number">0.57653752</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">2.32528069</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">0.73148394</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.77180275</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.85468428</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">0.12416012</span><span class="token punctuation">,</span> <span class="token number">1.61134702</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1.43279181</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1.95604934</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.35157647</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.04944844</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">0.58988188</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1.85340452</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1.66776823</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">1.7164966</span> <span class="token punctuation">,</span> <span class="token number">0.75122885</span><span class="token punctuation">,</span> <span class="token number">1.18356971</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">:</span> np<span class="token punctuation">.</span>dot<span class="token punctuation">(</span>arr<span class="token punctuation">.</span>T<span class="token punctuation">,</span>arr<span class="token punctuation">)</span> Out<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">8.04740025</span><span class="token punctuation">,</span> <span class="token number">1.2947744</span> <span class="token punctuation">,</span> <span class="token number">3.01854046</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">1.2947744</span> <span class="token punctuation">,</span> <span class="token number">7.6475733</span> <span class="token punctuation">,</span> <span class="token number">1.00787709</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">3.01854046</span><span class="token punctuation">,</span> <span class="token number">1.00787709</span><span class="token punctuation">,</span> <span class="token number">12.37504119</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">## 高维数组的转置</span> In <span class="token punctuation">[</span><span class="token number">11</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr Out<span class="token punctuation">[</span><span class="token number">11</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">13</span><span class="token punctuation">,</span> <span class="token number">14</span><span class="token punctuation">,</span> <span class="token number">15</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">##swapaxes需要输入一对轴编号</span> In <span class="token punctuation">[</span><span class="token number">12</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr<span class="token punctuation">.</span>swapaxes<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span> Out<span class="token punctuation">[</span><span class="token number">12</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">13</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">14</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">15</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span></code></pre><h3 id="通用函数:快速的元素级数组函数"><a href="#通用函数:快速的元素级数组函数" class="headerlink" title="通用函数:快速的元素级数组函数"></a>通用函数:快速的元素级数组函数</h3><p><code> ufunc</code> 是对ndarray中数据执行元素级运算的函数,可以看作为简单函数的矢量化包装器。</p><pre class=" language-python"><code class="language-python"><span class="token comment" spellcheck="true">## 一元的(unary)ufunc</span> In <span class="token punctuation">[</span><span class="token number">17</span><span class="token punctuation">]</span><span class="token punctuation">:</span> np<span class="token punctuation">.</span>sqrt<span class="token punctuation">(</span>arr<span class="token punctuation">)</span> <span class="token comment" spellcheck="true">## sqrt</span> Out<span class="token punctuation">[</span><span class="token number">17</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">1.41421356</span><span class="token punctuation">,</span> <span class="token number">1.73205081</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">2.23606798</span><span class="token punctuation">,</span> <span class="token number">2.44948974</span><span class="token punctuation">,</span> <span class="token number">2.64575131</span><span class="token punctuation">,</span> <span class="token number">2.82842712</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">.</span> <span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">18</span><span class="token punctuation">]</span><span class="token punctuation">:</span> np<span class="token punctuation">.</span>exp<span class="token punctuation">(</span>arr<span class="token punctuation">)</span> <span class="token comment" spellcheck="true">## exp</span> Out<span class="token punctuation">[</span><span class="token number">18</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1.00000000e+00</span><span class="token punctuation">,</span> <span class="token number">2.71828183e+00</span><span class="token punctuation">,</span> <span class="token number">7.38905610e+00</span><span class="token punctuation">,</span> <span class="token number">2.00855369e+01</span><span class="token punctuation">,</span> <span class="token number">5.45981500e+01</span><span class="token punctuation">,</span> <span class="token number">1.48413159e+02</span><span class="token punctuation">,</span> <span class="token number">4.03428793e+02</span><span class="token punctuation">,</span> <span class="token number">1.09663316e+03</span><span class="token punctuation">,</span> <span class="token number">2.98095799e+03</span><span class="token punctuation">,</span> <span class="token number">8.10308393e+03</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">## 接受两个数组,binary ufunc</span> In <span class="token punctuation">[</span><span class="token number">21</span><span class="token punctuation">]</span><span class="token punctuation">:</span> x <span class="token operator">=</span> np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>randn<span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">22</span><span class="token punctuation">]</span><span class="token punctuation">:</span> y <span class="token operator">=</span> np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>randn<span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">23</span><span class="token punctuation">]</span><span class="token punctuation">:</span> x Out<span class="token punctuation">[</span><span class="token number">23</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">0.04783073</span><span class="token punctuation">,</span> <span class="token number">0.13245895</span><span class="token punctuation">,</span> <span class="token number">0.76543302</span><span class="token punctuation">,</span> <span class="token number">1.38918994</span><span class="token punctuation">,</span> <span class="token number">0.74493761</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.50890608</span><span class="token punctuation">,</span> <span class="token number">0.88025824</span><span class="token punctuation">,</span> <span class="token number">0.15528426</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">24</span><span class="token punctuation">]</span><span class="token punctuation">:</span> y Out<span class="token punctuation">[</span><span class="token number">24</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">0.21411364</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1.67604085</span><span class="token punctuation">,</span> <span class="token number">0.21115768</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.65121492</span><span class="token punctuation">,</span> <span class="token number">0.35282956</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.43461655</span><span class="token punctuation">,</span> <span class="token number">0.1376999</span> <span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1.40451298</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">26</span><span class="token punctuation">]</span><span class="token punctuation">:</span> np<span class="token punctuation">.</span>maximum<span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span> Out<span class="token punctuation">[</span><span class="token number">26</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">0.21411364</span><span class="token punctuation">,</span> <span class="token number">0.13245895</span><span class="token punctuation">,</span> <span class="token number">0.76543302</span><span class="token punctuation">,</span> <span class="token number">1.38918994</span><span class="token punctuation">,</span> <span class="token number">0.74493761</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.43461655</span><span class="token punctuation">,</span> <span class="token number">0.88025824</span><span class="token punctuation">,</span> <span class="token number">0.15528426</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">## modf 分开小数部分和整数部分变为两个数组</span> In <span class="token punctuation">[</span><span class="token number">27</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr <span class="token operator">=</span> np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>randn<span class="token punctuation">(</span><span class="token number">7</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token number">5</span> In <span class="token punctuation">[</span><span class="token number">28</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr Out<span class="token punctuation">[</span><span class="token number">28</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">3.02722021</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">4.12509889</span><span class="token punctuation">,</span> <span class="token number">1.89815201</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.88401299</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">2.96308571</span><span class="token punctuation">,</span> <span class="token number">2.55761256</span><span class="token punctuation">,</span> <span class="token number">3.80613595</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">29</span><span class="token punctuation">]</span><span class="token punctuation">:</span> np<span class="token punctuation">.</span>modf<span class="token punctuation">(</span>arr<span class="token punctuation">)</span> Out<span class="token punctuation">[</span><span class="token number">29</span><span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token punctuation">(</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">0.02722021</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.12509889</span><span class="token punctuation">,</span> <span class="token number">0.89815201</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.88401299</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.96308571</span><span class="token punctuation">,</span> <span class="token number">0.55761256</span><span class="token punctuation">,</span> <span class="token number">0.80613595</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">3</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">4</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span></code></pre><ul><li><p>一些一元的<code> ufunc</code></p> <img src="/2020/09/30/numpy-de-ji-ben-shi-yong/image-20200930165757381.png" class title="image-20200930165757381"> <img src="/2020/09/30/numpy-de-ji-ben-shi-yong/image-20200930165809563.png" class title="image-20200930165809563"></li><li><p>二元ufunc</p> <img src="/2020/09/30/numpy-de-ji-ben-shi-yong/image-20200930165823669.png" class title="image-20200930165823669"></li></ul><h4 id="利用数组进行数据处理"><a href="#利用数组进行数据处理" class="headerlink" title="利用数组进行数据处理"></a>利用数组进行数据处理</h4><pre class=" language-python"><code class="language-python"><span class="token comment" spellcheck="true">## meshgrid接受两个一维数组,并产生两个二维矩阵。也就是两个数组中的(x,y)对</span> In <span class="token punctuation">[</span><span class="token number">30</span><span class="token punctuation">]</span><span class="token punctuation">:</span> points <span class="token operator">=</span> np<span class="token punctuation">.</span>arange<span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">0.01</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">## 生成0.01为间隔的点</span> In <span class="token punctuation">[</span><span class="token number">31</span><span class="token punctuation">]</span><span class="token punctuation">:</span> xs<span class="token punctuation">,</span>ys <span class="token operator">=</span> np<span class="token punctuation">.</span>meshgrid<span class="token punctuation">(</span>points<span class="token punctuation">,</span>points<span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">32</span><span class="token punctuation">]</span><span class="token punctuation">:</span> ys Out<span class="token punctuation">[</span><span class="token number">32</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">5</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">5</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">5</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">5</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">5</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">5</span><span class="token punctuation">.</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">4.99</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">4.99</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">4.99</span><span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">4.99</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">4.99</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">4.99</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">4.98</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">4.98</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">4.98</span><span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">4.98</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">4.98</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">4.98</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">4.97</span><span class="token punctuation">,</span> <span class="token number">4.97</span><span class="token punctuation">,</span> <span class="token number">4.97</span><span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">4.97</span><span class="token punctuation">,</span> <span class="token number">4.97</span><span class="token punctuation">,</span> <span class="token number">4.97</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">4.98</span><span class="token punctuation">,</span> <span class="token number">4.98</span><span class="token punctuation">,</span> <span class="token number">4.98</span><span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">4.98</span><span class="token punctuation">,</span> <span class="token number">4.98</span><span class="token punctuation">,</span> <span class="token number">4.98</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">4.99</span><span class="token punctuation">,</span> <span class="token number">4.99</span><span class="token punctuation">,</span> <span class="token number">4.99</span><span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">4.99</span><span class="token punctuation">,</span> <span class="token number">4.99</span><span class="token punctuation">,</span> <span class="token number">4.99</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">## 对函数进行求值</span> In <span class="token punctuation">[</span><span class="token number">36</span><span class="token punctuation">]</span><span class="token punctuation">:</span> z <span class="token operator">=</span> np<span class="token punctuation">.</span>sqrt<span class="token punctuation">(</span>xs<span class="token operator">**</span><span class="token number">2</span><span class="token operator">+</span>ys<span class="token operator">**</span><span class="token number">2</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">37</span><span class="token punctuation">]</span><span class="token punctuation">:</span> z Out<span class="token punctuation">[</span><span class="token number">37</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">7.07106781</span><span class="token punctuation">,</span> <span class="token number">7.06400028</span><span class="token punctuation">,</span> <span class="token number">7.05693985</span><span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">7.04988652</span><span class="token punctuation">,</span> <span class="token number">7.05693985</span><span class="token punctuation">,</span><span class="token number">85</span><span class="token punctuation">,</span> <span class="token number">7.06400028</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">7.06400028</span><span class="token punctuation">,</span> <span class="token number">7.05692568</span><span class="token punctuation">,</span> <span class="token number">7.04985815</span><span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">7.04279774</span><span class="token punctuation">,</span> <span class="token number">7.049858115</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">7.05692568</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">7.05693985</span><span class="token punctuation">,</span> <span class="token number">7.04985815</span><span class="token punctuation">,</span> <span class="token number">7.04278354</span><span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">7.03571603</span><span class="token punctuation">,</span> <span class="token number">7.042783554</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">7.04985815</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">7.04988652</span><span class="token punctuation">,</span> <span class="token number">7.04279774</span><span class="token punctuation">,</span> <span class="token number">7.03571603</span><span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">7.0286414</span> <span class="token punctuation">,</span> <span class="token number">7.035716003</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">7.04279774</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">7.05693985</span><span class="token punctuation">,</span> <span class="token number">7.04985815</span><span class="token punctuation">,</span> <span class="token number">7.04278354</span><span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">7.03571603</span><span class="token punctuation">,</span> <span class="token number">7.042783554</span><span class="token punctuation">,</span> <span class="token number">7.04985815</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">7.06400028</span><span class="token punctuation">,</span> <span class="token number">7.05692568</span><span class="token punctuation">,</span> <span class="token number">7.04985815</span><span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">7.04279774</span><span class="token punctuation">,</span> <span class="token number">7.049858115</span><span class="token punctuation">,</span> <span class="token number">7.05692568</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span></code></pre><h4 id="将条件逻辑表述为数组运算"><a href="#将条件逻辑表述为数组运算" class="headerlink" title="将条件逻辑表述为数组运算"></a>将条件逻辑表述为数组运算</h4><p><code> numpy.where</code> 是将三元表达式<code> x if condition else y</code> 的矢量化版本。</p><pre class=" language-python"><code class="language-python"><span class="token comment" spellcheck="true">## 如果condition为真,则从x 中选取数据,否则从y中选取数据</span> In <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">:</span> xarr <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1.1</span><span class="token punctuation">,</span><span class="token number">1.2</span><span class="token punctuation">,</span><span class="token number">1.3</span><span class="token punctuation">,</span><span class="token number">1.4</span><span class="token punctuation">,</span><span class="token number">1.5</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">:</span> yarr <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">2.1</span><span class="token punctuation">,</span><span class="token number">2.2</span><span class="token punctuation">,</span><span class="token number">2.3</span><span class="token punctuation">,</span><span class="token number">2.4</span><span class="token punctuation">,</span><span class="token number">2.5</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">:</span> cond <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token boolean">True</span><span class="token punctuation">,</span><span class="token boolean">False</span><span class="token punctuation">,</span><span class="token boolean">True</span><span class="token punctuation">,</span><span class="token boolean">True</span><span class="token punctuation">,</span><span class="token boolean">False</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">:</span> result <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">(</span>x <span class="token keyword">if</span> c <span class="token keyword">else</span> y<span class="token punctuation">)</span> <span class="token keyword">for</span> x<span class="token punctuation">,</span>y<span class="token punctuation">,</span>c <span class="token keyword">in</span> zip<span class="token punctuation">(</span>xarr<span class="token punctuation">,</span>yarr<span class="token punctuation">,</span>cond<span class="token punctuation">)</span><span class="token punctuation">]</span> In <span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">:</span> result Out<span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token number">1.1</span><span class="token punctuation">,</span> <span class="token number">2.2</span><span class="token punctuation">,</span> <span class="token number">1.3</span><span class="token punctuation">,</span> <span class="token number">1.4</span><span class="token punctuation">,</span> <span class="token number">2.5</span><span class="token punctuation">]</span><span class="token comment" spellcheck="true">##使用np.where时,</span> In <span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">:</span> result <span class="token operator">=</span> np<span class="token punctuation">.</span>where<span class="token punctuation">(</span>cond <span class="token punctuation">,</span>xarr<span class="token punctuation">,</span>yarr<span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">:</span> result Out<span class="token punctuation">[</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1.1</span><span class="token punctuation">,</span> <span class="token number">2.2</span><span class="token punctuation">,</span> <span class="token number">1.3</span><span class="token punctuation">,</span> <span class="token number">1.4</span><span class="token punctuation">,</span> <span class="token number">2.5</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">##使用标量对随机值进行替换</span> In <span class="token punctuation">[</span><span class="token number">11</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr <span class="token operator">=</span> np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>randn<span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">12</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr Out<span class="token punctuation">[</span><span class="token number">12</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1.38006448</span><span class="token punctuation">,</span> <span class="token number">1.03470052</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">2.31563393</span><span class="token punctuation">,</span> <span class="token number">0.59533816</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">0.09044103</span><span class="token punctuation">,</span> <span class="token number">1.26950435</span><span class="token punctuation">,</span> <span class="token number">0.84901703</span><span class="token punctuation">,</span> <span class="token number">0.02030237</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">0.1106881</span> <span class="token punctuation">,</span> <span class="token number">0.28593914</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.17059919</span><span class="token punctuation">,</span> <span class="token number">0.16249255</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">0.58910329</span><span class="token punctuation">,</span> <span class="token number">0.51696963</span><span class="token punctuation">,</span> <span class="token number">1.23522497</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.12077878</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">13</span><span class="token punctuation">]</span><span class="token punctuation">:</span> np<span class="token punctuation">.</span>where<span class="token punctuation">(</span>arr<span class="token operator">></span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">)</span> Out<span class="token punctuation">[</span><span class="token number">13</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span></code></pre><h3 id="数学和统计方法"><a href="#数学和统计方法" class="headerlink" title="数学和统计方法"></a>数学和统计方法</h3><pre class=" language-python"><code class="language-python"><span class="token comment" spellcheck="true">##求平均值与求和</span> In <span class="token punctuation">[</span><span class="token number">29</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr <span class="token operator">=</span> np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>randn<span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">30</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr Out<span class="token punctuation">[</span><span class="token number">30</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">0.05181563</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.39828905</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.48611322</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1.19503122</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">0.52945315</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.04421953</span><span class="token punctuation">,</span> <span class="token number">0.47152795</span><span class="token punctuation">,</span> <span class="token number">0.56862184</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">0.27676736</span><span class="token punctuation">,</span> <span class="token number">0.24786544</span><span class="token punctuation">,</span> <span class="token number">0.68803002</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.6542762</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">0.91189393</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.29872026</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.49039979</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1.19891941</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">0.0686299</span> <span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.34262006</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.81342273</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.16378498</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">31</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr<span class="token punctuation">.</span>mean<span class="token punctuation">(</span><span class="token punctuation">)</span> Out<span class="token punctuation">[</span><span class="token number">31</span><span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token operator">-</span><span class="token number">0.26287583349511634</span> In <span class="token punctuation">[</span><span class="token number">32</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr<span class="token punctuation">.</span>sum<span class="token punctuation">(</span><span class="token punctuation">)</span> Out<span class="token punctuation">[</span><span class="token number">32</span><span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token operator">-</span><span class="token number">5.257516669902326</span><span class="token comment" spellcheck="true">##求每一列的和与mean</span> In <span class="token punctuation">[</span><span class="token number">44</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr<span class="token punctuation">.</span>mean<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span> Out<span class="token punctuation">[</span><span class="token number">44</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">0.8</span><span class="token punctuation">,</span> <span class="token number">0.4</span><span class="token punctuation">,</span> <span class="token number">0.8</span><span class="token punctuation">,</span> <span class="token number">0.4</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">45</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr<span class="token punctuation">.</span>sum<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span> Out<span class="token punctuation">[</span><span class="token number">45</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">##求每一行的和与mean</span> In <span class="token punctuation">[</span><span class="token number">46</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr<span class="token punctuation">.</span>mean<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> Out<span class="token punctuation">[</span><span class="token number">46</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">1.5</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">0.5</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">47</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr<span class="token punctuation">.</span>sum<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> Out<span class="token punctuation">[</span><span class="token number">47</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">##累加和累乘</span> In <span class="token punctuation">[</span><span class="token number">50</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr Out<span class="token punctuation">[</span><span class="token number">50</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">52</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr<span class="token punctuation">.</span>cumsum<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span> Out<span class="token punctuation">[</span><span class="token number">52</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">15</span><span class="token punctuation">,</span> <span class="token number">18</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span> dtype<span class="token operator">=</span>int32<span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">53</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr<span class="token punctuation">.</span>cumsum<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> Out<span class="token punctuation">[</span><span class="token number">53</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">15</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">15</span><span class="token punctuation">,</span> <span class="token number">24</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span> dtype<span class="token operator">=</span>int32<span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">54</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr<span class="token punctuation">.</span>cumprod<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span> Out<span class="token punctuation">[</span><span class="token number">54</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">18</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">28</span><span class="token punctuation">,</span> <span class="token number">80</span><span class="token punctuation">,</span> <span class="token number">162</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span> dtype<span class="token operator">=</span>int32<span class="token punctuation">)</span></code></pre><ul><li><p>这里补充一个<code> axis</code> 的取值的理解.</p><p> <code> axis = 0</code>时代表的为纵轴,从上到下;<code> axis = 1</code> 时,代表的为横轴,意为从左到右。</p></li><li><img src="/2020/09/30/numpy-de-ji-ben-shi-yong/image-20200930165847297.png" class title="image-20200930165847297"> <img src="/2020/09/30/numpy-de-ji-ben-shi-yong/image-20200930165900847.png" class title="image-20200930165900847"></li></ul><h4 id="用于boolean型数组的方法"><a href="#用于boolean型数组的方法" class="headerlink" title="用于boolean型数组的方法"></a>用于boolean型数组的方法</h4><pre class=" language-python"><code class="language-python"><span class="token comment" spellcheck="true">##用于boolean计数</span> In <span class="token punctuation">[</span><span class="token number">59</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr <span class="token operator">=</span> np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>randn<span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">61</span><span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token punctuation">(</span>arr<span class="token operator">></span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span>sum<span class="token punctuation">(</span><span class="token punctuation">)</span> Out<span class="token punctuation">[</span><span class="token number">61</span><span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token number">46</span><span class="token comment" spellcheck="true">## any检查数组中是否存在true; all 检查是否全真</span> In <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">:</span> bools Out<span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token boolean">False</span><span class="token punctuation">,</span> <span class="token boolean">False</span><span class="token punctuation">,</span> <span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token boolean">True</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">:</span> bools<span class="token punctuation">.</span>any<span class="token punctuation">(</span><span class="token punctuation">)</span> Out<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token boolean">True</span> In <span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">:</span> bools<span class="token punctuation">.</span>all<span class="token punctuation">(</span><span class="token punctuation">)</span> Out<span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token boolean">False</span></code></pre><h4 id="排序"><a href="#排序" class="headerlink" title="排序"></a>排序</h4><pre class=" language-python"><code class="language-python"><span class="token comment" spellcheck="true">## 一维数组的排序</span> In <span class="token punctuation">[</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr Out<span class="token punctuation">[</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">0.39258622</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1.11714649</span><span class="token punctuation">,</span> <span class="token number">0.50671473</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.23227169</span><span class="token punctuation">,</span> <span class="token number">0.12930094</span><span class="token punctuation">,</span> <span class="token number">0.84757542</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1.20661321</span><span class="token punctuation">,</span> <span class="token number">0.278981</span> <span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr<span class="token punctuation">.</span>sort<span class="token punctuation">(</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">11</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr Out<span class="token punctuation">[</span><span class="token number">11</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1.20661321</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1.11714649</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.23227169</span><span class="token punctuation">,</span> <span class="token number">0.12930094</span><span class="token punctuation">,</span> <span class="token number">0.278981</span> <span class="token punctuation">,</span> <span class="token number">0.39258622</span><span class="token punctuation">,</span> <span class="token number">0.50671473</span><span class="token punctuation">,</span> <span class="token number">0.84757542</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">##多维数组的排序</span> In <span class="token punctuation">[</span><span class="token number">12</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr <span class="token operator">=</span> np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>randn<span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">13</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr Out<span class="token punctuation">[</span><span class="token number">13</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">0.27482117</span><span class="token punctuation">,</span> <span class="token number">0.23485617</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1.48308258</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">0.94351668</span><span class="token punctuation">,</span> <span class="token number">0.58806374</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.49400863</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">0.12990796</span><span class="token punctuation">,</span> <span class="token number">0.18079273</span><span class="token punctuation">,</span> <span class="token number">1.68860112</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">0.35479473</span><span class="token punctuation">,</span> <span class="token number">2.27508795</span><span class="token punctuation">,</span> <span class="token number">1.07266443</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">0.10126797</span><span class="token punctuation">,</span> <span class="token number">0.96939648</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.15114202</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">14</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr<span class="token punctuation">.</span>sort<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">15</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr Out<span class="token punctuation">[</span><span class="token number">15</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1.48308258</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.27482117</span><span class="token punctuation">,</span> <span class="token number">0.23485617</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">0.94351668</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.49400863</span><span class="token punctuation">,</span> <span class="token number">0.58806374</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">0.12990796</span><span class="token punctuation">,</span> <span class="token number">0.18079273</span><span class="token punctuation">,</span> <span class="token number">1.68860112</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">0.35479473</span><span class="token punctuation">,</span> <span class="token number">1.07266443</span><span class="token punctuation">,</span> <span class="token number">2.27508795</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">0.15114202</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.10126797</span><span class="token punctuation">,</span> <span class="token number">0.96939648</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span></code></pre><h4 id="唯一化以及其他的逻辑集合"><a href="#唯一化以及其他的逻辑集合" class="headerlink" title="唯一化以及其他的逻辑集合"></a>唯一化以及其他的逻辑集合</h4><pre class=" language-python"><code class="language-python"><span class="token comment" spellcheck="true">## 找出数组中的唯一值并返回已经排序的结果</span> In <span class="token punctuation">[</span><span class="token number">19</span><span class="token punctuation">]</span><span class="token punctuation">:</span> ints <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">21</span><span class="token punctuation">]</span><span class="token punctuation">:</span> np<span class="token punctuation">.</span>unique<span class="token punctuation">(</span>ints<span class="token punctuation">)</span> Out<span class="token punctuation">[</span><span class="token number">21</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">##python代码实现</span> In <span class="token punctuation">[</span><span class="token number">23</span><span class="token punctuation">]</span><span class="token punctuation">:</span> sorted<span class="token punctuation">(</span>set<span class="token punctuation">(</span>ints<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">## 首先转换为set,单一化然后排序</span> Out<span class="token punctuation">[</span><span class="token number">23</span><span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token comment" spellcheck="true">## 测试一个数组值在另一个数组中的成员资格</span> In <span class="token punctuation">[</span><span class="token number">30</span><span class="token punctuation">]</span><span class="token punctuation">:</span> values Out<span class="token punctuation">[</span><span class="token number">30</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">31</span><span class="token punctuation">]</span><span class="token punctuation">:</span> np<span class="token punctuation">.</span>in1d<span class="token punctuation">(</span>values<span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> Out<span class="token punctuation">[</span><span class="token number">31</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token boolean">False</span><span class="token punctuation">,</span> <span class="token boolean">False</span><span class="token punctuation">,</span> <span class="token boolean">False</span><span class="token punctuation">,</span> <span class="token boolean">False</span><span class="token punctuation">,</span> <span class="token boolean">False</span><span class="token punctuation">,</span> <span class="token boolean">False</span><span class="token punctuation">,</span> <span class="token boolean">False</span><span class="token punctuation">]</span><span class="token punctuation">)</span></code></pre><img src="/2020/09/30/numpy-de-ji-ben-shi-yong/image-20200930202124736.png" class title="image-20200930202124736"><h3 id="数组文件的输入输出"><a href="#数组文件的输入输出" class="headerlink" title="数组文件的输入输出"></a>数组文件的输入输出</h3><h4 id="将数组以二进制格式保存到磁盘上"><a href="#将数组以二进制格式保存到磁盘上" class="headerlink" title="将数组以二进制格式保存到磁盘上"></a>将数组以二进制格式保存到磁盘上</h4><p><code> np.save</code> 和 <code> np.load</code> 用于读写磁盘数组数据的两个主要函数。 默认情况下,数组以二进制格式保存在<code> .npy</code> 文件中。</p><pre class=" language-python"><code class="language-python"> In <span class="token punctuation">[</span><span class="token number">33</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr Out<span class="token punctuation">[</span><span class="token number">33</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">34</span><span class="token punctuation">]</span><span class="token punctuation">:</span> np<span class="token punctuation">.</span>save<span class="token punctuation">(</span><span class="token string">'a_array'</span><span class="token punctuation">,</span>arr<span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">36</span><span class="token punctuation">]</span><span class="token punctuation">:</span> np<span class="token punctuation">.</span>load<span class="token punctuation">(</span><span class="token string">'a_array.npy'</span><span class="token punctuation">)</span> Out<span class="token punctuation">[</span><span class="token number">36</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">)</span></code></pre><p><code> np.savez</code> 可以将多个数组保存压缩到一个压缩文件中。以关键字的形式传入。 </p><pre class=" language-python"><code class="language-python"> In <span class="token punctuation">[</span><span class="token number">41</span><span class="token punctuation">]</span><span class="token punctuation">:</span> b Out<span class="token punctuation">[</span><span class="token number">41</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">13</span><span class="token punctuation">,</span> <span class="token number">14</span><span class="token punctuation">,</span> <span class="token number">15</span><span class="token punctuation">,</span> <span class="token number">16</span><span class="token punctuation">,</span> <span class="token number">17</span><span class="token punctuation">,</span> <span class="token number">18</span><span class="token punctuation">,</span> <span class="token number">19</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">,</span> <span class="token number">21</span><span class="token punctuation">,</span> <span class="token number">22</span><span class="token punctuation">,</span> <span class="token number">23</span><span class="token punctuation">,</span> <span class="token number">24</span><span class="token punctuation">,</span> <span class="token number">25</span><span class="token punctuation">,</span> <span class="token number">26</span><span class="token punctuation">,</span> <span class="token number">27</span><span class="token punctuation">,</span> <span class="token number">28</span><span class="token punctuation">,</span> <span class="token number">29</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">42</span><span class="token punctuation">]</span><span class="token punctuation">:</span> np<span class="token punctuation">.</span>savez<span class="token punctuation">(</span><span class="token string">'array_a_b'</span><span class="token punctuation">,</span>a<span class="token operator">=</span>a<span class="token punctuation">,</span>b<span class="token operator">=</span>b<span class="token punctuation">)</span><span class="token comment" spellcheck="true">##这里a=a中,第一个a为传入给文件的keyword,第二个为在该程序执行过程中所使用的keyword</span> In <span class="token punctuation">[</span><span class="token number">45</span><span class="token punctuation">]</span><span class="token punctuation">:</span> a_and_b <span class="token operator">=</span> np<span class="token punctuation">.</span>load<span class="token punctuation">(</span><span class="token string">'array_a_b.npz'</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">46</span><span class="token punctuation">]</span><span class="token punctuation">:</span> a_and_b<span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">]</span> Out<span class="token punctuation">[</span><span class="token number">46</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">47</span><span class="token punctuation">]</span><span class="token punctuation">:</span> a_and_b<span class="token punctuation">[</span><span class="token string">'b'</span><span class="token punctuation">]</span> Out<span class="token punctuation">[</span><span class="token number">47</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">13</span><span class="token punctuation">,</span> <span class="token number">14</span><span class="token punctuation">,</span> <span class="token number">15</span><span class="token punctuation">,</span> <span class="token number">16</span><span class="token punctuation">,</span> <span class="token number">17</span><span class="token punctuation">,</span> <span class="token number">18</span><span class="token punctuation">,</span> <span class="token number">19</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">,</span> <span class="token number">21</span><span class="token punctuation">,</span> <span class="token number">22</span><span class="token punctuation">,</span> <span class="token number">23</span><span class="token punctuation">,</span> <span class="token number">24</span><span class="token punctuation">,</span> <span class="token number">25</span><span class="token punctuation">,</span> <span class="token number">26</span><span class="token punctuation">,</span> <span class="token number">27</span><span class="token punctuation">,</span> <span class="token number">28</span><span class="token punctuation">,</span> <span class="token number">29</span><span class="token punctuation">]</span><span class="token punctuation">)</span></code></pre><pre class=" language-python"><code class="language-python"><span class="token comment" spellcheck="true">## np.savetxt执行的为相反的内容</span> In <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr <span class="token operator">=</span> np<span class="token punctuation">.</span>loadtxt<span class="token punctuation">(</span><span class="token string">'helloworld.txt'</span><span class="token punctuation">,</span>delimiter <span class="token operator">=</span> <span class="token string">' '</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">## 以空格为分隔符的一个矩阵</span> In <span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr Out<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">6</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">13</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">14</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">15</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">16</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">17</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">18</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">19</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span></code></pre><h3 id="线性代数"><a href="#线性代数" class="headerlink" title="线性代数"></a>线性代数</h3><h4 id="矩阵的点乘"><a href="#矩阵的点乘" class="headerlink" title="矩阵的点乘"></a>矩阵的点乘</h4><pre class=" language-python"><code class="language-python"> In <span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">:</span> x <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">:</span> y <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">,</span><span class="token number">23</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">:</span> x<span class="token punctuation">.</span>dot<span class="token punctuation">(</span>y<span class="token punctuation">)</span> Out<span class="token punctuation">[</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">28</span><span class="token punctuation">,</span> <span class="token number">64</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">67</span><span class="token punctuation">,</span> <span class="token number">181</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">:</span> np<span class="token punctuation">.</span>dot<span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span> Out<span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">28</span><span class="token punctuation">,</span> <span class="token number">64</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">67</span><span class="token punctuation">,</span> <span class="token number">181</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span></code></pre><p><code> numpy.linalg</code> 中含有求逆,行列式之类的函数</p><pre class=" language-python"><code class="language-python"> In <span class="token punctuation">[</span><span class="token number">45</span><span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token keyword">from</span> numpy<span class="token punctuation">.</span>linalg <span class="token keyword">import</span> inv<span class="token punctuation">,</span>qr<span class="token punctuation">,</span>det In <span class="token punctuation">[</span><span class="token number">41</span><span class="token punctuation">]</span><span class="token punctuation">:</span> arr Out<span class="token punctuation">[</span><span class="token number">41</span><span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">]</span> In <span class="token punctuation">[</span><span class="token number">42</span><span class="token punctuation">]</span><span class="token punctuation">:</span> q<span class="token punctuation">,</span>r <span class="token operator">=</span> qr<span class="token punctuation">(</span>arr<span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">43</span><span class="token punctuation">]</span><span class="token punctuation">:</span> q Out<span class="token punctuation">[</span><span class="token number">43</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">0.31622777</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.9486833</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">0.9486833</span> <span class="token punctuation">,</span> <span class="token number">0.31622777</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">46</span><span class="token punctuation">]</span><span class="token punctuation">:</span> det<span class="token punctuation">(</span>arr<span class="token punctuation">)</span> Out<span class="token punctuation">[</span><span class="token number">46</span><span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token operator">-</span><span class="token number">2.0000000000000004</span></code></pre><img src="/2020/09/30/numpy-de-ji-ben-shi-yong/image-20200930165938968.png" class title="image-20200930165938968"><h4 id="随机数生成"><a href="#随机数生成" class="headerlink" title="随机数生成"></a>随机数生成</h4><p><code> numpy.random</code> 对python的<code> random</code> 进行了补充,用于生成各种概率分布的函数。例如用<code> normal</code> 生成一个标准正态分布的4*4的数组</p><pre class=" language-python"><code class="language-python"> In <span class="token punctuation">[</span><span class="token number">47</span><span class="token punctuation">]</span><span class="token punctuation">:</span> samples <span class="token operator">=</span> np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>normal<span class="token punctuation">(</span>size<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">)</span> In <span class="token punctuation">[</span><span class="token number">49</span><span class="token punctuation">]</span><span class="token punctuation">:</span> samples Out<span class="token punctuation">[</span><span class="token number">49</span><span class="token punctuation">]</span><span class="token punctuation">:</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">2.33757911</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.55762849</span><span class="token punctuation">,</span> <span class="token number">0.62081095</span><span class="token punctuation">,</span> <span class="token number">1.97218501</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">0.81287703</span><span class="token punctuation">,</span> <span class="token number">0.26854776</span><span class="token punctuation">,</span> <span class="token number">1.31357637</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.81733253</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">1.64104873</span><span class="token punctuation">,</span> <span class="token number">0.20683984</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.88431181</span><span class="token punctuation">,</span> <span class="token number">0.3817895</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">1.2312162</span> <span class="token punctuation">,</span> <span class="token number">0.16612548</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1.42153476</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.58949329</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span></code></pre><img src="/2020/09/30/numpy-de-ji-ben-shi-yong/image-20200930165951651.png" class title="image-20200930165951651"><img src="/2020/09/30/numpy-de-ji-ben-shi-yong/image-20200930165959706.png" class title="image-20200930165959706">]]></content>
<categories>
<category> 学习笔记 </category>
</categories>
<tags>
<tag> numpy </tag>
<tag> ipython </tag>
<tag> python </tag>
</tags>
</entry>
<entry>
<title>hexo学习笔记(一)</title>
<link href="2020/09/24/hexo-xue-xi-bi-ji-yi/"/>
<url>2020/09/24/hexo-xue-xi-bi-ji-yi/</url>
<content type="html"><![CDATA[<h3 id="hexo常用命令"><a href="#hexo常用命令" class="headerlink" title="hexo常用命令"></a>hexo常用命令</h3><h4 id="1-文章的书写"><a href="#1-文章的书写" class="headerlink" title="1. 文章的书写"></a>1. 文章的书写</h4><p><code> hexo new [layout] <title></code> </p><p>eg: <code> hexo new "post title with whitespace"</code> 其中 layout为_config.yml中的默认参数值。</p><ul><li><code> -p --path</code> 自定义新文章</li><li><code> -r --replace</code> 存在同名文章,替换</li><li><code> -s --slug</code> 文章的slug,作为新文章的文件名和发布后的URL</li></ul><p>eg: <code>hexo new page --path about/me "About me"</code></p><h4 id="2-生成静态文件"><a href="#2-生成静态文件" class="headerlink" title="2. 生成静态文件"></a>2. 生成静态文件</h4><p><code> hexo generate</code> 或 <code> hexo g</code></p><ul><li><code> -d 或--deploy</code> 文件生成后部署网站</li><li><code> -w 或--watch</code> 监视文件变动</li><li><code> -b 或--bail</code> 生成过程中出现异常则抛出。</li><li><code> -f 或--force</code> 强制重新生成文件</li><li><code> -c 或--concurrency</code> 最大同时生成文件数量,默认无限制。</li></ul><h4 id="3-发布草稿"><a href="#3-发布草稿" class="headerlink" title="3. 发布草稿"></a>3. 发布草稿</h4><p><code> hexo publish [layout] <filename></code></p><h4 id="4-启动服务器"><a href="#4-启动服务器" class="headerlink" title="4.启动服务器"></a>4.启动服务器</h4><p><code> hexo server</code> 启动服务器,ctrl+c 结束,默认地址为:<code> http://localhost:4000/</code></p><h4 id="5-部署网站"><a href="#5-部署网站" class="headerlink" title="5.部署网站"></a>5.部署网站</h4><p><code> hexo deploy</code> 或<code> hexo d</code></p><ul><li><code> -g 或--generate</code> 部署之前写成静态文件</li></ul><h4 id="6-渲染文件"><a href="#6-渲染文件" class="headerlink" title="6.渲染文件"></a>6.渲染文件</h4><p><code> hexo render <file1> [file2]</code></p><ul><li><code> -o或--output</code> 设置输出路径</li></ul><h4 id="7-清除缓存文件"><a href="#7-清除缓存文件" class="headerlink" title="7. 清除缓存文件"></a>7. 清除缓存文件</h4><p><code> hexo clean</code></p><h4 id="8-列出网站资料"><a href="#8-列出网站资料" class="headerlink" title="8. 列出网站资料"></a>8. 列出网站资料</h4><p><code> hexo list <type></code></p><h4 id="9-显示草稿"><a href="#9-显示草稿" class="headerlink" title="9. 显示草稿"></a>9. 显示草稿</h4><p><code> hexo --deaft</code></p><h4 id="10-自定义当前工作目录"><a href="#10-自定义当前工作目录" class="headerlink" title="10. 自定义当前工作目录"></a>10. 自定义当前工作目录</h4><p><code> hexo --cwd /path/to/cwd</code></p>]]></content>
<categories>
<category> 学习笔记 </category>
</categories>
<tags>
<tag> hexo </tag>
</tags>
</entry>
</search>