|
5 | 5 | "metadata": {}, |
6 | 6 | "source": [ |
7 | 7 | "# Functions for Creating NumPy Arrays \n", |
8 | | - "This section presents standard methods for creating NumPy arrays of varying shapes and contents. NumPy provides a laundry list of functions for creating arrays\n", |
| 8 | + "This section presents standard methods for creating NumPy arrays of varying shapes and contents. NumPy provides a laundry list of functions for creating arrays:\n", |
| 9 | + "\n", |
| 10 | + "```python\n", |
| 11 | + ">>> import numpy as np\n", |
| 12 | + "\n", |
| 13 | + "# creating an array from a Python-sequence\n", |
| 14 | + ">>> np.array([i**2 for i in range(5)])\n", |
| 15 | + "array([ 0, 1, 4, 9, 16])\n", |
| 16 | + "\n", |
| 17 | + "# creating an array filled with ones\n", |
| 18 | + ">>> np.ones((2, 4))\n", |
| 19 | + "array([[ 1., 1., 1., 1.],\n", |
| 20 | + " [ 1., 1., 1., 1.]])\n", |
| 21 | + "\n", |
| 22 | + "# creating an array of evenly-spaced points\n", |
| 23 | + ">>> np.linspace(0, 10, 5)\n", |
| 24 | + "array([ 0. , 2.5, 5. , 7.5, 10. ])\n", |
| 25 | + "\n", |
| 26 | + "# creating an array by sampling 10 numbers \n", |
| 27 | + "# randomly from a mean-1, std-dev-5 normal\n", |
| 28 | + "# distribution\n", |
| 29 | + ">>> np.random.normal(1, 5, 10)\n", |
| 30 | + "array([ 2.549537 , 2.75144951, 0.60031823, 3.75185732, 4.65543858,\n", |
| 31 | + " 0.55779525, 1.15574987, -1.98461337, 5.39771083, -7.81395192])\n", |
| 32 | + "\n", |
| 33 | + "# creating an array of a specified datatype\n", |
| 34 | + ">>> np.array([1.5, 3.20, 5.78], dtype=int)\n", |
| 35 | + "array([1, 3, 5])\n", |
| 36 | + "```\n", |
9 | 37 | "\n", |
10 | 38 | "## Creating Arrays from Python Sequences\n", |
11 | 39 | "You can create an array from a Python `list` or `tuple` by using NumPy's `array` function. NumPy will interpret the structure of the data it receives to determine the dimensionality and shape of the array. For example, a single list of numbers will be used to create a 1-dimensional array: \n", |
|
20 | 48 | "Nested lists/tuples will be used to constructing multidimensional arrays. For example, a \"list of equal-length lists of numbers\" will lead to a 2-dimensional array; each of the inner-lists comprises a row of the array. Thus a list of two, length-three lists will produce a (2,3)-shaped array:\n", |
21 | 49 | " \n", |
22 | 50 | "```python\n", |
23 | | - "# lists of lists of numbers will produce a 2D-array\n", |
| 51 | + "# a list of lists of numbers will produce a 2D-array\n", |
24 | 52 | ">>> np.array([[1., 2., 3.], [4., 5., 6.]]) # shape: (2, 3)\n", |
25 | 53 | "array([[ 1., 2., 3.],\n", |
26 | 54 | " [ 4., 5., 6.]])\n", |
|
46 | 74 | "\n", |
47 | 75 | "**Warning!** \n", |
48 | 76 | "\n", |
49 | | - "You actually *can* create an array from lists of *unequal* lengths. The resulting array is **not** an ND-array as it has no well-defined dimensionality. Instead, something called an *object-array* is produced, which does not benefit from the majoriyy of NumPy's features. This is a relatively obscure feature of the NumPy library, and should be avoided unless you really know what you're doing!\n", |
| 77 | + "You actually *can* create an array from lists of *unequal* lengths. The resulting array is **not** an ND-array as it has no well-defined dimensionality. Instead, something called an *object-array* is produced, which does not benefit from the majority of NumPy's features. This is a relatively obscure feature of the NumPy library, and should be avoided unless you really know what you're doing!\n", |
50 | 78 | "\n", |
51 | 79 | "</div>" |
52 | 80 | ] |
|
163 | 191 | "\n", |
164 | 192 | "Refer to [the official NumPy documentation](https://docs.scipy.org/doc/numpy/user/basics.types.html#array-types-and-conversions-between-types) for the complete list of available array datatypes." |
165 | 193 | ] |
| 194 | + }, |
| 195 | + { |
| 196 | + "cell_type": "markdown", |
| 197 | + "metadata": {}, |
| 198 | + "source": [ |
| 199 | + "## Joining Arrays Together\n", |
| 200 | + "Arrays can be concatenated together similar to Python lists and tuples, however because NumPy's arrays can be multi-dimensional, we can choose the dimension along which arrays are joined. \n", |
| 201 | + "```python\n", |
| 202 | + "# demonstrating methods for joining arrays \n", |
| 203 | + ">>> x = np.array([1, 2, 3])\n", |
| 204 | + ">>> y = np.array([-1, -2, -3])\n", |
| 205 | + "\n", |
| 206 | + "# stack `x` and `y` \"vertically\"\n", |
| 207 | + ">>> np.vstack([x, y])\n", |
| 208 | + "array([[ 1, 2, 3],\n", |
| 209 | + " [-1, -2, -3]])\n", |
| 210 | + "\n", |
| 211 | + "# stack `x` and `y` \"horizontally\"\n", |
| 212 | + ">>> np.vstack([x, y])\n", |
| 213 | + "array([ 1, 2, 3, -1, -2, -3])\n", |
| 214 | + "```\n", |
| 215 | + "\n", |
| 216 | + "A complete listing of functions for joining arrays can be [found in the official NumPy documentation](https://docs.scipy.org/doc/numpy-1.14.0/reference/routines.array-manipulation.html#joining-arrays). There are corresponding functions for splitting an array into independent arrays." |
| 217 | + ] |
166 | 218 | } |
167 | 219 | ], |
168 | 220 | "metadata": { |
|
0 commit comments