creation.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. """
  2. ==============
  3. Array Creation
  4. ==============
  5. Introduction
  6. ============
  7. There are 5 general mechanisms for creating arrays:
  8. 1) Conversion from other Python structures (e.g., lists, tuples)
  9. 2) Intrinsic numpy array creation objects (e.g., arange, ones, zeros,
  10. etc.)
  11. 3) Reading arrays from disk, either from standard or custom formats
  12. 4) Creating arrays from raw bytes through the use of strings or buffers
  13. 5) Use of special library functions (e.g., random)
  14. This section will not cover means of replicating, joining, or otherwise
  15. expanding or mutating existing arrays. Nor will it cover creating object
  16. arrays or structured arrays. Both of those are covered in their own sections.
  17. Converting Python array_like Objects to NumPy Arrays
  18. ====================================================
  19. In general, numerical data arranged in an array-like structure in Python can
  20. be converted to arrays through the use of the array() function. The most
  21. obvious examples are lists and tuples. See the documentation for array() for
  22. details for its use. Some objects may support the array-protocol and allow
  23. conversion to arrays this way. A simple way to find out if the object can be
  24. converted to a numpy array using array() is simply to try it interactively and
  25. see if it works! (The Python Way).
  26. Examples: ::
  27. >>> x = np.array([2,3,1,0])
  28. >>> x = np.array([2, 3, 1, 0])
  29. >>> x = np.array([[1,2.0],[0,0],(1+1j,3.)]) # note mix of tuple and lists,
  30. and types
  31. >>> x = np.array([[ 1.+0.j, 2.+0.j], [ 0.+0.j, 0.+0.j], [ 1.+1.j, 3.+0.j]])
  32. Intrinsic NumPy Array Creation
  33. ==============================
  34. NumPy has built-in functions for creating arrays from scratch:
  35. zeros(shape) will create an array filled with 0 values with the specified
  36. shape. The default dtype is float64. ::
  37. >>> np.zeros((2, 3))
  38. array([[ 0., 0., 0.], [ 0., 0., 0.]])
  39. ones(shape) will create an array filled with 1 values. It is identical to
  40. zeros in all other respects.
  41. arange() will create arrays with regularly incrementing values. Check the
  42. docstring for complete information on the various ways it can be used. A few
  43. examples will be given here: ::
  44. >>> np.arange(10)
  45. array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
  46. >>> np.arange(2, 10, dtype=float)
  47. array([ 2., 3., 4., 5., 6., 7., 8., 9.])
  48. >>> np.arange(2, 3, 0.1)
  49. array([ 2. , 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9])
  50. Note that there are some subtleties regarding the last usage that the user
  51. should be aware of that are described in the arange docstring.
  52. linspace() will create arrays with a specified number of elements, and
  53. spaced equally between the specified beginning and end values. For
  54. example: ::
  55. >>> np.linspace(1., 4., 6)
  56. array([ 1. , 1.6, 2.2, 2.8, 3.4, 4. ])
  57. The advantage of this creation function is that one can guarantee the
  58. number of elements and the starting and end point, which arange()
  59. generally will not do for arbitrary start, stop, and step values.
  60. indices() will create a set of arrays (stacked as a one-higher dimensioned
  61. array), one per dimension with each representing variation in that dimension.
  62. An example illustrates much better than a verbal description: ::
  63. >>> np.indices((3,3))
  64. array([[[0, 0, 0], [1, 1, 1], [2, 2, 2]], [[0, 1, 2], [0, 1, 2], [0, 1, 2]]])
  65. This is particularly useful for evaluating functions of multiple dimensions on
  66. a regular grid.
  67. Reading Arrays From Disk
  68. ========================
  69. This is presumably the most common case of large array creation. The details,
  70. of course, depend greatly on the format of data on disk and so this section
  71. can only give general pointers on how to handle various formats.
  72. Standard Binary Formats
  73. -----------------------
  74. Various fields have standard formats for array data. The following lists the
  75. ones with known python libraries to read them and return numpy arrays (there
  76. may be others for which it is possible to read and convert to numpy arrays so
  77. check the last section as well)
  78. ::
  79. HDF5: h5py
  80. FITS: Astropy
  81. Examples of formats that cannot be read directly but for which it is not hard to
  82. convert are those formats supported by libraries like PIL (able to read and
  83. write many image formats such as jpg, png, etc).
  84. Common ASCII Formats
  85. ------------------------
  86. Comma Separated Value files (CSV) are widely used (and an export and import
  87. option for programs like Excel). There are a number of ways of reading these
  88. files in Python. There are CSV functions in Python and functions in pylab
  89. (part of matplotlib).
  90. More generic ascii files can be read using the io package in scipy.
  91. Custom Binary Formats
  92. ---------------------
  93. There are a variety of approaches one can use. If the file has a relatively
  94. simple format then one can write a simple I/O library and use the numpy
  95. fromfile() function and .tofile() method to read and write numpy arrays
  96. directly (mind your byteorder though!) If a good C or C++ library exists that
  97. read the data, one can wrap that library with a variety of techniques though
  98. that certainly is much more work and requires significantly more advanced
  99. knowledge to interface with C or C++.
  100. Use of Special Libraries
  101. ------------------------
  102. There are libraries that can be used to generate arrays for special purposes
  103. and it isn't possible to enumerate all of them. The most common uses are use
  104. of the many array generation functions in random that can generate arrays of
  105. random values, and some utility functions to generate special matrices (e.g.
  106. diagonal).
  107. """