<div id="article_content" class="article_content clearfix csdn-tracking-statistics" data-pid="blog" data-mod="popu_307" data-dsm="post" style="height: 1198px; overflow: hidden;">
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-62cd27f8b9.css">
<div class="htmledit_views">
<h1 style="margin:0px 0px 5px; font-family:'Helvetica Neue',Helvetica,Tahoma,Arial,STXihei,'Microsoft YaHei',微软雅黑,sans-serif; font-weight:normal; line-height:1.5em; color:rgb(51,51,51); font-size:24px"><a name="t0"></a>
NumPy的详细教程</h1>
<div class="article_meta" style="color:rgb(153,153,153); padding-bottom:5px; border-bottom-width:1px; border-bottom-style:dashed; border-bottom-color:rgb(211,211,211); font-size:14px; font-family:'Helvetica Neue',Helvetica,Tahoma,Arial,STXihei,'Microsoft YaHei',微软雅黑,sans-serif; line-height:21px">
<div style="margin-bottom:5px">
</div>
</div>
<div class="article_body" id="nei" style="padding:20px 5px 25px; margin-bottom:0px; overflow-x:hidden; word-wrap:break-word; word-break:break-all; min-height:340px; font-size:16px; line-height:1.7em; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,Tahoma,Arial,STXihei,'Microsoft YaHei',微软雅黑,sans-serif">
<div style="line-height:1.7em">
<h2 style="margin:0px 0px 0.5em; font-family:inherit; line-height:1.6em; color:inherit; font-size:18px; text-indent:1em"><a name="t1"></a>
先决条件</h2>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
在阅读这个教程之前,你多少需要知道点python。如果你想从新回忆下,请看看 <a target="_blank" href="http://docs.python.org/tut/" rel="nofollow" style="color:rgb(148,148,148); text-decoration:none; outline:none 0px; border-bottom-width:1px; border-bottom-style:dashed; border-bottom-color:rgb(148,148,148); font-style:italic; font-weight:bold">Python
Tutorial</a> .</p>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
如果你想要运行教程中的示例,你至少需要在你的电脑上安装了以下一些软件:</p>
<ul style="padding:0px; margin:0px 0px 0.75em 25px; list-style-type:none; line-height:1.7em">
<li style="line-height:1.7em; list-style-type:disc"><a target="_blank" href="http://www.python.org/" rel="nofollow" style="color:rgb(148,148,148); text-decoration:none; outline:none 0px; border-bottom-width:1px; border-bottom-style:dashed; border-bottom-color:rgb(148,148,148); font-style:italic; font-weight:bold">Python</a></li><li style="line-height:1.7em; list-style-type:disc"><a target="_blank" href="http://numpy.scipy.org/" rel="nofollow" style="color:rgb(148,148,148); text-decoration:none; outline:none 0px; border-bottom-width:1px; border-bottom-style:dashed; border-bottom-color:rgb(148,148,148); font-style:italic; font-weight:bold">NumPy</a></li></ul>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
这些是可能对你有帮助的:</p>
<ul style="padding:0px; margin:0px 0px 0.75em 25px; list-style-type:none; line-height:1.7em">
<li style="line-height:1.7em; list-style-type:disc"><a target="_blank" href="http://ipython.scipy.org/" rel="nofollow" style="color:rgb(148,148,148); text-decoration:none; outline:none 0px; border-bottom-width:1px; border-bottom-style:dashed; border-bottom-color:rgb(148,148,148); font-style:italic; font-weight:bold">ipython</a> 是一个净强化的交互Python
Shell,对探索NumPy的特性非常方便。</li><li style="line-height:1.7em; list-style-type:disc"><a target="_blank" href="http://matplotlib.sourceforge.net/" rel="nofollow" style="color:rgb(148,148,148); text-decoration:none; outline:none 0px; border-bottom-width:1px; border-bottom-style:dashed; border-bottom-color:rgb(148,148,148); font-style:italic; font-weight:bold">matplotlib</a> 将允许你绘图</li><li style="line-height:1.7em; list-style-type:disc"><a target="_blank" href="http://scipy.org/" rel="nofollow" style="color:rgb(148,148,148); text-decoration:none; outline:none 0px; border-bottom-width:1px; border-bottom-style:dashed; border-bottom-color:rgb(148,148,148); font-style:italic; font-weight:bold">Scipy</a> 在NumPy的基础上提供了很多科学模块</li></ul>
<h2 style="margin:0px 0px 0.5em; font-family:inherit; line-height:1.6em; color:inherit; font-size:18px; text-indent:1em"><a name="t2"></a>
基础篇</h2>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
NumPy的主要对象是同种元素的多维数组。这是一个所有的元素都是一种类型、通过一个正整数元组索引的元素表格(通常是元素是数字)。在NumPy中维度(dimensions)叫做轴(axes),轴的个数叫做秩(rank)。</p>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
例如,在3D空间一个点的坐标 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">[1, 2, 3]</code> 是一个秩为1的数组,因为它只有一个轴。那个轴长度为3.又例如,在以下例子中,数组的秩为2(它有两个维度).第一个维度长度为2,第二个维度长度为3.</p>
<pre class="prettyprint json" style="padding:0.3em; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; margin-top:0px; margin-bottom:1.5em; font-size:12px; line-height:1.5em; word-break:break-all; word-wrap:break-word; white-space:pre-wrap; overflow-y:auto; background-color:rgb(246,246,246)" name="code" onclick="hljs.copyCode(event)"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent" class="hljs css"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-selector-attr">[[ </span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-selector-attr">1.</span></span><span class="hljs-selector-attr">, </span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-selector-attr">0.</span></span><span class="hljs-selector-attr">, </span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-selector-attr">0.</span></span><span class="hljs-selector-attr">]</span>,</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-selector-attr">[ </span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-selector-attr">0.</span></span><span class="hljs-selector-attr">, </span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-selector-attr">1.</span></span><span class="hljs-selector-attr">, </span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-selector-attr">2.</span></span><span class="hljs-selector-attr">]</span>]</div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
NumPy的数组类被称作 <span style="margin:0px; padding:0px">ndarray</span> 。通常被称作数组。注意numpy.array和标准Python库类array.array并不相同,后者只处理一维数组和提供少量功能。更多重要ndarray对象属性有:</p>
<ul style="padding:0px; margin:0px 0px 0.75em 25px; list-style-type:none; line-height:1.7em">
<li style="line-height:1.7em; list-style-type:disc">
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em">ndarray.ndim</p>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em">数组轴的个数,在python的世界中,轴的个数被称作秩</p>
</li><li style="line-height:1.7em; list-style-type:disc">
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em">ndarray.shape</p>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em">数组的维度。这是一个指示数组在每个维度上大小的整数元组。例如一个n排m列的矩阵,它的shape属性将是(2,3),这个元组的长度显然是秩,即维度或者ndim属性</p>
</li><li style="line-height:1.7em; list-style-type:disc">
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em">ndarray.size</p>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em">数组元素的总个数,等于shape属性中元组元素的乘积。</p>
</li><li style="line-height:1.7em; list-style-type:disc">
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em">ndarray.dtype</p>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em">一个用来描述数组中元素类型的对象,可以通过创造或指定dtype使用标准Python类型。另外NumPy提供它自己的数据类型。</p>
</li><li style="line-height:1.7em; list-style-type:disc">
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em">ndarray.itemsize</p>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em">数组中每个元素的字节大小。例如,一个元素类型为float64的数组itemsiz属性值为8(=64/8),又如,一个元素类型为complex32的数组item属性为4(=32/8).</p>
</li><li style="line-height:1.7em; list-style-type:disc">
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em">ndarray.data</p>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em">包含实际数组元素的缓冲区,通常我们不需要使用这个属性,因为我们总是通过索引来使用数组中的元素。</p>
</li></ul>
<h3 style="margin:0px 0px 0.5em; font-family:inherit; line-height:1.6em; color:inherit; font-size:18px; text-indent:1em"><a name="t3"></a>
一个例子 <span style="position:relative; font-size:13.5px; line-height:0; vertical-align:baseline; top:-0.5em; margin:0px; padding:0px"><a target="_blank" href="http://reverland.org/python/2012/08/22/numpy/#fn:1" rel="nofollow" style="color:rgb(148,148,148); text-decoration:none; outline:none 0px; border-bottom-width:1px; border-bottom-style:dashed; border-bottom-color:rgb(148,148,148); font-style:italic">1</a></span></h3>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span><span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">from</span></span> numpy <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">import</span></span> </code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a = arange(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">15</span></span>).reshape(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">9</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">11</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">12</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">13</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">14</span></span>]])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a.shape</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a.ndim</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a.dtype.name</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="string" style="color:rgb(221,17,68)"><span class="hljs-string">'int32'</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a.itemsize</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="14"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="15"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a.size</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="16"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">15</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="17"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>type(a)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="18"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">numpy.ndarray</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="19"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>b = array([<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8</span></span>])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="20"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>b</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="21"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8</span></span>])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="22"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>type(b)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="23"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">numpy.ndarray</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<h3 style="margin:0px 0px 0.5em; font-family:inherit; line-height:1.6em; color:inherit; font-size:18px; text-indent:1em"><a name="t4"></a>
创建数组</h3>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
有好几种创建数组的方法。</p>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
例如,你可以使用 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">array</code> 函数从常规的Python列表和元组创造数组。所创建的数组类型由原序列中的元素类型推导而来。</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span><span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">from</span></span> numpy <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">import</span></span> </code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a = array( [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>] )</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a.dtype</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">dtype(<span class="string" style="color:rgb(221,17,68)"><span class="hljs-string">'int32'</span></span>)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>b = array([<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.2</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.5</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5.1</span></span>])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>b.dtype</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">dtype(<span class="string" style="color:rgb(221,17,68)"><span class="hljs-string">'float64'</span></span>) 一个常见的错误包括用多个数值参数调用array
而不是提供一个由数值组成的列表作为一个参数。</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> </code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a = array(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>) <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># WRONG</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> </code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a = array([<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>]) <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># RIGHT</span></span></code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
数组将序列包含序列转化成二维的数组,序列包含序列包含序列转化成三维数组等等。</p>
<pre onclick="hljs.copyCode(event)"><code class="language-php hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> b = <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">array</span></span>( [ (<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.5</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>), (<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6</span></span>) ] )</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> b</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">array</span></span>([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.5</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2.</span></span> , <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.</span></span> ],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4.</span></span> , <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5.</span></span> , <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6.</span></span> ]])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
数组类型可以在创建时显示指定</p>
<pre onclick="hljs.copyCode(event)"><code class="language-cpp hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> c = <span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-built_in">array</span></span>( [ [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>], [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>] ], dtype=<span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">complex</span></span> )</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> c</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-built_in">array</span></span>([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.</span></span>+<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>j, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2.</span></span>+<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>j],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.</span></span>+<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>j, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4.</span></span>+<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>j]])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
通常,数组的元素开始都是未知的,但是它的大小已知。因此,NumPy提供了一些使用占位符创建数组的函数。这最小化了扩展数组的需要和高昂的运算代价。</p>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
函数 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">function</code> 创建一个全是0的数组,函数 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">ones</code> 创建一个全1的数组,函数 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">empty</code> 创建一个内容随机并且依赖与内存状态的数组。默认创建的数组类型(dtype)都是float64。</p>
<pre onclick="hljs.copyCode(event)"><code class="language-php hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> zeros( (<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>) )</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">array</span></span>([[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>]])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> ones( (<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>), dtype=int16 ) <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># dtype can also be specified</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">array</span></span>([[[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>]],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>]]], dtype=int16)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">empty</span></span>( (<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>) )</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">array</span></span>([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.73603959e-262</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6.02658058e-154</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6.55490914e-260</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="14"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5.30498948e-313</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.14673309e-307</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.00000000e+000</span></span>]])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
为了创建一个数列,NumPy提供一个类似arange的函数返回数组而不是列表:</p>
<pre onclick="hljs.copyCode(event)"><code class="language-php hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> arange( <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">30</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span> )</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">array</span></span>([<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">15</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">20</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">25</span></span>])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> arange( <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.3</span></span> ) <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># it accepts float arguments</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">array</span></span>([ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span> , <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.3</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.6</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.9</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.2</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.5</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.8</span></span>])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
当 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">arange</code> 使用浮点数参数时,由于有限的浮点数精度,通常无法预测获得的元素个数。因此,最好使用函数 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">linspace</code> 去接收我们想要的元素个数来代替用range来指定步长。</p>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
其它函数array, zeros, zeros_like, ones, ones_like, empty, empty_like, arange, linspace, rand, randn, fromfunction, fromfile参考: <a target="_blank" href="http://scipy.org/Numpy_Example_List" rel="nofollow" style="color:rgb(148,148,148); text-decoration:none; outline:none 0px; border-bottom-width:1px; border-bottom-style:dashed; border-bottom-color:rgb(148,148,148); font-style:italic; font-weight:bold">NumPy示例</a></p>
<h3 style="margin:0px 0px 0.5em; font-family:inherit; line-height:1.6em; color:inherit; font-size:18px; text-indent:1em"><a name="t5"></a>
打印数组</h3>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
当你打印一个数组,NumPy以类似嵌套列表的形式显示它,但是呈以下布局:</p>
<ul style="padding:0px; margin:0px 0px 0.75em 25px; list-style-type:none; line-height:1.7em">
<li style="line-height:1.7em; list-style-type:disc">最后的轴从左到右打印</li><li style="line-height:1.7em; list-style-type:disc">次后的轴从顶向下打印</li><li style="line-height:1.7em; list-style-type:disc">剩下的轴从顶向下打印,每个切片通过一个空行与下一个隔开</li></ul>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
一维数组被打印成行,二维数组成矩阵,三维数组成矩阵列表。</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a = arange(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6</span></span>) <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># 1d array</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span><span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">print</span></span> a</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>b = arange(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">12</span></span>).reshape(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>) <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># 2d array</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span><span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">print</span></span> b</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">[[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8</span></span>]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">9</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">11</span></span>]]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>c = arange(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">24</span></span>).reshape(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>) <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># 3d array</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span><span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">print</span></span> c</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="14"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">[[[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="15"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7</span></span>]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="16"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">9</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">11</span></span>]]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="17"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> </code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="18"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">12</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">13</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">14</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">15</span></span>]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="19"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">16</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">17</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">18</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">19</span></span>]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="20"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">20</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">21</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">22</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">23</span></span>]]]</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
查看形状操作一节获得有关reshape的更多细节</p>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
如果一个数组用来打印太大了,NumPy自动省略中间部分而只打印角落</p>
<pre onclick="hljs.copyCode(event)"><code class="language-css hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">print</span></span> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">arange</span></span>(10000)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="attr_selector"><span class="hljs-selector-attr">[ 0 1 2 ..., 9997 9998 9999]</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">print</span></span> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">arange</span></span>(10000)<span class="class" style="color:rgb(68,85,136); font-weight:bold"><span class="hljs-selector-class">.reshape</span></span>(100,100)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="attr_selector"><span class="hljs-selector-attr">[[ 0 1 2 ..., 97 98 99]</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[ 100 101 102 ..., 197 198 199]</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[ 200 201 202 ..., 297 298 299]</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> ...,</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[9700 9701 9702 ..., 9797 9798 9799]</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[9800 9801 9802 ..., 9897 9898 9899]</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[9900 9901 9902 ..., 9997 9998 9999]</span></span>]</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
禁用NumPy的这种行为并强制打印整个数组,你可以设置printoptions参数来更改打印选项。</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>set_printoptions(threshold=<span class="string" style="color:rgb(221,17,68)"><span class="hljs-string">'nan'</span></span>)</code></code><div class="hljs-button" data-title="复制"></div></pre>
<h3 style="margin:0px 0px 0.5em; font-family:inherit; line-height:1.6em; color:inherit; font-size:18px; text-indent:1em"><a name="t6"></a>
基本运算</h3>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
数组的算术运算是按元素的。新的数组被创建并且被结果填充。</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a = array( [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">20</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">30</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">40</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">50</span></span>] )</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>b = arange( <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span> )</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>b</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>c = a-b</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>c</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">20</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">29</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">38</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">47</span></span>])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>b<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">9</span></span>])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>sin(a)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">9.12945251</span></span>, <span class="hljs-number">-</span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">9.88031624</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7.4511316</span></span> , <span class="hljs-number">-</span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2.62374854</span></span>])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a<<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">35</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([<span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-keyword">True</span></span>, <span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-keyword">True</span></span>, <span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-keyword">False</span></span>, <span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-keyword">False</span></span>], dtype=bool)</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
不像许多矩阵语言,NumPy中的乘法运算符 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)"></code> 指示按元素计算,矩阵乘法可以使用 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">dot</code> 函数或创建矩阵对象实现(参见教程中的矩阵章节)</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>A = array( [[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">... </span></span> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>]] )</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>B = array( [[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">... </span></span> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>]] )</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>AB <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># elementwise product</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>]])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>dot(A,B) <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># matrix product</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>]])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
有些操作符像 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">+=</code> 和 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">=</code> 被用来更改已存在数组而不创建一个新的数组。</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a = ones((<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>), dtype=int)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>b = random.random((<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>))</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a = <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>]])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>b += a</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>b</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.69092703</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.8324276</span></span> , <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.0114541</span></span> ],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.18679111</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.3039349</span></span> , <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.37600289</span></span>]])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a += b <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># b is converted to integer type</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="14"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6</span></span>]])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
当运算的是不同类型的数组时,结果数组和更普遍和精确的已知(这种行为叫做upcast)。</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a = ones(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>, dtype=int32)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>b = linspace(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>,pi,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>b.dtype.name</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="string" style="color:rgb(221,17,68)"><span class="hljs-string">'float64'</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>c = a+b</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>c</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.</span></span> , <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2.57079633</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4.14159265</span></span>])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>c.dtype.name</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="string" style="color:rgb(221,17,68)"><span class="hljs-string">'float64'</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>d = exp(c<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span><span class="hljs-number">j</span>)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>d</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.54030231</span></span>+<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.84147098</span></span><span class="hljs-number">j</span>, <span class="hljs-number">-</span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.84147098</span></span>+<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.54030231</span></span><span class="hljs-number">j</span>,</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="hljs-number">-</span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.54030231</span></span><span class="hljs-number">-</span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.84147098</span></span><span class="hljs-number">j</span>])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="14"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>d.dtype.name</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="15"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="string" style="color:rgb(221,17,68)"><span class="hljs-string">'complex128'</span></span> 许多非数组运算,如计算数组所有元素之和,被作为ndarray类的方法实现</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="16"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> </code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="17"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a = random.random((<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>))</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="18"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="19"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.6903007</span></span> , <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.39168346</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.16524769</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="20"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.48819875</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.77188505</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.94792155</span></span>]])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="21"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a.sum()</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="22"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.4552372100521485</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="23"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a.min()</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="24"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.16524768654743593</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="25"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a.max()</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="26"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.9479215542670073</span></span></code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
这些运算默认应用到数组好像它就是一个数字组成的列表,无关数组的形状。然而,指定 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">axis</code> 参数你可以吧运算应用到数组指定的轴上:</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>b = arange(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">12</span></span>).reshape(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>b</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">9</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">11</span></span>]])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>b.sum(axis=<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>) <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># sum of each column</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">12</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">15</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">18</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">21</span></span>])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>b.min(axis=<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>) <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># min of each row</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8</span></span>])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>b.cumsum(axis=<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>) <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># cumulative sum along each row</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="14"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="15"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">9</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">15</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">22</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="16"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">17</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">27</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">38</span></span>]])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<h3 style="margin:0px 0px 0.5em; font-family:inherit; line-height:1.6em; color:inherit; font-size:18px; text-indent:1em"><a name="t7"></a>
通用函数(ufunc)</h3>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
NumPy提供常见的数学函数如 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">sin</code> , <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">cos</code> 和 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">exp</code> 。在NumPy中,这些叫作“通用函数”(ufunc)。在NumPy里这些函数作用按数组的元素运算,产生一个数组作为输出。 </p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>B = arange(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>B</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>exp(B)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.</span></span> , <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2.71828183</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7.3890561</span></span> ])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>sqrt(B)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span> , <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.</span></span> , <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.41421356</span></span>])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>C = array([<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2.</span></span>, <span class="hljs-number">-</span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4.</span></span>])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>add(B, C)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6.</span></span>])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
更多函数all, alltrue, any, apply along axis, argmax, argmin, argsort, average, bincount, ceil, clip, conj, conjugate, corrcoef, cov, cross, cumprod, cumsum, diff, dot, floor, inner, inv, lexsort, max, maximum, mean, median, min, minimum, nonzero, outer, prod, re,
round, sometrue, sort, std, sum, trace, transpose, var, vdot, vectorize, where 参见: <a target="blank" href="http://scipy.org/Numpy_Example_List" rel="nofollow" style="color:rgb(148,148,148); text-decoration:none; outline:none 0px; border-bottom-width:1px; border-bottom-style:dashed; border-bottom-color:rgb(148,148,148); font-style:italic; font-weight:bold">NumPy示例</a></p>
<h3 style="margin:0px 0px 0.5em; font-family:inherit; line-height:1.6em; color:inherit; font-size:18px; text-indent:1em"><a name="t8"></a>
索引,切片和迭代</h3>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
<span style="margin:0px; padding:0px">一维</span> 数组可以被索引、切片和迭代,就像 <a target="blank" href="http://docs.python.org/tutorial/introduction.html#SECTION005140000000000000000" rel="nofollow" style="color:rgb(148,148,148); text-decoration:none; outline:none 0px; border-bottom-width:1px; border-bottom-style:dashed; border-bottom-color:rgb(148,148,148); font-style:italic; font-weight:bold">列表</a> 和其它Python序列。</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln" style="width:1029px"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a = arange(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>)<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">27</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">64</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">125</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">216</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">343</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">512</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">729</span></span>])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>:<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">27</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">64</span></span>])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a[:<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6</span></span>:<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>] = <span class="hljs-number">-</span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1000</span></span> <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># equivalent to a[0:6:2] = -1000; from start to position 6, exclusive, set every 2nd element to -1000</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([<span class="hljs-number">-</span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1000</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="hljs-number">-</span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1000</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">27</span></span>, <span class="hljs-number">-</span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1000</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">125</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">216</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">343</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">512</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">729</span></span>])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a[ : :<span class="hljs-number">-</span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>] <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># reversed a</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">729</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">512</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">343</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">216</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">125</span></span>, <span class="hljs-number">-</span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1000</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">27</span></span>, <span class="hljs-number">-</span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1000</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="hljs-number">-</span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1000</span></span>])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span><span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">for</span></span> i <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">in</span></span> a:</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="14"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">... </span></span> <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">print</span></span> i(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>/<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.</span></span>),</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="15"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">...</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="16"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">nan <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.0</span></span> nan <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.0</span></span> nan <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5.0</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6.0</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7.0</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8.0</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">9.0</span></span></code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
<span style="margin:0px; padding:0px">多维</span> 数组可以每个轴有一个索引。这些索引由一个逗号分割的元组给出。</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span><span class="function"><span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-function"><span class="hljs-keyword">def</span></span></span><span class="hljs-function"> </span><span class="title" style="color:rgb(153,0,0); font-weight:bold"><span class="hljs-function"><span class="hljs-title">f</span></span></span><span class="params"><span class="hljs-function"><span class="hljs-params">(x,y)</span></span></span><span class="hljs-function">:</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">... </span></span> <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">return</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>x+y</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">...</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>b = fromfunction(f,(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>),dtype=int)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>b</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">11</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">12</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">13</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">20</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">21</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">22</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">23</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">30</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">31</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">32</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">33</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">40</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">41</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">42</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">43</span></span>]])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>b[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">23</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>b[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>:<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>] <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># each row in the second column of b</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="14"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">11</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">21</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">31</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">41</span></span>])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="15"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>b[ : ,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>] <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># equivalent to the previous example</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="16"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">11</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">21</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">31</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">41</span></span>])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="17"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>b[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>:<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>, : ] <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># each column in the second and third row of b</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="18"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">11</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">12</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">13</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="19"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">20</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">21</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">22</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">23</span></span>]])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
当少于轴数的索引被提供时,确失的索引被认为是整个切片:</p>
<pre onclick="hljs.copyCode(event)"><code class="language-css hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">b</span></span><span class="attr_selector"><span class="hljs-selector-attr">[-1]</span></span> # <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">the</span></span> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">last</span></span> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">row</span></span>. <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">Equivalent</span></span> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">to</span></span> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">b</span></span><span class="attr_selector"><span class="hljs-selector-attr">[-1,:]</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">array</span></span>(<span class="attr_selector"><span class="hljs-selector-attr">[40, 41, 42, 43]</span></span>)</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
<code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">b[i]</code> 中括号中的表达式被当作 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">i</code> 和一系列 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">:</code> ,来代表剩下的轴。NumPy也允许你使用“点”像 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">b[i,...]</code> 。</p>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
<span style="margin:0px; padding:0px">点</span> (…)代表许多产生一个完整的索引元组必要的分号。如果x是秩为5的数组(即它有5个轴),那么:</p>
<ul style="padding:0px; margin:0px 0px 0.75em 25px; list-style-type:none; line-height:1.7em">
<li style="line-height:1.7em; list-style-type:disc">x[1,2,…] 等同于 x[1,2,:,:,:],</li><li style="line-height:1.7em; list-style-type:disc">x[…,3] 等同于 x[:,:,:,:,3]</li><li style="line-height:1.7em; list-style-type:disc">x[4,…,5,:] 等同 x[4,:,:,5,:].</li></ul>
<div style="line-height:1.7em">
<pre onclick="hljs.copyCode(event)"><code class="language-php hljs"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span style="padding:0px; margin:0px">>>></span> <span style="padding:0px; margin:0px">c</span> <span style="padding:0px; margin:0px">=</span> <span style="padding:0px; margin:0px"><span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">array</span></span></span><span style="padding:0px; margin:0px">(</span> <span style="padding:0px; margin:0px">[</span> <span style="padding:0px; margin:0px">[[</span> <span style="padding:0px; margin:0px"><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span></span><span style="padding:0px; margin:0px">,</span> <span style="padding:0px; margin:0px"><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span></span><span style="padding:0px; margin:0px">,</span> <span style="padding:0px; margin:0px"><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span></span><span style="padding:0px; margin:0px">],</span> <span style="padding:0px; margin:0px"><span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># a 3D array (two stacked 2D arrays)</span></span></span><span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"> </span><span style="padding:0px; margin:0px"><span class="hljs-comment">...</span></span><span class="hljs-comment"> </span><span style="padding:0px; margin:0px"><span class="hljs-comment">[</span></span><span class="hljs-comment"> </span><span style="padding:0px; margin:0px"><span class="hljs-comment">10</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">,</span></span><span class="hljs-comment"> </span><span style="padding:0px; margin:0px"><span class="hljs-comment">12</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">,</span></span><span class="hljs-comment"> </span><span style="padding:0px; margin:0px"><span class="hljs-comment">13</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">]],</span></span><span class="hljs-comment"> </span><span style="padding:0px; margin:0px"><span class="hljs-comment">...</span></span><span class="hljs-comment"> </span><span style="padding:0px; margin:0px"><span class="hljs-comment">...</span></span><span class="hljs-comment"> </span><span style="padding:0px; margin:0px"><span class="hljs-comment">[[</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">100</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">,</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">101</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">,</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">102</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">],</span></span><span class="hljs-comment"> </span><span style="padding:0px; margin:0px"><span class="hljs-comment">...</span></span><span class="hljs-comment"> </span><span style="padding:0px; margin:0px"><span class="hljs-comment">[</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">110</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">,</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">112</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">,</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">113</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">]]</span></span><span class="hljs-comment"> </span><span style="padding:0px; margin:0px"><span class="hljs-comment">]</span></span><span class="hljs-comment"> </span><span style="padding:0px; margin:0px"><span class="hljs-comment">)</span></span><span class="hljs-comment"> </span><span style="padding:0px; margin:0px"><span class="hljs-comment">>>></span></span><span class="hljs-comment"> </span><span style="padding:0px; margin:0px"><span class="hljs-comment">c</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">.</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">shape</span></span><span class="hljs-comment"> </span><span style="padding:0px; margin:0px"><span class="hljs-comment">(</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">2</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">,</span></span><span class="hljs-comment"> </span><span style="padding:0px; margin:0px"><span class="hljs-comment">2</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">,</span></span><span class="hljs-comment"> </span><span style="padding:0px; margin:0px"><span class="hljs-comment">3</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">)</span></span><span class="hljs-comment"> </span><span style="padding:0px; margin:0px"><span class="hljs-comment">>>></span></span><span class="hljs-comment"> </span><span style="padding:0px; margin:0px"><span class="hljs-comment">c</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">[</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">1</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">,</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">...</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">]</span></span><span class="hljs-comment"> </span><span style="padding:0px; margin:0px"><span class="hljs-comment"># same as c[1,:,:] or c[1]</span></span><span class="hljs-comment"> </span><span style="padding:0px; margin:0px"><span class="hljs-comment">array</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">([[</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">100</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">,</span></span><span class="hljs-comment"> </span><span style="padding:0px; margin:0px"><span class="hljs-comment">101</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">,</span></span><span class="hljs-comment"> </span><span style="padding:0px; margin:0px"><span class="hljs-comment">102</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">],</span></span><span class="hljs-comment"> </span><span style="padding:0px; margin:0px"><span class="hljs-comment">[</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">110</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">,</span></span><span class="hljs-comment"> </span><span style="padding:0px; margin:0px"><span class="hljs-comment">112</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">,</span></span><span class="hljs-comment"> </span><span style="padding:0px; margin:0px"><span class="hljs-comment">113</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">]])</span></span><span class="hljs-comment"> </span><span style="padding:0px; margin:0px"><span class="hljs-comment">>>></span></span><span class="hljs-comment"> </span><span style="padding:0px; margin:0px"><span class="hljs-comment">c</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">[</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">...</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">,</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">2</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">]</span></span><span class="hljs-comment"> </span><span style="padding:0px; margin:0px"><span class="hljs-comment"># same as c[:,:,2]</span></span><span class="hljs-comment"> </span><span style="padding:0px; margin:0px"><span class="hljs-comment">array</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">([[</span></span><span class="hljs-comment"> </span><span style="padding:0px; margin:0px"><span class="hljs-comment">2</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">,</span></span><span class="hljs-comment"> </span><span style="padding:0px; margin:0px"><span class="hljs-comment">13</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">],</span></span><span class="hljs-comment"> </span><span style="padding:0px; margin:0px"><span class="hljs-comment">[</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">102</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">,</span></span><span class="hljs-comment"> </span><span style="padding:0px; margin:0px"><span class="hljs-comment">113</span></span><span style="padding:0px; margin:0px"><span class="hljs-comment">]])</span></span><span class="hljs-comment"> </span></span></code></code><div class="hljs-button" data-title="复制"></div></pre>
</div>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
<span style="margin:0px; padding:0px">迭代</span> 多维数组是就第一个轴而言的: <span style="position:relative; font-size:12px; line-height:0; vertical-align:baseline; top:-0.5em; margin:0px; padding:0px"><a target="_blank" href="http://reverland.org/python/2012/08/22/numpy/#fn:2" rel="nofollow" style="color:rgb(148,148,148); text-decoration:none; outline:0px; border-bottom-width:1px; border-bottom-style:dashed; border-bottom-color:rgb(148,148,148); font-style:italic; font-weight:bold; margin:0px; padding:0px">2</a></span></p>
<pre onclick="hljs.copyCode(event)"><code class="language-css hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">for</span></span> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">row</span></span> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">in</span></span> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">b</span></span>:</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">... <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">print</span></span> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">row</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">...</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="attr_selector"><span class="hljs-selector-attr">[0 1 2 3]</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="attr_selector"><span class="hljs-selector-attr">[10 11 12 13]</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="attr_selector"><span class="hljs-selector-attr">[20 21 22 23]</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="attr_selector"><span class="hljs-selector-attr">[30 31 32 33]</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="attr_selector"><span class="hljs-selector-attr">[40 41 42 43]</span></span></code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
然而,如果一个人想对每个数组中元素进行运算,我们可以使用flat属性,该属性是数组元素的一个迭代器:</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span><span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">for</span></span> element <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">in</span></span> b.flat:</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">... </span></span> <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">print</span></span> element,</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">...</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">11</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">12</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">13</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">20</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">21</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">22</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">23</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">30</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">31</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">32</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">33</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">40</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">41</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">42</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">43</span></span></code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
更多[], …, newaxis, ndenumerate, indices, index exp 参考 <a target="_blank" href="http://scipy.org/Numpy_Example_List" rel="nofollow" style="color:rgb(148,148,148); text-decoration:none; outline:none 0px; border-bottom-width:1px; border-bottom-style:dashed; border-bottom-color:rgb(148,148,148); font-style:italic; font-weight:bold">NumPy示例</a></p>
<h2 style="margin:0px 0px 0.5em; font-family:inherit; line-height:1.6em; color:inherit; font-size:18px; text-indent:1em"><a name="t9"></a>
形状操作</h2>
<h3 style="margin:0px 0px 0.5em; font-family:inherit; line-height:1.6em; color:inherit; font-size:18px; text-indent:1em"><a name="t10"></a>
更改数组的形状</h3>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
一个数组的形状由它每个轴上的元素个数给出:</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a = floor(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>random.random((<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>)))</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">9.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8.</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2.</span></span>]])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a.shape</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>)</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
一个数组的形状可以被多种命令修改:</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a.ravel() <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># flatten the array</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">9.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2.</span></span>])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a.shape = (<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a.transpose()</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">9.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2.</span></span>]])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
由 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">ravel()</code> 展平的数组元素的顺序通常是“C风格”的,就是说,最右边的索引变化得最快,所以元素a[0,0]之后是a[0,1]。如果数组被改变形状(reshape)成其它形状,数组仍然是“C风格”的。NumPy通常创建一个以这个顺序保存数据的数组,所以 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">ravel()</code> 将总是不需要复制它的参数 <span style="position:relative; font-size:12px; line-height:0; vertical-align:baseline; top:-0.5em; margin:0px; padding:0px"><a target="blank" href="http://reverland.org/python/2012/08/22/numpy/#fn:3" rel="nofollow" style="color:rgb(148,148,148); text-decoration:none; outline:0px; border-bottom-width:1px; border-bottom-style:dashed; border-bottom-color:rgb(148,148,148); font-style:italic; font-weight:bold; margin:0px; padding:0px">3</a> </span>。但是如果数组是通过切片其它数组或有不同寻常的选项时,它可能需要被复制。函数 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">reshape()</code> 和 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">ravel()</code> 还可以被同过一些可选参数构建成FORTRAN风格的数组,即最左边的索引变化最快。</p>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
<code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">reshape</code> 函数改变参数形状并返回它,而 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">resize</code> 函数改变数组自身。</p>
<pre onclick="hljs.copyCode(event)"><code class="language-css hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">a</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">array</span></span>(<span class="attr_selector"><span class="hljs-selector-attr">[[ 7., 5.]</span></span>,</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[ 9., 3.]</span></span>,</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[ 7., 2.]</span></span>,</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[ 7., 8.]</span></span>,</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[ 6., 8.]</span></span>,</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[ 3., 2.]</span></span>])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">a</span></span><span class="class" style="color:rgb(68,85,136); font-weight:bold"><span class="hljs-selector-class">.resize</span></span>((2,6))</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">a</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">array</span></span>(<span class="attr_selector"><span class="hljs-selector-attr">[[ 7., 5., 9., 3., 7., 2.]</span></span>,</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[ 7., 8., 6., 8., 3., 2.]</span></span>])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
如果在改变形状操作中一个维度被给做-1,其维度将自动被计算</p>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
更多 shape, reshape, resize, ravel 参考 <a target="blank" href="http://scipy.org/Numpy_Example_List" rel="nofollow" style="color:rgb(148,148,148); text-decoration:none; outline:none 0px; border-bottom-width:1px; border-bottom-style:dashed; border-bottom-color:rgb(148,148,148); font-style:italic; font-weight:bold">NumPy示例</a></p>
<h3 style="margin:0px 0px 0.5em; font-family:inherit; line-height:1.6em; color:inherit; font-size:18px; text-indent:1em"><a name="t11"></a>
组合(stack)不同的数组</h3>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
几种方法可以沿不同轴将数组堆叠在一起:</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a = floor(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>random.random((<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>)))</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8.</span></span>]])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>b = floor(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>random.random((<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>)))</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>b</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>]])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>vstack((a,b))</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8.</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>]])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="14"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>hstack((a,b))</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="15"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="16"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>]])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
函数 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">column_stack</code> 以列将一维数组合成二维数组,它等同与 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">vstack</code> 对一维数组。</p>
<pre onclick="hljs.copyCode(event)"><code class="language-php hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> column_stack((a,b)) <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># With 2D arrays</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">array</span></span>([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>]])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> a=<span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">array</span></span>([<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4.</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2.</span></span>])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> b=<span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">array</span></span>([<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2.</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8.</span></span>])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> a[:,newaxis] <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># This allows to have a 2D columns vector</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">array</span></span>([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4.</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2.</span></span>]])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> column_stack((a[:,newaxis],b[:,newaxis]))</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">array</span></span>([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2.</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8.</span></span>]])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> vstack((a[:,newaxis],b[:,newaxis])) <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># The behavior of vstack is different</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">array</span></span>([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4.</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="14"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2.</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="15"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2.</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="16"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8.</span></span>]])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
<code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">row_stack</code> 函数,另一方面,将一维数组以行组合成二维数组。</p>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
对那些维度比二维更高的数组, <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">hstack</code> 沿着第二个轴组合, <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">vstack</code> 沿着第一个轴组合, <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">concatenate</code> 允许可选参数给出组合时沿着的轴。</p>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
<span style="margin:0px; padding:0px">Note</span></p>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
在复杂情况下, <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">r[]</code> 和 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">c[]</code> 对创建沿着一个方向组合的数很有用,它们允许范围符号(“:”):</p>
<pre onclick="hljs.copyCode(event)"><code class="language-css hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">r_</span></span><span class="attr_selector"><span class="hljs-selector-attr">[1:4,0,4]</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">array</span></span>(<span class="attr_selector"><span class="hljs-selector-attr">[1, 2, 3, 0, 4]</span></span>)</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
当使用数组作为参数时, <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">r_</code> 和 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">c</code> 的默认行为和 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">vstack</code> 和 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">hstack</code> 很像,但是允许可选的参数给出组合所沿着的轴的代号。</p>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
更多函数hstack , vstack, column_stack , row_stack , concatenate , c , r_ 参见 <a target="_blank" href="http://scipy.org/Numpy_Example_List" rel="nofollow" style="color:rgb(148,148,148); text-decoration:none; outline:none 0px; border-bottom-width:1px; border-bottom-style:dashed; border-bottom-color:rgb(148,148,148); font-style:italic; font-weight:bold">NumPy示例</a> .</p>
<h3 style="margin:0px 0px 0.5em; font-family:inherit; line-height:1.6em; color:inherit; font-size:18px; text-indent:1em"><a name="t12"></a>
将一个数组分割(split)成几个小数组</h3>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
使用 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">hsplit</code> 你能将数组沿着它的水平轴分割,或者指定返回相同形状数组的个数,或者指定在哪些列后发生分割:</p>
<pre onclick="hljs.copyCode(event)"><code class="language-php hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> a = floor(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>random.random((<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">12</span></span>)))</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> a</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">array</span></span>([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">9.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4.</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">9.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4.</span></span>]])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> hsplit(a,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>) <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># Split a into 3</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">[<span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">array</span></span>([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">9.</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">9.</span></span>]]), <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">array</span></span>([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5.</span></span>]]), <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">array</span></span>([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4.</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4.</span></span>]])]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> hsplit(a,(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>)) <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># Split a after the third and the fourth column</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">[<span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">array</span></span>([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2.</span></span>]]), <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">array</span></span>([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">9.</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">9.</span></span>]]), <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">array</span></span>([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4.</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="14"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4.</span></span>]])]</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
<code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">vsplit</code> 沿着纵向的轴分割, <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">array
split</code> 允许指定沿哪个轴分割。</p>
<h2 style="margin:0px 0px 0.5em; font-family:inherit; line-height:1.6em; color:inherit; font-size:18px; text-indent:1em"><a name="t13"></a>
复制和视图</h2>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
当运算和处理数组时,它们的数据有时被拷贝到新的数组有时不是。这通常是新手的困惑之源。这有三种情况:</p>
<h3 style="margin:0px 0px 0.5em; font-family:inherit; line-height:1.6em; color:inherit; font-size:18px; text-indent:1em"><a name="t14"></a>
完全不拷贝</h3>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
简单的赋值不拷贝数组对象或它们的数据。</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a = arange(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">12</span></span>)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>b = a <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># no new object is created</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>b <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">is</span></span> a <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># a and b are two names for the same ndarray object</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-keyword">True</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>b.shape = <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span> <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># changes the shape of a</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a.shape</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>)</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
Python 传递不定对象作为参考 <span style="position:relative; font-size:12px; line-height:0; vertical-align:baseline; top:-0.5em; margin:0px; padding:0px"><a target="_blank" href="http://reverland.org/python/2012/08/22/numpy/#fn:4" rel="nofollow" style="color:rgb(148,148,148); text-decoration:none; outline:0px; border-bottom-width:1px; border-bottom-style:dashed; border-bottom-color:rgb(148,148,148); font-style:italic; font-weight:bold; margin:0px; padding:0px">4</a> </span>,所以函数调用不拷贝数组。</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span><span class="function"><span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-function"><span class="hljs-keyword">def</span></span></span><span class="hljs-function"> </span><span class="title" style="color:rgb(153,0,0); font-weight:bold"><span class="hljs-function"><span class="hljs-title">f</span></span></span><span class="params"><span class="hljs-function"><span class="hljs-params">(x)</span></span></span><span class="hljs-function">:</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">... </span></span> <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">print</span></span> id(x)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">...</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>id(a) <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># id is a unique identifier of an object</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">148293216</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>f(a)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">148293216</span></span></code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<h3 style="margin:0px 0px 0.5em; font-family:inherit; line-height:1.6em; color:inherit; font-size:18px; text-indent:1em"><a name="t15"></a>
视图(view)和浅复制</h3>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
不同的数组对象分享同一个数据。视图方法创造一个新的数组对象指向同一数据。</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>c = a.view()</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>c <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">is</span></span> a</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-keyword">False</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>c.base <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">is</span></span> a <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># c is a view of the data owned by a</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-keyword">True</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>c.flags.owndata</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-keyword">False</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>c.shape = <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6</span></span> <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># a's shape doesn't change</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a.shape</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>c[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>] = <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1234</span></span> <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># a's data changes</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="14"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="15"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1234</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="16"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">9</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">11</span></span>]])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
切片数组返回它的一个视图:</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>s = a[ : , <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>:<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>] <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># spaces added for clarity; could also be written "s = a[:,1:3]"</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>s[:] = <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span> <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># s[:] is a view of s. Note the difference between s=10 and s[:]=10</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1234</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">11</span></span>]])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<h2 style="margin:0px 0px 0.5em; font-family:inherit; line-height:1.6em; color:inherit; font-size:18px; text-indent:1em"><a name="t16"></a>
深复制</h2>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
这个复制方法完全复制数组和它的数据。</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>d = a.copy() <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># a new array object with new data is created</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>d <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">is</span></span> a</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-keyword">False</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>d.base <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">is</span></span> a <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># d doesn't share anything with a</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-keyword">False</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>d[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>] = <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">9999</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1234</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">11</span></span>]])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<h3 style="margin:0px 0px 0.5em; font-family:inherit; line-height:1.6em; color:inherit; font-size:18px; text-indent:1em"><a name="t17"></a>
函数和方法(method)总览</h3>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
这是个NumPy函数和方法分类排列目录。这些名字链接到 <a target="_blank" href="http://scipy.org/Numpy_Example_List" rel="nofollow" style="color:rgb(148,148,148); text-decoration:none; outline:none 0px; border-bottom-width:1px; border-bottom-style:dashed; border-bottom-color:rgb(148,148,148); font-style:italic; font-weight:bold">NumPy示例</a> ,你可以看到这些函数起作用。[^5]</p>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
<span style="margin:0px; padding:0px">创建数组</span></p>
<pre onclick="hljs.copyCode(event)"><code class="language-php hljs"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">arange, <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">array</span></span>, copy, <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">empty</span></span>, empty_like, eye, fromfile, fromfunction, identity, linspace, logspace, mgrid, ogrid, ones, ones_like, r , zeros, zeros_like </code></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
<span style="margin:0px; padding:0px">转化</span></p>
<pre class="prettyprint undefined" style="padding:0.3em; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; margin-top:0px; margin-bottom:1.5em; font-size:12px; line-height:1.5em; word-break:break-all; word-wrap:break-word; white-space:pre-wrap; overflow-y:auto; background-color:rgb(246,246,246)" name="code" onclick="hljs.copyCode(event)"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent" class="hljs go">astype, atleast <span class="hljs-number">1d</span>, atleast <span class="hljs-number">2d</span>, atleast <span class="hljs-number">3d</span>, mat </code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
<span style="margin:0px; padding:0px">操作</span></p>
<pre onclick="hljs.copyCode(event)"><code class="language-cpp hljs"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-built_in">array</span></span> split, column <span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-built_in">stack</span></span>, concatenate, diagonal, dsplit, dstack, hsplit, hstack, item, newaxis, ravel, repeat, reshape, resize, squeeze, swapaxes, take, transpose, vsplit, vstack </code></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
<span style="margin:0px; padding:0px">询问</span></p>
<pre class="prettyprint cs" style="padding:0.3em; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; margin-top:0px; margin-bottom:1.5em; font-size:12px; line-height:1.5em; word-break:break-all; word-wrap:break-word; white-space:pre-wrap; overflow-y:auto; background-color:rgb(246,246,246)" name="code" onclick="hljs.copyCode(event)"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent" class="hljs">all, any, nonzero, <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">where</span></span> </code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
<span style="margin:0px; padding:0px">排序</span></p>
<pre class="prettyprint perl" style="padding:0.3em; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; margin-top:0px; margin-bottom:1.5em; font-size:12px; line-height:1.5em; word-break:break-all; word-wrap:break-word; white-space:pre-wrap; overflow-y:auto; background-color:rgb(246,246,246)" name="code" onclick="hljs.copyCode(event)"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent" class="hljs">argmax, argmin, argsort, max, min, ptp, searchsorted, <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">sort</span></span> </code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
<span style="margin:0px; padding:0px">运算</span></p>
<pre class="prettyprint undefined" style="padding:0.3em; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; margin-top:0px; margin-bottom:1.5em; font-size:12px; line-height:1.5em; word-break:break-all; word-wrap:break-word; white-space:pre-wrap; overflow-y:auto; background-color:rgb(246,246,246)" name="code" onclick="hljs.copyCode(event)"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent" class="hljs go">choose, compress, cumprod, cumsum, inner, fill, <span class="hljs-built_in">imag</span>, prod, put, putmask, <span class="hljs-built_in">real</span>, sum </code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
<span style="margin:0px; padding:0px">基本统计</span></p>
<pre class="prettyprint cs" style="padding:0.3em; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; margin-top:0px; margin-bottom:1.5em; font-size:12px; line-height:1.5em; word-break:break-all; word-wrap:break-word; white-space:pre-wrap; overflow-y:auto; background-color:rgb(246,246,246)" name="code" onclick="hljs.copyCode(event)"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent" class="hljs">cov, mean, std, <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">var</span></span> </code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
<span style="margin:0px; padding:0px">基本线性代数</span></p>
<pre class="prettyprint undefined" style="padding:0.3em; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; margin-top:0px; margin-bottom:1.5em; font-size:12px; line-height:1.5em; word-break:break-all; word-wrap:break-word; white-space:pre-wrap; overflow-y:auto; background-color:rgb(246,246,246)" name="code" onclick="hljs.copyCode(event)"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent" class="hljs">cross, dot, outer, svd, vdot</code><div class="hljs-button" data-title="复制"></div></pre>
<h2 style="margin:0px 0px 0.5em; font-family:inherit; line-height:1.6em; color:inherit; font-size:18px; text-indent:1em"><a name="t18"></a>
进阶</h2>
<h3 style="margin:0px 0px 0.5em; font-family:inherit; line-height:1.6em; color:inherit; font-size:18px; text-indent:1em"><a name="t19"></a>
广播法则(rule)</h3>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
广播法则能使通用函数有意义地处理不具有相同形状的输入。</p>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
广播第一法则是,如果所有的输入数组维度不都相同,一个“1”将被重复地添加在维度较小的数组上直至所有的数组拥有一样的维度。</p>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
广播第二法则确定长度为1的数组沿着特殊的方向表现地好像它有沿着那个方向最大形状的大小。对数组来说,沿着那个维度的数组元素的值理应相同。</p>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
应用广播法则之后,所有数组的大小必须匹配。更多细节可以从这个 <a target="_blank" href="http://scipy.org/EricsBroadcastingDoc" rel="nofollow" style="color:rgb(148,148,148); text-decoration:none; outline:none 0px; border-bottom-width:1px; border-bottom-style:dashed; border-bottom-color:rgb(148,148,148); font-style:italic; font-weight:bold">文档</a> 找到。</p>
<h2 style="margin:0px 0px 0.5em; font-family:inherit; line-height:1.6em; color:inherit; font-size:18px; text-indent:1em"><a name="t20"></a>
花哨的索引和索引技巧</h2>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
NumPy比普通Python序列提供更多的索引功能。除了索引整数和切片,正如我们之前看到的,数组可以被整数数组和布尔数组索引。</p>
<h3 style="margin:0px 0px 0.5em; font-family:inherit; line-height:1.6em; color:inherit; font-size:18px; text-indent:1em"><a name="t21"></a>
通过数组索引</h3>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a = arange(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">12</span></span>)<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span> <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># the first 12 square numbers</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>i = array( [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span> ] ) <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># an array of indices</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a[i] <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># the elements of a at the positions i</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">9</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">64</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">25</span></span>])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>j = array( [ [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>], [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">9</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7</span></span> ] ] ) <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># a bidimensional array of indices</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a[j] <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># the same shape as j</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">9</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">16</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">81</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">49</span></span>]])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
当被索引数组a是多维的时,每一个唯一的索引数列指向a的第一维 <span style="position:relative; font-size:12px; line-height:0; vertical-align:baseline; top:-0.5em; margin:0px; padding:0px"><a target="_blank" href="http://reverland.org/python/2012/08/22/numpy/#fn:6" rel="nofollow" style="color:rgb(148,148,148); text-decoration:none; outline:0px; border-bottom-width:1px; border-bottom-style:dashed; border-bottom-color:rgb(148,148,148); font-style:italic; font-weight:bold; margin:0px; padding:0px">5</a> </span>。以下示例通过将图片标签用调色版转换成色彩图像展示了这种行为。</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>palette = array( [ [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>], <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># black</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">... </span></span> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">255</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>], <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># red</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">... </span></span> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">255</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>], <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># green</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">... </span></span> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">255</span></span>], <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># blue</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">... </span></span> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">255</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">255</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">255</span></span>] ] ) <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># white</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>image = array( [ [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span> ], <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># each value corresponds to a color in the palette</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">... </span></span> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span> ] ] )</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>palette[image] <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># the (2,4,3) color image</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">255</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">255</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>]],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="14"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">255</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="15"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">255</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">255</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">255</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="16"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>]]])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
我们也可以给出不不止一维的索引,每一维的索引数组必须有相同的形状。</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a = arange(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">12</span></span>).reshape(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">9</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">11</span></span>]])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>i = array( [ [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>], <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># indices for the first dim of a</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">... </span></span> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>] ] )</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>j = array( [ [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>], <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># indices for the second dim</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">... </span></span> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>] ] )</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a[i,j] <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># i and j must have equal shape</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">11</span></span>]])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="14"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="15"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a[i,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="16"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="17"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>]])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="18"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="19"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a[:,j] <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># i.e., a[ : , j]</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="20"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="21"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>]],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="22"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="23"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7</span></span>]],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="24"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">9</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="25"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">11</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">11</span></span>]]])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
自然,我们可以把i和j放到序列中(比如说列表)然后通过list索引。</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>l = [i,j]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a[l] <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># equivalent to a[i,j]</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">11</span></span>]])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
然而,我们不能把i和j放在一个数组中,因为这个数组将被解释成索引a的第一维。</p>
<pre onclick="hljs.copyCode(event)"><code class="language-sql hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> s = array( [i,j] )</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> a[s] # not what we want</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment">---------------------------------------------------------------------------</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">IndexError Traceback (most recent <span class="operator"><span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">call</span></span> <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">last</span></span>)</span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">in</span></span> ()</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="hljs-comment">----> </span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-comment">1</span></span><span class="hljs-comment"> a[s]</span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> </code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">IndexError: <span class="hljs-keyword">index</span> (<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>) <span class="hljs-keyword">out</span> <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">of</span></span> <span class="hljs-keyword">range</span> (<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span><=<span class="hljs-keyword">index</span><<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>) <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">in</span></span> <span class="hljs-keyword">dimension</span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> a[tuple(s)] # same <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">as</span></span> a[i,j]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="hljs-built_in">array</span>([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">11</span></span>]])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
另一个常用的数组索引用法是搜索时间序列最大值 <span style="position:relative; font-size:12px; line-height:0; vertical-align:baseline; top:-0.5em; margin:0px; padding:0px"><a target="_blank" href="http://reverland.org/python/2012/08/22/numpy/#fn:7" rel="nofollow" style="color:rgb(148,148,148); text-decoration:none; outline:0px; border-bottom-width:1px; border-bottom-style:dashed; border-bottom-color:rgb(148,148,148); font-style:italic; font-weight:bold; margin:0px; padding:0px">6</a> </span>。</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>time = linspace(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">20</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">145</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>) <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># time scale</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>data = sin(arange(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">20</span></span>)).reshape(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>) <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># 4 time-dependent series</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>time</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">20.</span></span> , <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">51.25</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">82.5</span></span> , <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">113.75</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">145.</span></span> ])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>data</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span> , <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.84147098</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.90929743</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.14112001</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="hljs-number">-</span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.7568025</span></span> , <span class="hljs-number">-</span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.95892427</span></span>, <span class="hljs-number">-</span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.2794155</span></span> , <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.6569866</span></span> ],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.98935825</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.41211849</span></span>, <span class="hljs-number">-</span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.54402111</span></span>, <span class="hljs-number">-</span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.99999021</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="hljs-number">-</span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.53657292</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.42016704</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.99060736</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.65028784</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="hljs-number">-</span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.28790332</span></span>, <span class="hljs-number">-</span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.96139749</span></span>, <span class="hljs-number">-</span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.75098725</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.14987721</span></span>]])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>ind = data.argmax(axis=<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>) <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># index of the maxima for each series</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>ind</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="14"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="15"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="16"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>time_max = time[ ind] <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># times corresponding to the maxima</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="17"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="18"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>data_max = data[ind, xrange(data.shape[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>])] <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># => data[ind[0],0], data[ind[1],1]...</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="19"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="20"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>time_max</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="21"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">82.5</span></span> , <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">20.</span></span> , <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">113.75</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">51.25</span></span>])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="22"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>data_max</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="23"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.98935825</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.84147098</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.99060736</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.6569866</span></span> ])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="24"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="25"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>all(data_max == data.max(axis=<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>))</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="26"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-keyword">True</span></span></code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
你也可以使用数组索引作为目标来赋值:</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a = arange(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a[[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>]] = <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
然而,当一个索引列表包含重复时,赋值被多次完成,保留最后的值:</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a = arange(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a[[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>]]=[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
这足够合理,但是小心如果你想用Python的 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">+=</code> 结构,可能结果并非你所期望:</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a = arange(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a[[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>]]+=<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
即使0在索引列表中出现两次,索引为0的元素仅仅增加一次。这是因为Python要求<code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">a+=1</code> 和 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">a=a+1</code> 等同。</p>
<h3 style="margin:0px 0px 0.5em; font-family:inherit; line-height:1.6em; color:inherit; font-size:18px; text-indent:1em"><a name="t22"></a>
通过布尔数组索引</h3>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
当我们使用整数数组索引数组时,我们提供一个索引列表去选择。通过布尔数组索引的方法是不同的我们显式地选择数组中我们想要和不想要的元素。</p>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
我们能想到的使用布尔数组的索引最自然方式就是使用和原数组一样形状的布尔数组。</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a = arange(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">12</span></span>).reshape(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>b = a > <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>b <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># b is a boolean with a's shape</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[<span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-keyword">False</span></span>, <span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-keyword">False</span></span>, <span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-keyword">False</span></span>, <span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-keyword">False</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-keyword">False</span></span>, <span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-keyword">True</span></span>, <span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-keyword">True</span></span>, <span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-keyword">True</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-keyword">True</span></span>, <span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-keyword">True</span></span>, <span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-keyword">True</span></span>, <span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-keyword">True</span></span>]], dtype=bool)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a[b] <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># 1d array with the selected elements</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">9</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">11</span></span>])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
这个属性在赋值时非常有用:</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a[b] = <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span> <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># All elements of 'a' higher than 4 become 0</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>]])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
你可以参考 <a target="blank" href="http://scipy.org/Tentative_NumPy_Tutorial/Mandelbrot_Set_Example" rel="nofollow" style="color:rgb(148,148,148); text-decoration:none; outline:none 0px; border-bottom-width:1px; border-bottom-style:dashed; border-bottom-color:rgb(148,148,148); font-style:italic; font-weight:bold">曼德博集合示例</a> 看看如何使用布尔索引来生成曼德博集合的图像。</p>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
第二种通过布尔来索引的方法更近似于整数索引;对数组的每个维度我们给一个一维布尔数组来选择我们想要的切片。</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a = arange(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">12</span></span>).reshape(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>b1 = array([<span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-keyword">False</span></span>,<span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-keyword">True</span></span>,<span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-keyword">True</span></span>]) <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># first dim selection</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>b2 = array([<span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-keyword">True</span></span>,<span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-keyword">False</span></span>,<span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-keyword">True</span></span>,<span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-keyword">False</span></span>]) <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># second dim selection</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a[b1,:] <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># selecting rows</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">9</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">11</span></span>]])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a[b1] <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># same thing</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">9</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">11</span></span>]])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a[:,b2] <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># selecting columns</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="14"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="15"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="16"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>]])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="17"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="18"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a[b1,b2] <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># a weird thing to do</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="19"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
注意一维数组的长度必须和你想要切片的维度或轴的长度一致,在之前的例子中,b1是一个秩为1长度为三的数组(a的行数),b2(长度为4)与a的第二秩(列)相一致。 <span style="position:relative; font-size:12px; line-height:0; vertical-align:baseline; top:-0.5em; margin:0px; padding:0px"><a target="blank" href="http://reverland.org/python/2012/08/22/numpy/#fn:8" rel="nofollow" style="color:rgb(148,148,148); text-decoration:none; outline:0px; border-bottom-width:1px; border-bottom-style:dashed; border-bottom-color:rgb(148,148,148); font-style:italic; font-weight:bold; margin:0px; padding:0px">7</a></span></p>
<h3 style="margin:0px 0px 0.5em; font-family:inherit; line-height:1.6em; color:inherit; font-size:18px; text-indent:1em"><a name="t23"></a>
ix()函数</h3>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
<code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">ix</code> 函数可以为了获得 <a target="_blank" href="http://zh.wikipedia.org/wiki/%E5%A4%9A%E5%85%83%E7%BB%84" rel="nofollow" style="color:rgb(148,148,148); text-decoration:none; outline:none 0px; border-bottom-width:1px; border-bottom-style:dashed; border-bottom-color:rgb(148,148,148); font-style:italic; font-weight:bold">多元组</a> 的结果而用来结合不同向量。例如,如果你想要用所有向量a、b和c元素组成的三元组来计算 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">a+bc</code> :</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a = array([<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>b = array([<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>c = array([<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>ax,bx,cx = ix_(a,b,c)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>ax</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>]],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> </code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>]],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> </code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>]],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> </code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>]]])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>bx</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="14"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="15"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="16"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>]]])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="17"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>cx</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="18"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>]]])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="19"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>ax.shape, bx.shape, cx.shape</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="20"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">((<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>), (<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>), (<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>))</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="21"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>result = ax+bxcx</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="22"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>result</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="23"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">42</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">34</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">50</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">66</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">26</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="24"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">27</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">22</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">32</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">42</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">17</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="25"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">22</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">18</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">26</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">34</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">14</span></span>]],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="26"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">43</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">35</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">51</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">67</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">27</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="27"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">28</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">23</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">33</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">43</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">18</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="28"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">23</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">19</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">27</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">35</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">15</span></span>]],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="29"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">44</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">36</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">52</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">68</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">28</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="30"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">29</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">24</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">34</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">44</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">19</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="31"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">24</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">20</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">28</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">36</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">16</span></span>]],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="32"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">45</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">37</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">53</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">69</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">29</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="33"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">30</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">25</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">35</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">45</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">20</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="34"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">25</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">21</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">29</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">37</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">17</span></span>]]])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="35"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>result[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="36"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">17</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="37"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>]+b[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>]c[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="38"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">17</span></span></code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
你也可以实行如下简化:</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="function"><span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-function"><span class="hljs-keyword">def</span></span></span><span class="hljs-function"> </span><span class="title" style="color:rgb(153,0,0); font-weight:bold"><span class="hljs-function"><span class="hljs-title">ufunc_reduce</span></span></span><span class="params"><span class="hljs-function"><span class="hljs-params">(ufct, vectors)</span></span></span><span class="hljs-function">:</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> vs = ix_(vectors)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> r = ufct.identity</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">for</span></span> v <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">in</span></span> vs:</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> r = ufct(r,v)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">return</span></span> r</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
然后这样使用它:</p>
<pre onclick="hljs.copyCode(event)"><code class="language-css hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">ufunc_reduce</span></span>(<span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">add</span></span>,<span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">a</span></span>,<span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">b</span></span>,<span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">c</span></span>)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">array</span></span>(<span class="attr_selector"><span class="hljs-selector-attr">[[[15, 14, 16, 18, 13]</span></span>,</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[12, 11, 13, 15, 10]</span></span>,</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[11, 10, 12, 14, 9]</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[[16, 15, 17, 19, 14]</span></span>,</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[13, 12, 14, 16, 11]</span></span>,</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[12, 11, 13, 15, 10]</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[[17, 16, 18, 20, 15]</span></span>,</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[14, 13, 15, 17, 12]</span></span>,</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[13, 12, 14, 16, 11]</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[[18, 17, 19, 21, 16]</span></span>,</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[15, 14, 16, 18, 13]</span></span>,</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[14, 13, 15, 17, 12]</span></span>]])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
这个reduce与ufunc.reduce(比如说add.reduce)相比的优势在于它利用了广播法则,避免了创建一个输出大小乘以向量个数的参数数组。 <span style="position:relative; font-size:12px; line-height:0; vertical-align:baseline; top:-0.5em; margin:0px; padding:0px"><a target="_blank" href="http://reverland.org/python/2012/08/22/numpy/#fn:9" rel="nofollow" style="color:rgb(148,148,148); text-decoration:none; outline:0px; border-bottom-width:1px; border-bottom-style:dashed; border-bottom-color:rgb(148,148,148); font-style:italic; font-weight:bold; margin:0px; padding:0px">8</a></span></p>
<h3 style="margin:0px 0px 0.5em; font-family:inherit; line-height:1.6em; color:inherit; font-size:18px; text-indent:1em"><a name="t24"></a>
用字符串索引</h3>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
参见 <a target="blank" href="http://www.scipy.org/RecordArrays" rel="nofollow" style="color:rgb(148,148,148); text-decoration:none; outline:none 0px; border-bottom-width:1px; border-bottom-style:dashed; border-bottom-color:rgb(148,148,148); font-style:italic; font-weight:bold">RecordArray</a> 。</p>
<h2 style="margin:0px 0px 0.5em; font-family:inherit; line-height:1.6em; color:inherit; font-size:18px; text-indent:1em"><a name="t25"></a>
线性代数</h2>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
继续前进,基本线性代数包含在这里。</p>
<h3 style="margin:0px 0px 0.5em; font-family:inherit; line-height:1.6em; color:inherit; font-size:18px; text-indent:1em"><a name="t26"></a>
简单数组运算</h3>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
参考numpy文件夹中的linalg.py获得更多信息</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span><span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">from</span></span> numpy <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">import</span></span> *</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span><span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">from</span></span> numpy.linalg <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">import</span></span> </code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> </code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a = array([[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2.0</span></span>], [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4.0</span></span>]])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span><span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">print</span></span> a</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">[[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2.</span></span>]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4.</span></span>]]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> </code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a.transpose()</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4.</span></span>]])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> </code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>inv(a)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="14"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[<span class="hljs-number">-</span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2.</span></span> , <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.</span></span> ],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="15"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.5</span></span>, <span class="hljs-number">-</span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.5</span></span>]])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="16"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> </code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="17"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>u = eye(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>) <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># unit 2x2 matrix; "eye" represents "I"</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="18"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>u</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="19"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="20"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.</span></span>]])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="21"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>j = array([[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.0</span></span>, <span class="hljs-number">-</span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.0</span></span>], [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.0</span></span>]])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="22"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> </code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="23"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>dot (j, j) <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># matrix product</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="24"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[<span class="hljs-number">-</span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="25"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>, <span class="hljs-number">-</span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.</span></span>]])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="26"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> </code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="27"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>trace(u) <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># trace</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="28"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2.0</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="29"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> </code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="30"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>y = array([[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5.</span></span>], [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7.</span></span>]])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="31"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>solve(a, y)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="32"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[<span class="hljs-number">-</span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3.</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="33"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4.</span></span>]])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="34"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> </code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="35"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>eig(j)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="36"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">(array([ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span>+<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.</span></span><span class="hljs-number">j</span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span><span class="hljs-number">-</span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1.</span></span><span class="hljs-number">j</span>]),</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="37"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.70710678</span></span>+<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span><span class="hljs-number">j</span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.70710678</span></span>+<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.</span></span><span class="hljs-number">j</span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="38"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.00000000</span></span><span class="hljs-number">-</span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.70710678</span></span><span class="hljs-number">j</span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.00000000</span></span>+<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.70710678</span></span><span class="hljs-number">j</span>]]))</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="39"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">Parameters:</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="40"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> square matrix</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="41"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> </code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="42"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">Returns</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="43"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> The eigenvalues, each repeated according to its multiplicity.</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="44"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> </code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="45"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> The normalized (unit <span class="string" style="color:rgb(221,17,68)"><span class="hljs-string">"length"</span></span>) eigenvectors, such that the</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="46"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> column v[:,i]
<span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">is</span></span> the eigenvector corresponding to the</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="47"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> eigenvalue w[i]
.</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<h3 style="margin:0px 0px 0.5em; font-family:inherit; line-height:1.6em; color:inherit; font-size:18px; text-indent:1em"><a name="t27"></a>
矩阵类</h3>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
这是一个关于矩阵类的简短介绍。</p>
<pre onclick="hljs.copyCode(event)"><code class="language-java hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> A = matrix(<span class="string" style="color:rgb(221,17,68)"><span class="hljs-string">'1.0 2.0; 3.0 4.0'</span></span>)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> A</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">[[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span>.</span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span>.</span>]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span>.</span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span>.</span>]]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> type(A) # file where <span class="class" style="color:rgb(68,85,136); font-weight:bold"><span class="keyword" style="color:rgb(51,51,51)"><span class="hljs-class"><span class="hljs-keyword">class</span></span></span><span class="hljs-class"> </span><span class="title"><span class="hljs-class"><span class="hljs-title">is</span></span></span><span class="hljs-class"> </span><span class="title"><span class="hljs-class"><span class="hljs-title">defined</span></span></span><span class="hljs-class"></span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> </code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> </code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> <span class="title"><span class="hljs-class"><span class="hljs-title">A</span></span></span><span class="hljs-class">.</span><span class="title"><span class="hljs-class"><span class="hljs-title">T</span></span></span><span class="hljs-class"> # </span><span class="title"><span class="hljs-class"><span class="hljs-title">transpose</span></span></span><span class="hljs-class"><span class="hljs-class"></span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="hljs-class">[[ 1. 3.]</span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="hljs-class"> [ 2. 4.]]</span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="hljs-class"></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="hljs-class">>>> </span><span class="title"><span class="hljs-class"><span class="hljs-title">X</span></span></span><span class="hljs-class"> </span>= <span class="title">matrix</span>(<span class="hljs-string">'5.0 7.0'</span>)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> <span class="title">Y</span> = <span class="title">X</span>.<span class="title">T</span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="14"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> <span class="title">Y</span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="15"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">[[<span class="hljs-number">5</span>.]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="16"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="hljs-number">7</span>.]]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="17"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> </code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="18"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> <span class="title">print</span> <span class="title">A</span><span class="title">Y</span> # <span class="title">matrix</span> <span class="title">multiplication</span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="19"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">[[<span class="hljs-number">19</span>.]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="20"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="hljs-number">43</span>.]]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="21"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> </code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="22"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> <span class="title">print</span> <span class="title">A</span>.<span class="title">I</span> # <span class="title">inverse</span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="23"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">[[-<span class="hljs-number">2</span>. <span class="hljs-number">1</span>. ]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="24"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="hljs-number">1.5</span> -<span class="hljs-number">0.5</span>]]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="25"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> </code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="26"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> <span class="title">solve</span>(<span class="title">A</span>, <span class="title">Y</span>) # <span class="title"><span class="hljs-function">solving</span></span><span class="hljs-function"> </span><span class="title"><span class="hljs-function">linear</span></span><span class="hljs-function"> </span><span class="title"><span class="hljs-function">equation</span></span><span class="hljs-function"></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="27"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="title"><span class="hljs-function"><span class="hljs-title">matrix</span></span></span><span class="hljs-function"><span class="hljs-params"><span class="hljs-params">([[<span class="hljs-number">-3.</span>],</span></span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="28"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="hljs-params"> [ <span class="hljs-number">4.</span>]])</span></code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<h3 style="margin:0px 0px 0.5em; font-family:inherit; line-height:1.6em; color:inherit; font-size:18px; text-indent:1em"><a name="t28"></a>
索引:比较矩阵和二维数组</h3>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
注意NumPy中数组和矩阵有些重要的区别。NumPy提供了两个基本的对象:一个N维数组对象和一个通用函数对象。其它对象都是建构在它们之上 的。特别的,矩阵是继承自NumPy数组对象的二维数组对象。对数组和矩阵,索引都必须包含合适的一个或多个这些组合:整数标量、省略号 (ellipses)、整数列表;布尔值,整数或布尔值构成的元组,和一个一维整数或布尔值数组。矩阵可以被用作矩阵的索引,但是通常需要数组、列表或者 其它形式来完成这个任务。</p>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
像平常在Python中一样,索引是从0开始的。传统上我们用矩形的行和列表示一个二维数组或矩阵,其中沿着0轴的方向被穿过的称作行,沿着1轴的方向被穿过的是列。 <span style="position:relative; font-size:12px; line-height:0; vertical-align:baseline; top:-0.5em; margin:0px; padding:0px"><a target="blank" href="http://reverland.org/python/2012/08/22/numpy/#fn:10" rel="nofollow" style="color:rgb(148,148,148); text-decoration:none; outline:0px; border-bottom-width:1px; border-bottom-style:dashed; border-bottom-color:rgb(148,148,148); font-style:italic; font-weight:bold; margin:0px; padding:0px">9</a> </span></p>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
让我们创建数组和矩阵用来切片:</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>A = arange(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">12</span></span>)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>A</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">9</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">11</span></span>])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>A.shape = (<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>M = mat(A.copy())</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span><span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">print</span></span> type(A),<span class="string" style="color:rgb(221,17,68)"><span class="hljs-string">" "</span></span>,type(M)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> </code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span><span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">print</span></span> A</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">[[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7</span></span>]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">9</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">11</span></span>]]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span><span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">print</span></span> M</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">[[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="14"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7</span></span>]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="15"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">9</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span> <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">11</span></span>]]</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
现在,让我们简单的切几片。基本的切片使用切片对象或整数。例如, <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">A[:]</code> 和 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">M[:]</code> 的求值将表现得和Python索引很相似。然而要注意很重要的一点就是NumPy切片数组 <span style="margin:0px; padding:0px">不</span> 创建数据的副本;切片提供统一数据的视图。</p>
<pre onclick="hljs.copyCode(event)"><code class="language-css hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">print</span></span> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">A</span></span><span class="attr_selector"><span class="hljs-selector-attr">[:]</span></span>; <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">print</span></span> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">A</span></span><span class="attr_selector"><span class="hljs-selector-attr">[:]</span></span><span class="class" style="color:rgb(68,85,136); font-weight:bold"><span class="hljs-selector-class">.shape</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="attr_selector"><span class="hljs-selector-attr">[[ 0 1 2 3]</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[ 4 5 6 7]</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[ 8 9 10 11]</span></span>]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">(3, 4)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">print</span></span> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">M</span></span><span class="attr_selector"><span class="hljs-selector-attr">[:]</span></span>; <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">print</span></span> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">M</span></span><span class="attr_selector"><span class="hljs-selector-attr">[:]</span></span><span class="class" style="color:rgb(68,85,136); font-weight:bold"><span class="hljs-selector-class">.shape</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="attr_selector"><span class="hljs-selector-attr">[[ 0 1 2 3]</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[ 4 5 6 7]</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[ 8 9 10 11]</span></span>]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">(3, 4)</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
现在有些和Python索引不同的了:你可以同时使用逗号分割索引来沿着多个轴索引。</p>
<pre onclick="hljs.copyCode(event)"><code class="language-css hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">print</span></span> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">A</span></span><span class="attr_selector"><span class="hljs-selector-attr">[:,1]</span></span>; <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">print</span></span> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">A</span></span><span class="attr_selector"><span class="hljs-selector-attr">[:,1]</span></span><span class="class" style="color:rgb(68,85,136); font-weight:bold"><span class="hljs-selector-class">.shape</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="attr_selector"><span class="hljs-selector-attr">[1 5 9]</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">(3,)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">print</span></span> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">M</span></span><span class="attr_selector"><span class="hljs-selector-attr">[:,1]</span></span>; <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">print</span></span> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">M</span></span><span class="attr_selector"><span class="hljs-selector-attr">[:,1]</span></span><span class="class" style="color:rgb(68,85,136); font-weight:bold"><span class="hljs-selector-class">.shape</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="attr_selector"><span class="hljs-selector-attr">[[1]</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[5]</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[9]</span></span>]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">(3, 1)</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
注意最后两个结果的不同。对二维数组使用一个冒号产生一个一维数组,然而矩阵产生了一个二维矩阵。 <span style="position:relative; font-size:12px; line-height:0; vertical-align:baseline; top:-0.5em; margin:0px; padding:0px"><a target="blank" href="http://reverland.org/python/2012/08/22/numpy/#fn:11" rel="nofollow" style="color:rgb(148,148,148); text-decoration:none; outline:0px; border-bottom-width:1px; border-bottom-style:dashed; border-bottom-color:rgb(148,148,148); font-style:italic; font-weight:bold; margin:0px; padding:0px">10</a> </span>例如,一个 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">M[2,:]</code> 切片产生了一个形状为(1,4)的矩阵,相比之下,一个数组的切片总是产生一个最低可能维度 <span style="position:relative; font-size:12px; line-height:0; vertical-align:baseline; top:-0.5em; margin:0px; padding:0px"><a target="blank" href="http://reverland.org/python/2012/08/22/numpy/#fn:12" rel="nofollow" style="color:rgb(148,148,148); text-decoration:none; outline:0px; border-bottom-width:1px; border-bottom-style:dashed; border-bottom-color:rgb(148,148,148); font-style:italic; font-weight:bold; margin:0px; padding:0px">11</a> </span>的数组。例如,如果C是一个三维数组, <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">C[...,1]</code> 产生一个二维的数组而 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">C[1,:,1]</code> 产生一个一维数组。从这时开始,如果相应的矩阵切片结果是相同的话,我们将只展示数组切片的结果。</p>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
假如我们想要一个数组的第一列和第三列,一种方法是使用列表切片:</p>
<pre onclick="hljs.copyCode(event)"><code class="language-css hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">A</span></span><span class="attr_selector"><span class="hljs-selector-attr">[:,[1,3]</span></span>]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">array</span></span>(<span class="attr_selector"><span class="hljs-selector-attr">[[ 1, 3]</span></span>,</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[ 5, 7]</span></span>,</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[ 9, 11]</span></span>])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
稍微复杂点的方法是使用 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">take()</code> 方法(method):</p>
<pre onclick="hljs.copyCode(event)"><code class="language-php hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> A[:,].take([<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>],axis=<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">array</span></span>([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">9</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">11</span></span>]])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
如果我们想跳过第一行,我们可以这样:</p>
<pre onclick="hljs.copyCode(event)"><code class="language-php hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> A[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>:,].take([<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>],axis=<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">array</span></span>([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">9</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">11</span></span>]])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
或者我们仅仅使用 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">A[1:,[1,3]]</code> 。还有一种方法是通过矩阵向量积(叉积)。</p>
<pre onclick="hljs.copyCode(event)"><code class="language-css hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">A</span></span><span class="attr_selector"><span class="hljs-selector-attr">[ix((1,2),(1,3))]</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">array</span></span>(<span class="attr_selector"><span class="hljs-selector-attr">[[ 5, 7]</span></span>,</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[ 9, 11]</span></span>])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
为了读者的方便,在次写下之前的矩阵:</p>
<pre onclick="hljs.copyCode(event)"><code class="language-css hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">A</span></span><span class="attr_selector"><span class="hljs-selector-attr">[ix((1,2),(1,3))]</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">array</span></span>(<span class="attr_selector"><span class="hljs-selector-attr">[[ 5, 7]</span></span>,</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[ 9, 11]</span></span>])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
现在让我们做些更复杂的。比如说我们想要保留第一行大于1的列。一种方法是创建布尔索引:</p>
<pre onclick="hljs.copyCode(event)"><code class="language-php hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> A[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>,:]><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">array</span></span>([<span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">False</span></span>, <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">False</span></span>, <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">True</span></span>, <span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">True</span></span>], dtype=bool)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> A[:,A[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>,:]><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">array</span></span>([[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">11</span></span>]])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
就是我们想要的!但是索引矩阵没这么方便。</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>M[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>,:]><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">matrix([[<span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-keyword">False</span></span>, <span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-keyword">False</span></span>, <span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-keyword">True</span></span>, <span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-keyword">True</span></span>]], dtype=bool)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>M[:,M[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>,:]><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">matrix([[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>]])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
这个过程的问题是用“矩阵切片”来切片产生一个矩阵 <span style="position:relative; font-size:12px; line-height:0; vertical-align:baseline; top:-0.5em; margin:0px; padding:0px"><a target="blank" href="http://reverland.org/python/2012/08/22/numpy/#fn:13" rel="nofollow" style="color:rgb(148,148,148); text-decoration:none; outline:0px; border-bottom-width:1px; border-bottom-style:dashed; border-bottom-color:rgb(148,148,148); font-style:italic; font-weight:bold; margin:0px; padding:0px">12</a> </span>,但是矩阵有个方便的 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">A</code> 属性,它的值是数组呈现的。所以我们仅仅做以下替代:</p>
<pre onclick="hljs.copyCode(event)"><code class="language-css hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">M</span></span><span class="attr_selector"><span class="hljs-selector-attr">[:,M.A[0,:]</span></span>>1]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">matrix</span></span>(<span class="attr_selector"><span class="hljs-selector-attr">[[ 2, 3]</span></span>,</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[ 6, 7]</span></span>,</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[10, 11]</span></span>])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
如果我们想要在矩阵两个方向有条件地切片,我们必须稍微调整策略,代之以:</p>
<pre onclick="hljs.copyCode(event)"><code class="language-css hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">A</span></span><span class="attr_selector"><span class="hljs-selector-attr">[A[:,0]</span></span>>2,<span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">A</span></span><span class="attr_selector"><span class="hljs-selector-attr">[0,:]</span></span>>1]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">array</span></span>(<span class="attr_selector"><span class="hljs-selector-attr">[ 6, 11]</span></span>)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">M</span></span><span class="attr_selector"><span class="hljs-selector-attr">[M.A[:,0]</span></span>>2,<span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">M</span></span><span class="class" style="color:rgb(68,85,136); font-weight:bold"><span class="hljs-selector-class">.A</span></span><span class="attr_selector"><span class="hljs-selector-attr">[0,:]</span></span>>1]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">matrix</span></span>(<span class="attr_selector"><span class="hljs-selector-attr">[[ 6, 11]</span></span>])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
我们需要使用向量积 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">ix</code> :</p>
<pre onclick="hljs.copyCode(event)"><code class="language-css hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">A</span></span><span class="attr_selector"><span class="hljs-selector-attr">[ix(A[:,0]</span></span>>2,<span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">A</span></span><span class="attr_selector"><span class="hljs-selector-attr">[0,:]</span></span>>1)]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">array</span></span>(<span class="attr_selector"><span class="hljs-selector-attr">[[ 6, 7]</span></span>,</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[10, 11]</span></span>])</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">>>> <span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">M</span></span><span class="attr_selector"><span class="hljs-selector-attr">[ix(M.A[:,0]</span></span>>2,<span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">M</span></span><span class="class" style="color:rgb(68,85,136); font-weight:bold"><span class="hljs-selector-class">.A</span></span><span class="attr_selector"><span class="hljs-selector-attr">[0,:]</span></span>>1)]</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="tag" style="color:rgb(0,0,128)"><span class="hljs-selector-tag">matrix</span></span>(<span class="attr_selector"><span class="hljs-selector-attr">[[ 6, 7]</span></span>,</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> <span class="attr_selector"><span class="hljs-selector-attr">[10, 11]</span></span>])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<h2 style="margin:0px 0px 0.5em; font-family:inherit; line-height:1.6em; color:inherit; font-size:18px; text-indent:1em"><a name="t29"></a>
技巧和提示</h2>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
下面我们给出简短和有用的提示。</p>
<h3 style="margin:0px 0px 0.5em; font-family:inherit; line-height:1.6em; color:inherit; font-size:18px; text-indent:1em"><a name="t30"></a>
“自动”改变形状</h3>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
更改数组的维度,你可以省略一个尺寸,它将被自动推导出来。</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a = arange(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">30</span></span>)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a.shape = <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>,<span class="hljs-number">-</span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span> <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># -1 means "whatever is needed"</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a.shape</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="prompt" style="color:rgb(153,0,115)"><span class="hljs-meta">>>> </span></span>a</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">array([[[ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">3</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">4</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">5</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">6</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">7</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">8</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [ <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">9</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">11</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">12</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">13</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">14</span></span>]],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">15</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">16</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">17</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">18</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">19</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">20</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">21</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">22</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">23</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="14"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">24</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">25</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">26</span></span>],</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="15"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"> [<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">27</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">28</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">29</span></span>]]])</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<h3 style="margin:0px 0px 0.5em; font-family:inherit; line-height:1.6em; color:inherit; font-size:18px; text-indent:1em"><a name="t31"></a>
向量组合(stacking)</h3>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
我们如何用两个相同尺寸的行向量列表构建一个二维数组?在MATLAB中这非常简单:如果x和y是两个相同长度的向量,你仅仅需要做 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">m=[x;y]</code> 。在NumPy中这个过程通过函数 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">column_stack</code> 、 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">dstack</code> 、 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">hstack</code> 和 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">vstack</code> 来完成,取决于你想要在那个维度上组合。例如:</p>
<pre class="prettyprint bash" style="padding:0.3em; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; margin-top:0px; margin-bottom:1.5em; font-size:12px; line-height:1.5em; word-break:break-all; word-wrap:break-word; white-space:pre-wrap; overflow-y:auto; background-color:rgb(246,246,246)" name="code" onclick="hljs.copyCode(event)"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent" class="hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">x = arange(0,10,2) <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># x=([0,2,4,6,8])</span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">y = arange(5) <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># y=([0,1,2,3,4])</span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">m = vstack([x,y]) <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># m=([[0,2,4,6,8],</span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># [0,1,2,3,4]])</span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">xy = hstack([x,y]) <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># xy =([0,2,4,6,8,0,1,2,3,4])</span></span></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
二维以上这些函数背后的逻辑会很奇怪。</p>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
参考 <a target="_blank" href="http://scipy.org/NumPy_for_Matlab_Users" rel="nofollow" style="color:rgb(148,148,148); text-decoration:none; outline:none 0px; border-bottom-width:1px; border-bottom-style:dashed; border-bottom-color:rgb(148,148,148); font-style:italic; font-weight:bold">写个Matlab用户的NumPy指南</a> 并且在这里添加你的新发现:
)</p>
<h3 style="margin:0px 0px 0.5em; font-family:inherit; line-height:1.6em; color:inherit; font-size:18px; text-indent:1em"><a name="t32"></a>
直方图(histogram)</h3>
<p style="margin-top:0px; margin-bottom:0.75em; line-height:1.7em; text-indent:1em">
NumPy中 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">histogram</code> 函数应用到一个数组返回一对变量:直方图数组和箱式向量。注意: <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">matplotlib</code> 也有一个用来建立直方图的函数(叫作 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">hist</code> ,正如matlab中一样)与NumPy中的不同。主要的差别是 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">pylab.hist</code> 自动绘制直方图,而 <code class="prettyprint" style="padding:2px 4px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; border:none; background-color:rgb(247,247,249)">numpy.histogram</code> 仅仅产生数据。</p>
<pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">import</span></span> numpy</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="keyword" style="color:rgb(51,51,51); font-weight:bold"><span class="hljs-keyword">import</span></span> pylab</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># Build a vector of 10000 normal deviates with variance 0.5^2 and mean 2</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">mu, sigma = <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">2</span></span>, <span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">0.5</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">v = numpy.random.normal(mu,sigma,<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">10000</span></span>)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># Plot a normalized histogram with 50 bins</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">pylab.hist(v, bins=<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">50</span></span>, normed=<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>) <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># matplotlib version (plot)</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">pylab.show()</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent"><span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># Compute the histogram with numpy and then plot it</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">(n, bins) = numpy.histogram(v, bins=<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">50</span></span>, normed=<span class="built_in" style="color:rgb(0,134,179)"><span class="hljs-keyword">True</span></span>) <span class="comment" style="color:rgb(153,153,136); font-style:italic"><span class="hljs-comment"># NumPy version (no plot)</span></span></code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">pylab.plot(<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">.5</span></span>*(bins[<span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>:]+bins[:<span class="hljs-number">-</span><span class="number" style="color:rgb(0,153,153)"><span class="hljs-number">1</span></span>]), n)</code></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><code style="padding:0px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,'monospace !important'; color:rgb(64,170,83); border:none; display:block; overflow-y:auto; margin:0px; word-break:break-all; background-color:transparent">pylab.show()</code></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
</div>
</div>
</div>
</div>
网友评论