├── NumPy Tutorial └── NumPy_Tutorial.ipynb └── README.md /NumPy Tutorial/NumPy_Tutorial.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "6997c1dc", 6 | "metadata": {}, 7 | "source": [ 8 | "
\n", 9 | "" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "id": "0fde7ebf", 15 | "metadata": {}, 16 | "source": [ 17 | "# NumPy Tutorial" 18 | ] 19 | }, 20 | { 21 | "cell_type": "markdown", 22 | "id": "71792b0e", 23 | "metadata": {}, 24 | "source": [ 25 | "NumPy is a Python library.\n", 26 | "\n", 27 | "NumPy is used for working with arrays.\n", 28 | "\n", 29 | "NumPy is short for \"Numerical Python\"." 30 | ] 31 | }, 32 | { 33 | "cell_type": "markdown", 34 | "id": "29e736cd", 35 | "metadata": {}, 36 | "source": [ 37 | "# Learning by Examples" 38 | ] 39 | }, 40 | { 41 | "cell_type": "markdown", 42 | "id": "28263703", 43 | "metadata": {}, 44 | "source": [ 45 | "You can work in any Python environment, you can use the NumPy module, and modify the code to see the result.\n", 46 | "\n", 47 | "## Example\n", 48 | "Create a NumPy array:" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 1, 54 | "id": "96ef6d2c", 55 | "metadata": {}, 56 | "outputs": [ 57 | { 58 | "name": "stdout", 59 | "output_type": "stream", 60 | "text": [ 61 | "[1 2 3 4 5]\n", 62 | "\n" 63 | ] 64 | } 65 | ], 66 | "source": [ 67 | "import numpy as np\n", 68 | "\n", 69 | "arr = np.array([1, 2, 3, 4, 5])\n", 70 | "\n", 71 | "print(arr)\n", 72 | "\n", 73 | "print(type(arr))" 74 | ] 75 | }, 76 | { 77 | "cell_type": "markdown", 78 | "id": "26328ec9", 79 | "metadata": {}, 80 | "source": [ 81 | "# NumPy Introduction" 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "id": "94f7de77", 87 | "metadata": {}, 88 | "source": [ 89 | "# What is NumPy?\n", 90 | "NumPy is a Python library used for working with arrays.\n", 91 | "\n", 92 | "It also has functions for working in domain of linear algebra, fourier transform, and matrices.\n", 93 | "\n", 94 | "NumPy was created in 2005 by Travis Oliphant. It is an open source project and you can use it freely.\n", 95 | "\n", 96 | "NumPy stands for Numerical Python.\n", 97 | "\n" 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "id": "36479aff", 103 | "metadata": {}, 104 | "source": [ 105 | "# Why Use NumPy?\n", 106 | "\n", 107 | "In Python we have lists that serve the purpose of arrays, but they are slow to process.\n", 108 | "\n", 109 | "NumPy aims to provide an array object that is up to 50x faster than traditional Python lists.\n", 110 | "\n", 111 | "The array object in NumPy is called ndarray, it provides a lot of supporting functions that make working with ndarray very easy.\n", 112 | "\n", 113 | "Arrays are very frequently used in data science, where speed and resources are very important." 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "id": "29e4ef69", 119 | "metadata": {}, 120 | "source": [ 121 | "> Data Science: is a branch of computer science where we study how to store, use and analyze data for deriving information from it." 122 | ] 123 | }, 124 | { 125 | "cell_type": "markdown", 126 | "id": "34bf2c43", 127 | "metadata": {}, 128 | "source": [ 129 | "# Why is NumPy Faster Than Lists?\n", 130 | "NumPy arrays are stored at one continuous place in memory unlike lists, so processes can access and manipulate them very efficiently.\n", 131 | "\n", 132 | "This behavior is called locality of reference in computer science.\n", 133 | "\n", 134 | "This is the main reason why NumPy is faster than lists. Also it is optimized to work with latest CPU architectures.\n" 135 | ] 136 | }, 137 | { 138 | "cell_type": "markdown", 139 | "id": "5ad81525", 140 | "metadata": {}, 141 | "source": [ 142 | "# Which Language is NumPy written in?\n", 143 | "NumPy is a Python library and is written partially in Python, but most of the parts that require fast computation are written in C or C++." 144 | ] 145 | }, 146 | { 147 | "cell_type": "markdown", 148 | "id": "923bfb49", 149 | "metadata": {}, 150 | "source": [ 151 | "# Where is the NumPy Codebase?\n", 152 | "\n", 153 | "The source code for NumPy is located at this github repository https://github.com/numpy/numpy\n", 154 | "\n", 155 | "> github: enables many people to work on the same codebase.\n" 156 | ] 157 | }, 158 | { 159 | "cell_type": "markdown", 160 | "id": "d2ac842c", 161 | "metadata": {}, 162 | "source": [ 163 | "# NumPy Getting Started\n", 164 | "\n", 165 | "### Installation of NumPy\n", 166 | "\n", 167 | "If you have Python and PIP already installed on a system, then installation of NumPy is very easy.\n", 168 | "\n", 169 | "Install it using this command:\n", 170 | "> C:\\Users\\Your Name> pip install numpy \n" 171 | ] 172 | }, 173 | { 174 | "cell_type": "markdown", 175 | "id": "11304082", 176 | "metadata": {}, 177 | "source": [ 178 | "If this command fails, then use a python distribution that already has NumPy installed like, \n", 179 | "[Anaconda](https://www.anaconda.com/), [Spyder](https://www.spyder-ide.org/) etc..\n" 180 | ] 181 | }, 182 | { 183 | "cell_type": "markdown", 184 | "id": "edd65ee0", 185 | "metadata": {}, 186 | "source": [ 187 | "# Import NumPy\n", 188 | "Once NumPy is installed, import it in your applications by adding the import keyword:\n" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": 2, 194 | "id": "b7bce53b", 195 | "metadata": {}, 196 | "outputs": [], 197 | "source": [ 198 | "import numpy" 199 | ] 200 | }, 201 | { 202 | "cell_type": "markdown", 203 | "id": "ee39ace4", 204 | "metadata": {}, 205 | "source": [ 206 | "Now NumPy is imported and ready to use." 207 | ] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "execution_count": 3, 212 | "id": "93470cab", 213 | "metadata": {}, 214 | "outputs": [ 215 | { 216 | "name": "stdout", 217 | "output_type": "stream", 218 | "text": [ 219 | "[1 2 3 4 5]\n" 220 | ] 221 | } 222 | ], 223 | "source": [ 224 | "import numpy\n", 225 | "\n", 226 | "arr = numpy.array([1, 2, 3, 4, 5])\n", 227 | "\n", 228 | "print(arr)\n" 229 | ] 230 | }, 231 | { 232 | "cell_type": "markdown", 233 | "id": "0b3c17a3", 234 | "metadata": {}, 235 | "source": [ 236 | "NumPy as np\n", 237 | "NumPy is usually imported under the `np` alias.\n", 238 | "\n", 239 | "> * alias: In Python alias are an alternate name for referring to the same thing." 240 | ] 241 | }, 242 | { 243 | "cell_type": "markdown", 244 | "id": "19cc29fc", 245 | "metadata": {}, 246 | "source": [ 247 | "Create an alias with the as keyword while importing:\n", 248 | "\n" 249 | ] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "execution_count": 4, 254 | "id": "1b1a59b9", 255 | "metadata": {}, 256 | "outputs": [], 257 | "source": [ 258 | "import numpy as np" 259 | ] 260 | }, 261 | { 262 | "cell_type": "markdown", 263 | "id": "66b99c20", 264 | "metadata": {}, 265 | "source": [ 266 | "Now the NumPy package can be referred to as np instead of numpy.\n", 267 | "> * Note: In this lecture, it is sufficient to call the library in the project and we will call it in the project via `np`\n", 268 | "\n", 269 | "### Example\n" 270 | ] 271 | }, 272 | { 273 | "cell_type": "code", 274 | "execution_count": 5, 275 | "id": "e1c86154", 276 | "metadata": {}, 277 | "outputs": [ 278 | { 279 | "name": "stdout", 280 | "output_type": "stream", 281 | "text": [ 282 | "[1 2 3 4 5]\n" 283 | ] 284 | } 285 | ], 286 | "source": [ 287 | "arr = np.array([1, 2, 3, 4, 5])\n", 288 | "\n", 289 | "print(arr)" 290 | ] 291 | }, 292 | { 293 | "cell_type": "markdown", 294 | "id": "4cee432e", 295 | "metadata": {}, 296 | "source": [ 297 | "## Checking NumPy Version\n", 298 | "The version string is stored under **__version__** attribute.\n", 299 | "## Example" 300 | ] 301 | }, 302 | { 303 | "cell_type": "code", 304 | "execution_count": 6, 305 | "id": "78036333", 306 | "metadata": {}, 307 | "outputs": [ 308 | { 309 | "name": "stdout", 310 | "output_type": "stream", 311 | "text": [ 312 | "1.21.5\n" 313 | ] 314 | } 315 | ], 316 | "source": [ 317 | "print(np.__version__)" 318 | ] 319 | }, 320 | { 321 | "cell_type": "markdown", 322 | "id": "2b5e9cdc", 323 | "metadata": {}, 324 | "source": [ 325 | "# NumPy Creating Arrays\n", 326 | "\n", 327 | "NumPy is used to work with arrays. The array object in NumPy is called `ndarray`.\n", 328 | "\n", 329 | "We can create a NumPy `ndarray` object by using the `array()` function.\n", 330 | "## Example" 331 | ] 332 | }, 333 | { 334 | "cell_type": "code", 335 | "execution_count": 7, 336 | "id": "b6ab1ed6", 337 | "metadata": {}, 338 | "outputs": [ 339 | { 340 | "name": "stdout", 341 | "output_type": "stream", 342 | "text": [ 343 | "[1 2 3 4 5]\n", 344 | "\n" 345 | ] 346 | } 347 | ], 348 | "source": [ 349 | "arr = np.array([1, 2, 3, 4, 5])\n", 350 | "\n", 351 | "print(arr)\n", 352 | "\n", 353 | "print(type(arr))" 354 | ] 355 | }, 356 | { 357 | "cell_type": "markdown", 358 | "id": "2e863ae8", 359 | "metadata": {}, 360 | "source": [ 361 | "> * type(): This built-in Python function tells us the type of the object passed to it. Like in above code it shows that `arr` is `numpy.ndarray` type." 362 | ] 363 | }, 364 | { 365 | "cell_type": "markdown", 366 | "id": "8e3dc2bc", 367 | "metadata": {}, 368 | "source": [ 369 | "To create an `ndarray`, we can pass a list, tuple or any array-like object into the `array()` method, and it will be converted into an `ndarray`:\n", 370 | "\n", 371 | "## Example\n", 372 | "Use a tuple to create a NumPy array:" 373 | ] 374 | }, 375 | { 376 | "cell_type": "code", 377 | "execution_count": 8, 378 | "id": "77fdcf5b", 379 | "metadata": {}, 380 | "outputs": [ 381 | { 382 | "name": "stdout", 383 | "output_type": "stream", 384 | "text": [ 385 | "[1 2 3 4 5]\n" 386 | ] 387 | } 388 | ], 389 | "source": [ 390 | "arr = np.array((1, 2, 3, 4, 5))\n", 391 | "\n", 392 | "print(arr)" 393 | ] 394 | }, 395 | { 396 | "cell_type": "markdown", 397 | "id": "cfbb12a2", 398 | "metadata": {}, 399 | "source": [ 400 | "# Dimensions in Arrays\n", 401 | "A dimension in arrays is one level of array depth (nested arrays).\n", 402 | "\n", 403 | "> * nested array: are arrays that have arrays as their elements." 404 | ] 405 | }, 406 | { 407 | "cell_type": "markdown", 408 | "id": "667b4705", 409 | "metadata": {}, 410 | "source": [ 411 | "# 0-D Arrays\n", 412 | "0-D arrays, or Scalars, are the elements in an array. Each value in an array is a 0-D array.\n", 413 | "\n", 414 | "## Example\n", 415 | "Create a 0-D array with value 42" 416 | ] 417 | }, 418 | { 419 | "cell_type": "code", 420 | "execution_count": 9, 421 | "id": "27b3f904", 422 | "metadata": {}, 423 | "outputs": [ 424 | { 425 | "name": "stdout", 426 | "output_type": "stream", 427 | "text": [ 428 | "42\n" 429 | ] 430 | } 431 | ], 432 | "source": [ 433 | "arr = np.array(42)\n", 434 | "\n", 435 | "print(arr)" 436 | ] 437 | }, 438 | { 439 | "cell_type": "markdown", 440 | "id": "12e16068", 441 | "metadata": {}, 442 | "source": [ 443 | "# 1-D Arrays\n", 444 | "An array that has 0-D arrays as its elements is called uni-dimensional or 1-D array.\n", 445 | "\n", 446 | "These are the most common and basic arrays.\n", 447 | "\n", 448 | "## Example\n", 449 | "Create a 1-D array containing the values 1,2,3,4,5:" 450 | ] 451 | }, 452 | { 453 | "cell_type": "code", 454 | "execution_count": 10, 455 | "id": "64473b26", 456 | "metadata": {}, 457 | "outputs": [ 458 | { 459 | "name": "stdout", 460 | "output_type": "stream", 461 | "text": [ 462 | "[1 2 3 4 5]\n" 463 | ] 464 | } 465 | ], 466 | "source": [ 467 | "arr = np.array([1, 2, 3, 4, 5])\n", 468 | "\n", 469 | "print(arr)" 470 | ] 471 | }, 472 | { 473 | "cell_type": "markdown", 474 | "id": "01869ae1", 475 | "metadata": {}, 476 | "source": [ 477 | "# 2-D Arrays\n", 478 | "An array that has 1-D arrays as its elements is called a 2-D array.\n", 479 | "\n", 480 | "These are often used to represent matrix or 2nd order tensors.\n", 481 | "\n", 482 | "> * NumPy has a whole sub module dedicated towards matrix operations called `numpy.mat`" 483 | ] 484 | }, 485 | { 486 | "cell_type": "markdown", 487 | "id": "0eb4c2c9", 488 | "metadata": {}, 489 | "source": [ 490 | "## Example\n", 491 | "Create a 2-D array containing two arrays with the values 1,2,3 and 4,5,6:" 492 | ] 493 | }, 494 | { 495 | "cell_type": "code", 496 | "execution_count": 11, 497 | "id": "fab88829", 498 | "metadata": {}, 499 | "outputs": [ 500 | { 501 | "name": "stdout", 502 | "output_type": "stream", 503 | "text": [ 504 | "[[1 2 3]\n", 505 | " [4 5 6]]\n" 506 | ] 507 | } 508 | ], 509 | "source": [ 510 | "arr = np.array([[1, 2, 3], [4, 5, 6]])\n", 511 | "\n", 512 | "print(arr)" 513 | ] 514 | }, 515 | { 516 | "cell_type": "markdown", 517 | "id": "9c54bc10", 518 | "metadata": {}, 519 | "source": [ 520 | "# 3-D arrays\n", 521 | "\n", 522 | "An array that has 2-D arrays (matrices) as its elements is called 3-D array.\n", 523 | "\n", 524 | "These are often used to represent a 3rd order tensor.\n", 525 | "\n", 526 | "## Example\n", 527 | "Create a 3-D array with two 2-D arrays, both containing two arrays with the values 1,2,3 and 4,5,6:" 528 | ] 529 | }, 530 | { 531 | "cell_type": "code", 532 | "execution_count": 12, 533 | "id": "63d158d5", 534 | "metadata": {}, 535 | "outputs": [ 536 | { 537 | "name": "stdout", 538 | "output_type": "stream", 539 | "text": [ 540 | "[[[1 2 3]\n", 541 | " [4 5 6]]\n", 542 | "\n", 543 | " [[1 2 3]\n", 544 | " [4 5 6]]]\n" 545 | ] 546 | } 547 | ], 548 | "source": [ 549 | "arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])\n", 550 | "\n", 551 | "print(arr)" 552 | ] 553 | }, 554 | { 555 | "cell_type": "markdown", 556 | "id": "491fbc19", 557 | "metadata": {}, 558 | "source": [ 559 | "# Check Number of Dimensions?\n", 560 | "NumPy Arrays provides the `ndim` attribute that returns an integer that tells us how many dimensions the array have.\n", 561 | "\n", 562 | "## Example\n", 563 | "Check how many dimensions the arrays have:" 564 | ] 565 | }, 566 | { 567 | "cell_type": "code", 568 | "execution_count": 13, 569 | "id": "fda75c61", 570 | "metadata": {}, 571 | "outputs": [ 572 | { 573 | "name": "stdout", 574 | "output_type": "stream", 575 | "text": [ 576 | "0\n", 577 | "1\n", 578 | "2\n", 579 | "3\n" 580 | ] 581 | } 582 | ], 583 | "source": [ 584 | "a = np.array(42)\n", 585 | "b = np.array([1, 2, 3, 4, 5])\n", 586 | "c = np.array([[1, 2, 3], [4, 5, 6]])\n", 587 | "d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])\n", 588 | "\n", 589 | "print(a.ndim)\n", 590 | "print(b.ndim)\n", 591 | "print(c.ndim)\n", 592 | "print(d.ndim)" 593 | ] 594 | }, 595 | { 596 | "cell_type": "markdown", 597 | "id": "ae769c51", 598 | "metadata": {}, 599 | "source": [ 600 | "# Higher Dimensional Arrays\n", 601 | "An array can have any number of dimensions.\n", 602 | "\n", 603 | "When the array is created, you can define the number of dimensions by using the `ndmin` argument.\n", 604 | "\n", 605 | "## Example\n", 606 | "Create an array with 5 dimensions and verify that it has 5 dimensions:" 607 | ] 608 | }, 609 | { 610 | "cell_type": "code", 611 | "execution_count": 14, 612 | "id": "05f57a78", 613 | "metadata": {}, 614 | "outputs": [ 615 | { 616 | "name": "stdout", 617 | "output_type": "stream", 618 | "text": [ 619 | "[[[[[1 2 3 4]]]]]\n", 620 | "number of dimensions : 5\n" 621 | ] 622 | } 623 | ], 624 | "source": [ 625 | "arr = np.array([1, 2, 3, 4], ndmin=5)\n", 626 | "\n", 627 | "print(arr)\n", 628 | "print('number of dimensions :', arr.ndim)" 629 | ] 630 | }, 631 | { 632 | "cell_type": "markdown", 633 | "id": "ea6a3a43", 634 | "metadata": {}, 635 | "source": [ 636 | "In this array the innermost dimension (5th dim) has 4 elements, the 4th dim has 1 element that is the vector, the 3rd dim has 1 element that is the matrix with the vector, the 2nd dim has 1 element that is 3D array and 1st dim has 1 element that is a 4D array.\n", 637 | "\n" 638 | ] 639 | }, 640 | { 641 | "cell_type": "markdown", 642 | "id": "569910e8", 643 | "metadata": {}, 644 | "source": [ 645 | "# NumPy Array Indexing\n", 646 | "\n", 647 | "## Access Array Elements\n", 648 | "Array indexing is the same as accessing an array element.\n", 649 | "\n", 650 | "You can access an array element by referring to its index number.\n", 651 | "\n", 652 | "The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.\n", 653 | "\n", 654 | "## Example\n", 655 | "Get the first element from the following array:" 656 | ] 657 | }, 658 | { 659 | "cell_type": "code", 660 | "execution_count": 15, 661 | "id": "299df7f4", 662 | "metadata": {}, 663 | "outputs": [ 664 | { 665 | "name": "stdout", 666 | "output_type": "stream", 667 | "text": [ 668 | "1\n" 669 | ] 670 | } 671 | ], 672 | "source": [ 673 | "arr = np.array([1, 2, 3, 4])\n", 674 | "\n", 675 | "print(arr[0])" 676 | ] 677 | }, 678 | { 679 | "cell_type": "markdown", 680 | "id": "762d6d0a", 681 | "metadata": {}, 682 | "source": [ 683 | "## Example\n", 684 | "Get the second element from the following array." 685 | ] 686 | }, 687 | { 688 | "cell_type": "code", 689 | "execution_count": 16, 690 | "id": "addd8334", 691 | "metadata": {}, 692 | "outputs": [ 693 | { 694 | "name": "stdout", 695 | "output_type": "stream", 696 | "text": [ 697 | "2\n" 698 | ] 699 | } 700 | ], 701 | "source": [ 702 | "arr = np.array([1, 2, 3, 4])\n", 703 | "\n", 704 | "print(arr[1])" 705 | ] 706 | }, 707 | { 708 | "cell_type": "markdown", 709 | "id": "60cd7dca", 710 | "metadata": {}, 711 | "source": [ 712 | "## Example\n", 713 | "Get third and fourth elements from the following array and add them." 714 | ] 715 | }, 716 | { 717 | "cell_type": "code", 718 | "execution_count": 17, 719 | "id": "a25ed85a", 720 | "metadata": {}, 721 | "outputs": [ 722 | { 723 | "name": "stdout", 724 | "output_type": "stream", 725 | "text": [ 726 | "7\n" 727 | ] 728 | } 729 | ], 730 | "source": [ 731 | "arr = np.array([1, 2, 3, 4])\n", 732 | "\n", 733 | "print(arr[2] + arr[3])" 734 | ] 735 | }, 736 | { 737 | "cell_type": "markdown", 738 | "id": "8297a495", 739 | "metadata": {}, 740 | "source": [ 741 | "# Access 2-D Arrays\n", 742 | "To access elements from 2-D arrays we can use comma separated integers representing the dimension and the index of the element.\n", 743 | "\n", 744 | "Think of 2-D arrays like a table with rows and columns, where the row represents the dimension and the index represents the column.\n", 745 | "\n", 746 | "## Example\n", 747 | "Access the element on the first row, second column:" 748 | ] 749 | }, 750 | { 751 | "cell_type": "code", 752 | "execution_count": 18, 753 | "id": "4869eed5", 754 | "metadata": {}, 755 | "outputs": [ 756 | { 757 | "name": "stdout", 758 | "output_type": "stream", 759 | "text": [ 760 | "2nd element on 1st row: 2\n" 761 | ] 762 | } 763 | ], 764 | "source": [ 765 | "arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])\n", 766 | "\n", 767 | "print('2nd element on 1st row: ', arr[0, 1])" 768 | ] 769 | }, 770 | { 771 | "cell_type": "markdown", 772 | "id": "9c22f862", 773 | "metadata": {}, 774 | "source": [ 775 | "## Example\n", 776 | "Access the element on the 2nd row, 5th column:" 777 | ] 778 | }, 779 | { 780 | "cell_type": "code", 781 | "execution_count": 19, 782 | "id": "b6aef758", 783 | "metadata": {}, 784 | "outputs": [ 785 | { 786 | "name": "stdout", 787 | "output_type": "stream", 788 | "text": [ 789 | "5th element on 2nd row: 10\n" 790 | ] 791 | } 792 | ], 793 | "source": [ 794 | "arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])\n", 795 | "\n", 796 | "print('5th element on 2nd row: ', arr[1, 4])" 797 | ] 798 | }, 799 | { 800 | "cell_type": "markdown", 801 | "id": "debaa60e", 802 | "metadata": {}, 803 | "source": [ 804 | "# Access 3-D Arrays\n", 805 | "To access elements from 3-D arrays we can use comma separated integers representing the dimensions and the index of the element.\n", 806 | "\n", 807 | "## Example\n", 808 | "Access the third element of the second array of the first array:" 809 | ] 810 | }, 811 | { 812 | "cell_type": "code", 813 | "execution_count": 20, 814 | "id": "4c793036", 815 | "metadata": {}, 816 | "outputs": [ 817 | { 818 | "name": "stdout", 819 | "output_type": "stream", 820 | "text": [ 821 | "6\n" 822 | ] 823 | } 824 | ], 825 | "source": [ 826 | "arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])\n", 827 | "\n", 828 | "print(arr[0, 1, 2])" 829 | ] 830 | }, 831 | { 832 | "cell_type": "markdown", 833 | "id": "5751aa02", 834 | "metadata": {}, 835 | "source": [ 836 | "# Example Explained\n", 837 | "`arr[0, 1, 2]` prints the value `6`.\n", 838 | "\n", 839 | "And this is why:\n", 840 | "\n", 841 | "The first number represents the first dimension, which contains two arrays:\n", 842 | "\n", 843 | "`[[1, 2, 3], [4, 5, 6]]`\n", 844 | "and:\n", 845 | "`[[7, 8, 9], [10, 11, 12]]`\n", 846 | "\n", 847 | "Since we selected 0, we are left with the first array:\n", 848 | "\n", 849 | "`[[1, 2, 3], [4, 5, 6]]`\n", 850 | "\n", 851 | "The second number represents the second dimension, which also contains two arrays:\n", 852 | "\n", 853 | "`[1, 2, 3]`\n", 854 | "and:\n", 855 | "`[4, 5, 6]`\n", 856 | "\n", 857 | "Since we selected `1`, we are left with the second array:\n", 858 | "\n", 859 | "`[4, 5, 6]`\n", 860 | "\n", 861 | "The third number represents the third dimension, which contains three values:\n", 862 | "\n", 863 | "4\n", 864 | "5\n", 865 | "6\n", 866 | "\n", 867 | "Since we selected `2`, we end up with the third value:\n", 868 | "\n", 869 | "6\n", 870 | "\n" 871 | ] 872 | }, 873 | { 874 | "cell_type": "markdown", 875 | "id": "b3a4bcaf", 876 | "metadata": {}, 877 | "source": [ 878 | "# Negative Indexing\n", 879 | "Use negative indexing to access an array from the end.\n", 880 | "## Example\n", 881 | "Print the last element from the 2nd dim:" 882 | ] 883 | }, 884 | { 885 | "cell_type": "code", 886 | "execution_count": 21, 887 | "id": "471116e0", 888 | "metadata": {}, 889 | "outputs": [ 890 | { 891 | "name": "stdout", 892 | "output_type": "stream", 893 | "text": [ 894 | "Last element from 2nd dim: 10\n" 895 | ] 896 | } 897 | ], 898 | "source": [ 899 | "arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])\n", 900 | "\n", 901 | "print('Last element from 2nd dim: ', arr[1, -1])" 902 | ] 903 | }, 904 | { 905 | "cell_type": "markdown", 906 | "id": "e860e7f7", 907 | "metadata": {}, 908 | "source": [ 909 | "# NumPy Array Slicing\n", 910 | "\n", 911 | "## Slicing arrays\n", 912 | "Slicing in python means taking elements from one given index to another given index.\n", 913 | "\n", 914 | "We pass slice instead of index like this: `[start:end]`.\n", 915 | "\n", 916 | "We can also define the step, like this: `[start:end:step]`.\n", 917 | "\n", 918 | "If we don't pass start its considered 0\n", 919 | "\n", 920 | "If we don't pass end its considered length of array in that dimension\n", 921 | "\n", 922 | "If we don't pass step its considered 1\n", 923 | "\n", 924 | "## Example\n", 925 | "Slice elements from index 1 to index 5 from the following array:\n", 926 | "\n" 927 | ] 928 | }, 929 | { 930 | "cell_type": "code", 931 | "execution_count": 22, 932 | "id": "6ae2a4ce", 933 | "metadata": {}, 934 | "outputs": [ 935 | { 936 | "name": "stdout", 937 | "output_type": "stream", 938 | "text": [ 939 | "[2 3 4 5]\n" 940 | ] 941 | } 942 | ], 943 | "source": [ 944 | "arr = np.array([1, 2, 3, 4, 5, 6, 7])\n", 945 | "\n", 946 | "print(arr[1:5])" 947 | ] 948 | }, 949 | { 950 | "cell_type": "markdown", 951 | "id": "6b76a14c", 952 | "metadata": {}, 953 | "source": [ 954 | "> * Note: The result includes the start index, but excludes the end index." 955 | ] 956 | }, 957 | { 958 | "cell_type": "markdown", 959 | "id": "f25481bd", 960 | "metadata": {}, 961 | "source": [ 962 | "## Example\n", 963 | "\n", 964 | "Slice elements from index 4 to the end of the array:" 965 | ] 966 | }, 967 | { 968 | "cell_type": "code", 969 | "execution_count": 23, 970 | "id": "4ccbd50e", 971 | "metadata": {}, 972 | "outputs": [ 973 | { 974 | "name": "stdout", 975 | "output_type": "stream", 976 | "text": [ 977 | "[5 6 7]\n" 978 | ] 979 | } 980 | ], 981 | "source": [ 982 | "arr = np.array([1, 2, 3, 4, 5, 6, 7])\n", 983 | "\n", 984 | "print(arr[4:])" 985 | ] 986 | }, 987 | { 988 | "cell_type": "markdown", 989 | "id": "52229824", 990 | "metadata": {}, 991 | "source": [ 992 | "# Example\n", 993 | "Slice elements from the beginning to index 4 (not included):" 994 | ] 995 | }, 996 | { 997 | "cell_type": "code", 998 | "execution_count": 24, 999 | "id": "2c42b20d", 1000 | "metadata": {}, 1001 | "outputs": [ 1002 | { 1003 | "name": "stdout", 1004 | "output_type": "stream", 1005 | "text": [ 1006 | "[1 2 3 4]\n" 1007 | ] 1008 | } 1009 | ], 1010 | "source": [ 1011 | "arr = np.array([1, 2, 3, 4, 5, 6, 7])\n", 1012 | "\n", 1013 | "print(arr[:4])" 1014 | ] 1015 | }, 1016 | { 1017 | "cell_type": "markdown", 1018 | "id": "fb026000", 1019 | "metadata": {}, 1020 | "source": [ 1021 | "# Negative Slicing\n", 1022 | "\n", 1023 | "Use the minus operator to refer to an index from the end:" 1024 | ] 1025 | }, 1026 | { 1027 | "cell_type": "markdown", 1028 | "id": "06305cb0", 1029 | "metadata": {}, 1030 | "source": [ 1031 | "## Example\n", 1032 | "\n", 1033 | "Slice from the index 3 from the end to index 1 from the end:" 1034 | ] 1035 | }, 1036 | { 1037 | "cell_type": "code", 1038 | "execution_count": 25, 1039 | "id": "38ff4af5", 1040 | "metadata": {}, 1041 | "outputs": [ 1042 | { 1043 | "name": "stdout", 1044 | "output_type": "stream", 1045 | "text": [ 1046 | "[5 6]\n" 1047 | ] 1048 | } 1049 | ], 1050 | "source": [ 1051 | "arr = np.array([1, 2, 3, 4, 5, 6, 7])\n", 1052 | "\n", 1053 | "print(arr[-3:-1])" 1054 | ] 1055 | }, 1056 | { 1057 | "cell_type": "markdown", 1058 | "id": "b21ad51f", 1059 | "metadata": {}, 1060 | "source": [ 1061 | "# STEP\n", 1062 | "Use the step value to determine the step of the slicing:\n", 1063 | "\n", 1064 | "### Example\n", 1065 | "Return every other element from index 1 to index 5:" 1066 | ] 1067 | }, 1068 | { 1069 | "cell_type": "code", 1070 | "execution_count": 26, 1071 | "id": "8799a47f", 1072 | "metadata": {}, 1073 | "outputs": [ 1074 | { 1075 | "name": "stdout", 1076 | "output_type": "stream", 1077 | "text": [ 1078 | "[2 4]\n" 1079 | ] 1080 | } 1081 | ], 1082 | "source": [ 1083 | "arr = np.array([1, 2, 3, 4, 5, 6, 7])\n", 1084 | "\n", 1085 | "print(arr[1:5:2])" 1086 | ] 1087 | }, 1088 | { 1089 | "cell_type": "markdown", 1090 | "id": "348877d4", 1091 | "metadata": {}, 1092 | "source": [ 1093 | "### Example\n", 1094 | "Return every other element from the entire array:" 1095 | ] 1096 | }, 1097 | { 1098 | "cell_type": "code", 1099 | "execution_count": 27, 1100 | "id": "69a9cf99", 1101 | "metadata": {}, 1102 | "outputs": [ 1103 | { 1104 | "name": "stdout", 1105 | "output_type": "stream", 1106 | "text": [ 1107 | "[1 3 5 7]\n" 1108 | ] 1109 | } 1110 | ], 1111 | "source": [ 1112 | "arr = np.array([1, 2, 3, 4, 5, 6, 7])\n", 1113 | "\n", 1114 | "print(arr[::2])" 1115 | ] 1116 | }, 1117 | { 1118 | "cell_type": "markdown", 1119 | "id": "5b000ccc", 1120 | "metadata": {}, 1121 | "source": [ 1122 | "# Slicing 2-D Arrays\n", 1123 | "### Example\n", 1124 | "From the second element, slice elements from index 1 to index 4 (not included):\n", 1125 | "\n" 1126 | ] 1127 | }, 1128 | { 1129 | "cell_type": "code", 1130 | "execution_count": 28, 1131 | "id": "eb580703", 1132 | "metadata": {}, 1133 | "outputs": [ 1134 | { 1135 | "name": "stdout", 1136 | "output_type": "stream", 1137 | "text": [ 1138 | "[7 8 9]\n" 1139 | ] 1140 | } 1141 | ], 1142 | "source": [ 1143 | "arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])\n", 1144 | "\n", 1145 | "print(arr[1, 1:4])" 1146 | ] 1147 | }, 1148 | { 1149 | "cell_type": "markdown", 1150 | "id": "154e0be9", 1151 | "metadata": {}, 1152 | "source": [ 1153 | "> * Note: Remember that second element has index 1." 1154 | ] 1155 | }, 1156 | { 1157 | "cell_type": "markdown", 1158 | "id": "491d3530", 1159 | "metadata": {}, 1160 | "source": [ 1161 | "### Example\n", 1162 | "From both elements, return index 2:" 1163 | ] 1164 | }, 1165 | { 1166 | "cell_type": "code", 1167 | "execution_count": 29, 1168 | "id": "0f443548", 1169 | "metadata": {}, 1170 | "outputs": [ 1171 | { 1172 | "name": "stdout", 1173 | "output_type": "stream", 1174 | "text": [ 1175 | "[3 8]\n" 1176 | ] 1177 | } 1178 | ], 1179 | "source": [ 1180 | "arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])\n", 1181 | "\n", 1182 | "print(arr[0:2, 2])" 1183 | ] 1184 | }, 1185 | { 1186 | "cell_type": "markdown", 1187 | "id": "5bd718be", 1188 | "metadata": {}, 1189 | "source": [ 1190 | "### Example\n", 1191 | "From both elements, slice index 1 to index 4 (not included), this will return a 2-D array:\n" 1192 | ] 1193 | }, 1194 | { 1195 | "cell_type": "code", 1196 | "execution_count": 30, 1197 | "id": "67d74b8d", 1198 | "metadata": {}, 1199 | "outputs": [ 1200 | { 1201 | "name": "stdout", 1202 | "output_type": "stream", 1203 | "text": [ 1204 | "[[2 3 4]\n", 1205 | " [7 8 9]]\n" 1206 | ] 1207 | } 1208 | ], 1209 | "source": [ 1210 | "arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])\n", 1211 | "\n", 1212 | "print(arr[0:2, 1:4])" 1213 | ] 1214 | }, 1215 | { 1216 | "cell_type": "markdown", 1217 | "id": "9cea698d", 1218 | "metadata": {}, 1219 | "source": [ 1220 | "# NumPy Data Types\n", 1221 | "\n", 1222 | "### Data Types in Python\n", 1223 | "\n", 1224 | "By default Python have these data types:\n", 1225 | " - `strings` - used to represent text data, the text is given under quote marks. e.g. \"ABCD\" \n", 1226 | " - `integer` - used to represent integer numbers. e.g. -1, -2, -3\n", 1227 | " - `float` - used to represent real numbers. e.g. 1.2, 42.42\n", 1228 | " - `boolean` - used to represent True or False.\n", 1229 | " - `complex` - used to represent complex numbers. e.g. 1.0 + 2.0j, 1.5 + 2.5j\n" 1230 | ] 1231 | }, 1232 | { 1233 | "cell_type": "markdown", 1234 | "id": "658b7157", 1235 | "metadata": {}, 1236 | "source": [ 1237 | "# Data Types in NumPy\n", 1238 | "\n", 1239 | "NumPy has some extra data types, and refer to data types with one character, like i for integers, u for unsigned integers etc.\n", 1240 | "\n", 1241 | "Below is a list of all data types in NumPy and the characters used to represent them.\n", 1242 | "\n", 1243 | " - `i` - integer\n", 1244 | " - `b` - boolean\n", 1245 | " - `u` - unsigned integer\n", 1246 | " - `f` - float\n", 1247 | " - `c` - complex float\n", 1248 | " - `m` - timedelta\n", 1249 | " - `M` - datetime\n", 1250 | " - `O` - object\n", 1251 | " - `S` - string\n", 1252 | " - `U` - unicode string\n", 1253 | " - `V` - fixed chunk of memory for other type ( void )\n" 1254 | ] 1255 | }, 1256 | { 1257 | "cell_type": "markdown", 1258 | "id": "acd5d55a", 1259 | "metadata": {}, 1260 | "source": [ 1261 | "# Checking the Data Type of an Array\n", 1262 | "\n", 1263 | "The NumPy array object has a property called `dtype` that returns the data type of the array:\n", 1264 | "\n", 1265 | "### Example\n", 1266 | "Get the data type of an array object:" 1267 | ] 1268 | }, 1269 | { 1270 | "cell_type": "code", 1271 | "execution_count": 31, 1272 | "id": "7144ae71", 1273 | "metadata": {}, 1274 | "outputs": [ 1275 | { 1276 | "name": "stdout", 1277 | "output_type": "stream", 1278 | "text": [ 1279 | "int64\n" 1280 | ] 1281 | } 1282 | ], 1283 | "source": [ 1284 | "arr = np.array([1, 2, 3, 4])\n", 1285 | "\n", 1286 | "print(arr.dtype)" 1287 | ] 1288 | }, 1289 | { 1290 | "cell_type": "markdown", 1291 | "id": "0cce7653", 1292 | "metadata": {}, 1293 | "source": [ 1294 | "### Example\n", 1295 | "Get the data type of an array containing strings:" 1296 | ] 1297 | }, 1298 | { 1299 | "cell_type": "code", 1300 | "execution_count": 32, 1301 | "id": "6e0d6b27", 1302 | "metadata": {}, 1303 | "outputs": [ 1304 | { 1305 | "name": "stdout", 1306 | "output_type": "stream", 1307 | "text": [ 1308 | " * ValueError: In Python ValueError is raised when the type of passed argument to a function is unexpected/incorrect.\n", 1403 | "\n" 1404 | ] 1405 | }, 1406 | { 1407 | "cell_type": "markdown", 1408 | "id": "29957abe", 1409 | "metadata": {}, 1410 | "source": [ 1411 | "## Example\n", 1412 | "A non integer string like 'a' can not be converted to integer (will raise an error):" 1413 | ] 1414 | }, 1415 | { 1416 | "cell_type": "code", 1417 | "execution_count": 35, 1418 | "id": "46478695", 1419 | "metadata": {}, 1420 | "outputs": [ 1421 | { 1422 | "ename": "ValueError", 1423 | "evalue": "invalid literal for int() with base 10: 'a'", 1424 | "output_type": "error", 1425 | "traceback": [ 1426 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 1427 | "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", 1428 | "\u001b[0;32m/tmp/ipykernel_96253/3318528490.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0marr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'a'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'2'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'3'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'i'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 1429 | "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: 'a'" 1430 | ] 1431 | } 1432 | ], 1433 | "source": [ 1434 | "arr = np.array(['a', '2', '3'], dtype='i')" 1435 | ] 1436 | }, 1437 | { 1438 | "cell_type": "markdown", 1439 | "id": "b839f118", 1440 | "metadata": {}, 1441 | "source": [ 1442 | "# Converting Data Type on Existing Arrays\n", 1443 | "The best way to change the data type of an existing array, is to make a copy of the array with the `astype()` method.\n", 1444 | "\n", 1445 | "The `astype()` function creates a copy of the array, and allows you to specify the data type as a parameter.\n", 1446 | "\n", 1447 | "The data type can be specified using a string, like `'f'` for float, `'i'` for integer etc. or you can use the data type directly like `float` for float and `int` for integer." 1448 | ] 1449 | }, 1450 | { 1451 | "cell_type": "markdown", 1452 | "id": "e27d5631", 1453 | "metadata": {}, 1454 | "source": [ 1455 | "## Example\n", 1456 | "Change data type from float to integer by using `'i'` as parameter value:\n", 1457 | "\n" 1458 | ] 1459 | }, 1460 | { 1461 | "cell_type": "code", 1462 | "execution_count": 36, 1463 | "id": "a6e040e1", 1464 | "metadata": {}, 1465 | "outputs": [ 1466 | { 1467 | "name": "stdout", 1468 | "output_type": "stream", 1469 | "text": [ 1470 | "[1 2 3]\n", 1471 | "int32\n" 1472 | ] 1473 | } 1474 | ], 1475 | "source": [ 1476 | "arr = np.array([1.1, 2.1, 3.1])\n", 1477 | "\n", 1478 | "newarr = arr.astype('i')\n", 1479 | "\n", 1480 | "print(newarr)\n", 1481 | "print(newarr.dtype)" 1482 | ] 1483 | }, 1484 | { 1485 | "cell_type": "markdown", 1486 | "id": "6d05b78f", 1487 | "metadata": {}, 1488 | "source": [ 1489 | "## Example\n", 1490 | "Change data type from float to integer by using `int` as parameter value:" 1491 | ] 1492 | }, 1493 | { 1494 | "cell_type": "code", 1495 | "execution_count": 37, 1496 | "id": "f16e0024", 1497 | "metadata": {}, 1498 | "outputs": [ 1499 | { 1500 | "name": "stdout", 1501 | "output_type": "stream", 1502 | "text": [ 1503 | "[1 2 3]\n", 1504 | "int64\n" 1505 | ] 1506 | } 1507 | ], 1508 | "source": [ 1509 | "arr = np.array([1.1, 2.1, 3.1])\n", 1510 | "\n", 1511 | "newarr = arr.astype(int)\n", 1512 | "\n", 1513 | "print(newarr)\n", 1514 | "print(newarr.dtype)" 1515 | ] 1516 | }, 1517 | { 1518 | "cell_type": "markdown", 1519 | "id": "808ab803", 1520 | "metadata": {}, 1521 | "source": [ 1522 | "## Example\n", 1523 | "Change data type from integer to boolean:" 1524 | ] 1525 | }, 1526 | { 1527 | "cell_type": "code", 1528 | "execution_count": 38, 1529 | "id": "3d6e8b3f", 1530 | "metadata": {}, 1531 | "outputs": [ 1532 | { 1533 | "name": "stdout", 1534 | "output_type": "stream", 1535 | "text": [ 1536 | "[ True False True]\n", 1537 | "bool\n" 1538 | ] 1539 | } 1540 | ], 1541 | "source": [ 1542 | "arr = np.array([1, 0, 3])\n", 1543 | "\n", 1544 | "newarr = arr.astype(bool)\n", 1545 | "\n", 1546 | "print(newarr)\n", 1547 | "print(newarr.dtype)" 1548 | ] 1549 | }, 1550 | { 1551 | "cell_type": "markdown", 1552 | "id": "8f61d205", 1553 | "metadata": {}, 1554 | "source": [ 1555 | "# NumPy Array Copy vs View\n", 1556 | "\n", 1557 | "### he Difference Between Copy and View\n", 1558 | "The main difference between a copy and a view of an array is that the copy is a new array, and the view is just a view of the original array.\n", 1559 | "\n", 1560 | "The copy owns the data and any changes made to the copy will not affect original array, and any changes made to the original array will not affect the copy.\n", 1561 | "\n", 1562 | "The view does not own the data and any changes made to the view will affect the original array, and any changes made to the original array will affect the view." 1563 | ] 1564 | }, 1565 | { 1566 | "cell_type": "markdown", 1567 | "id": "a78bb6fd", 1568 | "metadata": {}, 1569 | "source": [ 1570 | "# COPY:\n", 1571 | "### Example\n", 1572 | "Make a copy, change the original array, and display both arrays:\n", 1573 | "\n" 1574 | ] 1575 | }, 1576 | { 1577 | "cell_type": "code", 1578 | "execution_count": 39, 1579 | "id": "9575030b", 1580 | "metadata": {}, 1581 | "outputs": [ 1582 | { 1583 | "name": "stdout", 1584 | "output_type": "stream", 1585 | "text": [ 1586 | "[42 2 3 4 5]\n", 1587 | "[1 2 3 4 5]\n" 1588 | ] 1589 | } 1590 | ], 1591 | "source": [ 1592 | "arr = np.array([1, 2, 3, 4, 5])\n", 1593 | "x = arr.copy()\n", 1594 | "arr[0] = 42\n", 1595 | "\n", 1596 | "print(arr)\n", 1597 | "print(x)" 1598 | ] 1599 | }, 1600 | { 1601 | "cell_type": "markdown", 1602 | "id": "9f3c5e59", 1603 | "metadata": {}, 1604 | "source": [ 1605 | "> * The copy SHOULD NOT be affected by the changes made to the original array.\n", 1606 | "\n" 1607 | ] 1608 | }, 1609 | { 1610 | "cell_type": "markdown", 1611 | "id": "3959dbcd", 1612 | "metadata": {}, 1613 | "source": [ 1614 | "# VIEW:\n", 1615 | "### Example\n", 1616 | "Make a view, change the original array, and display both arrays:" 1617 | ] 1618 | }, 1619 | { 1620 | "cell_type": "code", 1621 | "execution_count": 40, 1622 | "id": "f336a527", 1623 | "metadata": {}, 1624 | "outputs": [ 1625 | { 1626 | "name": "stdout", 1627 | "output_type": "stream", 1628 | "text": [ 1629 | "[42 2 3 4 5]\n", 1630 | "[42 2 3 4 5]\n" 1631 | ] 1632 | } 1633 | ], 1634 | "source": [ 1635 | "arr = np.array([1, 2, 3, 4, 5])\n", 1636 | "x = arr.view()\n", 1637 | "arr[0] = 42\n", 1638 | "\n", 1639 | "print(arr)\n", 1640 | "print(x)" 1641 | ] 1642 | }, 1643 | { 1644 | "cell_type": "markdown", 1645 | "id": "a62da91c", 1646 | "metadata": {}, 1647 | "source": [ 1648 | "> * The view SHOULD be affected by the changes made to the original array.\n", 1649 | "\n" 1650 | ] 1651 | }, 1652 | { 1653 | "cell_type": "markdown", 1654 | "id": "d5b551b5", 1655 | "metadata": {}, 1656 | "source": [ 1657 | "# Make Changes in the VIEW:\n", 1658 | "### Example\n", 1659 | "Make a view, change the view, and display both arrays:" 1660 | ] 1661 | }, 1662 | { 1663 | "cell_type": "code", 1664 | "execution_count": 41, 1665 | "id": "6cf9cd23", 1666 | "metadata": {}, 1667 | "outputs": [ 1668 | { 1669 | "name": "stdout", 1670 | "output_type": "stream", 1671 | "text": [ 1672 | "[31 2 3 4 5]\n", 1673 | "[31 2 3 4 5]\n" 1674 | ] 1675 | } 1676 | ], 1677 | "source": [ 1678 | "arr = np.array([1, 2, 3, 4, 5])\n", 1679 | "x = arr.view()\n", 1680 | "x[0] = 31\n", 1681 | "\n", 1682 | "print(arr)\n", 1683 | "print(x)" 1684 | ] 1685 | }, 1686 | { 1687 | "cell_type": "markdown", 1688 | "id": "91eaf84e", 1689 | "metadata": {}, 1690 | "source": [ 1691 | "> * The original array SHOULD be affected by the changes made to the view.\n" 1692 | ] 1693 | }, 1694 | { 1695 | "cell_type": "markdown", 1696 | "id": "2651a25d", 1697 | "metadata": {}, 1698 | "source": [ 1699 | "# Check if Array Owns its Data\n", 1700 | "As mentioned above, copies owns the data, and views does not own the data, but how can we check this?\n", 1701 | "\n", 1702 | "Every NumPy array has the attribute `base` that returns `None` if the array owns the data.\n", 1703 | "\n", 1704 | "Otherwise, the `base` attribute refers to the original object." 1705 | ] 1706 | }, 1707 | { 1708 | "cell_type": "markdown", 1709 | "id": "da969c0e", 1710 | "metadata": {}, 1711 | "source": [ 1712 | "> * The copy returns None.\n", 1713 | "> * The view returns the original array." 1714 | ] 1715 | }, 1716 | { 1717 | "cell_type": "markdown", 1718 | "id": "4ce8a913", 1719 | "metadata": {}, 1720 | "source": [ 1721 | "# NumPy Array Shape\n", 1722 | "## Shape of an Array\n", 1723 | "The shape of an array is the number of elements in each dimension.\n", 1724 | "\n" 1725 | ] 1726 | }, 1727 | { 1728 | "cell_type": "markdown", 1729 | "id": "f7046f83", 1730 | "metadata": {}, 1731 | "source": [ 1732 | "# Get the Shape of an Array\n", 1733 | "NumPy arrays have an attribute called `shape` that returns a tuple with each index having the number of corresponding elements." 1734 | ] 1735 | }, 1736 | { 1737 | "cell_type": "markdown", 1738 | "id": "7e650f2e", 1739 | "metadata": {}, 1740 | "source": [ 1741 | "## Example\n", 1742 | "Print the shape of a 2-D array:\n", 1743 | "\n" 1744 | ] 1745 | }, 1746 | { 1747 | "cell_type": "code", 1748 | "execution_count": 42, 1749 | "id": "cdd8daff", 1750 | "metadata": {}, 1751 | "outputs": [ 1752 | { 1753 | "name": "stdout", 1754 | "output_type": "stream", 1755 | "text": [ 1756 | "(2, 4)\n" 1757 | ] 1758 | } 1759 | ], 1760 | "source": [ 1761 | "arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])\n", 1762 | "\n", 1763 | "print(arr.shape)" 1764 | ] 1765 | }, 1766 | { 1767 | "cell_type": "markdown", 1768 | "id": "38174039", 1769 | "metadata": {}, 1770 | "source": [ 1771 | "**The example above returns `(2, 4)`, which means that the array has 2 dimensions, where the first dimension has 2 elements and the second has 4.**\n", 1772 | "\n" 1773 | ] 1774 | }, 1775 | { 1776 | "cell_type": "markdown", 1777 | "id": "b241b505", 1778 | "metadata": {}, 1779 | "source": [ 1780 | "## Example\n", 1781 | "Create an array with 5 dimensions using `ndmin` using a vector with values 1,2,3,4 and verify that last dimension has value 4:" 1782 | ] 1783 | }, 1784 | { 1785 | "cell_type": "code", 1786 | "execution_count": 43, 1787 | "id": "7d70d4ac", 1788 | "metadata": {}, 1789 | "outputs": [ 1790 | { 1791 | "name": "stdout", 1792 | "output_type": "stream", 1793 | "text": [ 1794 | "[[[[[1 2 3 4]]]]]\n", 1795 | "shape of array : (1, 1, 1, 1, 4)\n" 1796 | ] 1797 | } 1798 | ], 1799 | "source": [ 1800 | "arr = np.array([1, 2, 3, 4], ndmin=5)\n", 1801 | "\n", 1802 | "print(arr)\n", 1803 | "print('shape of array :', arr.shape)" 1804 | ] 1805 | }, 1806 | { 1807 | "cell_type": "markdown", 1808 | "id": "2d71fd01", 1809 | "metadata": {}, 1810 | "source": [ 1811 | "# What does the shape tuple represent? 🤔\n", 1812 | "\n", 1813 | "**Integers at every index tells about the number of elements the corresponding dimension has.**\n", 1814 | "\n", 1815 | "**In the example above at index-4 we have value 4, so we can say that 5th ( 4 + 1 th) dimension has 4 elements.**" 1816 | ] 1817 | }, 1818 | { 1819 | "cell_type": "markdown", 1820 | "id": "572ebb4d", 1821 | "metadata": {}, 1822 | "source": [ 1823 | "# NumPy Array Reshaping\n", 1824 | "\n", 1825 | "## Reshaping arrays\n", 1826 | "\n", 1827 | "**Reshaping means changing the shape of an array.**\n", 1828 | "\n", 1829 | "**The shape of an array is the number of elements in each dimension.**\n", 1830 | "\n", 1831 | "**By reshaping we can add or remove dimensions or change number of elements in each dimension.**" 1832 | ] 1833 | }, 1834 | { 1835 | "cell_type": "markdown", 1836 | "id": "2f825877", 1837 | "metadata": {}, 1838 | "source": [ 1839 | "# Reshape From 1-D to 2-D\n", 1840 | "## Example\n", 1841 | "Convert the following 1-D array with 12 elements into a 2-D array.\n", 1842 | "\n", 1843 | "The outermost dimension will have 4 arrays, each with 3 elements:" 1844 | ] 1845 | }, 1846 | { 1847 | "cell_type": "code", 1848 | "execution_count": 44, 1849 | "id": "708ee349", 1850 | "metadata": {}, 1851 | "outputs": [ 1852 | { 1853 | "name": "stdout", 1854 | "output_type": "stream", 1855 | "text": [ 1856 | "[[ 1 2 3]\n", 1857 | " [ 4 5 6]\n", 1858 | " [ 7 8 9]\n", 1859 | " [10 11 12]]\n" 1860 | ] 1861 | } 1862 | ], 1863 | "source": [ 1864 | "arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])\n", 1865 | "\n", 1866 | "newarr = arr.reshape(4, 3)\n", 1867 | "\n", 1868 | "print(newarr)" 1869 | ] 1870 | }, 1871 | { 1872 | "cell_type": "markdown", 1873 | "id": "9533bf06", 1874 | "metadata": {}, 1875 | "source": [ 1876 | "# Reshape From 1-D to 3-D\n", 1877 | "## Example\n", 1878 | "Convert the following 1-D array with 12 elements into a 3-D array.\n", 1879 | "\n", 1880 | "The outermost dimension will have 2 arrays that contains 3 arrays, each with 2 elements:" 1881 | ] 1882 | }, 1883 | { 1884 | "cell_type": "code", 1885 | "execution_count": 45, 1886 | "id": "932e783e", 1887 | "metadata": {}, 1888 | "outputs": [ 1889 | { 1890 | "name": "stdout", 1891 | "output_type": "stream", 1892 | "text": [ 1893 | "[[[ 1 2]\n", 1894 | " [ 3 4]\n", 1895 | " [ 5 6]]\n", 1896 | "\n", 1897 | " [[ 7 8]\n", 1898 | " [ 9 10]\n", 1899 | " [11 12]]]\n" 1900 | ] 1901 | } 1902 | ], 1903 | "source": [ 1904 | "arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])\n", 1905 | "\n", 1906 | "newarr = arr.reshape(2, 3, 2)\n", 1907 | "\n", 1908 | "print(newarr)" 1909 | ] 1910 | }, 1911 | { 1912 | "cell_type": "markdown", 1913 | "id": "732961f2", 1914 | "metadata": {}, 1915 | "source": [ 1916 | "# Can We Reshape Into any Shape?\n", 1917 | "Yes, as long as the elements required for reshaping are equal in both shapes.\n", 1918 | "\n", 1919 | "We can reshape an 8 elements 1D array into 4 elements in 2 rows 2D array but we cannot reshape it into a 3 elements 3 rows 2D array as that would require 3x3 = 9 elements.\n", 1920 | "\n", 1921 | "## Example\n", 1922 | "Try converting 1D array with 8 elements to a 2D array with 3 elements in each dimension (will raise an error):\n" 1923 | ] 1924 | }, 1925 | { 1926 | "cell_type": "code", 1927 | "execution_count": 46, 1928 | "id": "a5db85c4", 1929 | "metadata": {}, 1930 | "outputs": [ 1931 | { 1932 | "ename": "ValueError", 1933 | "evalue": "cannot reshape array of size 8 into shape (3,3)", 1934 | "output_type": "error", 1935 | "traceback": [ 1936 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 1937 | "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", 1938 | "\u001b[0;32m/tmp/ipykernel_96253/1394430823.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0marr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m6\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m7\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m8\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mnewarr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0marr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnewarr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 1939 | "\u001b[0;31mValueError\u001b[0m: cannot reshape array of size 8 into shape (3,3)" 1940 | ] 1941 | } 1942 | ], 1943 | "source": [ 1944 | "arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])\n", 1945 | "\n", 1946 | "newarr = arr.reshape(3, 3)\n", 1947 | "\n", 1948 | "print(newarr)" 1949 | ] 1950 | }, 1951 | { 1952 | "cell_type": "markdown", 1953 | "id": "5fc53486", 1954 | "metadata": {}, 1955 | "source": [ 1956 | "# Returns Copy or View?\n", 1957 | "\n", 1958 | "### Example\n", 1959 | "Check if the returned array is a copy or a view:\n", 1960 | "\n" 1961 | ] 1962 | }, 1963 | { 1964 | "cell_type": "code", 1965 | "execution_count": 47, 1966 | "id": "0f61bd8f", 1967 | "metadata": {}, 1968 | "outputs": [ 1969 | { 1970 | "name": "stdout", 1971 | "output_type": "stream", 1972 | "text": [ 1973 | "[1 2 3 4 5 6 7 8]\n" 1974 | ] 1975 | } 1976 | ], 1977 | "source": [ 1978 | "arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])\n", 1979 | "\n", 1980 | "print(arr.reshape(2, 4).base)" 1981 | ] 1982 | }, 1983 | { 1984 | "cell_type": "markdown", 1985 | "id": "7136b777", 1986 | "metadata": {}, 1987 | "source": [ 1988 | "> * The example above returns the original array, so it is a view.\n", 1989 | "\n" 1990 | ] 1991 | }, 1992 | { 1993 | "cell_type": "markdown", 1994 | "id": "c3e155b5", 1995 | "metadata": {}, 1996 | "source": [ 1997 | "# Unknown Dimension\n", 1998 | "You are allowed to have one \"unknown\" dimension.\n", 1999 | "\n", 2000 | "Meaning that you do not have to specify an exact number for one of the dimensions in the reshape method.\n", 2001 | "\n", 2002 | "Pass `-1` as the value, and NumPy will calculate this number for you." 2003 | ] 2004 | }, 2005 | { 2006 | "cell_type": "markdown", 2007 | "id": "03f2c779", 2008 | "metadata": {}, 2009 | "source": [ 2010 | "## Example\n", 2011 | "Convert 1D array with 8 elements to 3D array with 2x2 elements:" 2012 | ] 2013 | }, 2014 | { 2015 | "cell_type": "code", 2016 | "execution_count": 48, 2017 | "id": "a32862f1", 2018 | "metadata": {}, 2019 | "outputs": [ 2020 | { 2021 | "name": "stdout", 2022 | "output_type": "stream", 2023 | "text": [ 2024 | "[[[1 2]\n", 2025 | " [3 4]]\n", 2026 | "\n", 2027 | " [[5 6]\n", 2028 | " [7 8]]]\n" 2029 | ] 2030 | } 2031 | ], 2032 | "source": [ 2033 | "arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])\n", 2034 | "\n", 2035 | "newarr = arr.reshape(2, 2, -1)\n", 2036 | "\n", 2037 | "print(newarr)" 2038 | ] 2039 | }, 2040 | { 2041 | "cell_type": "markdown", 2042 | "id": "bfae210a", 2043 | "metadata": {}, 2044 | "source": [ 2045 | "> * Note: We can not pass -1 to more than one dimension.\n", 2046 | "\n" 2047 | ] 2048 | }, 2049 | { 2050 | "cell_type": "markdown", 2051 | "id": "ebc077b8", 2052 | "metadata": {}, 2053 | "source": [ 2054 | "# Flattening the arrays\n", 2055 | "Flattening array means converting a multidimensional array into a 1D array.\n", 2056 | "\n", 2057 | "We can use `reshape(-1)` to do this.\n", 2058 | "\n", 2059 | "## Example\n", 2060 | "Convert the array into a 1D array:" 2061 | ] 2062 | }, 2063 | { 2064 | "cell_type": "code", 2065 | "execution_count": 49, 2066 | "id": "47610463", 2067 | "metadata": {}, 2068 | "outputs": [ 2069 | { 2070 | "name": "stdout", 2071 | "output_type": "stream", 2072 | "text": [ 2073 | "[1 2 3 4 5 6]\n" 2074 | ] 2075 | } 2076 | ], 2077 | "source": [ 2078 | "arr = np.array([[1, 2, 3], [4, 5, 6]])\n", 2079 | "\n", 2080 | "newarr = arr.reshape(-1)\n", 2081 | "\n", 2082 | "print(newarr)" 2083 | ] 2084 | }, 2085 | { 2086 | "cell_type": "markdown", 2087 | "id": "462e6ef7", 2088 | "metadata": {}, 2089 | "source": [ 2090 | "> * Note: There are a lot of functions for changing the shapes of arrays in numpy `flatten`, `ravel` and also for rearranging the elements `rot90`, `flip`, `fliplr`, `flipud` etc. These fall under Intermediate to Advanced section of numpy." 2091 | ] 2092 | }, 2093 | { 2094 | "cell_type": "markdown", 2095 | "id": "912f7fb6", 2096 | "metadata": {}, 2097 | "source": [ 2098 | "# NumPy Array Iterating\n", 2099 | "\n", 2100 | "## Iterating Arrays\n", 2101 | "Iterating means going through elements one by one.\n", 2102 | "\n", 2103 | "As we deal with multi-dimensional arrays in numpy, we can do this using basic `for` loop of python.\n", 2104 | "\n", 2105 | "If we iterate on a 1-D array it will go through each element one by one." 2106 | ] 2107 | }, 2108 | { 2109 | "cell_type": "markdown", 2110 | "id": "5fd51e68", 2111 | "metadata": {}, 2112 | "source": [ 2113 | "## Example\n", 2114 | "Iterate on the elements of the following 1-D array:\n", 2115 | "\n" 2116 | ] 2117 | }, 2118 | { 2119 | "cell_type": "code", 2120 | "execution_count": 50, 2121 | "id": "a8822937", 2122 | "metadata": {}, 2123 | "outputs": [ 2124 | { 2125 | "name": "stdout", 2126 | "output_type": "stream", 2127 | "text": [ 2128 | "1\n", 2129 | "2\n", 2130 | "3\n" 2131 | ] 2132 | } 2133 | ], 2134 | "source": [ 2135 | "arr = np.array([1, 2, 3])\n", 2136 | "\n", 2137 | "for x in arr:\n", 2138 | " print(x)" 2139 | ] 2140 | }, 2141 | { 2142 | "cell_type": "markdown", 2143 | "id": "faf25ade", 2144 | "metadata": {}, 2145 | "source": [ 2146 | "# Iterating 2-D Arrays\n", 2147 | "In a 2-D array it will go through all the rows.\n", 2148 | "\n", 2149 | "## Example\n", 2150 | "Iterate on the elements of the following 2-D array:\n", 2151 | "\n" 2152 | ] 2153 | }, 2154 | { 2155 | "cell_type": "code", 2156 | "execution_count": 51, 2157 | "id": "43871c5c", 2158 | "metadata": {}, 2159 | "outputs": [ 2160 | { 2161 | "name": "stdout", 2162 | "output_type": "stream", 2163 | "text": [ 2164 | "[1 2 3]\n", 2165 | "[4 5 6]\n" 2166 | ] 2167 | } 2168 | ], 2169 | "source": [ 2170 | "arr = np.array([[1, 2, 3], [4, 5, 6]])\n", 2171 | "\n", 2172 | "for x in arr:\n", 2173 | " print(x)" 2174 | ] 2175 | }, 2176 | { 2177 | "cell_type": "markdown", 2178 | "id": "8ba3f308", 2179 | "metadata": {}, 2180 | "source": [ 2181 | "> * If we iterate on a n-D array it will go through n-1th dimension one by one.\n", 2182 | "\n" 2183 | ] 2184 | }, 2185 | { 2186 | "cell_type": "markdown", 2187 | "id": "38d933db", 2188 | "metadata": {}, 2189 | "source": [ 2190 | "**To return the actual values, the scalars, we have to iterate the arrays in each dimension.**" 2191 | ] 2192 | }, 2193 | { 2194 | "cell_type": "markdown", 2195 | "id": "be22e894", 2196 | "metadata": {}, 2197 | "source": [ 2198 | "## Example\n", 2199 | "Iterate on each scalar element of the 2-D array:" 2200 | ] 2201 | }, 2202 | { 2203 | "cell_type": "code", 2204 | "execution_count": 52, 2205 | "id": "6de30529", 2206 | "metadata": {}, 2207 | "outputs": [ 2208 | { 2209 | "name": "stdout", 2210 | "output_type": "stream", 2211 | "text": [ 2212 | "1\n", 2213 | "2\n", 2214 | "3\n", 2215 | "4\n", 2216 | "5\n", 2217 | "6\n" 2218 | ] 2219 | } 2220 | ], 2221 | "source": [ 2222 | "arr = np.array([[1, 2, 3], [4, 5, 6]])\n", 2223 | "\n", 2224 | "for x in arr:\n", 2225 | " for y in x:\n", 2226 | " print(y)" 2227 | ] 2228 | }, 2229 | { 2230 | "cell_type": "markdown", 2231 | "id": "fc1bd88a", 2232 | "metadata": {}, 2233 | "source": [ 2234 | "# Iterating 3-D Arrays\n", 2235 | "In a 3-D array it will go through all the 2-D arrays.\n", 2236 | "\n", 2237 | "## Example\n", 2238 | "Iterate on the elements of the following 3-D array:" 2239 | ] 2240 | }, 2241 | { 2242 | "cell_type": "code", 2243 | "execution_count": 53, 2244 | "id": "f5c6cf5a", 2245 | "metadata": {}, 2246 | "outputs": [ 2247 | { 2248 | "name": "stdout", 2249 | "output_type": "stream", 2250 | "text": [ 2251 | "[[1 2 3]\n", 2252 | " [4 5 6]]\n", 2253 | "[[ 7 8 9]\n", 2254 | " [10 11 12]]\n" 2255 | ] 2256 | } 2257 | ], 2258 | "source": [ 2259 | "arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])\n", 2260 | "\n", 2261 | "for x in arr:\n", 2262 | " print(x)" 2263 | ] 2264 | }, 2265 | { 2266 | "cell_type": "markdown", 2267 | "id": "67716f12", 2268 | "metadata": {}, 2269 | "source": [ 2270 | "**To return the actual values, the scalars, we have to iterate the arrays in each dimension.**" 2271 | ] 2272 | }, 2273 | { 2274 | "cell_type": "markdown", 2275 | "id": "0a74ea81", 2276 | "metadata": {}, 2277 | "source": [ 2278 | "## Example\n", 2279 | "Iterate down to the scalars:" 2280 | ] 2281 | }, 2282 | { 2283 | "cell_type": "code", 2284 | "execution_count": 54, 2285 | "id": "593e8a70", 2286 | "metadata": {}, 2287 | "outputs": [ 2288 | { 2289 | "name": "stdout", 2290 | "output_type": "stream", 2291 | "text": [ 2292 | "1\n", 2293 | "2\n", 2294 | "3\n", 2295 | "4\n", 2296 | "5\n", 2297 | "6\n", 2298 | "7\n", 2299 | "8\n", 2300 | "9\n", 2301 | "10\n", 2302 | "11\n", 2303 | "12\n" 2304 | ] 2305 | } 2306 | ], 2307 | "source": [ 2308 | "arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])\n", 2309 | "\n", 2310 | "for x in arr:\n", 2311 | " for y in x:\n", 2312 | " for z in y:\n", 2313 | " print(z)" 2314 | ] 2315 | }, 2316 | { 2317 | "cell_type": "markdown", 2318 | "id": "982f8d0f", 2319 | "metadata": {}, 2320 | "source": [ 2321 | "# Iterating Arrays Using nditer()\n", 2322 | "The function `nditer()` is a helping function that can be used from very basic to very advanced iterations. It solves some basic issues which we face in iteration, lets go through it with examples.\n", 2323 | "\n", 2324 | "# Iterating on Each Scalar Element\n", 2325 | "In basic `for` loops, iterating through each scalar of an array we need to use n `for` loops which can be difficult to write for arrays with very high dimensionality." 2326 | ] 2327 | }, 2328 | { 2329 | "cell_type": "markdown", 2330 | "id": "c6e3520a", 2331 | "metadata": {}, 2332 | "source": [ 2333 | "## Example\n", 2334 | "Iterate through the following 3-D array:\n", 2335 | "\n" 2336 | ] 2337 | }, 2338 | { 2339 | "cell_type": "code", 2340 | "execution_count": 55, 2341 | "id": "af494d1d", 2342 | "metadata": {}, 2343 | "outputs": [ 2344 | { 2345 | "name": "stdout", 2346 | "output_type": "stream", 2347 | "text": [ 2348 | "1\n", 2349 | "2\n", 2350 | "3\n", 2351 | "4\n", 2352 | "5\n", 2353 | "6\n", 2354 | "7\n", 2355 | "8\n" 2356 | ] 2357 | } 2358 | ], 2359 | "source": [ 2360 | "arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])\n", 2361 | "\n", 2362 | "for x in np.nditer(arr):\n", 2363 | " print(x)" 2364 | ] 2365 | }, 2366 | { 2367 | "cell_type": "markdown", 2368 | "id": "f4dbbf24", 2369 | "metadata": {}, 2370 | "source": [ 2371 | "# Iterating Array With Different Data Types\n", 2372 | "We can use `op_dtypes` argument and pass it the expected datatype to change the datatype of elements while iterating.\n", 2373 | "\n", 2374 | "NumPy does not change the data type of the element in-place (where the element is in array) so it needs some other space to perform this action, that extra space is called buffer, and in order to enable it in `nditer()` we pass `flags=['buffered']`." 2375 | ] 2376 | }, 2377 | { 2378 | "cell_type": "markdown", 2379 | "id": "dfff1690", 2380 | "metadata": {}, 2381 | "source": [ 2382 | "## Example\n", 2383 | "Iterate through the array as a string:\n", 2384 | "\n" 2385 | ] 2386 | }, 2387 | { 2388 | "cell_type": "code", 2389 | "execution_count": 56, 2390 | "id": "9b10e845", 2391 | "metadata": {}, 2392 | "outputs": [ 2393 | { 2394 | "name": "stdout", 2395 | "output_type": "stream", 2396 | "text": [ 2397 | "b'1'\n", 2398 | "b'2'\n", 2399 | "b'3'\n" 2400 | ] 2401 | } 2402 | ], 2403 | "source": [ 2404 | "arr = np.array([1, 2, 3])\n", 2405 | "\n", 2406 | "for x in np.nditer(arr, flags=['buffered'], op_dtypes=['S']):\n", 2407 | " print(x)" 2408 | ] 2409 | }, 2410 | { 2411 | "cell_type": "markdown", 2412 | "id": "11bc6283", 2413 | "metadata": {}, 2414 | "source": [ 2415 | "# Iterating With Different Step Size\n", 2416 | "We can use filtering and followed by iteration.\n", 2417 | "\n", 2418 | "## Example\n", 2419 | "Iterate through every scalar element of the 2D array skipping 1 element:" 2420 | ] 2421 | }, 2422 | { 2423 | "cell_type": "code", 2424 | "execution_count": 57, 2425 | "id": "6e0c53df", 2426 | "metadata": {}, 2427 | "outputs": [ 2428 | { 2429 | "name": "stdout", 2430 | "output_type": "stream", 2431 | "text": [ 2432 | "1\n", 2433 | "3\n", 2434 | "5\n", 2435 | "7\n" 2436 | ] 2437 | } 2438 | ], 2439 | "source": [ 2440 | "arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])\n", 2441 | "\n", 2442 | "for x in np.nditer(arr[:, ::2]):\n", 2443 | " print(x)" 2444 | ] 2445 | }, 2446 | { 2447 | "cell_type": "markdown", 2448 | "id": "6ed07b57", 2449 | "metadata": {}, 2450 | "source": [ 2451 | "# Enumerated Iteration Using ndenumerate()\n", 2452 | "Enumeration means mentioning sequence number of somethings one by one.\n", 2453 | "\n", 2454 | "Sometimes we require corresponding index of the element while iterating, the `ndenumerate()` method can be used for those usecases.\n", 2455 | "\n" 2456 | ] 2457 | }, 2458 | { 2459 | "cell_type": "markdown", 2460 | "id": "dedcbfe9", 2461 | "metadata": {}, 2462 | "source": [ 2463 | "## Example\n", 2464 | "Enumerate on following 1D arrays elements:" 2465 | ] 2466 | }, 2467 | { 2468 | "cell_type": "code", 2469 | "execution_count": 58, 2470 | "id": "0d38442f", 2471 | "metadata": {}, 2472 | "outputs": [ 2473 | { 2474 | "name": "stdout", 2475 | "output_type": "stream", 2476 | "text": [ 2477 | "(0,) 1\n", 2478 | "(1,) 2\n", 2479 | "(2,) 3\n" 2480 | ] 2481 | } 2482 | ], 2483 | "source": [ 2484 | "arr = np.array([1, 2, 3])\n", 2485 | "\n", 2486 | "for idx, x in np.ndenumerate(arr):\n", 2487 | " print(idx, x)" 2488 | ] 2489 | }, 2490 | { 2491 | "cell_type": "markdown", 2492 | "id": "317de83d", 2493 | "metadata": {}, 2494 | "source": [ 2495 | "## Example\n", 2496 | "Enumerate on following 2D array's elements:" 2497 | ] 2498 | }, 2499 | { 2500 | "cell_type": "code", 2501 | "execution_count": 59, 2502 | "id": "fd00b53b", 2503 | "metadata": {}, 2504 | "outputs": [ 2505 | { 2506 | "name": "stdout", 2507 | "output_type": "stream", 2508 | "text": [ 2509 | "(0, 0) 1\n", 2510 | "(0, 1) 2\n", 2511 | "(0, 2) 3\n", 2512 | "(0, 3) 4\n", 2513 | "(1, 0) 5\n", 2514 | "(1, 1) 6\n", 2515 | "(1, 2) 7\n", 2516 | "(1, 3) 8\n" 2517 | ] 2518 | } 2519 | ], 2520 | "source": [ 2521 | "arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])\n", 2522 | "\n", 2523 | "for idx, x in np.ndenumerate(arr):\n", 2524 | " print(idx, x)" 2525 | ] 2526 | }, 2527 | { 2528 | "cell_type": "markdown", 2529 | "id": "4096d466", 2530 | "metadata": {}, 2531 | "source": [ 2532 | "# NumPy Joining Array\n", 2533 | "\n", 2534 | "## Joining NumPy Arrays\n", 2535 | "\n", 2536 | "Joining means putting contents of two or more arrays in a single array.\n", 2537 | "\n", 2538 | "In SQL we join tables based on a key, whereas in NumPy we join arrays by axes.\n", 2539 | "\n", 2540 | "We pass a sequence of arrays that we want to join to the `concatenate()` function, along with the axis. If axis is not explicitly passed, it is taken as 0." 2541 | ] 2542 | }, 2543 | { 2544 | "cell_type": "markdown", 2545 | "id": "726d6fbd", 2546 | "metadata": {}, 2547 | "source": [ 2548 | "## Example\n", 2549 | "Join two arrays" 2550 | ] 2551 | }, 2552 | { 2553 | "cell_type": "code", 2554 | "execution_count": 60, 2555 | "id": "58dc3a14", 2556 | "metadata": {}, 2557 | "outputs": [ 2558 | { 2559 | "name": "stdout", 2560 | "output_type": "stream", 2561 | "text": [ 2562 | "[1 2 3 4 5 6]\n" 2563 | ] 2564 | } 2565 | ], 2566 | "source": [ 2567 | "arr1 = np.array([1, 2, 3])\n", 2568 | "\n", 2569 | "arr2 = np.array([4, 5, 6])\n", 2570 | "\n", 2571 | "arr = np.concatenate((arr1, arr2))\n", 2572 | "\n", 2573 | "print(arr)" 2574 | ] 2575 | }, 2576 | { 2577 | "cell_type": "markdown", 2578 | "id": "1fb48dff", 2579 | "metadata": {}, 2580 | "source": [ 2581 | "## Example\n", 2582 | "Join two 2-D arrays along rows (axis=1):" 2583 | ] 2584 | }, 2585 | { 2586 | "cell_type": "code", 2587 | "execution_count": 61, 2588 | "id": "78299574", 2589 | "metadata": {}, 2590 | "outputs": [ 2591 | { 2592 | "name": "stdout", 2593 | "output_type": "stream", 2594 | "text": [ 2595 | "[[1 2 5 6]\n", 2596 | " [3 4 7 8]]\n" 2597 | ] 2598 | } 2599 | ], 2600 | "source": [ 2601 | "arr1 = np.array([[1, 2], [3, 4]])\n", 2602 | "\n", 2603 | "arr2 = np.array([[5, 6], [7, 8]])\n", 2604 | "\n", 2605 | "arr = np.concatenate((arr1, arr2), axis=1)\n", 2606 | "\n", 2607 | "print(arr)" 2608 | ] 2609 | }, 2610 | { 2611 | "cell_type": "markdown", 2612 | "id": "5d09d802", 2613 | "metadata": {}, 2614 | "source": [ 2615 | "# Joining Arrays Using Stack Functions\n", 2616 | "Stacking is same as concatenation, the only difference is that stacking is done along a new axis.\n", 2617 | "\n", 2618 | "We can concatenate two 1-D arrays along the second axis which would result in putting them one over the other, ie. stacking.\n", 2619 | "\n", 2620 | "We pass a sequence of arrays that we want to join to the `stack()` method along with the axis. If axis is not explicitly passed it is taken as 0.\n", 2621 | "\n" 2622 | ] 2623 | }, 2624 | { 2625 | "cell_type": "markdown", 2626 | "id": "756d2c3d", 2627 | "metadata": {}, 2628 | "source": [ 2629 | "# Example" 2630 | ] 2631 | }, 2632 | { 2633 | "cell_type": "code", 2634 | "execution_count": 62, 2635 | "id": "aefaecf1", 2636 | "metadata": {}, 2637 | "outputs": [ 2638 | { 2639 | "name": "stdout", 2640 | "output_type": "stream", 2641 | "text": [ 2642 | "[[1 4]\n", 2643 | " [2 5]\n", 2644 | " [3 6]]\n" 2645 | ] 2646 | } 2647 | ], 2648 | "source": [ 2649 | "arr1 = np.array([1, 2, 3])\n", 2650 | "\n", 2651 | "arr2 = np.array([4, 5, 6])\n", 2652 | "\n", 2653 | "arr = np.stack((arr1, arr2), axis=1)\n", 2654 | "\n", 2655 | "print(arr)" 2656 | ] 2657 | }, 2658 | { 2659 | "cell_type": "markdown", 2660 | "id": "2adb58a8", 2661 | "metadata": {}, 2662 | "source": [ 2663 | "# Stacking Along Rows\n", 2664 | "NumPy provides a helper function: `stack()` to stack along rows.\n", 2665 | "\n", 2666 | "## Example\n" 2667 | ] 2668 | }, 2669 | { 2670 | "cell_type": "code", 2671 | "execution_count": 63, 2672 | "id": "eea00eb6", 2673 | "metadata": {}, 2674 | "outputs": [ 2675 | { 2676 | "name": "stdout", 2677 | "output_type": "stream", 2678 | "text": [ 2679 | "[1 2 3 4 5 6]\n" 2680 | ] 2681 | } 2682 | ], 2683 | "source": [ 2684 | "arr1 = np.array([1, 2, 3])\n", 2685 | "\n", 2686 | "arr2 = np.array([4, 5, 6])\n", 2687 | "\n", 2688 | "arr = np.hstack((arr1, arr2))\n", 2689 | "\n", 2690 | "print(arr)\n" 2691 | ] 2692 | }, 2693 | { 2694 | "cell_type": "markdown", 2695 | "id": "0faef1d0", 2696 | "metadata": {}, 2697 | "source": [ 2698 | "# Stacking Along Columns\n", 2699 | "NumPy provides a helper function: `vstack()` to stack along columns.\n", 2700 | "\n", 2701 | "## Example" 2702 | ] 2703 | }, 2704 | { 2705 | "cell_type": "code", 2706 | "execution_count": 64, 2707 | "id": "f6d5afe6", 2708 | "metadata": {}, 2709 | "outputs": [ 2710 | { 2711 | "name": "stdout", 2712 | "output_type": "stream", 2713 | "text": [ 2714 | "[[1 2 3]\n", 2715 | " [4 5 6]]\n" 2716 | ] 2717 | } 2718 | ], 2719 | "source": [ 2720 | "arr1 = np.array([1, 2, 3])\n", 2721 | "\n", 2722 | "arr2 = np.array([4, 5, 6])\n", 2723 | "\n", 2724 | "arr = np.vstack((arr1, arr2))\n", 2725 | "\n", 2726 | "print(arr)" 2727 | ] 2728 | }, 2729 | { 2730 | "cell_type": "markdown", 2731 | "id": "40407797", 2732 | "metadata": {}, 2733 | "source": [ 2734 | "# Stacking Along Height (depth)\n", 2735 | "NumPy provides a helper function: `dstack()` to stack along height, which is the same as depth.\n", 2736 | "\n", 2737 | "## Example" 2738 | ] 2739 | }, 2740 | { 2741 | "cell_type": "code", 2742 | "execution_count": 65, 2743 | "id": "c51f919f", 2744 | "metadata": {}, 2745 | "outputs": [ 2746 | { 2747 | "name": "stdout", 2748 | "output_type": "stream", 2749 | "text": [ 2750 | "[[[1 4]\n", 2751 | " [2 5]\n", 2752 | " [3 6]]]\n" 2753 | ] 2754 | } 2755 | ], 2756 | "source": [ 2757 | "arr1 = np.array([1, 2, 3])\n", 2758 | "\n", 2759 | "arr2 = np.array([4, 5, 6])\n", 2760 | "\n", 2761 | "arr = np.dstack((arr1, arr2))\n", 2762 | "\n", 2763 | "print(arr)" 2764 | ] 2765 | }, 2766 | { 2767 | "cell_type": "markdown", 2768 | "id": "0254363f", 2769 | "metadata": {}, 2770 | "source": [ 2771 | "# NumPy Splitting Array\n", 2772 | "\n", 2773 | "## Splitting NumPy Arrays\n", 2774 | "\n", 2775 | "Splitting is reverse operation of Joining.\n", 2776 | "\n", 2777 | "Joining merges multiple arrays into one and Splitting breaks one array into multiple.\n", 2778 | "\n", 2779 | "We use `array_split()` for splitting arrays, we pass it the array we want to split and the number of splits." 2780 | ] 2781 | }, 2782 | { 2783 | "cell_type": "markdown", 2784 | "id": "15861b48", 2785 | "metadata": {}, 2786 | "source": [ 2787 | "## Example\n", 2788 | "Split the array in 3 parts:" 2789 | ] 2790 | }, 2791 | { 2792 | "cell_type": "code", 2793 | "execution_count": 66, 2794 | "id": "48d56bac", 2795 | "metadata": {}, 2796 | "outputs": [ 2797 | { 2798 | "name": "stdout", 2799 | "output_type": "stream", 2800 | "text": [ 2801 | "[array([1, 2]), array([3, 4]), array([5, 6])]\n" 2802 | ] 2803 | } 2804 | ], 2805 | "source": [ 2806 | "arr = np.array([1, 2, 3, 4, 5, 6])\n", 2807 | "\n", 2808 | "newarr = np.array_split(arr, 3)\n", 2809 | "\n", 2810 | "print(newarr)" 2811 | ] 2812 | }, 2813 | { 2814 | "cell_type": "markdown", 2815 | "id": "f01a4bf8", 2816 | "metadata": {}, 2817 | "source": [ 2818 | "> * Note: The return value is an array containing three arrays.\n", 2819 | "\n" 2820 | ] 2821 | }, 2822 | { 2823 | "cell_type": "markdown", 2824 | "id": "a2ae18c2", 2825 | "metadata": {}, 2826 | "source": [ 2827 | "**If the array has less elements than required, it will adjust from the end accordingly.**" 2828 | ] 2829 | }, 2830 | { 2831 | "cell_type": "markdown", 2832 | "id": "5024eec3", 2833 | "metadata": {}, 2834 | "source": [ 2835 | "## Example\n", 2836 | "Split the array in 4 parts:\n", 2837 | "\n" 2838 | ] 2839 | }, 2840 | { 2841 | "cell_type": "code", 2842 | "execution_count": 67, 2843 | "id": "fa456218", 2844 | "metadata": {}, 2845 | "outputs": [ 2846 | { 2847 | "name": "stdout", 2848 | "output_type": "stream", 2849 | "text": [ 2850 | "[array([1, 2]), array([3, 4]), array([5]), array([6])]\n" 2851 | ] 2852 | } 2853 | ], 2854 | "source": [ 2855 | "arr = np.array([1, 2, 3, 4, 5, 6])\n", 2856 | "\n", 2857 | "newarr = np.array_split(arr, 4)\n", 2858 | "\n", 2859 | "print(newarr)" 2860 | ] 2861 | }, 2862 | { 2863 | "cell_type": "markdown", 2864 | "id": "9fac4f21", 2865 | "metadata": {}, 2866 | "source": [ 2867 | "> * Note: We also have the method `split()` available but it will not adjust the elements when elements are less in source array for splitting like in example above, `array_split()` worked properly but `split()` would fail." 2868 | ] 2869 | }, 2870 | { 2871 | "cell_type": "markdown", 2872 | "id": "5453407c", 2873 | "metadata": {}, 2874 | "source": [ 2875 | "# Split Into Arrays\n", 2876 | "The return value of the `array_split()` method is an array containing each of the split as an array.\n", 2877 | "\n", 2878 | "If you split an array into 3 arrays, you can access them from the result just like any array element:\n", 2879 | "\n" 2880 | ] 2881 | }, 2882 | { 2883 | "cell_type": "markdown", 2884 | "id": "bd303d19", 2885 | "metadata": {}, 2886 | "source": [ 2887 | "# Example\n", 2888 | "Access the splitted arrays:" 2889 | ] 2890 | }, 2891 | { 2892 | "cell_type": "code", 2893 | "execution_count": 68, 2894 | "id": "22f49bfd", 2895 | "metadata": {}, 2896 | "outputs": [ 2897 | { 2898 | "name": "stdout", 2899 | "output_type": "stream", 2900 | "text": [ 2901 | "[1 2]\n", 2902 | "[3 4]\n", 2903 | "[5 6]\n" 2904 | ] 2905 | } 2906 | ], 2907 | "source": [ 2908 | "arr = np.array([1, 2, 3, 4, 5, 6])\n", 2909 | "\n", 2910 | "newarr = np.array_split(arr, 3)\n", 2911 | "\n", 2912 | "print(newarr[0])\n", 2913 | "print(newarr[1])\n", 2914 | "print(newarr[2])" 2915 | ] 2916 | }, 2917 | { 2918 | "cell_type": "markdown", 2919 | "id": "2d6cbd46", 2920 | "metadata": {}, 2921 | "source": [ 2922 | "# Splitting 2-D Arrays\n", 2923 | "Use the same syntax when splitting 2-D arrays.\n", 2924 | "\n", 2925 | "Use the `array_split()` method, pass in the array you want to split and the number of splits you want to do.\n", 2926 | "\n", 2927 | "## Example\n", 2928 | "Split the 2-D array into three 2-D arrays." 2929 | ] 2930 | }, 2931 | { 2932 | "cell_type": "code", 2933 | "execution_count": 69, 2934 | "id": "c48f2588", 2935 | "metadata": {}, 2936 | "outputs": [ 2937 | { 2938 | "name": "stdout", 2939 | "output_type": "stream", 2940 | "text": [ 2941 | "[array([[1, 2],\n", 2942 | " [3, 4]]), array([[5, 6],\n", 2943 | " [7, 8]]), array([[ 9, 10],\n", 2944 | " [11, 12]])]\n" 2945 | ] 2946 | } 2947 | ], 2948 | "source": [ 2949 | "arr = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]])\n", 2950 | "\n", 2951 | "newarr = np.array_split(arr, 3)\n", 2952 | "\n", 2953 | "print(newarr)" 2954 | ] 2955 | }, 2956 | { 2957 | "cell_type": "markdown", 2958 | "id": "56102f43", 2959 | "metadata": {}, 2960 | "source": [ 2961 | "**The example above returns three 2-D arrays.**\n", 2962 | "\n", 2963 | "**Let's look at another example, this time each element in the 2-D arrays contains 3 elements.**\n", 2964 | "\n" 2965 | ] 2966 | }, 2967 | { 2968 | "cell_type": "markdown", 2969 | "id": "2762f6d8", 2970 | "metadata": {}, 2971 | "source": [ 2972 | "# Example\n", 2973 | "Split the 2-D array into three 2-D arrays.\n", 2974 | "\n" 2975 | ] 2976 | }, 2977 | { 2978 | "cell_type": "code", 2979 | "execution_count": 70, 2980 | "id": "92d2a027", 2981 | "metadata": {}, 2982 | "outputs": [ 2983 | { 2984 | "name": "stdout", 2985 | "output_type": "stream", 2986 | "text": [ 2987 | "[array([[1, 2, 3],\n", 2988 | " [4, 5, 6]]), array([[ 7, 8, 9],\n", 2989 | " [10, 11, 12]]), array([[13, 14, 15],\n", 2990 | " [16, 17, 18]])]\n" 2991 | ] 2992 | } 2993 | ], 2994 | "source": [ 2995 | "arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]])\n", 2996 | "\n", 2997 | "newarr = np.array_split(arr, 3)\n", 2998 | "\n", 2999 | "print(newarr)" 3000 | ] 3001 | }, 3002 | { 3003 | "cell_type": "markdown", 3004 | "id": "250c2261", 3005 | "metadata": {}, 3006 | "source": [ 3007 | "**The example above returns three 2-D arrays.**\n", 3008 | "\n", 3009 | "**In addition, you can specify which axis you want to do the split around.**\n", 3010 | "\n", 3011 | "**The example below also returns three 2-D arrays, but they are split along the row (axis=1).**" 3012 | ] 3013 | }, 3014 | { 3015 | "cell_type": "markdown", 3016 | "id": "d4fbdd6a", 3017 | "metadata": {}, 3018 | "source": [ 3019 | "## Example\n", 3020 | "Split the 2-D array into three 2-D arrays along rows.\n", 3021 | "\n" 3022 | ] 3023 | }, 3024 | { 3025 | "cell_type": "code", 3026 | "execution_count": 71, 3027 | "id": "aded6493", 3028 | "metadata": {}, 3029 | "outputs": [ 3030 | { 3031 | "name": "stdout", 3032 | "output_type": "stream", 3033 | "text": [ 3034 | "[array([[ 1],\n", 3035 | " [ 4],\n", 3036 | " [ 7],\n", 3037 | " [10],\n", 3038 | " [13],\n", 3039 | " [16]]), array([[ 2],\n", 3040 | " [ 5],\n", 3041 | " [ 8],\n", 3042 | " [11],\n", 3043 | " [14],\n", 3044 | " [17]]), array([[ 3],\n", 3045 | " [ 6],\n", 3046 | " [ 9],\n", 3047 | " [12],\n", 3048 | " [15],\n", 3049 | " [18]])]\n" 3050 | ] 3051 | } 3052 | ], 3053 | "source": [ 3054 | "arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]])\n", 3055 | "\n", 3056 | "newarr = np.array_split(arr, 3, axis=1)\n", 3057 | "\n", 3058 | "print(newarr)" 3059 | ] 3060 | }, 3061 | { 3062 | "cell_type": "markdown", 3063 | "id": "453fd1a6", 3064 | "metadata": {}, 3065 | "source": [ 3066 | "An alternate solution is using `hsplit()` opposite of `hstack()`\n", 3067 | "\n" 3068 | ] 3069 | }, 3070 | { 3071 | "cell_type": "markdown", 3072 | "id": "3e0bd1ab", 3073 | "metadata": {}, 3074 | "source": [ 3075 | "## Example\n", 3076 | "Use the `hsplit()` method to split the 2-D array into three 2-D arrays along rows." 3077 | ] 3078 | }, 3079 | { 3080 | "cell_type": "code", 3081 | "execution_count": 72, 3082 | "id": "0e04b7df", 3083 | "metadata": {}, 3084 | "outputs": [ 3085 | { 3086 | "name": "stdout", 3087 | "output_type": "stream", 3088 | "text": [ 3089 | "[array([[ 1],\n", 3090 | " [ 4],\n", 3091 | " [ 7],\n", 3092 | " [10],\n", 3093 | " [13],\n", 3094 | " [16]]), array([[ 2],\n", 3095 | " [ 5],\n", 3096 | " [ 8],\n", 3097 | " [11],\n", 3098 | " [14],\n", 3099 | " [17]]), array([[ 3],\n", 3100 | " [ 6],\n", 3101 | " [ 9],\n", 3102 | " [12],\n", 3103 | " [15],\n", 3104 | " [18]])]\n" 3105 | ] 3106 | } 3107 | ], 3108 | "source": [ 3109 | "arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]])\n", 3110 | "\n", 3111 | "newarr = np.hsplit(arr, 3)\n", 3112 | "\n", 3113 | "print(newarr)" 3114 | ] 3115 | }, 3116 | { 3117 | "cell_type": "markdown", 3118 | "id": "3b5452b6", 3119 | "metadata": {}, 3120 | "source": [ 3121 | "> * Note: Similar alternates to `vstack()` and `dstack()` are available as `vsplit()` and `dsplit()`." 3122 | ] 3123 | }, 3124 | { 3125 | "cell_type": "markdown", 3126 | "id": "450cdea9", 3127 | "metadata": {}, 3128 | "source": [ 3129 | "# NumPy Searching Arrays\n", 3130 | "\n", 3131 | "## Searching Arrays\n", 3132 | "\n", 3133 | "You can search an array for a certain value, and return the indexes that get a match.\n", 3134 | "\n", 3135 | "To search an array, use the `where()` method" 3136 | ] 3137 | }, 3138 | { 3139 | "cell_type": "markdown", 3140 | "id": "27a67c9d", 3141 | "metadata": {}, 3142 | "source": [ 3143 | "## Example\n", 3144 | "Find the indexes where the value is 4:\n", 3145 | "\n" 3146 | ] 3147 | }, 3148 | { 3149 | "cell_type": "code", 3150 | "execution_count": 73, 3151 | "id": "61769517", 3152 | "metadata": {}, 3153 | "outputs": [ 3154 | { 3155 | "name": "stdout", 3156 | "output_type": "stream", 3157 | "text": [ 3158 | "(array([3, 5, 6]),)\n" 3159 | ] 3160 | } 3161 | ], 3162 | "source": [ 3163 | "arr = np.array([1, 2, 3, 4, 5, 4, 4])\n", 3164 | "\n", 3165 | "x = np.where(arr == 4)\n", 3166 | "\n", 3167 | "print(x)" 3168 | ] 3169 | }, 3170 | { 3171 | "cell_type": "markdown", 3172 | "id": "f3c1a009", 3173 | "metadata": {}, 3174 | "source": [ 3175 | "The example above will return a tuple: `(array([3, 5, 6],)`\n", 3176 | "\n", 3177 | "Which means that the value 4 is present at index 3, 5, and 6.\n", 3178 | "\n", 3179 | "## Example\n", 3180 | "Find the indexes where the values are even:\n" 3181 | ] 3182 | }, 3183 | { 3184 | "cell_type": "code", 3185 | "execution_count": 74, 3186 | "id": "b2b6f93c", 3187 | "metadata": {}, 3188 | "outputs": [ 3189 | { 3190 | "name": "stdout", 3191 | "output_type": "stream", 3192 | "text": [ 3193 | "(array([1, 3, 5, 7]),)\n" 3194 | ] 3195 | } 3196 | ], 3197 | "source": [ 3198 | "arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])\n", 3199 | "\n", 3200 | "x = np.where(arr%2 == 0)\n", 3201 | "\n", 3202 | "print(x)" 3203 | ] 3204 | }, 3205 | { 3206 | "cell_type": "markdown", 3207 | "id": "d1d40308", 3208 | "metadata": {}, 3209 | "source": [ 3210 | "## Example\n", 3211 | "Find the indexes where the values are odd:" 3212 | ] 3213 | }, 3214 | { 3215 | "cell_type": "code", 3216 | "execution_count": 75, 3217 | "id": "1a012dbb", 3218 | "metadata": {}, 3219 | "outputs": [ 3220 | { 3221 | "name": "stdout", 3222 | "output_type": "stream", 3223 | "text": [ 3224 | "(array([0, 2, 4, 6]),)\n" 3225 | ] 3226 | } 3227 | ], 3228 | "source": [ 3229 | "arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])\n", 3230 | "\n", 3231 | "x = np.where(arr%2 == 1)\n", 3232 | "\n", 3233 | "print(x)" 3234 | ] 3235 | }, 3236 | { 3237 | "cell_type": "markdown", 3238 | "id": "9a5537c0", 3239 | "metadata": {}, 3240 | "source": [ 3241 | "# Search Sorted\n", 3242 | "There is a method called `searchsorted()` which performs a binary search in the array, and returns the index where the specified value would be inserted to maintain the search order." 3243 | ] 3244 | }, 3245 | { 3246 | "cell_type": "markdown", 3247 | "id": "b1c9ca8c", 3248 | "metadata": {}, 3249 | "source": [ 3250 | "**The `searchsorted()` method is assumed to be used on sorted arrays.**\n", 3251 | "\n" 3252 | ] 3253 | }, 3254 | { 3255 | "cell_type": "markdown", 3256 | "id": "40644f83", 3257 | "metadata": {}, 3258 | "source": [ 3259 | "## Example\n", 3260 | "Find the indexes where the value 7 should be inserted:" 3261 | ] 3262 | }, 3263 | { 3264 | "cell_type": "code", 3265 | "execution_count": 76, 3266 | "id": "d95f0869", 3267 | "metadata": {}, 3268 | "outputs": [ 3269 | { 3270 | "name": "stdout", 3271 | "output_type": "stream", 3272 | "text": [ 3273 | "1\n" 3274 | ] 3275 | } 3276 | ], 3277 | "source": [ 3278 | "arr = np.array([6, 7, 8, 9])\n", 3279 | "\n", 3280 | "x = np.searchsorted(arr, 7)\n", 3281 | "\n", 3282 | "print(x)" 3283 | ] 3284 | }, 3285 | { 3286 | "cell_type": "markdown", 3287 | "id": "b8af20a0", 3288 | "metadata": {}, 3289 | "source": [ 3290 | "Example explained: The number 7 should be inserted on index 1 to remain the sort order.\n", 3291 | "\n", 3292 | "The method starts the search from the left and returns the first index where the number 7 is no longer larger than the next value.\n", 3293 | "\n" 3294 | ] 3295 | }, 3296 | { 3297 | "cell_type": "markdown", 3298 | "id": "b5f09237", 3299 | "metadata": {}, 3300 | "source": [ 3301 | "# Search From the Right Side\n", 3302 | "By default the left most index is returned, but we can give `side='right'` to return the right most index instead.\n", 3303 | "\n", 3304 | "## Example\n", 3305 | "Find the indexes where the value 7 should be inserted, starting from the right:\n", 3306 | "\n" 3307 | ] 3308 | }, 3309 | { 3310 | "cell_type": "code", 3311 | "execution_count": 77, 3312 | "id": "7555c0ad", 3313 | "metadata": {}, 3314 | "outputs": [ 3315 | { 3316 | "name": "stdout", 3317 | "output_type": "stream", 3318 | "text": [ 3319 | "2\n" 3320 | ] 3321 | } 3322 | ], 3323 | "source": [ 3324 | "arr = np.array([6, 7, 8, 9])\n", 3325 | "\n", 3326 | "x = np.searchsorted(arr, 7, side='right')\n", 3327 | "\n", 3328 | "print(x)" 3329 | ] 3330 | }, 3331 | { 3332 | "cell_type": "markdown", 3333 | "id": "a5257abe", 3334 | "metadata": {}, 3335 | "source": [ 3336 | "Example explained: The number 7 should be inserted on index 2 to remain the sort order.\n", 3337 | "\n", 3338 | "The method starts the search from the right and returns the first index where the number 7 is no longer less than the next value.\n", 3339 | "\n" 3340 | ] 3341 | }, 3342 | { 3343 | "cell_type": "markdown", 3344 | "id": "d45b5aad", 3345 | "metadata": {}, 3346 | "source": [ 3347 | "## Multiple Values\n", 3348 | "To search for more than one value, use an array with the specified values.\n", 3349 | "\n", 3350 | "## Example\n", 3351 | "Find the indexes where the values 2, 4, and 6 should be inserted:" 3352 | ] 3353 | }, 3354 | { 3355 | "cell_type": "code", 3356 | "execution_count": 78, 3357 | "id": "e59be1ba", 3358 | "metadata": {}, 3359 | "outputs": [ 3360 | { 3361 | "name": "stdout", 3362 | "output_type": "stream", 3363 | "text": [ 3364 | "[1 2 3]\n" 3365 | ] 3366 | } 3367 | ], 3368 | "source": [ 3369 | "arr = np.array([1, 3, 5, 7])\n", 3370 | "\n", 3371 | "x = np.searchsorted(arr, [2, 4, 6])\n", 3372 | "\n", 3373 | "print(x)" 3374 | ] 3375 | }, 3376 | { 3377 | "cell_type": "markdown", 3378 | "id": "b41fa08c", 3379 | "metadata": {}, 3380 | "source": [ 3381 | "The return value is an array: `[1 2 3]` containing the three indexes where 2, 4, 6 would be inserted in the original array to maintain the order.\n", 3382 | "\n" 3383 | ] 3384 | }, 3385 | { 3386 | "cell_type": "markdown", 3387 | "id": "7e09f0aa", 3388 | "metadata": {}, 3389 | "source": [ 3390 | "# NumPy Sorting Arrays\n", 3391 | "\n", 3392 | "## Sorting Arrays\n", 3393 | "Sorting means putting elements in an ordered sequence.\n", 3394 | "\n", 3395 | "Ordered sequence is any sequence that has an order corresponding to elements, like numeric or alphabetical, ascending or descending.\n", 3396 | "\n", 3397 | "The NumPy ndarray object has a function called `sort()`, that will sort a specified array.\n", 3398 | "\n", 3399 | "## Example\n", 3400 | "Sort the array:" 3401 | ] 3402 | }, 3403 | { 3404 | "cell_type": "code", 3405 | "execution_count": 79, 3406 | "id": "b33845bd", 3407 | "metadata": {}, 3408 | "outputs": [ 3409 | { 3410 | "name": "stdout", 3411 | "output_type": "stream", 3412 | "text": [ 3413 | "[0 1 2 3]\n" 3414 | ] 3415 | } 3416 | ], 3417 | "source": [ 3418 | "arr = np.array([3, 2, 0, 1])\n", 3419 | "\n", 3420 | "print(np.sort(arr))" 3421 | ] 3422 | }, 3423 | { 3424 | "cell_type": "markdown", 3425 | "id": "4561908b", 3426 | "metadata": {}, 3427 | "source": [ 3428 | "> * Note: This method returns a copy of the array, leaving the original array unchanged.\n", 3429 | "\n" 3430 | ] 3431 | }, 3432 | { 3433 | "cell_type": "markdown", 3434 | "id": "48f501b8", 3435 | "metadata": {}, 3436 | "source": [ 3437 | "**You can also sort arrays of strings, or any other data type:**\n", 3438 | "\n", 3439 | "## Example\n", 3440 | "Sort the array alphabetically:\n", 3441 | "\n" 3442 | ] 3443 | }, 3444 | { 3445 | "cell_type": "code", 3446 | "execution_count": 80, 3447 | "id": "d9b76aa8", 3448 | "metadata": {}, 3449 | "outputs": [ 3450 | { 3451 | "name": "stdout", 3452 | "output_type": "stream", 3453 | "text": [ 3454 | "['apple' 'banana' 'cherry']\n" 3455 | ] 3456 | } 3457 | ], 3458 | "source": [ 3459 | "arr = np.array(['banana', 'cherry', 'apple'])\n", 3460 | "\n", 3461 | "print(np.sort(arr))" 3462 | ] 3463 | }, 3464 | { 3465 | "cell_type": "markdown", 3466 | "id": "d27008f8", 3467 | "metadata": {}, 3468 | "source": [ 3469 | "## Example\n", 3470 | "Sort a boolean array:" 3471 | ] 3472 | }, 3473 | { 3474 | "cell_type": "code", 3475 | "execution_count": 81, 3476 | "id": "305e2907", 3477 | "metadata": {}, 3478 | "outputs": [ 3479 | { 3480 | "name": "stdout", 3481 | "output_type": "stream", 3482 | "text": [ 3483 | "[False True True]\n" 3484 | ] 3485 | } 3486 | ], 3487 | "source": [ 3488 | "arr = np.array([True, False, True])\n", 3489 | "\n", 3490 | "print(np.sort(arr))" 3491 | ] 3492 | }, 3493 | { 3494 | "cell_type": "markdown", 3495 | "id": "200a3ec2", 3496 | "metadata": {}, 3497 | "source": [ 3498 | "# Sorting a 2-D Array\n", 3499 | "If you use the `sort()` method on a 2-D array, both arrays will be sorted:\n", 3500 | "\n", 3501 | "## Example\n", 3502 | "Sort a 2-D array:" 3503 | ] 3504 | }, 3505 | { 3506 | "cell_type": "code", 3507 | "execution_count": 82, 3508 | "id": "ab68b2f3", 3509 | "metadata": {}, 3510 | "outputs": [ 3511 | { 3512 | "name": "stdout", 3513 | "output_type": "stream", 3514 | "text": [ 3515 | "[[2 3 4]\n", 3516 | " [0 1 5]]\n" 3517 | ] 3518 | } 3519 | ], 3520 | "source": [ 3521 | "arr = np.array([[3, 2, 4], [5, 0, 1]])\n", 3522 | "\n", 3523 | "print(np.sort(arr))" 3524 | ] 3525 | }, 3526 | { 3527 | "cell_type": "markdown", 3528 | "id": "74d967e7", 3529 | "metadata": {}, 3530 | "source": [ 3531 | "# NumPy Filter Array\n", 3532 | "## Filtering Arrays\n", 3533 | "Getting some elements out of an existing array and creating a new array out of them is called filtering.\n", 3534 | "\n", 3535 | "In NumPy, you filter an array using a boolean index list." 3536 | ] 3537 | }, 3538 | { 3539 | "cell_type": "markdown", 3540 | "id": "3c5fa3b2", 3541 | "metadata": {}, 3542 | "source": [ 3543 | "> * A boolean index list is a list of booleans corresponding to indexes in the array.\n", 3544 | "\n" 3545 | ] 3546 | }, 3547 | { 3548 | "cell_type": "markdown", 3549 | "id": "7995d225", 3550 | "metadata": {}, 3551 | "source": [ 3552 | "If the value at an index is True that element is contained in the filtered array, if the value at that index is False that element is excluded from the filtered array." 3553 | ] 3554 | }, 3555 | { 3556 | "cell_type": "markdown", 3557 | "id": "08ec45b7", 3558 | "metadata": {}, 3559 | "source": [ 3560 | "## Example\n", 3561 | "Create an array from the elements on index 0 and 2:" 3562 | ] 3563 | }, 3564 | { 3565 | "cell_type": "code", 3566 | "execution_count": 83, 3567 | "id": "b78ba075", 3568 | "metadata": {}, 3569 | "outputs": [ 3570 | { 3571 | "name": "stdout", 3572 | "output_type": "stream", 3573 | "text": [ 3574 | "[41 43]\n" 3575 | ] 3576 | } 3577 | ], 3578 | "source": [ 3579 | "arr = np.array([41, 42, 43, 44])\n", 3580 | "\n", 3581 | "x = [True, False, True, False]\n", 3582 | "\n", 3583 | "newarr = arr[x]\n", 3584 | "\n", 3585 | "print(newarr)" 3586 | ] 3587 | }, 3588 | { 3589 | "cell_type": "markdown", 3590 | "id": "2d8491cf", 3591 | "metadata": {}, 3592 | "source": [ 3593 | "The example above will return `[41, 43]`, why?\n", 3594 | "\n", 3595 | "Because the new array contains only the values where the filter array had the value `True`, in this case, index 0 and 2." 3596 | ] 3597 | }, 3598 | { 3599 | "cell_type": "markdown", 3600 | "id": "4156a989", 3601 | "metadata": {}, 3602 | "source": [ 3603 | "# Creating the Filter Array\n", 3604 | "In the example above we hard-coded the `True` and `False` values, but the common use is to create a filter array based on conditions.\n", 3605 | "\n" 3606 | ] 3607 | }, 3608 | { 3609 | "cell_type": "markdown", 3610 | "id": "1e774ad2", 3611 | "metadata": {}, 3612 | "source": [ 3613 | "# Example\n", 3614 | "Create a filter array that will return only values higher than 42:\n", 3615 | "\n" 3616 | ] 3617 | }, 3618 | { 3619 | "cell_type": "code", 3620 | "execution_count": 84, 3621 | "id": "cde02adc", 3622 | "metadata": {}, 3623 | "outputs": [ 3624 | { 3625 | "name": "stdout", 3626 | "output_type": "stream", 3627 | "text": [ 3628 | "[False, False, True, True]\n", 3629 | "[43 44]\n" 3630 | ] 3631 | } 3632 | ], 3633 | "source": [ 3634 | "arr = np.array([41, 42, 43, 44])\n", 3635 | "\n", 3636 | "# Create an empty list\n", 3637 | "filter_arr = []\n", 3638 | "\n", 3639 | "# go through each element in arr\n", 3640 | "for element in arr:\n", 3641 | " # if the element is higher than 42, set the value to True, otherwise False:\n", 3642 | " if element > 42:\n", 3643 | " filter_arr.append(True)\n", 3644 | " else:\n", 3645 | " filter_arr.append(False)\n", 3646 | "\n", 3647 | "newarr = arr[filter_arr]\n", 3648 | "\n", 3649 | "print(filter_arr)\n", 3650 | "print(newarr)" 3651 | ] 3652 | }, 3653 | { 3654 | "cell_type": "markdown", 3655 | "id": "b96d64a5", 3656 | "metadata": {}, 3657 | "source": [ 3658 | "## Example\n", 3659 | "Create a filter array that will return only even elements from the original array:" 3660 | ] 3661 | }, 3662 | { 3663 | "cell_type": "code", 3664 | "execution_count": 85, 3665 | "id": "64527ebb", 3666 | "metadata": {}, 3667 | "outputs": [ 3668 | { 3669 | "name": "stdout", 3670 | "output_type": "stream", 3671 | "text": [ 3672 | "[False, True, False, True, False, True, False]\n", 3673 | "[2 4 6]\n" 3674 | ] 3675 | } 3676 | ], 3677 | "source": [ 3678 | "arr = np.array([1, 2, 3, 4, 5, 6, 7])\n", 3679 | "\n", 3680 | "# Create an empty list\n", 3681 | "filter_arr = []\n", 3682 | "\n", 3683 | "# go through each element in arr\n", 3684 | "for element in arr:\n", 3685 | " # if the element is completely divisble by 2, set the value to True, otherwise False\n", 3686 | " if element % 2 == 0:\n", 3687 | " filter_arr.append(True)\n", 3688 | " else:\n", 3689 | " filter_arr.append(False)\n", 3690 | "\n", 3691 | "newarr = arr[filter_arr]\n", 3692 | "\n", 3693 | "print(filter_arr)\n", 3694 | "print(newarr)" 3695 | ] 3696 | }, 3697 | { 3698 | "cell_type": "markdown", 3699 | "id": "43d92e2a", 3700 | "metadata": {}, 3701 | "source": [ 3702 | "# Creating Filter Directly From Array\n", 3703 | "The above example is quite a common task in NumPy and NumPy provides a nice way to tackle it.\n", 3704 | "\n", 3705 | "We can directly substitute the array instead of the iterable variable in our condition and it will work just as we expect it to." 3706 | ] 3707 | }, 3708 | { 3709 | "cell_type": "markdown", 3710 | "id": "e58ae88f", 3711 | "metadata": {}, 3712 | "source": [ 3713 | "## Example\n", 3714 | "Create a filter array that will return only values higher than 42:" 3715 | ] 3716 | }, 3717 | { 3718 | "cell_type": "code", 3719 | "execution_count": 86, 3720 | "id": "9f61a318", 3721 | "metadata": {}, 3722 | "outputs": [ 3723 | { 3724 | "name": "stdout", 3725 | "output_type": "stream", 3726 | "text": [ 3727 | "[False False True True]\n", 3728 | "[43 44]\n" 3729 | ] 3730 | } 3731 | ], 3732 | "source": [ 3733 | "arr = np.array([41, 42, 43, 44])\n", 3734 | "\n", 3735 | "filter_arr = arr > 42\n", 3736 | "\n", 3737 | "newarr = arr[filter_arr]\n", 3738 | "\n", 3739 | "print(filter_arr)\n", 3740 | "print(newarr)" 3741 | ] 3742 | }, 3743 | { 3744 | "cell_type": "markdown", 3745 | "id": "0f5b38f2", 3746 | "metadata": {}, 3747 | "source": [ 3748 | "## Example\n", 3749 | "Create a filter array that will return only even elements from the original array:" 3750 | ] 3751 | }, 3752 | { 3753 | "cell_type": "code", 3754 | "execution_count": 87, 3755 | "id": "378e237a", 3756 | "metadata": {}, 3757 | "outputs": [ 3758 | { 3759 | "name": "stdout", 3760 | "output_type": "stream", 3761 | "text": [ 3762 | "[False True False True False True False]\n", 3763 | "[2 4 6]\n" 3764 | ] 3765 | } 3766 | ], 3767 | "source": [ 3768 | "arr = np.array([1, 2, 3, 4, 5, 6, 7])\n", 3769 | "\n", 3770 | "filter_arr = arr % 2 == 0\n", 3771 | "\n", 3772 | "newarr = arr[filter_arr]\n", 3773 | "\n", 3774 | "print(filter_arr)\n", 3775 | "print(newarr)" 3776 | ] 3777 | }, 3778 | { 3779 | "cell_type": "markdown", 3780 | "id": "c29c9da5", 3781 | "metadata": {}, 3782 | "source": [ 3783 | " # **The lecture is over, good luck to everyone, any questions about the lecture leave it in the comments , Lecture by Eng. Rasheed Al-Qadhi **" 3784 | ] 3785 | } 3786 | ], 3787 | "metadata": { 3788 | "kernelspec": { 3789 | "display_name": "Python 3 (ipykernel)", 3790 | "language": "python", 3791 | "name": "python3" 3792 | }, 3793 | "language_info": { 3794 | "codemirror_mode": { 3795 | "name": "ipython", 3796 | "version": 3 3797 | }, 3798 | "file_extension": ".py", 3799 | "mimetype": "text/x-python", 3800 | "name": "python", 3801 | "nbconvert_exporter": "python", 3802 | "pygments_lexer": "ipython3", 3803 | "version": "3.10.4" 3804 | } 3805 | }, 3806 | "nbformat": 4, 3807 | "nbformat_minor": 5 3808 | } 3809 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NumPy Tutorial 2 | A lecture to learn the Nambay Library 3 | Lecture titles: 4 | 5 | > * **introduction** 6 | > * **Creating Arrays** 7 | > * **Array Indexing** 8 | > * **Array Slicing** 9 | > * **Data Types** 10 | > * **Array Copy vs View** 11 | > * **Array Shape** 12 | > * **Array Reshaping** 13 | > * **Array Iterating** 14 | > * **Joining Array** 15 | > * **Splitting Array** 16 | > * **Searching Arrays** 17 | > * **Sorting Arrays** 18 | > * **Filter Array** 19 | --------------------------------------------------------------------------------