|
11 | 11 | "NumPy arrays therefore **must** be the same datatype (float, int etc).\n",
|
12 | 12 | "\n",
|
13 | 13 | "The flow of this notebook is as follows:\n",
|
14 |
| - "1. Creating an array\n", |
15 |
| - "2. Creating zeros, ones, linspace...\n", |
| 14 | + "1. Supported types\n", |
| 15 | + "1. Creating an array from scratch\n", |
16 | 16 | "3. Generating random numbers\n",
|
17 | 17 | "4. Inspecting the array\n",
|
18 | 18 | "5. Arithmetic operations\n",
|
19 | 19 | "6. Aggregation\n",
|
20 | 20 | "7. Subsetting, slicing, indexing\n",
|
21 | 21 | "\n",
|
| 22 | + "## Supported types\n", |
| 23 | + "\n", |
| 24 | + "NumPy arrays contain values of a *single type*, so it's important to know which types are available to use, in addition to the fact that NumPy is built in C. These can usually be specified in NumPy array-creating functions using the `dtype` parameter:\n", |
| 25 | + "\n", |
| 26 | + "| **Data Type** | **Description** |\n", |
| 27 | + "| ---------- | ----------------------------- |\n", |
| 28 | + "| `bool_` | Boolean (`True` or `False` stored as a byte |\n", |
| 29 | + "| `int_` | Default integer type (same as C `long`) |\n", |
| 30 | + "| `intc` | Identical to C `int` |\n", |
| 31 | + "| `intp` | Integer for indexing |\n", |
| 32 | + "| `int8` | A byte |\n", |
| 33 | + "| `int16` | Integer (-32768 to 32767) |\n", |
| 34 | + "| `int32` | Integer (-2147483648 to 2147483647) |\n", |
| 35 | + "| `int64` | Integer (-9223372036854775808 to 9223372036854775807) |\n", |
| 36 | + "| `uint8` | Unsigned Integer (0 to 255) |\n", |
| 37 | + "| `uint16` | Unsigned Integer (0 to 65535) |\n", |
| 38 | + "| `uint32` | Unsigned Integer (0 to 4294967295) |\n", |
| 39 | + "| `uint64` | Unsigned Integer (0 to 18446744073709551615) |\n", |
| 40 | + "| `float_` | Same as `float64` |\n", |
| 41 | + "| `float16` | Half precision float (1-bit sign, 5-bit exponent, 10-bit mantissa) |\n", |
| 42 | + "| `float32` | Single precision float (1-bit sign, 8-bit exponent, 23-bit mantissa) |\n", |
| 43 | + "| `float64` | Double precision float (1-bit sign, 11-bit exponent, 52-bit mantissa) |\n", |
| 44 | + "|`complex_` | Same as `complex128` |\n", |
| 45 | + "| `complex64` | Complex number, as two 32-bit floats |\n", |
| 46 | + "| `complex128` | Complex number, represented by two 64-bit floats |\n", |
22 | 47 | "\n",
|
23 | 48 | "We use the following convention **np** for numpy import:"
|
24 | 49 | ]
|
|
36 | 61 | "cell_type": "markdown",
|
37 | 62 | "metadata": {},
|
38 | 63 | "source": [
|
39 |
| - "## Creating an array\n", |
| 64 | + "## Creating arrays from scratch\n", |
40 | 65 | "\n",
|
41 | 66 | "We can initialize numpy arrays from nested Python lists, and access elements using square brackets:"
|
42 | 67 | ]
|
|
61 | 86 | "outputs": [],
|
62 | 87 | "source": [
|
63 | 88 | "# 2-d ints\n",
|
64 |
| - "b = np.array([[3.0, 2.0],[1.0, 2.0]], dtype=int)\n", |
| 89 | + "b = np.array([[3.0, 2.0],[1.0, 2.0]], dtype=np.int_)\n", |
65 | 90 | "b"
|
66 | 91 | ]
|
67 | 92 | },
|
|
87 | 112 | "cell_type": "markdown",
|
88 | 113 | "metadata": {},
|
89 | 114 | "source": [
|
90 |
| - "## Creating zeros, ones, linspace, identity matrix...\n", |
91 |
| - "\n", |
92 | 115 | "Numpy also provides many functions to create arrays from the same value or not, for example `zeros()` creates an array full of zeros, given a specific size (or tuple of dimensions!), and `linspace()` creates an incrementally-ordered vector of numbers between two given values, and given a size."
|
93 | 116 | ]
|
94 | 117 | },
|
|
170 | 193 | "g"
|
171 | 194 | ]
|
172 | 195 | },
|
| 196 | + { |
| 197 | + "cell_type": "markdown", |
| 198 | + "metadata": {}, |
| 199 | + "source": [ |
| 200 | + "An even faster form of allocation, if you're going to fill the array/matrix yourself, is to allocate an *empty array*, meaning the values are uninitialized:" |
| 201 | + ] |
| 202 | + }, |
| 203 | + { |
| 204 | + "cell_type": "code", |
| 205 | + "execution_count": null, |
| 206 | + "metadata": {}, |
| 207 | + "outputs": [], |
| 208 | + "source": [ |
| 209 | + "c2 = np.empty((10,), dtype=np.float64)\n", |
| 210 | + "c2" |
| 211 | + ] |
| 212 | + }, |
173 | 213 | {
|
174 | 214 | "cell_type": "markdown",
|
175 | 215 | "metadata": {},
|
|
227 | 267 | "cell_type": "markdown",
|
228 | 268 | "metadata": {},
|
229 | 269 | "source": [
|
230 |
| - "## Data Types" |
| 270 | + "## Array attributes\n", |
| 271 | + "\n", |
| 272 | + "The three primary attributes a NumPy array has are:\n", |
| 273 | + "\n", |
| 274 | + "1. Number of dimensions (`ndim`)\n", |
| 275 | + "2. Array shape (`shape`)\n", |
| 276 | + "3. Total size of the array (`size`)\n", |
| 277 | + "\n", |
| 278 | + "If we begin by allocating a 1-D, 2-D and 3-D array:" |
| 279 | + ] |
| 280 | + }, |
| 281 | + { |
| 282 | + "cell_type": "code", |
| 283 | + "execution_count": null, |
| 284 | + "metadata": {}, |
| 285 | + "outputs": [], |
| 286 | + "source": [ |
| 287 | + "# for reproducibility\n", |
| 288 | + "np.random.seed(0)\n", |
| 289 | + "array1d = np.random.randint(10, size=6)\n", |
| 290 | + "array2d = np.random.randint(10, size=(3, 4))\n", |
| 291 | + "array3d = np.random.randint(10, size=(3,4,5))" |
231 | 292 | ]
|
232 | 293 | },
|
233 | 294 | {
|
|
236 | 297 | "metadata": {},
|
237 | 298 | "outputs": [],
|
238 | 299 | "source": [
|
239 |
| - "print(np.int64)\n", |
240 |
| - "print(np.float64)\n", |
241 |
| - "print(np.bool)\n", |
242 |
| - "print(np.string_)" |
| 300 | + "def print_array(arr):\n", |
| 301 | + " print(\"array ndim: %d\" % arr.ndim)\n", |
| 302 | + " print(\"array shape: {}\".format(arr.shape))\n", |
| 303 | + " print(\"array size: %d\" % arr.size)" |
243 | 304 | ]
|
244 | 305 | },
|
245 | 306 | {
|
246 |
| - "cell_type": "markdown", |
| 307 | + "cell_type": "code", |
| 308 | + "execution_count": null, |
247 | 309 | "metadata": {},
|
| 310 | + "outputs": [], |
248 | 311 | "source": [
|
249 |
| - "## Array shape\n", |
250 |
| - "\n", |
251 |
| - "Returns a *tuple* whereby the first number represents the **number of rows** (FORTRAN-style memory-mapping!), the second number represents the **number of columns** and so on into higher dimensions." |
| 312 | + "print_array(array1d)" |
252 | 313 | ]
|
253 | 314 | },
|
254 | 315 | {
|
|
257 | 318 | "metadata": {},
|
258 | 319 | "outputs": [],
|
259 | 320 | "source": [
|
260 |
| - "print(a.shape)\n", |
261 |
| - "print(j.shape)\n", |
262 |
| - "print(c.shape)" |
| 321 | + "print_array(array2d)" |
263 | 322 | ]
|
264 | 323 | },
|
265 | 324 | {
|
|
268 | 327 | "metadata": {},
|
269 | 328 | "outputs": [],
|
270 | 329 | "source": [
|
271 |
| - "# the number of dimensions!\n", |
272 |
| - "b.ndim" |
| 330 | + "print_array(array3d)" |
273 | 331 | ]
|
274 | 332 | },
|
275 | 333 | {
|
|
278 | 336 | "metadata": {},
|
279 | 337 | "outputs": [],
|
280 | 338 | "source": [
|
281 |
| - "print(a.dtype)\n", |
282 |
| - "print(b.dtype)" |
| 339 | + "print(array3d.dtype)" |
283 | 340 | ]
|
284 | 341 | },
|
285 | 342 | {
|
|
1200 | 1257 | },
|
1201 | 1258 | {
|
1202 | 1259 | "cell_type": "markdown",
|
1203 |
| - "metadata": { |
1204 |
| - "collapsed": true |
1205 |
| - }, |
| 1260 | + "metadata": {}, |
1206 | 1261 | "source": [
|
1207 | 1262 | "### Task 3.\n",
|
1208 | 1263 | "\n",
|
|
1235 | 1290 | "name": "python",
|
1236 | 1291 | "nbconvert_exporter": "python",
|
1237 | 1292 | "pygments_lexer": "ipython3",
|
1238 |
| - "version": "3.6.6" |
| 1293 | + "version": "3.6.7" |
1239 | 1294 | }
|
1240 | 1295 | },
|
1241 | 1296 | "nbformat": 4,
|
|
0 commit comments