1.3.1.1 什么是Numpy以及Numpy数组?

1.3.1.1.1 Numpy数组

Python对象:

  • 高级数值对象:整数、浮点
  • 容器:列表(无成本插入和附加),字典(快速查找)

Numpy提供:

  • 对于多维度数组的Python扩展包
  • 更贴近硬件(高效)
  • 为科学计算设计(方便)
  • 也称为面向数组计算

In [1]:

  1. import numpy as np
  2. a = np.array([0, 1, 2, 3])
  3. a

Out[1]:

  1. array([0, 1, 2, 3])

例如,数组包含:

  • 实验或模拟在离散时间阶段的值
  • 测量设备记录的信号,比如声波
  • 图像的像素、灰度或颜色
  • 用不同X-Y-Z位置测量的3-D数据,例如MRI扫描

为什么有用:提供了高速数值操作的节省内存的容器。

In [2]:

  1. L = range(1000)
  2. %timeit [i**2 for i in L]
  1. 10000 loops, best of 3: 93.7 µs per loop

In [4]:

  1. a = np.arange(1000)
  2. %timeit a**2
  1. 100000 loops, best of 3: 2.16 µs per loop

1.3.1.1.2 Numpy参考文档

  1. np.array?
  2. String Form:<built-in function array>
  3. Docstring:
  4. array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0, ...

查找东西:

In [6]:

  1. np.lookfor('create array')
  1. Search results for 'create array'
  2.  ------------- --------------------
  3. numpy.array
  4. Create an array.
  5. numpy.memmap
  6. Create a memory-map to an array stored in a *binary* file on disk.
  7. numpy.diagflat
  8. Create a two-dimensional array with the flattened input as a diagonal.
  9. numpy.fromiter
  10. Create a new 1-dimensional array from an iterable object.
  11. numpy.partition
  12. Return a partitioned copy of an array.
  13. numpy.ma.diagflat
  14. Create a two-dimensional array with the flattened input as a diagonal.
  15. numpy.ctypeslib.as_array
  16. Create a numpy array from a ctypes array or a ctypes POINTER.
  17. numpy.ma.make_mask
  18. Create a boolean mask from an array.
  19. numpy.ctypeslib.as_ctypes
  20. Create and return a ctypes object from a numpy array. Actually
  21. numpy.ma.mrecords.fromarrays
  22. Creates a mrecarray from a (flat) list of masked arrays.
  23. numpy.lib.format.open_memmap
  24. Open a .npy file as a memory-mapped array.
  25. numpy.ma.MaskedArray.__new__
  26. Create a new masked array from scratch.
  27. numpy.lib.arrayterator.Arrayterator
  28. Buffered iterator for big arrays.
  29. numpy.ma.mrecords.fromtextfile
  30. Creates a mrecarray from data stored in the file `filename`.
  31. numpy.asarray
  32. Convert the input to an array.
  33. numpy.ndarray
  34. ndarray(shape, dtype=float, buffer=None, offset=0,
  35. numpy.recarray
  36. Construct an ndarray that allows field access using attributes.
  37. numpy.chararray
  38. chararray(shape, itemsize=1, unicode=False, buffer=None, offset=0,
  39. numpy.pad
  40. Pads an array.
  41. numpy.sum
  42. Sum of array elements over a given axis.
  43. numpy.asanyarray
  44. Convert the input to an ndarray, but pass ndarray subclasses through.
  45. numpy.copy
  46. Return an array copy of the given object.
  47. numpy.diag
  48. Extract a diagonal or construct a diagonal array.
  49. numpy.load
  50. Load arrays or pickled objects from ``.npy``, ``.npz`` or pickled files.
  51. numpy.sort
  52. Return a sorted copy of an array.
  53. numpy.array_equiv
  54. Returns True if input arrays are shape consistent and all elements equal.
  55. numpy.dtype
  56. Create a data type object.
  57. numpy.choose
  58. Construct an array from an index array and a set of arrays to choose from.
  59. numpy.nditer
  60. Efficient multi-dimensional iterator object to iterate over arrays.
  61. numpy.swapaxes
  62. Interchange two axes of an array.
  63. numpy.full_like
  64. Return a full array with the same shape and type as a given array.
  65. numpy.ones_like
  66. Return an array of ones with the same shape and type as a given array.
  67. numpy.empty_like
  68. Return a new array with the same shape and type as a given array.
  69. numpy.zeros_like
  70. Return an array of zeros with the same shape and type as a given array.
  71. numpy.asarray_chkfinite
  72. Convert the input to an array, checking for NaNs or Infs.
  73. numpy.diag_indices
  74. Return the indices to access the main diagonal of an array.
  75. numpy.ma.choose
  76. Use an index array to construct a new array from a set of choices.
  77. numpy.chararray.tolist
  78. a.tolist()
  79. numpy.matlib.rand
  80. Return a matrix of random values with given shape.
  81. numpy.savez_compressed
  82. Save several arrays into a single file in compressed ``.npz`` format.
  83. numpy.ma.empty_like
  84. Return a new array with the same shape and type as a given array.
  85. numpy.ma.make_mask_none
  86. Return a boolean mask of the given shape, filled with False.
  87. numpy.ma.mrecords.fromrecords
  88. Creates a MaskedRecords from a list of records.
  89. numpy.around
  90. Evenly round to the given number of decimals.
  91. numpy.source
  92. Print or write to a file the source code for a Numpy object.
  93. numpy.diagonal
  94. Return specified diagonals.
  95. numpy.histogram2d
  96. Compute the bi-dimensional histogram of two data samples.
  97. numpy.fft.ifft
  98. Compute the one-dimensional inverse discrete Fourier Transform.
  99. numpy.fft.ifftn
  100. Compute the N-dimensional inverse discrete Fourier Transform.
  101. numpy.busdaycalendar
  102. A business day calendar object that efficiently stores information
  1. np.con*?
  2. np.concatenate
  3. np.conj
  4. np.conjugate
  5. np.convolve

1.3.1.1.3 导入惯例

导入numpy的推荐惯例是:

In [8]:

  1. import numpy as np