├── .ipynb_checkpoints └── NumPy-my_notes-checkpoint.ipynb ├── NumPy-my_notes.ipynb └── README.md /.ipynb_checkpoints/NumPy-my_notes-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "be8fa87b-69ea-4ab4-b1d8-41e18ca2b6ec", 6 | "metadata": {}, 7 | "source": [ 8 | "# NumPy (Numerical Python)" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "88b91d5b-ef53-4e42-8bb3-b31e671fe341", 14 | "metadata": {}, 15 | "source": [ 16 | "+ NumPy is a fundamental package for scientific (numeric) computing with Python. \n", 17 | "+ It allows us to create, store and manipulate data. \n", 18 | "+ It is the foundation that Pandas package is built on. \n", 19 | "+ It support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays, like min(), max(), etc. \n", 20 | "+ It was created by Jim Hugunin, and was released in 1995. " 21 | ] 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "015d9354-1520-44ba-ad37-823ee973b66a", 26 | "metadata": {}, 27 | "source": [ 28 | "### Installing, importing, creating arrays, etc. " 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 21, 34 | "id": "dba5dbc6-4098-4e2b-9a22-a9676015bbe8", 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "#use this command in cmd to download and install the numpy package\n", 39 | "# \"pip install numpy\"" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 3, 45 | "id": "3d372051-a328-41aa-8e83-de21329de8c9", 46 | "metadata": {}, 47 | "outputs": [], 48 | "source": [ 49 | "import numpy #importing it.\n", 50 | "import math" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 4, 56 | "id": "f6e659ca-420a-4ce4-b9c5-00446ee42718", 57 | "metadata": {}, 58 | "outputs": [], 59 | "source": [ 60 | "import numpy as np #generally np is used as abbreviation for numpy." 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 7, 66 | "id": "005b9dfc-39e6-4138-a812-13799ad52c5d", 67 | "metadata": {}, 68 | "outputs": [ 69 | { 70 | "name": "stdout", 71 | "output_type": "stream", 72 | "text": [ 73 | "[1 2 3]\n" 74 | ] 75 | } 76 | ], 77 | "source": [ 78 | "a = np.array([1,2,3]) #to create an array I have used array() function and notice that I have added a list into that function,\n", 79 | " #that will be our array. \n", 80 | "print(a)" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 8, 86 | "id": "8276ae6a-e93d-452a-ae95-390ea25592c9", 87 | "metadata": {}, 88 | "outputs": [ 89 | { 90 | "data": { 91 | "text/plain": [ 92 | "1" 93 | ] 94 | }, 95 | "execution_count": 8, 96 | "metadata": {}, 97 | "output_type": "execute_result" 98 | } 99 | ], 100 | "source": [ 101 | "a.ndim #to check the dimension of the array. " 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": 9, 107 | "id": "ce5d2f1d-6f9e-4224-b0b2-5d461866a8f5", 108 | "metadata": {}, 109 | "outputs": [ 110 | { 111 | "data": { 112 | "text/plain": [ 113 | "dtype('int32')" 114 | ] 115 | }, 116 | "execution_count": 9, 117 | "metadata": {}, 118 | "output_type": "execute_result" 119 | } 120 | ], 121 | "source": [ 122 | "a.dtype #tells the type of elements in the array. " 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 14, 128 | "id": "f6c5709c-dc5b-4448-b694-0b39c5c450c0", 129 | "metadata": {}, 130 | "outputs": [ 131 | { 132 | "name": "stdout", 133 | "output_type": "stream", 134 | "text": [ 135 | "[[1 2 3]\n", 136 | " [4 5 6]]\n", 137 | "2\n", 138 | "int32\n" 139 | ] 140 | } 141 | ], 142 | "source": [ 143 | "#creating a multi-dimensional array. \n", 144 | "#We can pass a list of lists for this into the array() function. \n", 145 | "b = np.array([[1,2,3], [4,5,6]])\n", 146 | "print(b)\n", 147 | "print(b.ndim)\n", 148 | "print(b.dtype)" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": 16, 154 | "id": "72d46a0b-a9c9-4efc-b8c3-25bb84cb8dbb", 155 | "metadata": {}, 156 | "outputs": [ 157 | { 158 | "data": { 159 | "text/plain": [ 160 | "(2, 3)" 161 | ] 162 | }, 163 | "execution_count": 16, 164 | "metadata": {}, 165 | "output_type": "execute_result" 166 | } 167 | ], 168 | "source": [ 169 | "#now checking the shape of our array.\n", 170 | "#it returns a tuple. \n", 171 | "b.shape " 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": 18, 177 | "id": "1d51c348-f697-4a0c-9286-d6442acd0d68", 178 | "metadata": {}, 179 | "outputs": [ 180 | { 181 | "name": "stdout", 182 | "output_type": "stream", 183 | "text": [ 184 | "float64\n", 185 | "[2.2 5. 1.1]\n" 186 | ] 187 | } 188 | ], 189 | "source": [ 190 | "c = np.array([2.2, 5, 1.1])\n", 191 | "print(c.dtype)\n", 192 | "print(c) \n", 193 | "\n", 194 | "#notice that numpy automatically converted the element 5 into a floating point number.\n", 195 | "# It tries to maintain the homogeneity of our array. " 196 | ] 197 | }, 198 | { 199 | "cell_type": "code", 200 | "execution_count": 22, 201 | "id": "3fef4f6c-3677-4c33-8216-ee969934c202", 202 | "metadata": {}, 203 | "outputs": [ 204 | { 205 | "name": "stdout", 206 | "output_type": "stream", 207 | "text": [ 208 | "[[0. 0. 0.]\n", 209 | " [0. 0. 0.]]\n", 210 | "[[1. 1. 1.]\n", 211 | " [1. 1. 1.]]\n" 212 | ] 213 | } 214 | ], 215 | "source": [ 216 | "#other ways to create arrays. \n", 217 | "d = np.zeros((2,3)) # an array with all elements that are 0. We have to pass the shape as an argument in the function. \n", 218 | "e = np.ones((2,3)) #an array with all elements that are 1. We have to pass the shape as an argument in the function.\n", 219 | "\n", 220 | "print(d)\n", 221 | "print(e)" 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": 23, 227 | "id": "fbfcd8ca-edf3-4bc5-a833-34401e5c1fe4", 228 | "metadata": {}, 229 | "outputs": [ 230 | { 231 | "data": { 232 | "text/plain": [ 233 | "array([[0.67882369, 0.32388559, 0.53153806],\n", 234 | " [0.73717815, 0.28627471, 0.4530464 ]])" 235 | ] 236 | }, 237 | "execution_count": 23, 238 | "metadata": {}, 239 | "output_type": "execute_result" 240 | } 241 | ], 242 | "source": [ 243 | "#generating an array with random numbers:\n", 244 | "np.random.rand(2,3)" 245 | ] 246 | }, 247 | { 248 | "cell_type": "code", 249 | "execution_count": 28, 250 | "id": "2ee7857c-a194-4cf2-9ee0-11dfd68432de", 251 | "metadata": {}, 252 | "outputs": [ 253 | { 254 | "name": "stdout", 255 | "output_type": "stream", 256 | "text": [ 257 | "[10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48]\n" 258 | ] 259 | } 260 | ], 261 | "source": [ 262 | "#Now, I am going to generate a sequence of numbers as an array, using the arange() function. The first argument is the starting number, \n", 263 | "#second argument is the ending number and the third argument is the difference between each consecutive numbers. \n", 264 | "#The default third argument is 1. \n", 265 | "\n", 266 | "f = np.arange(10, 50, 2) #10 is inclusive, 50 is exclusive. \n", 267 | "print(f)" 268 | ] 269 | }, 270 | { 271 | "cell_type": "code", 272 | "execution_count": 32, 273 | "id": "9988c8ce-b224-4734-b399-348ddda1b75e", 274 | "metadata": {}, 275 | "outputs": [ 276 | { 277 | "name": "stdout", 278 | "output_type": "stream", 279 | "text": [ 280 | "[1. 1.05263158 1.10526316 1.15789474 1.21052632 1.26315789\n", 281 | " 1.31578947 1.36842105 1.42105263 1.47368421 1.52631579 1.57894737\n", 282 | " 1.63157895 1.68421053 1.73684211 1.78947368 1.84210526 1.89473684\n", 283 | " 1.94736842 2. ]\n" 284 | ] 285 | } 286 | ], 287 | "source": [ 288 | "#Now, I am going to generate a sequence of floats as an array, using the linspace() function. The first argument is the starting bound, \n", 289 | "#second argument is the ending bound and the third argument is the number of elements we cant between them. \n", 290 | "#The default third argument is 1. \n", 291 | "\n", 292 | "g = np.linspace(1, 2, 20) #20 numbers from 1 (inclusive) to 2 (inclusive). \n", 293 | "print(g)" 294 | ] 295 | }, 296 | { 297 | "cell_type": "markdown", 298 | "id": "0990a4c7-b5a0-4bec-a8dc-4e4336679b98", 299 | "metadata": {}, 300 | "source": [ 301 | "### Arithmetic Operations" 302 | ] 303 | }, 304 | { 305 | "cell_type": "code", 306 | "execution_count": 46, 307 | "id": "091429ef-71e9-410a-9db8-3c08925e191d", 308 | "metadata": {}, 309 | "outputs": [ 310 | { 311 | "name": "stdout", 312 | "output_type": "stream", 313 | "text": [ 314 | "[11 22 33 44]\n", 315 | "[ 9 18 27 36]\n", 316 | "[ 10 40 90 160]\n", 317 | "[10. 10. 10. 10.]\n", 318 | "[0 0 0 0]\n", 319 | "[ 1 4 9 16]\n", 320 | "[False False True True]\n", 321 | "[False False False False]\n", 322 | "[ True True True False]\n", 323 | "[ True True True True]\n" 324 | ] 325 | } 326 | ], 327 | "source": [ 328 | "a = np.array([10, 20, 30, 40])\n", 329 | "b = np.array([1, 2, 3, 4])\n", 330 | "\n", 331 | "print(a+b)\n", 332 | "print(a-b)\n", 333 | "print(a*b)\n", 334 | "print(a/b)\n", 335 | "print(a%b)\n", 336 | "print(b**2)\n", 337 | "\n", 338 | "#boolean \n", 339 | "#gives arrays with boolean True & False\n", 340 | "print(a>20)\n", 341 | "print(a<10)\n", 342 | "print(b!=4)\n", 343 | "print(a%2 == 0)" 344 | ] 345 | }, 346 | { 347 | "cell_type": "code", 348 | "execution_count": 45, 349 | "id": "64a5bf0c-ac8a-4a8e-9816-02d5fb264a13", 350 | "metadata": {}, 351 | "outputs": [ 352 | { 353 | "name": "stdout", 354 | "output_type": "stream", 355 | "text": [ 356 | "[[2 0]\n", 357 | " [0 4]]\n", 358 | "[[5 4]\n", 359 | " [3 4]]\n" 360 | ] 361 | } 362 | ], 363 | "source": [ 364 | "#I have created two matrices. \n", 365 | "A = np.array([[1, 1], [0, 1]])\n", 366 | "B = np.array([[2, 0], [3, 4]])\n", 367 | "\n", 368 | "print(A*B) # asterisk * is used for element wise multiplication. \n", 369 | "print(A@B) # at sign @ is used for dot product.\n" 370 | ] 371 | }, 372 | { 373 | "cell_type": "code", 374 | "execution_count": 47, 375 | "id": "b9329c8a-141a-419d-b631-f7fdf5143bcb", 376 | "metadata": {}, 377 | "outputs": [ 378 | { 379 | "name": "stdout", 380 | "output_type": "stream", 381 | "text": [ 382 | "int32\n", 383 | "float64\n", 384 | "float64\n" 385 | ] 386 | } 387 | ], 388 | "source": [ 389 | "#UPCASTING \n", 390 | "#while manipulating two arrays, the resulting array will correspond to the more general of the two types. This is called as Upcasting. \n", 391 | "\n", 392 | "A = np.array([[1, 2, 3], [4, 5, 6]])\n", 393 | "B = np.array([[2.0, 1.0, 4.5], [3.4, 4.5, 2.0]])\n", 394 | "\n", 395 | "print(A.dtype)\n", 396 | "print(B.dtype)\n", 397 | "print((A+B).dtype)\n", 398 | "#we can see that the result is float type. " 399 | ] 400 | }, 401 | { 402 | "cell_type": "code", 403 | "execution_count": 50, 404 | "id": "95c96912-87fb-4d1e-8c43-fe1e34c8e095", 405 | "metadata": {}, 406 | "outputs": [ 407 | { 408 | "name": "stdout", 409 | "output_type": "stream", 410 | "text": [ 411 | "1\n", 412 | "6\n", 413 | "21\n", 414 | "3.5\n" 415 | ] 416 | } 417 | ], 418 | "source": [ 419 | "#Aggregation functions\n", 420 | "\n", 421 | "A = np.array([1, 2, 3, 4, 5, 6])\n", 422 | "\n", 423 | "print(A.min())\n", 424 | "print(A.max())\n", 425 | "print(A.sum())\n", 426 | "print(A.mean())\n" 427 | ] 428 | }, 429 | { 430 | "cell_type": "code", 431 | "execution_count": 52, 432 | "id": "00665288-df7a-491f-98fc-58b7cb095b97", 433 | "metadata": {}, 434 | "outputs": [ 435 | { 436 | "data": { 437 | "text/plain": [ 438 | "array([[ 1, 2, 3, 4, 5],\n", 439 | " [ 6, 7, 8, 9, 10],\n", 440 | " [11, 12, 13, 14, 15]])" 441 | ] 442 | }, 443 | "execution_count": 52, 444 | "metadata": {}, 445 | "output_type": "execute_result" 446 | } 447 | ], 448 | "source": [ 449 | "a=np.arange(1,16,1).reshape(3,5) #I have reshaped it immediately. Note that it will ony work if the reshaping and creating array both \n", 450 | " #have same number of elements. That is 15. So, 3*5=15. \n", 451 | "print(a) " 452 | ] 453 | }, 454 | { 455 | "cell_type": "markdown", 456 | "id": "8d8c2f14-0374-4ef1-8cc6-a8f543deb968", 457 | "metadata": {}, 458 | "source": [ 459 | "### Indexing, Slicing & Iterating" 460 | ] 461 | }, 462 | { 463 | "cell_type": "code", 464 | "execution_count": 58, 465 | "id": "1819ddb7-ea3f-4564-87ce-28f455939b7b", 466 | "metadata": {}, 467 | "outputs": [ 468 | { 469 | "data": { 470 | "text/plain": [ 471 | "1" 472 | ] 473 | }, 474 | "execution_count": 58, 475 | "metadata": {}, 476 | "output_type": "execute_result" 477 | } 478 | ], 479 | "source": [ 480 | "#Indexing is similar as indeing in lists. \n", 481 | "#index starts with 0. \n", 482 | "#indexing for 1D array is simple. \n", 483 | "\n", 484 | "a = np.array([1, 2, 3, 4, 5, 6])\n", 485 | "a[0]" 486 | ] 487 | }, 488 | { 489 | "cell_type": "code", 490 | "execution_count": 68, 491 | "id": "bf8ec95e-335c-463b-9393-d08dbf2815be", 492 | "metadata": {}, 493 | "outputs": [ 494 | { 495 | "name": "stdout", 496 | "output_type": "stream", 497 | "text": [ 498 | "[[1 2 3]\n", 499 | " [4 5 6]\n", 500 | " [7 8 9]]\n", 501 | " \n", 502 | "6\n", 503 | " \n", 504 | "[1 2 6 9]\n", 505 | " \n" 506 | ] 507 | }, 508 | { 509 | "data": { 510 | "text/plain": [ 511 | "array([1, 2, 6, 9])" 512 | ] 513 | }, 514 | "execution_count": 68, 515 | "metadata": {}, 516 | "output_type": "execute_result" 517 | } 518 | ], 519 | "source": [ 520 | "#indexing for multi-dimensional arrays. \n", 521 | "\n", 522 | "a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n", 523 | "print(a)\n", 524 | "print(\" \")\n", 525 | "print(a[1,2]) #first element is the row, and second is the column. \n", 526 | "print(\" \")\n", 527 | "print(np.array([a[0,0], a[0,1], a[1,2], a[2,2]])) #taking multiple elements and putting them in one array. \n", 528 | "print(\" \")\n", 529 | "\n", 530 | "#Another and interesting way of doing this...\n", 531 | "#We will index in such a way that uses zipping \n", 532 | "a[[0, 0, 1, 2], [0, 1, 2, 2]]" 533 | ] 534 | }, 535 | { 536 | "cell_type": "code", 537 | "execution_count": 70, 538 | "id": "5f1c0ac8-a8f0-44da-ae6d-c55211c14163", 539 | "metadata": {}, 540 | "outputs": [ 541 | { 542 | "name": "stdout", 543 | "output_type": "stream", 544 | "text": [ 545 | "[6]\n" 546 | ] 547 | } 548 | ], 549 | "source": [ 550 | "#Now I am doing boolean indexing. We used boolean operations previously. Here we will use them for indexng. \n", 551 | "a = np.array([1, 2, 3, 4, 5, 6])\n", 552 | "print(a[a>5])" 553 | ] 554 | }, 555 | { 556 | "cell_type": "code", 557 | "execution_count": 80, 558 | "id": "46ec55b3-7469-46f2-be79-c81526ffe852", 559 | "metadata": {}, 560 | "outputs": [ 561 | { 562 | "name": "stdout", 563 | "output_type": "stream", 564 | "text": [ 565 | "[0 1 2]\n", 566 | "[1 2]\n", 567 | "[0 1 2 3 4 5 6]\n", 568 | "[4 5 6]\n", 569 | "[0 1 2 3 4 5]\n", 570 | "[2 3 4]\n" 571 | ] 572 | } 573 | ], 574 | "source": [ 575 | "#Slicing is the way of creating a sub-array from an array. It is similar to what we do with lists.\n", 576 | "a = np.array([0, 1, 2, 3, 4, 5, 6])\n", 577 | "print(a[:3]) #index 3 is excluded\n", 578 | "print(a[1:3])\n", 579 | "print(a[:]) #everything\n", 580 | "print(a[4:])\n", 581 | "print(a[:-1])\n", 582 | "print(a[-5:-2])" 583 | ] 584 | }, 585 | { 586 | "cell_type": "code", 587 | "execution_count": 83, 588 | "id": "3eb6e440-d282-4bfc-9667-140e84fcdaaa", 589 | "metadata": {}, 590 | "outputs": [ 591 | { 592 | "name": "stdout", 593 | "output_type": "stream", 594 | "text": [ 595 | "[[1 2 3]\n", 596 | " [4 5 6]]\n", 597 | "[[2 3]\n", 598 | " [5 6]]\n" 599 | ] 600 | } 601 | ], 602 | "source": [ 603 | "#Slicing for 2D arrays\n", 604 | "a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n", 605 | "print(a[:2])\n", 606 | "print(a[:2, 1:3]) #first argument for rows an second for columns. \n" 607 | ] 608 | }, 609 | { 610 | "cell_type": "markdown", 611 | "id": "629fdd10-f10e-470d-8b9d-357e7ae16333", 612 | "metadata": {}, 613 | "source": [ 614 | "##### That's it for now" 615 | ] 616 | }, 617 | { 618 | "cell_type": "code", 619 | "execution_count": null, 620 | "id": "8bff8b16-d583-4838-a387-85426edc8d92", 621 | "metadata": {}, 622 | "outputs": [], 623 | "source": [] 624 | } 625 | ], 626 | "metadata": { 627 | "kernelspec": { 628 | "display_name": "Python 3 (ipykernel)", 629 | "language": "python", 630 | "name": "python3" 631 | }, 632 | "language_info": { 633 | "codemirror_mode": { 634 | "name": "ipython", 635 | "version": 3 636 | }, 637 | "file_extension": ".py", 638 | "mimetype": "text/x-python", 639 | "name": "python", 640 | "nbconvert_exporter": "python", 641 | "pygments_lexer": "ipython3", 642 | "version": "3.9.5" 643 | } 644 | }, 645 | "nbformat": 4, 646 | "nbformat_minor": 5 647 | } 648 | -------------------------------------------------------------------------------- /NumPy-my_notes.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "be8fa87b-69ea-4ab4-b1d8-41e18ca2b6ec", 6 | "metadata": {}, 7 | "source": [ 8 | "# NumPy (Numerical Python)" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "88b91d5b-ef53-4e42-8bb3-b31e671fe341", 14 | "metadata": {}, 15 | "source": [ 16 | "+ NumPy is a fundamental package for scientific (numeric) computing with Python. \n", 17 | "+ It allows us to create, store and manipulate data. \n", 18 | "+ It is the foundation that Pandas package is built on. \n", 19 | "+ It support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays, like min(), max(), etc. \n", 20 | "+ It was created by Jim Hugunin, and was released in 1995. " 21 | ] 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "015d9354-1520-44ba-ad37-823ee973b66a", 26 | "metadata": {}, 27 | "source": [ 28 | "### Installing, importing, creating arrays, etc. " 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 21, 34 | "id": "dba5dbc6-4098-4e2b-9a22-a9676015bbe8", 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "#use this command in cmd to download and install the numpy package\n", 39 | "# \"pip install numpy\"" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 3, 45 | "id": "3d372051-a328-41aa-8e83-de21329de8c9", 46 | "metadata": {}, 47 | "outputs": [], 48 | "source": [ 49 | "import numpy #importing it.\n", 50 | "import math" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 4, 56 | "id": "f6e659ca-420a-4ce4-b9c5-00446ee42718", 57 | "metadata": {}, 58 | "outputs": [], 59 | "source": [ 60 | "import numpy as np #generally np is used as abbreviation for numpy." 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 7, 66 | "id": "005b9dfc-39e6-4138-a812-13799ad52c5d", 67 | "metadata": {}, 68 | "outputs": [ 69 | { 70 | "name": "stdout", 71 | "output_type": "stream", 72 | "text": [ 73 | "[1 2 3]\n" 74 | ] 75 | } 76 | ], 77 | "source": [ 78 | "a = np.array([1,2,3]) #to create an array I have used array() function and notice that I have added a list into that function,\n", 79 | " #that will be our array. \n", 80 | "print(a)" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 8, 86 | "id": "8276ae6a-e93d-452a-ae95-390ea25592c9", 87 | "metadata": {}, 88 | "outputs": [ 89 | { 90 | "data": { 91 | "text/plain": [ 92 | "1" 93 | ] 94 | }, 95 | "execution_count": 8, 96 | "metadata": {}, 97 | "output_type": "execute_result" 98 | } 99 | ], 100 | "source": [ 101 | "a.ndim #to check the dimension of the array. " 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": 9, 107 | "id": "ce5d2f1d-6f9e-4224-b0b2-5d461866a8f5", 108 | "metadata": {}, 109 | "outputs": [ 110 | { 111 | "data": { 112 | "text/plain": [ 113 | "dtype('int32')" 114 | ] 115 | }, 116 | "execution_count": 9, 117 | "metadata": {}, 118 | "output_type": "execute_result" 119 | } 120 | ], 121 | "source": [ 122 | "a.dtype #tells the type of elements in the array. " 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 14, 128 | "id": "f6c5709c-dc5b-4448-b694-0b39c5c450c0", 129 | "metadata": {}, 130 | "outputs": [ 131 | { 132 | "name": "stdout", 133 | "output_type": "stream", 134 | "text": [ 135 | "[[1 2 3]\n", 136 | " [4 5 6]]\n", 137 | "2\n", 138 | "int32\n" 139 | ] 140 | } 141 | ], 142 | "source": [ 143 | "#creating a multi-dimensional array. \n", 144 | "#We can pass a list of lists for this into the array() function. \n", 145 | "b = np.array([[1,2,3], [4,5,6]])\n", 146 | "print(b)\n", 147 | "print(b.ndim)\n", 148 | "print(b.dtype)" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": 16, 154 | "id": "72d46a0b-a9c9-4efc-b8c3-25bb84cb8dbb", 155 | "metadata": {}, 156 | "outputs": [ 157 | { 158 | "data": { 159 | "text/plain": [ 160 | "(2, 3)" 161 | ] 162 | }, 163 | "execution_count": 16, 164 | "metadata": {}, 165 | "output_type": "execute_result" 166 | } 167 | ], 168 | "source": [ 169 | "#now checking the shape of our array.\n", 170 | "#it returns a tuple. \n", 171 | "b.shape " 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": 18, 177 | "id": "1d51c348-f697-4a0c-9286-d6442acd0d68", 178 | "metadata": {}, 179 | "outputs": [ 180 | { 181 | "name": "stdout", 182 | "output_type": "stream", 183 | "text": [ 184 | "float64\n", 185 | "[2.2 5. 1.1]\n" 186 | ] 187 | } 188 | ], 189 | "source": [ 190 | "c = np.array([2.2, 5, 1.1])\n", 191 | "print(c.dtype)\n", 192 | "print(c) \n", 193 | "\n", 194 | "#notice that numpy automatically converted the element 5 into a floating point number.\n", 195 | "# It tries to maintain the homogeneity of our array. " 196 | ] 197 | }, 198 | { 199 | "cell_type": "code", 200 | "execution_count": 22, 201 | "id": "3fef4f6c-3677-4c33-8216-ee969934c202", 202 | "metadata": {}, 203 | "outputs": [ 204 | { 205 | "name": "stdout", 206 | "output_type": "stream", 207 | "text": [ 208 | "[[0. 0. 0.]\n", 209 | " [0. 0. 0.]]\n", 210 | "[[1. 1. 1.]\n", 211 | " [1. 1. 1.]]\n" 212 | ] 213 | } 214 | ], 215 | "source": [ 216 | "#other ways to create arrays. \n", 217 | "d = np.zeros((2,3)) # an array with all elements that are 0. We have to pass the shape as an argument in the function. \n", 218 | "e = np.ones((2,3)) #an array with all elements that are 1. We have to pass the shape as an argument in the function.\n", 219 | "\n", 220 | "print(d)\n", 221 | "print(e)" 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": 23, 227 | "id": "fbfcd8ca-edf3-4bc5-a833-34401e5c1fe4", 228 | "metadata": {}, 229 | "outputs": [ 230 | { 231 | "data": { 232 | "text/plain": [ 233 | "array([[0.67882369, 0.32388559, 0.53153806],\n", 234 | " [0.73717815, 0.28627471, 0.4530464 ]])" 235 | ] 236 | }, 237 | "execution_count": 23, 238 | "metadata": {}, 239 | "output_type": "execute_result" 240 | } 241 | ], 242 | "source": [ 243 | "#generating an array with random numbers:\n", 244 | "np.random.rand(2,3)" 245 | ] 246 | }, 247 | { 248 | "cell_type": "code", 249 | "execution_count": 28, 250 | "id": "2ee7857c-a194-4cf2-9ee0-11dfd68432de", 251 | "metadata": {}, 252 | "outputs": [ 253 | { 254 | "name": "stdout", 255 | "output_type": "stream", 256 | "text": [ 257 | "[10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48]\n" 258 | ] 259 | } 260 | ], 261 | "source": [ 262 | "#Now, I am going to generate a sequence of numbers as an array, using the arange() function. The first argument is the starting number, \n", 263 | "#second argument is the ending number and the third argument is the difference between each consecutive numbers. \n", 264 | "#The default third argument is 1. \n", 265 | "\n", 266 | "f = np.arange(10, 50, 2) #10 is inclusive, 50 is exclusive. \n", 267 | "print(f)" 268 | ] 269 | }, 270 | { 271 | "cell_type": "code", 272 | "execution_count": 32, 273 | "id": "9988c8ce-b224-4734-b399-348ddda1b75e", 274 | "metadata": {}, 275 | "outputs": [ 276 | { 277 | "name": "stdout", 278 | "output_type": "stream", 279 | "text": [ 280 | "[1. 1.05263158 1.10526316 1.15789474 1.21052632 1.26315789\n", 281 | " 1.31578947 1.36842105 1.42105263 1.47368421 1.52631579 1.57894737\n", 282 | " 1.63157895 1.68421053 1.73684211 1.78947368 1.84210526 1.89473684\n", 283 | " 1.94736842 2. ]\n" 284 | ] 285 | } 286 | ], 287 | "source": [ 288 | "#Now, I am going to generate a sequence of floats as an array, using the linspace() function. The first argument is the starting bound, \n", 289 | "#second argument is the ending bound and the third argument is the number of elements we cant between them. \n", 290 | "#The default third argument is 1. \n", 291 | "\n", 292 | "g = np.linspace(1, 2, 20) #20 numbers from 1 (inclusive) to 2 (inclusive). \n", 293 | "print(g)" 294 | ] 295 | }, 296 | { 297 | "cell_type": "markdown", 298 | "id": "0990a4c7-b5a0-4bec-a8dc-4e4336679b98", 299 | "metadata": {}, 300 | "source": [ 301 | "### Arithmetic Operations" 302 | ] 303 | }, 304 | { 305 | "cell_type": "code", 306 | "execution_count": 46, 307 | "id": "091429ef-71e9-410a-9db8-3c08925e191d", 308 | "metadata": {}, 309 | "outputs": [ 310 | { 311 | "name": "stdout", 312 | "output_type": "stream", 313 | "text": [ 314 | "[11 22 33 44]\n", 315 | "[ 9 18 27 36]\n", 316 | "[ 10 40 90 160]\n", 317 | "[10. 10. 10. 10.]\n", 318 | "[0 0 0 0]\n", 319 | "[ 1 4 9 16]\n", 320 | "[False False True True]\n", 321 | "[False False False False]\n", 322 | "[ True True True False]\n", 323 | "[ True True True True]\n" 324 | ] 325 | } 326 | ], 327 | "source": [ 328 | "a = np.array([10, 20, 30, 40])\n", 329 | "b = np.array([1, 2, 3, 4])\n", 330 | "\n", 331 | "print(a+b)\n", 332 | "print(a-b)\n", 333 | "print(a*b)\n", 334 | "print(a/b)\n", 335 | "print(a%b)\n", 336 | "print(b**2)\n", 337 | "\n", 338 | "#boolean \n", 339 | "#gives arrays with boolean True & False\n", 340 | "print(a>20)\n", 341 | "print(a<10)\n", 342 | "print(b!=4)\n", 343 | "print(a%2 == 0)" 344 | ] 345 | }, 346 | { 347 | "cell_type": "code", 348 | "execution_count": 45, 349 | "id": "64a5bf0c-ac8a-4a8e-9816-02d5fb264a13", 350 | "metadata": {}, 351 | "outputs": [ 352 | { 353 | "name": "stdout", 354 | "output_type": "stream", 355 | "text": [ 356 | "[[2 0]\n", 357 | " [0 4]]\n", 358 | "[[5 4]\n", 359 | " [3 4]]\n" 360 | ] 361 | } 362 | ], 363 | "source": [ 364 | "#I have created two matrices. \n", 365 | "A = np.array([[1, 1], [0, 1]])\n", 366 | "B = np.array([[2, 0], [3, 4]])\n", 367 | "\n", 368 | "print(A*B) # asterisk * is used for element wise multiplication. \n", 369 | "print(A@B) # at sign @ is used for dot product.\n" 370 | ] 371 | }, 372 | { 373 | "cell_type": "code", 374 | "execution_count": 47, 375 | "id": "b9329c8a-141a-419d-b631-f7fdf5143bcb", 376 | "metadata": {}, 377 | "outputs": [ 378 | { 379 | "name": "stdout", 380 | "output_type": "stream", 381 | "text": [ 382 | "int32\n", 383 | "float64\n", 384 | "float64\n" 385 | ] 386 | } 387 | ], 388 | "source": [ 389 | "#UPCASTING \n", 390 | "#while manipulating two arrays, the resulting array will correspond to the more general of the two types. This is called as Upcasting. \n", 391 | "\n", 392 | "A = np.array([[1, 2, 3], [4, 5, 6]])\n", 393 | "B = np.array([[2.0, 1.0, 4.5], [3.4, 4.5, 2.0]])\n", 394 | "\n", 395 | "print(A.dtype)\n", 396 | "print(B.dtype)\n", 397 | "print((A+B).dtype)\n", 398 | "#we can see that the result is float type. " 399 | ] 400 | }, 401 | { 402 | "cell_type": "code", 403 | "execution_count": 50, 404 | "id": "95c96912-87fb-4d1e-8c43-fe1e34c8e095", 405 | "metadata": {}, 406 | "outputs": [ 407 | { 408 | "name": "stdout", 409 | "output_type": "stream", 410 | "text": [ 411 | "1\n", 412 | "6\n", 413 | "21\n", 414 | "3.5\n" 415 | ] 416 | } 417 | ], 418 | "source": [ 419 | "#Aggregation functions\n", 420 | "\n", 421 | "A = np.array([1, 2, 3, 4, 5, 6])\n", 422 | "\n", 423 | "print(A.min())\n", 424 | "print(A.max())\n", 425 | "print(A.sum())\n", 426 | "print(A.mean())\n" 427 | ] 428 | }, 429 | { 430 | "cell_type": "code", 431 | "execution_count": 52, 432 | "id": "00665288-df7a-491f-98fc-58b7cb095b97", 433 | "metadata": {}, 434 | "outputs": [ 435 | { 436 | "data": { 437 | "text/plain": [ 438 | "array([[ 1, 2, 3, 4, 5],\n", 439 | " [ 6, 7, 8, 9, 10],\n", 440 | " [11, 12, 13, 14, 15]])" 441 | ] 442 | }, 443 | "execution_count": 52, 444 | "metadata": {}, 445 | "output_type": "execute_result" 446 | } 447 | ], 448 | "source": [ 449 | "a=np.arange(1,16,1).reshape(3,5) #I have reshaped it immediately. Note that it will ony work if the reshaping and creating array both \n", 450 | " #have same number of elements. That is 15. So, 3*5=15. \n", 451 | "print(a) " 452 | ] 453 | }, 454 | { 455 | "cell_type": "markdown", 456 | "id": "8d8c2f14-0374-4ef1-8cc6-a8f543deb968", 457 | "metadata": {}, 458 | "source": [ 459 | "### Indexing, Slicing & Iterating" 460 | ] 461 | }, 462 | { 463 | "cell_type": "code", 464 | "execution_count": 58, 465 | "id": "1819ddb7-ea3f-4564-87ce-28f455939b7b", 466 | "metadata": {}, 467 | "outputs": [ 468 | { 469 | "data": { 470 | "text/plain": [ 471 | "1" 472 | ] 473 | }, 474 | "execution_count": 58, 475 | "metadata": {}, 476 | "output_type": "execute_result" 477 | } 478 | ], 479 | "source": [ 480 | "#Indexing is similar as indeing in lists. \n", 481 | "#index starts with 0. \n", 482 | "#indexing for 1D array is simple. \n", 483 | "\n", 484 | "a = np.array([1, 2, 3, 4, 5, 6])\n", 485 | "a[0]" 486 | ] 487 | }, 488 | { 489 | "cell_type": "code", 490 | "execution_count": 68, 491 | "id": "bf8ec95e-335c-463b-9393-d08dbf2815be", 492 | "metadata": {}, 493 | "outputs": [ 494 | { 495 | "name": "stdout", 496 | "output_type": "stream", 497 | "text": [ 498 | "[[1 2 3]\n", 499 | " [4 5 6]\n", 500 | " [7 8 9]]\n", 501 | " \n", 502 | "6\n", 503 | " \n", 504 | "[1 2 6 9]\n", 505 | " \n" 506 | ] 507 | }, 508 | { 509 | "data": { 510 | "text/plain": [ 511 | "array([1, 2, 6, 9])" 512 | ] 513 | }, 514 | "execution_count": 68, 515 | "metadata": {}, 516 | "output_type": "execute_result" 517 | } 518 | ], 519 | "source": [ 520 | "#indexing for multi-dimensional arrays. \n", 521 | "\n", 522 | "a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n", 523 | "print(a)\n", 524 | "print(\" \")\n", 525 | "print(a[1,2]) #first element is the row, and second is the column. \n", 526 | "print(\" \")\n", 527 | "print(np.array([a[0,0], a[0,1], a[1,2], a[2,2]])) #taking multiple elements and putting them in one array. \n", 528 | "print(\" \")\n", 529 | "\n", 530 | "#Another and interesting way of doing this...\n", 531 | "#We will index in such a way that uses zipping \n", 532 | "a[[0, 0, 1, 2], [0, 1, 2, 2]]" 533 | ] 534 | }, 535 | { 536 | "cell_type": "code", 537 | "execution_count": 70, 538 | "id": "5f1c0ac8-a8f0-44da-ae6d-c55211c14163", 539 | "metadata": {}, 540 | "outputs": [ 541 | { 542 | "name": "stdout", 543 | "output_type": "stream", 544 | "text": [ 545 | "[6]\n" 546 | ] 547 | } 548 | ], 549 | "source": [ 550 | "#Now I am doing boolean indexing. We used boolean operations previously. Here we will use them for indexng. \n", 551 | "a = np.array([1, 2, 3, 4, 5, 6])\n", 552 | "print(a[a>5])" 553 | ] 554 | }, 555 | { 556 | "cell_type": "code", 557 | "execution_count": 80, 558 | "id": "46ec55b3-7469-46f2-be79-c81526ffe852", 559 | "metadata": {}, 560 | "outputs": [ 561 | { 562 | "name": "stdout", 563 | "output_type": "stream", 564 | "text": [ 565 | "[0 1 2]\n", 566 | "[1 2]\n", 567 | "[0 1 2 3 4 5 6]\n", 568 | "[4 5 6]\n", 569 | "[0 1 2 3 4 5]\n", 570 | "[2 3 4]\n" 571 | ] 572 | } 573 | ], 574 | "source": [ 575 | "#Slicing is the way of creating a sub-array from an array. It is similar to what we do with lists.\n", 576 | "a = np.array([0, 1, 2, 3, 4, 5, 6])\n", 577 | "print(a[:3]) #index 3 is excluded\n", 578 | "print(a[1:3])\n", 579 | "print(a[:]) #everything\n", 580 | "print(a[4:])\n", 581 | "print(a[:-1])\n", 582 | "print(a[-5:-2])" 583 | ] 584 | }, 585 | { 586 | "cell_type": "code", 587 | "execution_count": 83, 588 | "id": "3eb6e440-d282-4bfc-9667-140e84fcdaaa", 589 | "metadata": {}, 590 | "outputs": [ 591 | { 592 | "name": "stdout", 593 | "output_type": "stream", 594 | "text": [ 595 | "[[1 2 3]\n", 596 | " [4 5 6]]\n", 597 | "[[2 3]\n", 598 | " [5 6]]\n" 599 | ] 600 | } 601 | ], 602 | "source": [ 603 | "#Slicing for 2D arrays\n", 604 | "a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n", 605 | "print(a[:2])\n", 606 | "print(a[:2, 1:3]) #first argument for rows an second for columns. \n" 607 | ] 608 | }, 609 | { 610 | "cell_type": "markdown", 611 | "id": "629fdd10-f10e-470d-8b9d-357e7ae16333", 612 | "metadata": {}, 613 | "source": [ 614 | "##### That's it for now" 615 | ] 616 | }, 617 | { 618 | "cell_type": "code", 619 | "execution_count": null, 620 | "id": "8bff8b16-d583-4838-a387-85426edc8d92", 621 | "metadata": {}, 622 | "outputs": [], 623 | "source": [] 624 | } 625 | ], 626 | "metadata": { 627 | "kernelspec": { 628 | "display_name": "Python 3 (ipykernel)", 629 | "language": "python", 630 | "name": "python3" 631 | }, 632 | "language_info": { 633 | "codemirror_mode": { 634 | "name": "ipython", 635 | "version": 3 636 | }, 637 | "file_extension": ".py", 638 | "mimetype": "text/x-python", 639 | "name": "python", 640 | "nbconvert_exporter": "python", 641 | "pygments_lexer": "ipython3", 642 | "version": "3.9.5" 643 | } 644 | }, 645 | "nbformat": 4, 646 | "nbformat_minor": 5 647 | } 648 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## NumPy 2 | 3 | + **NumPy is a fundamental package for scientific (numeric) computing with Python. It allows us to create, store and manipulate data.** 4 | + ***In the "NumPy-my_notes.ipynb" file I've added code to learn basics of Numpy and get familiar with it.*** 5 | --------------------------------------------------------------------------------