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