Skip to content

Commit 6e4be05

Browse files
committed
add array-joining
1 parent 8e836d4 commit 6e4be05

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

Python/Module3_IntroducingNumpy/AccessingDataAlongMultipleDimensions.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
" [[4, 5],\n",
4343
" [6, 7]]])\n",
4444
"\n",
45-
"# sheet-0, both rows, flip order of columns\n",
45+
"# get: sheet-0, both rows, flip order of columns\n",
4646
">>> x[0, :, ::-1]\n",
4747
"array([[1, 0],\n",
4848
" [3, 2]])\n",

Python/Module3_IntroducingNumpy/FunctionsForCreatingNumpyArrays.ipynb

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,35 @@
55
"metadata": {},
66
"source": [
77
"# 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",
937
"\n",
1038
"## Creating Arrays from Python Sequences\n",
1139
"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,7 +48,7 @@
2048
"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",
2149
" \n",
2250
"```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",
2452
">>> np.array([[1., 2., 3.], [4., 5., 6.]]) # shape: (2, 3)\n",
2553
"array([[ 1., 2., 3.],\n",
2654
" [ 4., 5., 6.]])\n",
@@ -46,7 +74,7 @@
4674
"\n",
4775
"**Warning!** \n",
4876
"\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",
5078
"\n",
5179
"</div>"
5280
]
@@ -163,6 +191,30 @@
163191
"\n",
164192
"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."
165193
]
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+
]
166218
}
167219
],
168220
"metadata": {

0 commit comments

Comments
 (0)