Numpy 入门教程(1)

来源:互联网 发布:矢量图网站 知乎 编辑:程序博客网 时间:2024/05/18 10:20

翻译自官方文档Tentative NumPy Tutorial,有删节。

Numpy 入门教程

NumPy 提供了对多维数组的支持,与Python原生支持的List类型不同,数组的所有元素必须同样的类型。数组的维度被称为axes,维数称为 rank。 

Numpy的数组类型为 ndarray, ndarray 的重要属性包括

  • ndarray.ndim:数组的维数,也称为rank
  • ndarray.shape:数组各维的大小tuple 类型,对一个列的矩阵来说, shape 为 (n,m)
  • ndarray.size:元素的总数。 
  • Ndarray.dtype:每个元素的类型,可以是 numpy.int32, numpy.int16, and numpy.float64 等。 
  • Ndarray.itemsize:每个元素占用的字节数。
  • Ndarray.data:指向数据内存。

 

一个简单的例子:

[python] view plaincopy在CODE上查看代码片派生到我的代码片
  1. >>> from numpy  import *  
  2. >>> a = arange(15).reshape(35)  
  3. >>> a  
  4. array([[ 0,  1,  2,  3,  4],  
  5.        [ 5,  6,  7,  8,  9],  
  6.        [1011121314]])  
  7. >>> a.shape  
  8. (35)  
  9. >>> a.ndim  
  10. 2  
  11. >>> a.dtype.name  
  12. 'int32'  
  13. >>> a.itemsize  
  14. 4  
  15. >>> a.size  
  16. 15  
  17. >>> type(a)  
  18. numpy.ndarray  
  19. >>> b = array([678])  
  20. >>> b  
  21. array([678])  
  22. >>> type(b)  
  23. numpy.ndarray  


生成数组

有许多种方法生成数组。比如,可以将Python list 或 tuple 转化为数组,转化后的数组元素的类型由原来的对象的类型来决定。

[python] view plaincopy在CODE上查看代码片派生到我的代码片
  1. >>> from numpy  import *  
  2. >>> a = array( [2,3,4] )  
  3. >>> a  
  4. array([234])  
  5. >>> a.dtype  
  6. dtype('int32')  
  7. >>> b = array([1.23.55.1])  
  8. >>> b.dtype  
  9. dtype('float64')  
  10. >>> b = array( [ (1.5,2,3), (4,5,6) ] )  
  11. >>> b  
  12. array([[ 1.5,  2. ,  3. ],  
  13.        [ 4. ,  5. ,  6. ]])  

生成数组时也可以指定元素的数据类型

[python] view plaincopy在CODE上查看代码片派生到我的代码片
  1. >>> c = array( [ [1,2], [3,4] ], dtype=complex )  
  2. >>> c  
  3. array([[ 1.+0.j,  2.+0.j],  
  4.        [ 3.+0.j,  4.+0.j]])  

通常,我们无法事先知道数组元素的具体值,但是数组大小是已知的。 这时可以用下面几种方法生成数组。 

zeros 函数生成元素全部为0的数组,ones函数生成元素全部为1的数组empty函数生成元素没有赋值的数组,这时元素值由内存中原来的内容决定。 默认的,生成的数组的元素类型为float64.

[python] view plaincopy在CODE上查看代码片派生到我的代码片
  1. >>> zeros( (3,4) )  
  2. array([[0.,  0.,  0.,  0.],  
  3.        [0.,  0.,  0.,  0.],  
  4.        [0.,  0.,  0.,  0.]])  
  5. >>> ones( (2,3,4), dtype=int16 )                # dtype can also be specified  
  6. array([[[ 1111],  
  7.         [ 1111],  
  8.         [ 1111]],  
  9.        [[ 1111],  
  10.         [ 1111],  
  11.         [ 1111]]], dtype=int16)  
  12. >>> empty( (2,3) )  
  13. array([[  3.73603959e-262,   6.02658058e-154,   6.55490914e-260],  
  14.        [  5.30498948e-313,   3.14673309e-307,   1.00000000e+000]])  

arange 函数生成的数组的元素按照等比数列排布,类似于 range函数。

[python] view plaincopy在CODE上查看代码片派生到我的代码片
  1. >>> arange( 10305 )  
  2. array([10152025])  
  3. >>> arange( 020.3 )                 # it accepts float arguments  
  4. array([ 0. ,  0.3,  0.6,  0.9,  1.2,  1.5,  1.8])  

linspace 函数有些类似matlab中的同名函数,下面是个例子

[python] view plaincopy在CODE上查看代码片派生到我的代码片
  1. >>> linspace( 029 )                 # 9 numbers from 0 to 2  
  2. array([ 0.  ,  0.25,  0.5 ,  0.75,  1.  ,  1.25,  1.5 ,  1.75,  2.  ])  
  3. >>> x = linspace( 02*pi, 100 )        # useful to evaluate function at lots of points  
  4. >>> f = sin(x)  

屏幕输出 Arrays

当用print 打印一个 array输出结果类似于 lists: 

[python] view plaincopy在CODE上查看代码片派生到我的代码片
  1. >>> a = arange(6)                         # 1d array  
  2. >>> print a  
  3. [0 1 2 3 4 5]  
  4. >>>  
  5. >>> b = arange(12).reshape(4,3)           # 2d array  
  6. >>> print b  
  7. [[ 0  1  2]  
  8.  [ 3  4  5]  
  9.  [ 6  7  8]  
  10.  [ 9 10 11]]  
  11. >>>  
  12. >>> c = arange(24).reshape(2,3,4)         # 3d array  
  13. >>> print c  
  14. [[[ 0  1  2  3]  
  15.   [ 4  5  6  7]  
  16.   [ 8  9 10 11]]  
  17.   
  18.  [[12 13 14 15]  
  19.   [16 17 18 19]  
  20.   [20 21 22 23]]]  

如果数组过大,显示时会有一些省略号

[python] view plaincopy在CODE上查看代码片派生到我的代码片
  1. >>> print arange(10000)  
  2. [   0    1    2 ..., 9997 9998 9999]  
  3. >>>  
  4. >>> print arange(10000).reshape(100,100)  
  5. [[   0    1    2 ...,   97   98   99]  
  6.  [ 100  101  102 ...,  197  198  199]  
  7.  [ 200  201  202 ...,  297  298  299]  
  8.  ...,  
  9.  [9700 9701 9702 ..., 9797 9798 9799]  
  10.  [9800 9801 9802 ..., 9897 9898 9899]  
  11.  [9900 9901 9902 ..., 9997 9998 9999]]  

如果我们需要显示完整的数组,可以如下设置 

[python] view plaincopy在CODE上查看代码片派生到我的代码片
  1. >>> set_printoptions(threshold='nan')  



0 0