目前我的工作是将NumPy引入到Pyston中(一款Dropbox实现的Python编译器/解释器)。在工作过程中,我深入接触了NumPy源码,了解其实现并提交了PR修复NumPy的bug。在与NumPy源码以及NumPy开发者打交道的过程中,我发现当今中文NumPy教程大部分都是翻译或参考英文文档,因此导致了许多疏漏。比如NumPy数组中的broadcast功能,几乎所有中文文档都翻译为“广播”。而NumPy的开发者之一,回复到“broadcast is a compound -- native English speakers can see that it's " broad" + "cast" = "cast (scatter, distribute) broadly, I guess "cast (scatter, distribute) broadly" probably is closer to the meaning(NumPy中的含义)"。有鉴于此,我打算启动一个项目,以我对NumPy使用以及源码层面的了解编写一个系列的教程。
地址随后会更新。CSDN的排版(列表)怎么显示不正常了。。。
NumPy数组
NumPy数组是一个多维数组对象,称为ndarray。其由两部分组成:
- 实际的数据
- 描述这些数据的元数据
大部分操作仅针对于元数据,而不改变底层实际的数据。
关于NumPy数组有几点必需了解的:
- NumPy数组的下标从0开始。
- 同一个NumPy数组中所有元素的类型必须是相同的。
NumPy数组属性
在详细介绍NumPy数组之前。先详细介绍下NumPy数组的基本属性。NumPy数组的维数称为秩(rank),一维数组的秩为1,二维数组的秩为2,以此类推。在NumPy中,每一个线性的数组称为是一个轴(axes),秩其实是描述轴的数量。比如说,二维数组相当于是两个一维数组,其中第一个一维数组中每个元素又是一个一维数组。所以一维数组就是NumPy中的轴(axes),第一个轴相当于是底层数组,第二个轴是底层数组里的数组。而轴的数量——秩,就是数组的维数。
NumPy的数组中比较重要ndarray对象属性有:
-
ndarray.ndim:数组的维数(即数组轴的个数),等于秩。最常见的为二维数组(矩阵)。
-
ndarray.shape:数组的维度。为一个表示数组在每个维度上大小的整数元组。例如二维数组中,表示数组的“行数”和“列数”。ndarray.shape返回一个元组,这个元组的长度就是维度的数目,即ndim属性。
-
ndarray.size:数组元素的总个数,等于shape属性中元组元素的乘积。
-
ndarray.dtype:表示数组中元素类型的对象,可使用标准的Python类型创建或指定dtype。另外也可使用前一篇文章中介绍的NumPy提供的数据类型。
-
ndarray.itemsize:数组中每个元素的字节大小。例如,一个元素类型为float64的数组itemsiz属性值为8(float64占用64个bits,每个字节长度为8,所以64/8,占用8个字节),又如,一个元素类型为complex32的数组item属性为4(32/8)。
- ndarray.data:包含实际数组元素的缓冲区,由于一般通过数组的索引获取元素,所以通常不需要使用这个属性。
创建数组
<span style="font-size: 18px;"> <span style="font-family: SimSun; font-size: 14px;"><span lang="zh-CN">先来介绍</span>创建数组。<span lang="zh-CN">创建数组的方法有很多。</span>如可以使用<span lang="en-US">array</span>函数从常规的<span lang="en-US">Python</span>列表和元组创造数组。所创建的数组类型由原序列中的元素类型推导而来。</span><span style="font-family: SimSun;"> </span></span><div class="dp-highlighter bg_python" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; width: 700.912px; overflow: auto; padding-top: 1px; margin: 18px 0px !important; background-color: rgb(231, 229, 220);"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-stretch: normal; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(108, 226, 108); background-color: rgb(248, 248, 248);"><strong>[python]</strong> <a target=_blank href="http://blog.csdn.net/sunny2038/article/details/9002531#" class="ViewSource" title="view plain" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url("images/default/ico_plain.gif"); background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0% 0%; background-repeat: no-repeat;">view plain</a><span data-mod="popu_168"> <a target=_blank href="http://blog.csdn.net/sunny2038/article/details/9002531#" class="CopyToClipboard" title="copy" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url("images/default/ico_copy.gif"); background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0% 0%; background-repeat: no-repeat;">copy</a></span><div style="position: absolute; left: 653px; top: 1912px; width: 18px; height: 18px; z-index: 99;"></div><span data-mod="popu_169"> </span></div></div><ol start="1" class="dp-py" style="padding: 0px; border: none; color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important; background-color: rgb(255, 255, 255);"><li class="alt" style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">>>> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">from</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> numpy </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">import</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> * </span></span></li><li style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> </span></li><li class="alt" style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">>>> a = array( [<span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">2</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">,</span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">3</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">,</span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">4</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">] ) </span></span></li><li style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">>>> a </span></li><li class="alt" style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> array([<span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">2</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">, </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">3</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">, </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">4</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">]) </span></span></li><li style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">>>> a.dtype </span></li><li class="alt" style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> dtype(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">'int32'</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">) </span></span></li><li style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">>>> b = array([<span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">1.2</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">, </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">3.5</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">, </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">5.1</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">]) </span></span></li><li class="alt" style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">>>> b.dtype </span></li><li style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> dtype(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">'float64'</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">) </span></span></li></ol></div>使用array函数创建时,参数必须是由方括号括起来的列表,而不能使用多个数值作为参数调用array。
- >>> a = array(1,2,3,4) # 错误
- >>> a = array([1,2,3,4]) # 正确
- >>> b = array( [ (1.5,2,3), (4,5,6) ] )
- >>> b
- array([[ 1.5, 2. , 3. ],
- [ 4. , 5. , 6. ]])
- >>> c = array( [ [1,2], [3,4] ], dtype=complex)
- >>> c
- array([[ 1.+0.j, 2.+0.j],
- [ 3.+0.j, 4.+0.j]])
用函数zeros可创建一个全是0的数组,用函数ones可创建一个全为1的数组,函数empty创建一个内容随机并且依赖与内存状态的数组。默认创建的数组类型(dtype)都是float64。
可以哟娜特d.dtype.itemsize来查看数组中元素占用的字节数目。
- >>> d = zeros((3,4))
- >>> d.dtype
- dtype('float64')
- >>> d
- array([[ 0., 0., 0., 0.],
- [ 0., 0., 0., 0.],
- [ 0., 0., 0., 0.]])
- >>> d.dtype.itemsize
- 8
- >>> ones( (2,3,4), dtype=int16 ) #手动指定数组中元素类型
- array([[[1, 1, 1, 1],
- [1, 1, 1, 1],
- [1, 1, 1, 1]],
- [[1, 1, 1, 1],
- [1, 1, 1, 1],
- [1, 1, 1, 1]]], dtype=int16)
- >>> empty((2,3))
- array([[ 2.65565858e-316, 0.00000000e+000, 0.00000000e+000],
- [ 0.00000000e+000, 0.00000000e+000, 0.00000000e+000]])
- >>> arange(10, 30, 5)
- array([10, 15, 20, 25])
- >>> arange(0,2,0.5)
- array([ 0. , 0.5, 1. , 1.5])
当arange使用浮点数参数时,由于浮点数精度有限,通常无法预测获得的元素个数。因此,最好使用函数linspace去接收我们想要的元素个数来代替用range来指定步长。linespace用法如下,将在通用函数一节中详细介绍。
- >>> numpy.linspace(-1, 0, 5)
- array([-1. , -0.75, -0.5 , -0.25, 0. ])
知识点:NumPy中的数据类型
对于科学计算来说,Python中自带的整型、浮点型和复数类型远远不够,因此NumPy中添加了许多数据类型。如下:
名称 | 描述 |
bool | 用一个字节存储的布尔类型(True或False) |
inti | 由所在平台决定其大小的整数(一般为int32或int64) |
int8 | 一个字节大小,-128 至 127 |
int16 | 整数,-32768 至 32767 |
int32 | 整数,-2 ** 31 至 2 ** 32 -1 |
int64 | 整数,-2 ** 63 至 2 ** 63 - 1 |
uint8 | 无符号整数,0 至 255 |
uint16 | 无符号整数,0 至 65535 |
uint32 | 无符号整数,0 至 2 ** 32 - 1 |
uint64 | 无符号整数,0 至 2 ** 64 - 1 |
float16 | 半精度浮点数:16位,正负号1位,指数5位,精度10位 |
float32 | 单精度浮点数:32位,正负号1位,指数8位,精度23位 |
float64或float | 双精度浮点数:64位,正负号1位,指数11位,精度52位 |
complex64 | 复数,分别用两个32位浮点数表示实部和虚部 |
complex128或complex | 复数,分别用两个64位浮点数表示实部和虚部 |
NumPy类型转换方式如下:
- >>> float64(42)
- 42.0
- >>> int8(42.0)
- 42
- >>> bool(42)
- True
- >>> bool(42.0)
- True
- >>> float(True)
- 1.0
- >>> arange(7, dtype=uint16)
- array([0, 1, 2, 3, 4, 5, 6], dtype=uint16)
输出数组
<span style="font-size: 18px;"> <span style="font-size: 14px;">当输出一个数组时,<span style="font-family: 'Courier New', monospace;"><span lang="en-US">NumPy</span></span>以特定的布局用类似嵌套列表的形式显示: </span></span>
- 第一行从左到右输出
- 每行依次自上而下输出
- 每个切片通过一个空行与下一个隔开
- 一维数组被打印成行,二维数组成矩阵,三维数组成矩阵列表。
<span style="font-size: 18px;"> </span><div class="dp-highlighter bg_python" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; width: 700.912px; overflow: auto; padding-top: 1px; margin: 18px 0px !important; background-color: rgb(231, 229, 220);"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-stretch: normal; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(108, 226, 108); background-color: rgb(248, 248, 248);"><strong>[python]</strong> <a target=_blank href="http://blog.csdn.net/sunny2038/article/details/9002531#" class="ViewSource" title="view plain" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url("images/default/ico_plain.gif"); background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0% 0%; background-repeat: no-repeat;">view plain</a><span data-mod="popu_168"> <a target=_blank href="http://blog.csdn.net/sunny2038/article/details/9002531#" class="CopyToClipboard" title="copy" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url("images/default/ico_copy.gif"); background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0% 0%; background-repeat: no-repeat;">copy</a></span><div style="position: absolute; left: 653px; top: 5163px; width: 18px; height: 18px; z-index: 99;"></div><span data-mod="popu_169"> </span></div></div><ol start="1" class="dp-py" style="padding: 0px; border: none; color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important; background-color: rgb(255, 255, 255);"><li class="alt" style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">>>> a = arange(</span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">6</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">) </span><span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;"># 1d array</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">>>> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">print</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> a </span></span></li><li class="alt" style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> [<span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">0</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">1</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">2</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">3</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">4</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">5</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">] </span></span></li><li style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> </span></li><li class="alt" style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">>>> b = arange(<span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">12</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">).reshape(</span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">4</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">,</span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">3</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">) </span><span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;"># 2d array</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">>>> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">print</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> b </span></span></li><li class="alt" style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> [[ <span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">0</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">1</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">2</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">] </span></span></li><li style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> [ <span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">3</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">4</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">5</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">] </span></span></li><li class="alt" style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> [ <span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">6</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">7</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">8</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">] </span></span></li><li style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> [ <span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">9</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">10</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">11</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">]] </span></span></li><li class="alt" style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">>>> c = arange(<span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">24</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">).reshape(</span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">2</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">,</span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">3</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">,</span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">4</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">) </span><span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;"># 3d array</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">>>> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">print</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> c </span></span></li><li class="alt" style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> [[[ <span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">0</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">1</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">2</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">3</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">] </span></span></li><li style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> [ <span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">4</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">5</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">6</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">7</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">] </span></span></li><li class="alt" style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> [ <span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">8</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">9</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">10</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">11</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">]] </span></span></li><li style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> </span></li><li class="alt" style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> [[<span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">12</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">13</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">14</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">15</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">] </span></span></li><li style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> [<span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">16</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">17</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">18</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">19</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">] </span></span></li><li class="alt" style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> [<span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">20</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">21</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">22</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">23</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">]]] </span></span></li></ol></div><span style="font-size: 18px;"><span style="font-size: 14px;"><span style="font-family: SimSun;"> <span lang="en-US">reshape</span>将在下一篇文章中介绍 如果一个数组太长,则<span lang="en-US">NumPy</span>自动省略中间部分而只打印两端的数据: </span> </span></span><div class="dp-highlighter bg_python" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; width: 700.912px; overflow: auto; padding-top: 1px; margin: 18px 0px !important; background-color: rgb(231, 229, 220);"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-stretch: normal; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(108, 226, 108); background-color: rgb(248, 248, 248);"><strong>[python]</strong> <a target=_blank href="http://blog.csdn.net/sunny2038/article/details/9002531#" class="ViewSource" title="view plain" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url("images/default/ico_plain.gif"); background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0% 0%; background-repeat: no-repeat;">view plain</a><span data-mod="popu_168"> <a target=_blank href="http://blog.csdn.net/sunny2038/article/details/9002531#" class="CopyToClipboard" title="copy" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url("images/default/ico_copy.gif"); background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0% 0%; background-repeat: no-repeat;">copy</a></span><div style="position: absolute; left: 653px; top: 5618px; width: 18px; height: 18px; z-index: 99;"></div><span data-mod="popu_169"> </span></div></div><ol start="1" class="dp-py" style="padding: 0px; border: none; color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important; background-color: rgb(255, 255, 255);"><li class="alt" style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">>>> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">print</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> arange(</span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">10000</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">) </span></span></li><li style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> [ <span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">0</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">1</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">2</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> ..., </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">9997</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">9998</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">9999</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">] </span></span></li><li class="alt" style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> </span></li><li style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">>>> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">print</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> arange(</span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">10000</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">).reshape(</span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">100</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">,</span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">100</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">) </span></span></li><li class="alt" style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> [[ <span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">0</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">1</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">2</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> ..., </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">97</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">98</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">99</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">] </span></span></li><li style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> [ <span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">100</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">101</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">102</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> ..., </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">197</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">198</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">199</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">] </span></span></li><li class="alt" style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> [ <span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">200</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">201</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">202</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> ..., </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">297</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">298</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">299</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">] </span></span></li><li style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> ..., </span></li><li class="alt" style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> [<span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">9700</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">9701</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">9702</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> ..., </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">9797</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">9798</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">9799</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">] </span></span></li><li style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> [<span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">9800</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">9801</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">9802</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> ..., </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">9897</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">9898</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">9899</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">] </span></span></li><li class="alt" style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> [<span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">9900</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">9901</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">9902</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> ..., </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">9997</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">9998</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="number" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">9999</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">]] </span></span></li></ol></div><span style="font-size: 18px;"> <span style="font-size: 14px;"> 可通过</span></span><span style="font-size: 18px;"><span style="font-size: 14px;">设置<span style="font-family: 'Courier New', monospace;"><span lang="en-US">printoptions</span></span>参数来</span></span><span style="font-size: 18px;"><span style="font-size: 14px;">禁用<span style="font-family: 'Courier New', monospace;"><span lang="en-US">NumPy</span></span>的这种行为并强制打印整个数组。</span> </span><pre class="cjk" name="code" style="white-space: pre-wrap; word-wrap: break-word;"><div class="dp-highlighter bg_python" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; width: 700.912px; overflow: auto; padding-top: 1px; margin: 18px 0px !important; background-color: rgb(231, 229, 220);"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-stretch: normal; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(108, 226, 108); background-color: rgb(248, 248, 248);"><strong>[python]</strong> <a target=_blank href="http://blog.csdn.net/sunny2038/article/details/9002531#" class="ViewSource" title="view plain" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url("images/default/ico_plain.gif"); background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0% 0%; background-repeat: no-repeat;">view plain</a><span data-mod="popu_168"> <a target=_blank href="http://blog.csdn.net/sunny2038/article/details/9002531#" class="CopyToClipboard" title="copy" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url("images/default/ico_copy.gif"); background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0% 0%; background-repeat: no-repeat;">copy</a></span><div style="position: absolute; left: 653px; top: 5933px; width: 18px; height: 18px; z-index: 99;"></div><span data-mod="popu_169"> </span></div></div><ol start="1" class="dp-py" style="padding: 0px; border: none; color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important; background-color: rgb(255, 255, 255);"><li class="alt" style="border-left-width: 3px; border-style: none none none solid; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">set_printoptions(threshold=</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">'nan'</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">) </span></span></li></ol></div><span style="font-size: 18px;"><span id="_xhe_cursor"></span><span style="font-family: 'Courier New', monospace;"><span lang="en-US"></span></span></span>
这样,输出时数组的所有元素都会显示出来。
未完待续,如有错误,敬请指正!
参考文献:
《NumPy for Beginner》
《Python科学计算》
《Tentative NumPy Tutorial》