├── 100_Numpy_exercises-Solution.ipynb ├── 100_Numpy_exercises.ipynb └── README.md /100_Numpy_exercises-Solution.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 100 numpy exercises\n" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "#### 1. Import the numpy package under the name `np` (★☆☆)" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "import numpy as np" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "#### 2. Print the numpy version and the configuration (★☆☆)" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 2, 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "name": "stdout", 40 | "output_type": "stream", 41 | "text": [ 42 | "1.21.2\n", 43 | "blas_mkl_info:\n", 44 | " NOT AVAILABLE\n", 45 | "blis_info:\n", 46 | " NOT AVAILABLE\n", 47 | "openblas_info:\n", 48 | " library_dirs = ['D:\\\\a\\\\1\\\\s\\\\numpy\\\\build\\\\openblas_info']\n", 49 | " libraries = ['openblas_info']\n", 50 | " language = f77\n", 51 | " define_macros = [('HAVE_CBLAS', None)]\n", 52 | "blas_opt_info:\n", 53 | " library_dirs = ['D:\\\\a\\\\1\\\\s\\\\numpy\\\\build\\\\openblas_info']\n", 54 | " libraries = ['openblas_info']\n", 55 | " language = f77\n", 56 | " define_macros = [('HAVE_CBLAS', None)]\n", 57 | "lapack_mkl_info:\n", 58 | " NOT AVAILABLE\n", 59 | "openblas_lapack_info:\n", 60 | " library_dirs = ['D:\\\\a\\\\1\\\\s\\\\numpy\\\\build\\\\openblas_lapack_info']\n", 61 | " libraries = ['openblas_lapack_info']\n", 62 | " language = f77\n", 63 | " define_macros = [('HAVE_CBLAS', None)]\n", 64 | "lapack_opt_info:\n", 65 | " library_dirs = ['D:\\\\a\\\\1\\\\s\\\\numpy\\\\build\\\\openblas_lapack_info']\n", 66 | " libraries = ['openblas_lapack_info']\n", 67 | " language = f77\n", 68 | " define_macros = [('HAVE_CBLAS', None)]\n", 69 | "Supported SIMD extensions in this NumPy install:\n", 70 | " baseline = SSE,SSE2,SSE3\n", 71 | " found = SSSE3,SSE41,POPCNT,SSE42,AVX,F16C,FMA3,AVX2\n", 72 | " not found = AVX512F,AVX512CD,AVX512_SKX,AVX512_CLX,AVX512_CNL\n" 73 | ] 74 | } 75 | ], 76 | "source": [ 77 | "print(np.__version__)\n", 78 | "np.show_config()" 79 | ] 80 | }, 81 | { 82 | "cell_type": "markdown", 83 | "metadata": {}, 84 | "source": [ 85 | "#### 3. Create a null vector of size 10 (★☆☆)" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 3, 91 | "metadata": {}, 92 | "outputs": [ 93 | { 94 | "name": "stdout", 95 | "output_type": "stream", 96 | "text": [ 97 | "[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n" 98 | ] 99 | } 100 | ], 101 | "source": [ 102 | "Z = np.zeros(10)\n", 103 | "print(Z)" 104 | ] 105 | }, 106 | { 107 | "cell_type": "markdown", 108 | "metadata": {}, 109 | "source": [ 110 | "#### 4. How to find the memory size of any array (★☆☆)" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": 4, 116 | "metadata": {}, 117 | "outputs": [ 118 | { 119 | "name": "stdout", 120 | "output_type": "stream", 121 | "text": [ 122 | "800 bytes\n" 123 | ] 124 | } 125 | ], 126 | "source": [ 127 | "Z = np.zeros((10,10))\n", 128 | "print(\"%d bytes\" % (Z.size * Z.itemsize))" 129 | ] 130 | }, 131 | { 132 | "cell_type": "markdown", 133 | "metadata": {}, 134 | "source": [ 135 | "#### 5. How to get the documentation of the numpy add function from the command line? (★☆☆)" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": 5, 141 | "metadata": {}, 142 | "outputs": [ 143 | { 144 | "name": "stderr", 145 | "output_type": "stream", 146 | "text": [ 147 | "ERROR:root:File `'`python.py'` not found.\n" 148 | ] 149 | } 150 | ], 151 | "source": [ 152 | "\n", 153 | "%run `python -c \"import numpy; numpy.info(numpy.add)\"`" 154 | ] 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "metadata": {}, 159 | "source": [ 160 | "#### 6. Create a null vector of size 10 but the fifth value which is 1 (★☆☆)" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": 6, 166 | "metadata": {}, 167 | "outputs": [ 168 | { 169 | "name": "stdout", 170 | "output_type": "stream", 171 | "text": [ 172 | "[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]\n" 173 | ] 174 | } 175 | ], 176 | "source": [ 177 | "Z = np.zeros(10)\n", 178 | "Z[4] = 1\n", 179 | "print(Z)" 180 | ] 181 | }, 182 | { 183 | "cell_type": "markdown", 184 | "metadata": {}, 185 | "source": [ 186 | "#### 7. Create a vector with values ranging from 10 to 49 (★☆☆)" 187 | ] 188 | }, 189 | { 190 | "cell_type": "code", 191 | "execution_count": 7, 192 | "metadata": {}, 193 | "outputs": [ 194 | { 195 | "name": "stdout", 196 | "output_type": "stream", 197 | "text": [ 198 | "[10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33\n", 199 | " 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49]\n" 200 | ] 201 | } 202 | ], 203 | "source": [ 204 | "Z = np.arange(10,50)\n", 205 | "print(Z)" 206 | ] 207 | }, 208 | { 209 | "cell_type": "markdown", 210 | "metadata": {}, 211 | "source": [ 212 | "#### 8. Reverse a vector (first element becomes last) (★☆☆)" 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": 8, 218 | "metadata": {}, 219 | "outputs": [ 220 | { 221 | "name": "stdout", 222 | "output_type": "stream", 223 | "text": [ 224 | "[49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26\n", 225 | " 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2\n", 226 | " 1 0]\n" 227 | ] 228 | } 229 | ], 230 | "source": [ 231 | "Z = np.arange(50)\n", 232 | "Z = Z[::-1]\n", 233 | "print(Z)" 234 | ] 235 | }, 236 | { 237 | "cell_type": "markdown", 238 | "metadata": {}, 239 | "source": [ 240 | "#### 9. Create a 3x3 matrix with values ranging from 0 to 8 (★☆☆)" 241 | ] 242 | }, 243 | { 244 | "cell_type": "code", 245 | "execution_count": 9, 246 | "metadata": {}, 247 | "outputs": [ 248 | { 249 | "name": "stdout", 250 | "output_type": "stream", 251 | "text": [ 252 | "[[0 1 2]\n", 253 | " [3 4 5]\n", 254 | " [6 7 8]]\n" 255 | ] 256 | } 257 | ], 258 | "source": [ 259 | "Z = np.arange(9).reshape(3, 3)\n", 260 | "print(Z)" 261 | ] 262 | }, 263 | { 264 | "cell_type": "markdown", 265 | "metadata": {}, 266 | "source": [ 267 | "#### 10. Find indices of non-zero elements from [1,2,0,0,4,0] (★☆☆)" 268 | ] 269 | }, 270 | { 271 | "cell_type": "code", 272 | "execution_count": 10, 273 | "metadata": {}, 274 | "outputs": [ 275 | { 276 | "name": "stdout", 277 | "output_type": "stream", 278 | "text": [ 279 | "(array([0, 1, 4], dtype=int64),)\n" 280 | ] 281 | } 282 | ], 283 | "source": [ 284 | "nz = np.nonzero([1,2,0,0,4,0])\n", 285 | "print(nz)" 286 | ] 287 | }, 288 | { 289 | "cell_type": "markdown", 290 | "metadata": {}, 291 | "source": [ 292 | "#### 11. Create a 3x3 identity matrix (★☆☆)" 293 | ] 294 | }, 295 | { 296 | "cell_type": "code", 297 | "execution_count": 11, 298 | "metadata": {}, 299 | "outputs": [ 300 | { 301 | "name": "stdout", 302 | "output_type": "stream", 303 | "text": [ 304 | "[[1. 0. 0.]\n", 305 | " [0. 1. 0.]\n", 306 | " [0. 0. 1.]]\n" 307 | ] 308 | } 309 | ], 310 | "source": [ 311 | "Z = np.eye(3)\n", 312 | "print(Z)" 313 | ] 314 | }, 315 | { 316 | "cell_type": "markdown", 317 | "metadata": {}, 318 | "source": [ 319 | "#### 12. Create a 3x3x3 array with random values (★☆☆)" 320 | ] 321 | }, 322 | { 323 | "cell_type": "code", 324 | "execution_count": 12, 325 | "metadata": {}, 326 | "outputs": [ 327 | { 328 | "name": "stdout", 329 | "output_type": "stream", 330 | "text": [ 331 | "[[[0.63543179 0.39524718 0.14547533]\n", 332 | " [0.75433735 0.71223379 0.68415395]\n", 333 | " [0.8946513 0.97332845 0.94764011]]\n", 334 | "\n", 335 | " [[0.33069702 0.57988564 0.4025338 ]\n", 336 | " [0.50081125 0.2394252 0.03664688]\n", 337 | " [0.96420413 0.17684102 0.78234763]]\n", 338 | "\n", 339 | " [[0.60096411 0.25932969 0.60016746]\n", 340 | " [0.01646019 0.78511339 0.24798903]\n", 341 | " [0.77462387 0.96695113 0.75916246]]]\n" 342 | ] 343 | } 344 | ], 345 | "source": [ 346 | "Z = np.random.random((3,3,3))\n", 347 | "print(Z)" 348 | ] 349 | }, 350 | { 351 | "cell_type": "markdown", 352 | "metadata": {}, 353 | "source": [ 354 | "#### 13. Create a 10x10 array with random values and find the minimum and maximum values (★☆☆)" 355 | ] 356 | }, 357 | { 358 | "cell_type": "code", 359 | "execution_count": 13, 360 | "metadata": {}, 361 | "outputs": [ 362 | { 363 | "name": "stdout", 364 | "output_type": "stream", 365 | "text": [ 366 | "0.059809642344716196 0.9999213031220705\n" 367 | ] 368 | } 369 | ], 370 | "source": [ 371 | "Z = np.random.random((10,10))\n", 372 | "Zmin, Zmax = Z.min(), Z.max()\n", 373 | "print(Zmin, Zmax)" 374 | ] 375 | }, 376 | { 377 | "cell_type": "markdown", 378 | "metadata": {}, 379 | "source": [ 380 | "#### 14. Create a random vector of size 30 and find the mean value (★☆☆)" 381 | ] 382 | }, 383 | { 384 | "cell_type": "code", 385 | "execution_count": 14, 386 | "metadata": {}, 387 | "outputs": [ 388 | { 389 | "name": "stdout", 390 | "output_type": "stream", 391 | "text": [ 392 | "0.49309227940052747\n" 393 | ] 394 | } 395 | ], 396 | "source": [ 397 | "Z = np.random.random(30)\n", 398 | "m = Z.mean()\n", 399 | "print(m)" 400 | ] 401 | }, 402 | { 403 | "cell_type": "markdown", 404 | "metadata": {}, 405 | "source": [ 406 | "#### 15. Create a 2d array with 1 on the border and 0 inside (★☆☆)" 407 | ] 408 | }, 409 | { 410 | "cell_type": "code", 411 | "execution_count": 15, 412 | "metadata": {}, 413 | "outputs": [ 414 | { 415 | "name": "stdout", 416 | "output_type": "stream", 417 | "text": [ 418 | "[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n", 419 | " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", 420 | " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", 421 | " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", 422 | " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", 423 | " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", 424 | " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", 425 | " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", 426 | " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", 427 | " [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]\n" 428 | ] 429 | } 430 | ], 431 | "source": [ 432 | "Z = np.ones((10,10))\n", 433 | "Z[1:-1,1:-1] = 0\n", 434 | "print(Z)" 435 | ] 436 | }, 437 | { 438 | "cell_type": "markdown", 439 | "metadata": {}, 440 | "source": [ 441 | "#### 16. How to add a border (filled with 0's) around an existing array? (★☆☆)" 442 | ] 443 | }, 444 | { 445 | "cell_type": "code", 446 | "execution_count": 16, 447 | "metadata": {}, 448 | "outputs": [ 449 | { 450 | "name": "stdout", 451 | "output_type": "stream", 452 | "text": [ 453 | "[[0. 0. 0. 0. 0. 0. 0.]\n", 454 | " [0. 1. 1. 1. 1. 1. 0.]\n", 455 | " [0. 1. 1. 1. 1. 1. 0.]\n", 456 | " [0. 1. 1. 1. 1. 1. 0.]\n", 457 | " [0. 1. 1. 1. 1. 1. 0.]\n", 458 | " [0. 1. 1. 1. 1. 1. 0.]\n", 459 | " [0. 0. 0. 0. 0. 0. 0.]]\n", 460 | "[[0. 0. 0. 0. 0. 0. 0.]\n", 461 | " [0. 1. 1. 1. 1. 1. 0.]\n", 462 | " [0. 1. 1. 1. 1. 1. 0.]\n", 463 | " [0. 1. 1. 1. 1. 1. 0.]\n", 464 | " [0. 1. 1. 1. 1. 1. 0.]\n", 465 | " [0. 1. 1. 1. 1. 1. 0.]\n", 466 | " [0. 0. 0. 0. 0. 0. 0.]]\n" 467 | ] 468 | } 469 | ], 470 | "source": [ 471 | "Z = np.ones((5,5))\n", 472 | "Z = np.pad(Z, pad_width=1, mode='constant', constant_values=0)\n", 473 | "print(Z)\n", 474 | "\n", 475 | "# Using fancy indexing\n", 476 | "Z[:, [0, -1]] = 0\n", 477 | "Z[[0, -1], :] = 0\n", 478 | "print(Z)" 479 | ] 480 | }, 481 | { 482 | "cell_type": "markdown", 483 | "metadata": {}, 484 | "source": [ 485 | "#### 17. What is the result of the following expression? (★☆☆)\n", 486 | "```python\n", 487 | "0 * np.nan\n", 488 | "np.nan == np.nan\n", 489 | "np.inf > np.nan\n", 490 | "np.nan - np.nan\n", 491 | "np.nan in set([np.nan])\n", 492 | "0.3 == 3 * 0.1\n", 493 | "```" 494 | ] 495 | }, 496 | { 497 | "cell_type": "code", 498 | "execution_count": 17, 499 | "metadata": {}, 500 | "outputs": [ 501 | { 502 | "name": "stdout", 503 | "output_type": "stream", 504 | "text": [ 505 | "nan\n", 506 | "False\n", 507 | "False\n", 508 | "nan\n", 509 | "True\n", 510 | "False\n" 511 | ] 512 | } 513 | ], 514 | "source": [ 515 | "print(0 * np.nan)\n", 516 | "print(np.nan == np.nan)\n", 517 | "print(np.inf > np.nan)\n", 518 | "print(np.nan - np.nan)\n", 519 | "print(np.nan in set([np.nan]))\n", 520 | "print(0.3 == 3 * 0.1)" 521 | ] 522 | }, 523 | { 524 | "cell_type": "markdown", 525 | "metadata": {}, 526 | "source": [ 527 | "#### 18. Create a 5x5 matrix with values 1,2,3,4 just below the diagonal (★☆☆)" 528 | ] 529 | }, 530 | { 531 | "cell_type": "code", 532 | "execution_count": 18, 533 | "metadata": {}, 534 | "outputs": [ 535 | { 536 | "name": "stdout", 537 | "output_type": "stream", 538 | "text": [ 539 | "[[0 0 0 0 0]\n", 540 | " [1 0 0 0 0]\n", 541 | " [0 2 0 0 0]\n", 542 | " [0 0 3 0 0]\n", 543 | " [0 0 0 4 0]]\n" 544 | ] 545 | } 546 | ], 547 | "source": [ 548 | "Z = np.diag(1+np.arange(4),k=-1)\n", 549 | "print(Z)" 550 | ] 551 | }, 552 | { 553 | "cell_type": "markdown", 554 | "metadata": {}, 555 | "source": [ 556 | "#### 19. Create a 8x8 matrix and fill it with a checkerboard pattern (★☆☆)" 557 | ] 558 | }, 559 | { 560 | "cell_type": "code", 561 | "execution_count": 19, 562 | "metadata": {}, 563 | "outputs": [ 564 | { 565 | "name": "stdout", 566 | "output_type": "stream", 567 | "text": [ 568 | "[[0 1 0 1 0 1 0 1]\n", 569 | " [1 0 1 0 1 0 1 0]\n", 570 | " [0 1 0 1 0 1 0 1]\n", 571 | " [1 0 1 0 1 0 1 0]\n", 572 | " [0 1 0 1 0 1 0 1]\n", 573 | " [1 0 1 0 1 0 1 0]\n", 574 | " [0 1 0 1 0 1 0 1]\n", 575 | " [1 0 1 0 1 0 1 0]]\n" 576 | ] 577 | } 578 | ], 579 | "source": [ 580 | "Z = np.zeros((8,8),dtype=int)\n", 581 | "Z[1::2,::2] = 1\n", 582 | "Z[::2,1::2] = 1\n", 583 | "print(Z)" 584 | ] 585 | }, 586 | { 587 | "cell_type": "markdown", 588 | "metadata": {}, 589 | "source": [ 590 | "#### 20. Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element? (★☆☆)" 591 | ] 592 | }, 593 | { 594 | "cell_type": "code", 595 | "execution_count": 20, 596 | "metadata": {}, 597 | "outputs": [ 598 | { 599 | "name": "stdout", 600 | "output_type": "stream", 601 | "text": [ 602 | "(1, 5, 3)\n" 603 | ] 604 | } 605 | ], 606 | "source": [ 607 | "print(np.unravel_index(99,(6,7,8)))" 608 | ] 609 | }, 610 | { 611 | "cell_type": "markdown", 612 | "metadata": {}, 613 | "source": [ 614 | "#### 21. Create a checkerboard 8x8 matrix using the tile function (★☆☆)" 615 | ] 616 | }, 617 | { 618 | "cell_type": "code", 619 | "execution_count": 21, 620 | "metadata": {}, 621 | "outputs": [ 622 | { 623 | "name": "stdout", 624 | "output_type": "stream", 625 | "text": [ 626 | "[[0 1 0 1 0 1 0 1]\n", 627 | " [1 0 1 0 1 0 1 0]\n", 628 | " [0 1 0 1 0 1 0 1]\n", 629 | " [1 0 1 0 1 0 1 0]\n", 630 | " [0 1 0 1 0 1 0 1]\n", 631 | " [1 0 1 0 1 0 1 0]\n", 632 | " [0 1 0 1 0 1 0 1]\n", 633 | " [1 0 1 0 1 0 1 0]]\n" 634 | ] 635 | } 636 | ], 637 | "source": [ 638 | "Z = np.tile( np.array([[0,1],[1,0]]), (4,4))\n", 639 | "print(Z)" 640 | ] 641 | }, 642 | { 643 | "cell_type": "markdown", 644 | "metadata": {}, 645 | "source": [ 646 | "#### 22. Normalize a 5x5 random matrix (★☆☆)" 647 | ] 648 | }, 649 | { 650 | "cell_type": "code", 651 | "execution_count": 22, 652 | "metadata": {}, 653 | "outputs": [ 654 | { 655 | "name": "stdout", 656 | "output_type": "stream", 657 | "text": [ 658 | "[[-1.74811662 -1.69791986 0.65066729 0.46081984 -0.43094106]\n", 659 | " [-1.62926647 -0.29487521 -0.12282355 0.74437843 -0.4941109 ]\n", 660 | " [ 0.45506266 -0.09739051 -0.19063915 1.35408676 0.74032523]\n", 661 | " [ 1.04400383 1.19033517 0.27554999 0.25706717 -1.58611491]\n", 662 | " [ 0.99572669 1.08368108 -1.50298526 -0.80373467 1.34721402]]\n" 663 | ] 664 | } 665 | ], 666 | "source": [ 667 | "Z = np.random.random((5,5))\n", 668 | "Z = (Z - np.mean (Z)) / (np.std (Z))\n", 669 | "print(Z)" 670 | ] 671 | }, 672 | { 673 | "cell_type": "markdown", 674 | "metadata": {}, 675 | "source": [ 676 | "#### 23. Create a custom dtype that describes a color as four unsigned bytes (RGBA) (★☆☆)" 677 | ] 678 | }, 679 | { 680 | "cell_type": "code", 681 | "execution_count": 23, 682 | "metadata": {}, 683 | "outputs": [], 684 | "source": [ 685 | "color = np.dtype([(\"r\", np.ubyte),\n", 686 | " (\"g\", np.ubyte),\n", 687 | " (\"b\", np.ubyte),\n", 688 | " (\"a\", np.ubyte)])" 689 | ] 690 | }, 691 | { 692 | "cell_type": "markdown", 693 | "metadata": {}, 694 | "source": [ 695 | "#### 24. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) (★☆☆)" 696 | ] 697 | }, 698 | { 699 | "cell_type": "code", 700 | "execution_count": 24, 701 | "metadata": {}, 702 | "outputs": [ 703 | { 704 | "name": "stdout", 705 | "output_type": "stream", 706 | "text": [ 707 | "[[3. 3.]\n", 708 | " [3. 3.]\n", 709 | " [3. 3.]\n", 710 | " [3. 3.]\n", 711 | " [3. 3.]]\n", 712 | "[[3. 3.]\n", 713 | " [3. 3.]\n", 714 | " [3. 3.]\n", 715 | " [3. 3.]\n", 716 | " [3. 3.]]\n" 717 | ] 718 | } 719 | ], 720 | "source": [ 721 | "Z = np.dot(np.ones((5,3)), np.ones((3,2)))\n", 722 | "print(Z)\n", 723 | "\n", 724 | "# Alternative solution, in Python 3.5 and above\n", 725 | "Z = np.ones((5,3)) @ np.ones((3,2))\n", 726 | "print(Z)" 727 | ] 728 | }, 729 | { 730 | "cell_type": "markdown", 731 | "metadata": {}, 732 | "source": [ 733 | "#### 25. Given a 1D array, negate all elements which are between 3 and 8, in place. (★☆☆)" 734 | ] 735 | }, 736 | { 737 | "cell_type": "code", 738 | "execution_count": 25, 739 | "metadata": {}, 740 | "outputs": [ 741 | { 742 | "name": "stdout", 743 | "output_type": "stream", 744 | "text": [ 745 | "[ 0 1 2 3 -4 -5 -6 -7 8 9 10]\n" 746 | ] 747 | } 748 | ], 749 | "source": [ 750 | "\n", 751 | "Z = np.arange(11)\n", 752 | "Z[(3 < Z) & (Z < 8)] *= -1\n", 753 | "print(Z)" 754 | ] 755 | }, 756 | { 757 | "cell_type": "markdown", 758 | "metadata": {}, 759 | "source": [ 760 | "#### 26. What is the output of the following script? (★☆☆)\n", 761 | "```python\n", 762 | "\n", 763 | "\n", 764 | "print(sum(range(5),-1))\n", 765 | "from numpy import *\n", 766 | "print(sum(range(5),-1))\n", 767 | "```" 768 | ] 769 | }, 770 | { 771 | "cell_type": "code", 772 | "execution_count": 26, 773 | "metadata": {}, 774 | "outputs": [ 775 | { 776 | "name": "stdout", 777 | "output_type": "stream", 778 | "text": [ 779 | "9\n", 780 | "10\n" 781 | ] 782 | } 783 | ], 784 | "source": [ 785 | "\n", 786 | "print(sum(range(5),-1))\n", 787 | "from numpy import *\n", 788 | "print(sum(range(5),-1))" 789 | ] 790 | }, 791 | { 792 | "cell_type": "code", 793 | "execution_count": 27, 794 | "metadata": { 795 | "scrolled": true 796 | }, 797 | "outputs": [ 798 | { 799 | "name": "stdout", 800 | "output_type": "stream", 801 | "text": [ 802 | "10\n", 803 | "10\n" 804 | ] 805 | } 806 | ], 807 | "source": [ 808 | "\n", 809 | "print(sum(range(5),-1))\n", 810 | "from numpy import *\n", 811 | "print(sum(range(5),-1))" 812 | ] 813 | }, 814 | { 815 | "cell_type": "code", 816 | "execution_count": null, 817 | "metadata": {}, 818 | "outputs": [], 819 | "source": [] 820 | }, 821 | { 822 | "cell_type": "markdown", 823 | "metadata": {}, 824 | "source": [ 825 | "#### 27. Consider an integer vector Z, which of these expressions are legal? (★☆☆)\n", 826 | "```python\n", 827 | "Z**Z\n", 828 | "2 << Z >> 2\n", 829 | "Z <- Z\n", 830 | "1j*Z\n", 831 | "Z/1/1\n", 832 | "ZZ\n", 833 | "```" 834 | ] 835 | }, 836 | { 837 | "cell_type": "code", 838 | "execution_count": 28, 839 | "metadata": { 840 | "scrolled": true 841 | }, 842 | "outputs": [ 843 | { 844 | "ename": "ValueError", 845 | "evalue": "Integers to negative integer powers are not allowed.", 846 | "output_type": "error", 847 | "traceback": [ 848 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 849 | "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", 850 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mZ\u001b[0m\u001b[1;33m**\u001b[0m\u001b[0mZ\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[1;36m2\u001b[0m \u001b[1;33m<<\u001b[0m \u001b[0mZ\u001b[0m \u001b[1;33m>>\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mZ\u001b[0m \u001b[1;33m<\u001b[0m\u001b[1;33m-\u001b[0m \u001b[0mZ\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;36m1j\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0mZ\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0mZ\u001b[0m\u001b[1;33m/\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m/\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 851 | "\u001b[1;31mValueError\u001b[0m: Integers to negative integer powers are not allowed." 852 | ] 853 | } 854 | ], 855 | "source": [ 856 | "Z**Z\n", 857 | "2 << Z >> 2\n", 858 | "Z <- Z\n", 859 | "1j*Z\n", 860 | "Z/1/1\n", 861 | "ZZ" 862 | ] 863 | }, 864 | { 865 | "cell_type": "code", 866 | "execution_count": null, 867 | "metadata": {}, 868 | "outputs": [], 869 | "source": [ 870 | "Z**Z\n", 871 | "2 << Z >> 2\n", 872 | "Z <- Z\n", 873 | "1j*Z\n", 874 | "Z/1/1\n", 875 | "ZZ" 876 | ] 877 | }, 878 | { 879 | "cell_type": "markdown", 880 | "metadata": {}, 881 | "source": [ 882 | "#### 28. What are the result of the following expressions? (★☆☆)\n", 883 | "```python\n", 884 | "np.array(0) / np.array(0)\n", 885 | "np.array(0) // np.array(0)\n", 886 | "np.array([np.nan]).astype(int).astype(float)\n", 887 | "```" 888 | ] 889 | }, 890 | { 891 | "cell_type": "code", 892 | "execution_count": 29, 893 | "metadata": { 894 | "scrolled": true 895 | }, 896 | "outputs": [ 897 | { 898 | "name": "stderr", 899 | "output_type": "stream", 900 | "text": [ 901 | "C:\\Users\\Devdatta Supnekar\\anaconda3\\lib\\site-packages\\ipykernel_launcher.py:1: RuntimeWarning: invalid value encountered in true_divide\n", 902 | " \"\"\"Entry point for launching an IPython kernel.\n", 903 | "C:\\Users\\Devdatta Supnekar\\anaconda3\\lib\\site-packages\\ipykernel_launcher.py:2: RuntimeWarning: divide by zero encountered in floor_divide\n", 904 | " \n" 905 | ] 906 | }, 907 | { 908 | "data": { 909 | "text/plain": [ 910 | "array([-2.14748365e+09])" 911 | ] 912 | }, 913 | "execution_count": 29, 914 | "metadata": {}, 915 | "output_type": "execute_result" 916 | } 917 | ], 918 | "source": [ 919 | "np.array(0) / np.array(0)\n", 920 | "np.array(0) // np.array(0)\n", 921 | "np.array([np.nan]).astype(int).astype(float)" 922 | ] 923 | }, 924 | { 925 | "cell_type": "code", 926 | "execution_count": 30, 927 | "metadata": {}, 928 | "outputs": [ 929 | { 930 | "name": "stdout", 931 | "output_type": "stream", 932 | "text": [ 933 | "nan\n", 934 | "0\n", 935 | "[-2.14748365e+09]\n" 936 | ] 937 | }, 938 | { 939 | "name": "stderr", 940 | "output_type": "stream", 941 | "text": [ 942 | "C:\\Users\\Devdatta Supnekar\\anaconda3\\lib\\site-packages\\ipykernel_launcher.py:1: RuntimeWarning: invalid value encountered in true_divide\n", 943 | " \"\"\"Entry point for launching an IPython kernel.\n", 944 | "C:\\Users\\Devdatta Supnekar\\anaconda3\\lib\\site-packages\\ipykernel_launcher.py:2: RuntimeWarning: divide by zero encountered in floor_divide\n", 945 | " \n" 946 | ] 947 | } 948 | ], 949 | "source": [ 950 | "print(np.array(0) / np.array(0))\n", 951 | "print(np.array(0) // np.array(0))\n", 952 | "print(np.array([np.nan]).astype(int).astype(float))" 953 | ] 954 | }, 955 | { 956 | "cell_type": "markdown", 957 | "metadata": {}, 958 | "source": [ 959 | "#### 29. How to round away from zero a float array ? (★☆☆)" 960 | ] 961 | }, 962 | { 963 | "cell_type": "code", 964 | "execution_count": 31, 965 | "metadata": {}, 966 | "outputs": [ 967 | { 968 | "name": "stdout", 969 | "output_type": "stream", 970 | "text": [ 971 | "[ 8. 3. 2. 7. 1. 3. 7. 3. 5. -7.]\n", 972 | "[ 8. 3. 2. 7. 1. 3. 7. 3. 5. -7.]\n" 973 | ] 974 | } 975 | ], 976 | "source": [ 977 | "# Author: Charles R Harris\n", 978 | "\n", 979 | "Z = np.random.uniform(-10,+10,10)\n", 980 | "print(np.copysign(np.ceil(np.abs(Z)), Z))\n", 981 | "\n", 982 | "# More readable but less efficient\n", 983 | "print(np.where(Z>0, np.ceil(Z), np.floor(Z)))" 984 | ] 985 | }, 986 | { 987 | "cell_type": "markdown", 988 | "metadata": {}, 989 | "source": [ 990 | "#### 30. How to find common values between two arrays? (★☆☆)" 991 | ] 992 | }, 993 | { 994 | "cell_type": "code", 995 | "execution_count": 32, 996 | "metadata": {}, 997 | "outputs": [ 998 | { 999 | "name": "stdout", 1000 | "output_type": "stream", 1001 | "text": [ 1002 | "[0 1 3 4 6 7]\n" 1003 | ] 1004 | } 1005 | ], 1006 | "source": [ 1007 | "Z1 = np.random.randint(0,10,10)\n", 1008 | "Z2 = np.random.randint(0,10,10)\n", 1009 | "print(np.intersect1d(Z1,Z2))" 1010 | ] 1011 | }, 1012 | { 1013 | "cell_type": "markdown", 1014 | "metadata": {}, 1015 | "source": [ 1016 | "#### 31. How to ignore all numpy warnings (not recommended)? (★☆☆)" 1017 | ] 1018 | }, 1019 | { 1020 | "cell_type": "code", 1021 | "execution_count": 33, 1022 | "metadata": {}, 1023 | "outputs": [], 1024 | "source": [ 1025 | "# Suicide mode on\n", 1026 | "defaults = np.seterr(all=\"ignore\")\n", 1027 | "Z = np.ones(1) / 0\n", 1028 | "\n", 1029 | "# Back to sanity\n", 1030 | "_ = np.seterr(**defaults)\n", 1031 | "\n", 1032 | "# Equivalently with a context manager\n", 1033 | "with np.errstate(all=\"ignore\"):\n", 1034 | " np.arange(3) / 0" 1035 | ] 1036 | }, 1037 | { 1038 | "cell_type": "markdown", 1039 | "metadata": {}, 1040 | "source": [ 1041 | "#### 32. Is the following expressions true? (★☆☆)\n", 1042 | "```python\n", 1043 | "np.sqrt(-1) == np.emath.sqrt(-1)\n", 1044 | "```" 1045 | ] 1046 | }, 1047 | { 1048 | "cell_type": "code", 1049 | "execution_count": 34, 1050 | "metadata": {}, 1051 | "outputs": [ 1052 | { 1053 | "name": "stderr", 1054 | "output_type": "stream", 1055 | "text": [ 1056 | "C:\\Users\\Devdatta Supnekar\\anaconda3\\lib\\site-packages\\ipykernel_launcher.py:1: RuntimeWarning: invalid value encountered in sqrt\n", 1057 | " \"\"\"Entry point for launching an IPython kernel.\n" 1058 | ] 1059 | }, 1060 | { 1061 | "data": { 1062 | "text/plain": [ 1063 | "False" 1064 | ] 1065 | }, 1066 | "execution_count": 34, 1067 | "metadata": {}, 1068 | "output_type": "execute_result" 1069 | } 1070 | ], 1071 | "source": [ 1072 | "np.sqrt(-1) == np.emath.sqrt(-1)" 1073 | ] 1074 | }, 1075 | { 1076 | "cell_type": "code", 1077 | "execution_count": 35, 1078 | "metadata": {}, 1079 | "outputs": [ 1080 | { 1081 | "name": "stderr", 1082 | "output_type": "stream", 1083 | "text": [ 1084 | "C:\\Users\\Devdatta Supnekar\\anaconda3\\lib\\site-packages\\ipykernel_launcher.py:1: RuntimeWarning: invalid value encountered in sqrt\n", 1085 | " \"\"\"Entry point for launching an IPython kernel.\n" 1086 | ] 1087 | }, 1088 | { 1089 | "data": { 1090 | "text/plain": [ 1091 | "False" 1092 | ] 1093 | }, 1094 | "execution_count": 35, 1095 | "metadata": {}, 1096 | "output_type": "execute_result" 1097 | } 1098 | ], 1099 | "source": [ 1100 | "np.sqrt(-1) == np.emath.sqrt(-1)" 1101 | ] 1102 | }, 1103 | { 1104 | "cell_type": "markdown", 1105 | "metadata": {}, 1106 | "source": [ 1107 | "#### 33. How to get the dates of yesterday, today and tomorrow? (★☆☆)" 1108 | ] 1109 | }, 1110 | { 1111 | "cell_type": "code", 1112 | "execution_count": 36, 1113 | "metadata": {}, 1114 | "outputs": [], 1115 | "source": [ 1116 | "yesterday = np.datetime64('today') - np.timedelta64(1)\n", 1117 | "today = np.datetime64('today')\n", 1118 | "tomorrow = np.datetime64('today') + np.timedelta64(1)" 1119 | ] 1120 | }, 1121 | { 1122 | "cell_type": "markdown", 1123 | "metadata": {}, 1124 | "source": [ 1125 | "#### 34. How to get all the dates corresponding to the month of July 2016? (★★☆)" 1126 | ] 1127 | }, 1128 | { 1129 | "cell_type": "code", 1130 | "execution_count": 37, 1131 | "metadata": {}, 1132 | "outputs": [ 1133 | { 1134 | "name": "stdout", 1135 | "output_type": "stream", 1136 | "text": [ 1137 | "['2016-07-01' '2016-07-02' '2016-07-03' '2016-07-04' '2016-07-05'\n", 1138 | " '2016-07-06' '2016-07-07' '2016-07-08' '2016-07-09' '2016-07-10'\n", 1139 | " '2016-07-11' '2016-07-12' '2016-07-13' '2016-07-14' '2016-07-15'\n", 1140 | " '2016-07-16' '2016-07-17' '2016-07-18' '2016-07-19' '2016-07-20'\n", 1141 | " '2016-07-21' '2016-07-22' '2016-07-23' '2016-07-24' '2016-07-25'\n", 1142 | " '2016-07-26' '2016-07-27' '2016-07-28' '2016-07-29' '2016-07-30'\n", 1143 | " '2016-07-31']\n" 1144 | ] 1145 | } 1146 | ], 1147 | "source": [ 1148 | "Z = np.arange('2016-07', '2016-08', dtype='datetime64[D]')\n", 1149 | "print(Z)" 1150 | ] 1151 | }, 1152 | { 1153 | "cell_type": "markdown", 1154 | "metadata": {}, 1155 | "source": [ 1156 | "#### 35. How to compute ((A+B)*(-A/2)) in place (without copy)? (★★☆)" 1157 | ] 1158 | }, 1159 | { 1160 | "cell_type": "code", 1161 | "execution_count": 38, 1162 | "metadata": {}, 1163 | "outputs": [ 1164 | { 1165 | "data": { 1166 | "text/plain": [ 1167 | "array([-1.5, -1.5, -1.5])" 1168 | ] 1169 | }, 1170 | "execution_count": 38, 1171 | "metadata": {}, 1172 | "output_type": "execute_result" 1173 | } 1174 | ], 1175 | "source": [ 1176 | "A = np.ones(3)*1\n", 1177 | "B = np.ones(3)*2\n", 1178 | "np.add(A,B,out=B)\n", 1179 | "np.divide(A,2,out=A)\n", 1180 | "np.negative(A,out=A)\n", 1181 | "np.multiply(A,B,out=A)" 1182 | ] 1183 | }, 1184 | { 1185 | "cell_type": "markdown", 1186 | "metadata": {}, 1187 | "source": [ 1188 | "#### 36. Extract the integer part of a random array of positive numbers using 4 different methods (★★☆)" 1189 | ] 1190 | }, 1191 | { 1192 | "cell_type": "code", 1193 | "execution_count": 39, 1194 | "metadata": {}, 1195 | "outputs": [ 1196 | { 1197 | "name": "stdout", 1198 | "output_type": "stream", 1199 | "text": [ 1200 | "[9. 4. 8. 4. 6. 2. 2. 9. 6. 5.]\n", 1201 | "[9. 4. 8. 4. 6. 2. 2. 9. 6. 5.]\n", 1202 | "[9. 4. 8. 4. 6. 2. 2. 9. 6. 5.]\n", 1203 | "[9 4 8 4 6 2 2 9 6 5]\n", 1204 | "[9. 4. 8. 4. 6. 2. 2. 9. 6. 5.]\n" 1205 | ] 1206 | } 1207 | ], 1208 | "source": [ 1209 | "Z = np.random.uniform(0,10,10)\n", 1210 | "\n", 1211 | "print(Z - Z%1)\n", 1212 | "print(Z // 1)\n", 1213 | "print(np.floor(Z))\n", 1214 | "print(Z.astype(int))\n", 1215 | "print(np.trunc(Z))" 1216 | ] 1217 | }, 1218 | { 1219 | "cell_type": "markdown", 1220 | "metadata": {}, 1221 | "source": [ 1222 | "#### 37. Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆)" 1223 | ] 1224 | }, 1225 | { 1226 | "cell_type": "code", 1227 | "execution_count": 40, 1228 | "metadata": {}, 1229 | "outputs": [ 1230 | { 1231 | "name": "stdout", 1232 | "output_type": "stream", 1233 | "text": [ 1234 | "[[0. 1. 2. 3. 4.]\n", 1235 | " [0. 1. 2. 3. 4.]\n", 1236 | " [0. 1. 2. 3. 4.]\n", 1237 | " [0. 1. 2. 3. 4.]\n", 1238 | " [0. 1. 2. 3. 4.]]\n", 1239 | "[[0 1 2 3 4]\n", 1240 | " [0 1 2 3 4]\n", 1241 | " [0 1 2 3 4]\n", 1242 | " [0 1 2 3 4]\n", 1243 | " [0 1 2 3 4]]\n" 1244 | ] 1245 | } 1246 | ], 1247 | "source": [ 1248 | "Z = np.zeros((5,5))\n", 1249 | "Z += np.arange(5)\n", 1250 | "print(Z)\n", 1251 | "\n", 1252 | "# without broadcasting\n", 1253 | "Z = np.tile(np.arange(0, 5), (5,1))\n", 1254 | "print(Z)" 1255 | ] 1256 | }, 1257 | { 1258 | "cell_type": "markdown", 1259 | "metadata": {}, 1260 | "source": [ 1261 | "#### 38. Consider a generator function that generates 10 integers and use it to build an array (★☆☆)" 1262 | ] 1263 | }, 1264 | { 1265 | "cell_type": "code", 1266 | "execution_count": 41, 1267 | "metadata": {}, 1268 | "outputs": [ 1269 | { 1270 | "name": "stdout", 1271 | "output_type": "stream", 1272 | "text": [ 1273 | "[0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]\n" 1274 | ] 1275 | } 1276 | ], 1277 | "source": [ 1278 | "def generate():\n", 1279 | " for x in range(10):\n", 1280 | " yield x\n", 1281 | "Z = np.fromiter(generate(),dtype=float,count=-1)\n", 1282 | "print(Z)" 1283 | ] 1284 | }, 1285 | { 1286 | "cell_type": "markdown", 1287 | "metadata": {}, 1288 | "source": [ 1289 | "#### 39. Create a vector of size 10 with values ranging from 0 to 1, both excluded (★★☆)" 1290 | ] 1291 | }, 1292 | { 1293 | "cell_type": "code", 1294 | "execution_count": 42, 1295 | "metadata": {}, 1296 | "outputs": [ 1297 | { 1298 | "name": "stdout", 1299 | "output_type": "stream", 1300 | "text": [ 1301 | "[0.09090909 0.18181818 0.27272727 0.36363636 0.45454545 0.54545455\n", 1302 | " 0.63636364 0.72727273 0.81818182 0.90909091]\n" 1303 | ] 1304 | } 1305 | ], 1306 | "source": [ 1307 | "Z = np.linspace(0,1,11,endpoint=False)[1:]\n", 1308 | "print(Z)" 1309 | ] 1310 | }, 1311 | { 1312 | "cell_type": "markdown", 1313 | "metadata": {}, 1314 | "source": [ 1315 | "#### 40. Create a random vector of size 10 and sort it (★★☆)" 1316 | ] 1317 | }, 1318 | { 1319 | "cell_type": "code", 1320 | "execution_count": 43, 1321 | "metadata": {}, 1322 | "outputs": [ 1323 | { 1324 | "name": "stdout", 1325 | "output_type": "stream", 1326 | "text": [ 1327 | "[0.0017567 0.15614578 0.2056483 0.31064996 0.37393274 0.67677452\n", 1328 | " 0.87285649 0.89093117 0.90037661 0.93688901]\n" 1329 | ] 1330 | } 1331 | ], 1332 | "source": [ 1333 | "Z = np.random.random(10)\n", 1334 | "Z.sort()\n", 1335 | "print(Z)" 1336 | ] 1337 | }, 1338 | { 1339 | "cell_type": "markdown", 1340 | "metadata": {}, 1341 | "source": [ 1342 | "#### 41. How to sum a small array faster than np.sum? (★★☆)" 1343 | ] 1344 | }, 1345 | { 1346 | "cell_type": "code", 1347 | "execution_count": 44, 1348 | "metadata": {}, 1349 | "outputs": [ 1350 | { 1351 | "data": { 1352 | "text/plain": [ 1353 | "45" 1354 | ] 1355 | }, 1356 | "execution_count": 44, 1357 | "metadata": {}, 1358 | "output_type": "execute_result" 1359 | } 1360 | ], 1361 | "source": [ 1362 | "\n", 1363 | "Z = np.arange(10)\n", 1364 | "np.add.reduce(Z)" 1365 | ] 1366 | }, 1367 | { 1368 | "cell_type": "markdown", 1369 | "metadata": {}, 1370 | "source": [ 1371 | "#### 42. Consider two random array A and B, check if they are equal (★★☆)" 1372 | ] 1373 | }, 1374 | { 1375 | "cell_type": "code", 1376 | "execution_count": 45, 1377 | "metadata": {}, 1378 | "outputs": [ 1379 | { 1380 | "name": "stdout", 1381 | "output_type": "stream", 1382 | "text": [ 1383 | "False\n", 1384 | "False\n" 1385 | ] 1386 | } 1387 | ], 1388 | "source": [ 1389 | "A = np.random.randint(0,2,5)\n", 1390 | "B = np.random.randint(0,2,5)\n", 1391 | "\n", 1392 | "# Assuming identical shape of the arrays and a tolerance for the comparison of values\n", 1393 | "equal = np.allclose(A,B)\n", 1394 | "print(equal)\n", 1395 | "\n", 1396 | "# Checking both the shape and the element values, no tolerance (values have to be exactly equal)\n", 1397 | "equal = np.array_equal(A,B)\n", 1398 | "print(equal)" 1399 | ] 1400 | }, 1401 | { 1402 | "cell_type": "markdown", 1403 | "metadata": {}, 1404 | "source": [ 1405 | "#### 43. Make an array immutable (read-only) (★★☆)" 1406 | ] 1407 | }, 1408 | { 1409 | "cell_type": "code", 1410 | "execution_count": 46, 1411 | "metadata": {}, 1412 | "outputs": [ 1413 | { 1414 | "ename": "ValueError", 1415 | "evalue": "assignment destination is read-only", 1416 | "output_type": "error", 1417 | "traceback": [ 1418 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 1419 | "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", 1420 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mZ\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mzeros\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0mZ\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mflags\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwriteable\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mFalse\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mZ\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 1421 | "\u001b[1;31mValueError\u001b[0m: assignment destination is read-only" 1422 | ] 1423 | } 1424 | ], 1425 | "source": [ 1426 | "Z = np.zeros(10)\n", 1427 | "Z.flags.writeable = False\n", 1428 | "Z[0] = 1" 1429 | ] 1430 | }, 1431 | { 1432 | "cell_type": "markdown", 1433 | "metadata": {}, 1434 | "source": [ 1435 | "#### 44. Consider a random 10x2 matrix representing cartesian coordinates, convert them to polar coordinates (★★☆)" 1436 | ] 1437 | }, 1438 | { 1439 | "cell_type": "code", 1440 | "execution_count": 47, 1441 | "metadata": {}, 1442 | "outputs": [ 1443 | { 1444 | "name": "stdout", 1445 | "output_type": "stream", 1446 | "text": [ 1447 | "[0.85071116 0.64996305 1.00570577 0.71719478 1.04849093 0.55379273\n", 1448 | " 1.10331269 0.93034318 1.12751479 0.32964632]\n", 1449 | "[1.26191604 0.17990364 0.5322042 1.00827928 1.16401571 1.13006701\n", 1450 | " 0.8067915 0.51664443 0.78903659 1.28949452]\n" 1451 | ] 1452 | } 1453 | ], 1454 | "source": [ 1455 | "Z = np.random.random((10,2))\n", 1456 | "X,Y = Z[:,0], Z[:,1]\n", 1457 | "R = np.sqrt(X**2+Y**2)\n", 1458 | "T = np.arctan2(Y,X)\n", 1459 | "print(R)\n", 1460 | "print(T)" 1461 | ] 1462 | }, 1463 | { 1464 | "cell_type": "markdown", 1465 | "metadata": {}, 1466 | "source": [ 1467 | "#### 45. Create random vector of size 10 and replace the maximum value by 0 (★★☆)" 1468 | ] 1469 | }, 1470 | { 1471 | "cell_type": "code", 1472 | "execution_count": 48, 1473 | "metadata": {}, 1474 | "outputs": [ 1475 | { 1476 | "name": "stdout", 1477 | "output_type": "stream", 1478 | "text": [ 1479 | "[0.29310298 0. 0.47579003 0.02226686 0.56230739 0.09663111\n", 1480 | " 0.21410262 0.74906189 0.75847845 0.73392849]\n" 1481 | ] 1482 | } 1483 | ], 1484 | "source": [ 1485 | "Z = np.random.random(10)\n", 1486 | "Z[Z.argmax()] = 0\n", 1487 | "print(Z)" 1488 | ] 1489 | }, 1490 | { 1491 | "cell_type": "markdown", 1492 | "metadata": {}, 1493 | "source": [ 1494 | "#### 46. Create a structured array with `x` and `y` coordinates covering the [0,1]x[0,1] area (★★☆)" 1495 | ] 1496 | }, 1497 | { 1498 | "cell_type": "code", 1499 | "execution_count": 49, 1500 | "metadata": {}, 1501 | "outputs": [ 1502 | { 1503 | "name": "stdout", 1504 | "output_type": "stream", 1505 | "text": [ 1506 | "[[(0. , 0. ) (0.25, 0. ) (0.5 , 0. ) (0.75, 0. ) (1. , 0. )]\n", 1507 | " [(0. , 0.25) (0.25, 0.25) (0.5 , 0.25) (0.75, 0.25) (1. , 0.25)]\n", 1508 | " [(0. , 0.5 ) (0.25, 0.5 ) (0.5 , 0.5 ) (0.75, 0.5 ) (1. , 0.5 )]\n", 1509 | " [(0. , 0.75) (0.25, 0.75) (0.5 , 0.75) (0.75, 0.75) (1. , 0.75)]\n", 1510 | " [(0. , 1. ) (0.25, 1. ) (0.5 , 1. ) (0.75, 1. ) (1. , 1. )]]\n" 1511 | ] 1512 | } 1513 | ], 1514 | "source": [ 1515 | "Z = np.zeros((5,5), [('x',float),('y',float)])\n", 1516 | "Z['x'], Z['y'] = np.meshgrid(np.linspace(0,1,5),\n", 1517 | " np.linspace(0,1,5))\n", 1518 | "print(Z)" 1519 | ] 1520 | }, 1521 | { 1522 | "cell_type": "markdown", 1523 | "metadata": {}, 1524 | "source": [ 1525 | "#### 47. Given two arrays, X and Y, construct the Cauchy matrix C (Cij =1/(xi - yj)) (★★☆)" 1526 | ] 1527 | }, 1528 | { 1529 | "cell_type": "code", 1530 | "execution_count": 50, 1531 | "metadata": {}, 1532 | "outputs": [ 1533 | { 1534 | "name": "stdout", 1535 | "output_type": "stream", 1536 | "text": [ 1537 | "3638.163637117973\n" 1538 | ] 1539 | } 1540 | ], 1541 | "source": [ 1542 | "\n", 1543 | "X = np.arange(8)\n", 1544 | "Y = X + 0.5\n", 1545 | "C = 1.0 / np.subtract.outer(X, Y)\n", 1546 | "print(np.linalg.det(C))" 1547 | ] 1548 | }, 1549 | { 1550 | "cell_type": "markdown", 1551 | "metadata": {}, 1552 | "source": [ 1553 | "#### 48. Print the minimum and maximum representable value for each numpy scalar type (★★☆)" 1554 | ] 1555 | }, 1556 | { 1557 | "cell_type": "code", 1558 | "execution_count": 51, 1559 | "metadata": {}, 1560 | "outputs": [ 1561 | { 1562 | "name": "stdout", 1563 | "output_type": "stream", 1564 | "text": [ 1565 | "-128\n", 1566 | "127\n", 1567 | "-2147483648\n", 1568 | "2147483647\n", 1569 | "-9223372036854775808\n", 1570 | "9223372036854775807\n", 1571 | "-3.4028235e+38\n", 1572 | "3.4028235e+38\n", 1573 | "1.1920929e-07\n", 1574 | "-1.7976931348623157e+308\n", 1575 | "1.7976931348623157e+308\n", 1576 | "2.220446049250313e-16\n" 1577 | ] 1578 | } 1579 | ], 1580 | "source": [ 1581 | "for dtype in [np.int8, np.int32, np.int64]:\n", 1582 | " print(np.iinfo(dtype).min)\n", 1583 | " print(np.iinfo(dtype).max)\n", 1584 | "for dtype in [np.float32, np.float64]:\n", 1585 | " print(np.finfo(dtype).min)\n", 1586 | " print(np.finfo(dtype).max)\n", 1587 | " print(np.finfo(dtype).eps)" 1588 | ] 1589 | }, 1590 | { 1591 | "cell_type": "markdown", 1592 | "metadata": {}, 1593 | "source": [ 1594 | "#### 49. How to print all the values of an array? (★★☆)" 1595 | ] 1596 | }, 1597 | { 1598 | "cell_type": "code", 1599 | "execution_count": 52, 1600 | "metadata": {}, 1601 | "outputs": [ 1602 | { 1603 | "name": "stdout", 1604 | "output_type": "stream", 1605 | "text": [ 1606 | "[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1607 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1608 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1609 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1610 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1611 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1612 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1613 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1614 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1615 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1616 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1617 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1618 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1619 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1620 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1621 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1622 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1623 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1624 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1625 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1626 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1627 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1628 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1629 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1630 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1631 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1632 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1633 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1634 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1635 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1636 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1637 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1638 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1639 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1640 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1641 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1642 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1643 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1644 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1645 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1646 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1647 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1648 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1649 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1650 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1651 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1652 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1653 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1654 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1655 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1656 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1657 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1658 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1659 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1660 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1661 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1662 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1663 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1664 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1665 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1666 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1667 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1668 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1669 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1670 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1671 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1672 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1673 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1674 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1675 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1676 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1677 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1678 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1679 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1680 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1681 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1682 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1683 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 1684 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 1685 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]\n" 1686 | ] 1687 | } 1688 | ], 1689 | "source": [ 1690 | "np.set_printoptions(threshold=float(\"inf\"))\n", 1691 | "Z = np.zeros((40,40))\n", 1692 | "print(Z)" 1693 | ] 1694 | }, 1695 | { 1696 | "cell_type": "markdown", 1697 | "metadata": {}, 1698 | "source": [ 1699 | "#### 50. How to find the closest value (to a given scalar) in a vector? (★★☆)" 1700 | ] 1701 | }, 1702 | { 1703 | "cell_type": "code", 1704 | "execution_count": 53, 1705 | "metadata": {}, 1706 | "outputs": [ 1707 | { 1708 | "name": "stdout", 1709 | "output_type": "stream", 1710 | "text": [ 1711 | "23\n" 1712 | ] 1713 | } 1714 | ], 1715 | "source": [ 1716 | "Z = np.arange(100)\n", 1717 | "v = np.random.uniform(0,100)\n", 1718 | "index = (np.abs(Z-v)).argmin()\n", 1719 | "print(Z[index])" 1720 | ] 1721 | }, 1722 | { 1723 | "cell_type": "markdown", 1724 | "metadata": {}, 1725 | "source": [ 1726 | "#### 51. Create a structured array representing a position (x,y) and a color (r,g,b) (★★☆)" 1727 | ] 1728 | }, 1729 | { 1730 | "cell_type": "code", 1731 | "execution_count": 54, 1732 | "metadata": {}, 1733 | "outputs": [ 1734 | { 1735 | "name": "stdout", 1736 | "output_type": "stream", 1737 | "text": [ 1738 | "[((0., 0.), (0., 0., 0.)) ((0., 0.), (0., 0., 0.))\n", 1739 | " ((0., 0.), (0., 0., 0.)) ((0., 0.), (0., 0., 0.))\n", 1740 | " ((0., 0.), (0., 0., 0.)) ((0., 0.), (0., 0., 0.))\n", 1741 | " ((0., 0.), (0., 0., 0.)) ((0., 0.), (0., 0., 0.))\n", 1742 | " ((0., 0.), (0., 0., 0.)) ((0., 0.), (0., 0., 0.))]\n" 1743 | ] 1744 | }, 1745 | { 1746 | "name": "stderr", 1747 | "output_type": "stream", 1748 | "text": [ 1749 | "C:\\Users\\Devdatta Supnekar\\anaconda3\\lib\\site-packages\\ipykernel_launcher.py:5: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n", 1750 | " \"\"\"\n" 1751 | ] 1752 | } 1753 | ], 1754 | "source": [ 1755 | "Z = np.zeros(10, [ ('position', [ ('x', float, 1),\n", 1756 | " ('y', float, 1)]),\n", 1757 | " ('color', [ ('r', float, 1),\n", 1758 | " ('g', float, 1),\n", 1759 | " ('b', float, 1)])])\n", 1760 | "print(Z)" 1761 | ] 1762 | }, 1763 | { 1764 | "cell_type": "markdown", 1765 | "metadata": {}, 1766 | "source": [ 1767 | "#### 52. Consider a random vector with shape (100,2) representing coordinates, find point by point distances (★★☆)" 1768 | ] 1769 | }, 1770 | { 1771 | "cell_type": "code", 1772 | "execution_count": 55, 1773 | "metadata": {}, 1774 | "outputs": [ 1775 | { 1776 | "name": "stdout", 1777 | "output_type": "stream", 1778 | "text": [ 1779 | "[[0. 0.22806257 0.41355005 0.55763161 0.43634124 0.47594931\n", 1780 | " 0.10270337 0.49048057 0.22984881 0.603245 ]\n", 1781 | " [0.22806257 0. 0.21041294 0.43269927 0.49181634 0.3908971\n", 1782 | " 0.13985775 0.34576951 0.05395532 0.39132612]\n", 1783 | " [0.41355005 0.21041294 0. 0.53517396 0.49529704 0.53282923\n", 1784 | " 0.3441453 0.17935458 0.18572929 0.36135167]\n", 1785 | " [0.55763161 0.43269927 0.53517396 0. 0.92318102 0.11065329\n", 1786 | " 0.4647609 0.71407082 0.48469249 0.29446158]\n", 1787 | " [0.43634124 0.49181634 0.49529704 0.92318102 0. 0.8688371\n", 1788 | " 0.48609386 0.39844899 0.44248921 0.84206579]\n", 1789 | " [0.47594931 0.3908971 0.53282923 0.11065329 0.8688371 0.\n", 1790 | " 0.39176308 0.70655454 0.44482009 0.3660567 ]\n", 1791 | " [0.10270337 0.13985775 0.3441453 0.4647609 0.48609386 0.39176308\n", 1792 | " 0. 0.45269836 0.16007758 0.50124846]\n", 1793 | " [0.49048057 0.34576951 0.17935458 0.71407082 0.39844899 0.70655454\n", 1794 | " 0.45269836 0. 0.30070953 0.52784081]\n", 1795 | " [0.22984881 0.05395532 0.18572929 0.48469249 0.44248921 0.44482009\n", 1796 | " 0.16007758 0.30070953 0. 0.4224619 ]\n", 1797 | " [0.603245 0.39132612 0.36135167 0.29446158 0.84206579 0.3660567\n", 1798 | " 0.50124846 0.52784081 0.4224619 0. ]]\n", 1799 | "[[0. 0.6931619 0.90661838 0.5958553 0.75100033 0.69243664\n", 1800 | " 0.77331569 0.85091014 0.37119809 0.62411075]\n", 1801 | " [0.6931619 0. 0.52203021 0.91178186 0.83541923 0.04270482\n", 1802 | " 0.2381933 1.00251293 0.69331434 0.22002567]\n", 1803 | " [0.90661838 0.52203021 0. 0.74381492 0.52775329 0.56469023\n", 1804 | " 0.75379719 0.67865844 0.66373747 0.73098448]\n", 1805 | " [0.5958553 0.91178186 0.74381492 0. 0.27131383 0.93871536\n", 1806 | " 1.10798266 0.28072229 0.25686745 0.9870284 ]\n", 1807 | " [0.75100033 0.83541923 0.52775329 0.27131383 0. 0.87083663\n", 1808 | " 1.06072418 0.16815932 0.38152953 0.96665899]\n", 1809 | " [0.69243664 0.04270482 0.56469023 0.93871536 0.87083663 0.\n", 1810 | " 0.19688952 1.03738221 0.71456268 0.1820106 ]\n", 1811 | " [0.77331569 0.2381933 0.75379719 1.10798266 1.06072418 0.19688952\n", 1812 | " 0. 1.22582375 0.87006088 0.14941488]\n", 1813 | " [0.85091014 1.00251293 0.67865844 0.28072229 0.16815932 1.03738221\n", 1814 | " 1.22582375 0. 0.48303681 1.12751102]\n", 1815 | " [0.37119809 0.69331434 0.66373747 0.25686745 0.38152953 0.71456268\n", 1816 | " 0.87006088 0.48303681 0. 0.74140984]\n", 1817 | " [0.62411075 0.22002567 0.73098448 0.9870284 0.96665899 0.1820106\n", 1818 | " 0.14941488 1.12751102 0.74140984 0. ]]\n" 1819 | ] 1820 | } 1821 | ], 1822 | "source": [ 1823 | "Z = np.random.random((10,2))\n", 1824 | "X,Y = np.atleast_2d(Z[:,0], Z[:,1])\n", 1825 | "D = np.sqrt( (X-X.T)**2 + (Y-Y.T)**2)\n", 1826 | "print(D)\n", 1827 | "\n", 1828 | "# Much faster with scipy\n", 1829 | "import scipy\n", 1830 | "import scipy.spatial\n", 1831 | "\n", 1832 | "Z = np.random.random((10,2))\n", 1833 | "D = scipy.spatial.distance.cdist(Z,Z)\n", 1834 | "print(D)" 1835 | ] 1836 | }, 1837 | { 1838 | "cell_type": "markdown", 1839 | "metadata": {}, 1840 | "source": [ 1841 | "#### 53. How to convert a float (32 bits) array into an integer (32 bits) in place?" 1842 | ] 1843 | }, 1844 | { 1845 | "cell_type": "code", 1846 | "execution_count": 56, 1847 | "metadata": {}, 1848 | "outputs": [ 1849 | { 1850 | "name": "stdout", 1851 | "output_type": "stream", 1852 | "text": [ 1853 | "[39 90 33 84 26 95 4 59 69 30]\n" 1854 | ] 1855 | } 1856 | ], 1857 | "source": [ 1858 | "Z = (np.random.rand(10)*100).astype(np.float32)\n", 1859 | "Y = Z.view(np.int32)\n", 1860 | "Y[:] = Z\n", 1861 | "print(Y)" 1862 | ] 1863 | }, 1864 | { 1865 | "cell_type": "markdown", 1866 | "metadata": {}, 1867 | "source": [ 1868 | "#### 54. How to read the following file? (★★☆)\n", 1869 | "```\n", 1870 | "1, 2, 3, 4, 5\n", 1871 | "6, , , 7, 8\n", 1872 | " , , 9,10,11\n", 1873 | "```" 1874 | ] 1875 | }, 1876 | { 1877 | "cell_type": "code", 1878 | "execution_count": 57, 1879 | "metadata": {}, 1880 | "outputs": [ 1881 | { 1882 | "name": "stdout", 1883 | "output_type": "stream", 1884 | "text": [ 1885 | "[[ 1 2 3 4 5]\n", 1886 | " [ 6 -1 -1 7 8]\n", 1887 | " [-1 -1 9 10 11]]\n" 1888 | ] 1889 | }, 1890 | { 1891 | "name": "stderr", 1892 | "output_type": "stream", 1893 | "text": [ 1894 | "C:\\Users\\Devdatta Supnekar\\anaconda3\\lib\\site-packages\\ipykernel_launcher.py:10: DeprecationWarning: `np.int` is a deprecated alias for the builtin `int`. To silence this warning, use `int` by itself. Doing this will not modify any behavior and is safe. When replacing `np.int`, you may wish to use e.g. `np.int64` or `np.int32` to specify the precision. If you wish to review your current use, check the release note link for additional information.\n", 1895 | "Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n", 1896 | " # Remove the CWD from sys.path while we load stuff.\n" 1897 | ] 1898 | } 1899 | ], 1900 | "source": [ 1901 | "from io import StringIO\n", 1902 | "\n", 1903 | "# Fake file\n", 1904 | "s = StringIO('''1, 2, 3, 4, 5\n", 1905 | "\n", 1906 | " 6, , , 7, 8\n", 1907 | "\n", 1908 | " , , 9,10,11\n", 1909 | "''')\n", 1910 | "Z = np.genfromtxt(s, delimiter=\",\", dtype=np.int)\n", 1911 | "print(Z)" 1912 | ] 1913 | }, 1914 | { 1915 | "cell_type": "markdown", 1916 | "metadata": {}, 1917 | "source": [ 1918 | "#### 55. What is the equivalent of enumerate for numpy arrays? (★★☆)" 1919 | ] 1920 | }, 1921 | { 1922 | "cell_type": "code", 1923 | "execution_count": 58, 1924 | "metadata": {}, 1925 | "outputs": [ 1926 | { 1927 | "name": "stdout", 1928 | "output_type": "stream", 1929 | "text": [ 1930 | "(0, 0) 0\n", 1931 | "(0, 1) 1\n", 1932 | "(0, 2) 2\n", 1933 | "(1, 0) 3\n", 1934 | "(1, 1) 4\n", 1935 | "(1, 2) 5\n", 1936 | "(2, 0) 6\n", 1937 | "(2, 1) 7\n", 1938 | "(2, 2) 8\n", 1939 | "(0, 0) 0\n", 1940 | "(0, 1) 1\n", 1941 | "(0, 2) 2\n", 1942 | "(1, 0) 3\n", 1943 | "(1, 1) 4\n", 1944 | "(1, 2) 5\n", 1945 | "(2, 0) 6\n", 1946 | "(2, 1) 7\n", 1947 | "(2, 2) 8\n" 1948 | ] 1949 | } 1950 | ], 1951 | "source": [ 1952 | "Z = np.arange(9).reshape(3,3)\n", 1953 | "for index, value in np.ndenumerate(Z):\n", 1954 | " print(index, value)\n", 1955 | "for index in np.ndindex(Z.shape):\n", 1956 | " print(index, Z[index])" 1957 | ] 1958 | }, 1959 | { 1960 | "cell_type": "markdown", 1961 | "metadata": {}, 1962 | "source": [ 1963 | "#### 56. Generate a generic 2D Gaussian-like array (★★☆)" 1964 | ] 1965 | }, 1966 | { 1967 | "cell_type": "code", 1968 | "execution_count": 59, 1969 | "metadata": {}, 1970 | "outputs": [ 1971 | { 1972 | "name": "stdout", 1973 | "output_type": "stream", 1974 | "text": [ 1975 | "[[0.36787944 0.44822088 0.51979489 0.57375342 0.60279818 0.60279818\n", 1976 | " 0.57375342 0.51979489 0.44822088 0.36787944]\n", 1977 | " [0.44822088 0.54610814 0.63331324 0.69905581 0.73444367 0.73444367\n", 1978 | " 0.69905581 0.63331324 0.54610814 0.44822088]\n", 1979 | " [0.51979489 0.63331324 0.73444367 0.81068432 0.85172308 0.85172308\n", 1980 | " 0.81068432 0.73444367 0.63331324 0.51979489]\n", 1981 | " [0.57375342 0.69905581 0.81068432 0.89483932 0.9401382 0.9401382\n", 1982 | " 0.89483932 0.81068432 0.69905581 0.57375342]\n", 1983 | " [0.60279818 0.73444367 0.85172308 0.9401382 0.98773022 0.98773022\n", 1984 | " 0.9401382 0.85172308 0.73444367 0.60279818]\n", 1985 | " [0.60279818 0.73444367 0.85172308 0.9401382 0.98773022 0.98773022\n", 1986 | " 0.9401382 0.85172308 0.73444367 0.60279818]\n", 1987 | " [0.57375342 0.69905581 0.81068432 0.89483932 0.9401382 0.9401382\n", 1988 | " 0.89483932 0.81068432 0.69905581 0.57375342]\n", 1989 | " [0.51979489 0.63331324 0.73444367 0.81068432 0.85172308 0.85172308\n", 1990 | " 0.81068432 0.73444367 0.63331324 0.51979489]\n", 1991 | " [0.44822088 0.54610814 0.63331324 0.69905581 0.73444367 0.73444367\n", 1992 | " 0.69905581 0.63331324 0.54610814 0.44822088]\n", 1993 | " [0.36787944 0.44822088 0.51979489 0.57375342 0.60279818 0.60279818\n", 1994 | " 0.57375342 0.51979489 0.44822088 0.36787944]]\n" 1995 | ] 1996 | } 1997 | ], 1998 | "source": [ 1999 | "X, Y = np.meshgrid(np.linspace(-1,1,10), np.linspace(-1,1,10))\n", 2000 | "D = np.sqrt(X*X+Y*Y)\n", 2001 | "sigma, mu = 1.0, 0.0\n", 2002 | "G = np.exp(-( (D-mu)**2 / ( 2.0 * sigma**2 ) ) )\n", 2003 | "print(G)" 2004 | ] 2005 | }, 2006 | { 2007 | "cell_type": "markdown", 2008 | "metadata": {}, 2009 | "source": [ 2010 | "#### 57. How to randomly place p elements in a 2D array? (★★☆)" 2011 | ] 2012 | }, 2013 | { 2014 | "cell_type": "code", 2015 | "execution_count": 60, 2016 | "metadata": {}, 2017 | "outputs": [ 2018 | { 2019 | "name": "stdout", 2020 | "output_type": "stream", 2021 | "text": [ 2022 | "[[0. 0. 0. 1. 0. 0. 0. 0. 1. 0.]\n", 2023 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 2024 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 2025 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 2026 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 2027 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 2028 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 2029 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 2030 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", 2031 | " [0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]]\n" 2032 | ] 2033 | } 2034 | ], 2035 | "source": [ 2036 | "n = 10\n", 2037 | "p = 3\n", 2038 | "Z = np.zeros((n,n))\n", 2039 | "np.put(Z, np.random.choice(range(n*n), p, replace=False),1)\n", 2040 | "print(Z)" 2041 | ] 2042 | }, 2043 | { 2044 | "cell_type": "markdown", 2045 | "metadata": {}, 2046 | "source": [ 2047 | "#### 58. Subtract the mean of each row of a matrix (★★☆)" 2048 | ] 2049 | }, 2050 | { 2051 | "cell_type": "code", 2052 | "execution_count": 61, 2053 | "metadata": {}, 2054 | "outputs": [ 2055 | { 2056 | "name": "stdout", 2057 | "output_type": "stream", 2058 | "text": [ 2059 | "[[-0.19604905 0.53943676 0.28094897 0.38695143 -0.13792831 -0.18996587\n", 2060 | " -0.22623242 -0.25692319 -0.29847832 0.09824 ]\n", 2061 | " [ 0.13864342 0.08922941 0.48422871 -0.24101927 0.0944123 0.17951673\n", 2062 | " -0.19476358 -0.03544237 -0.28547592 -0.22932944]\n", 2063 | " [ 0.51777126 -0.30684847 -0.35168733 -0.22964683 0.34614166 -0.07331451\n", 2064 | " 0.2682707 -0.25951537 0.4489663 -0.36013741]\n", 2065 | " [ 0.43534696 0.28047985 -0.01997988 -0.04650366 0.13122244 -0.23262032\n", 2066 | " -0.41497387 -0.01797434 0.11668998 -0.23168717]\n", 2067 | " [-0.28563974 0.40342374 -0.34669337 0.39380319 -0.26209605 0.09931093\n", 2068 | " -0.41455962 -0.03846619 0.13850466 0.31241243]]\n" 2069 | ] 2070 | } 2071 | ], 2072 | "source": [ 2073 | "X = np.random.rand(5, 10)\n", 2074 | "\n", 2075 | "# Recent versions of numpy\n", 2076 | "Y = X - X.mean(axis=1, keepdims=True)\n", 2077 | "\n", 2078 | "# Older versions of numpy\n", 2079 | "Y = X - X.mean(axis=1).reshape(-1, 1)\n", 2080 | "\n", 2081 | "print(Y)" 2082 | ] 2083 | }, 2084 | { 2085 | "cell_type": "markdown", 2086 | "metadata": {}, 2087 | "source": [ 2088 | "#### 59. How to sort an array by the nth column? (★★☆)" 2089 | ] 2090 | }, 2091 | { 2092 | "cell_type": "code", 2093 | "execution_count": 62, 2094 | "metadata": {}, 2095 | "outputs": [ 2096 | { 2097 | "name": "stdout", 2098 | "output_type": "stream", 2099 | "text": [ 2100 | "[[4 0 0]\n", 2101 | " [2 7 5]\n", 2102 | " [3 8 3]]\n", 2103 | "[[4 0 0]\n", 2104 | " [2 7 5]\n", 2105 | " [3 8 3]]\n" 2106 | ] 2107 | } 2108 | ], 2109 | "source": [ 2110 | "Z = np.random.randint(0,10,(3,3))\n", 2111 | "print(Z)\n", 2112 | "print(Z[Z[:,1].argsort()])" 2113 | ] 2114 | }, 2115 | { 2116 | "cell_type": "markdown", 2117 | "metadata": {}, 2118 | "source": [ 2119 | "#### 60. How to tell if a given 2D array has null columns? (★★☆)" 2120 | ] 2121 | }, 2122 | { 2123 | "cell_type": "code", 2124 | "execution_count": 63, 2125 | "metadata": {}, 2126 | "outputs": [ 2127 | { 2128 | "name": "stdout", 2129 | "output_type": "stream", 2130 | "text": [ 2131 | "False\n" 2132 | ] 2133 | } 2134 | ], 2135 | "source": [ 2136 | "\n", 2137 | "Z = np.random.randint(0,3,(3,10))\n", 2138 | "print((~Z.any(axis=0)).any())" 2139 | ] 2140 | }, 2141 | { 2142 | "cell_type": "markdown", 2143 | "metadata": {}, 2144 | "source": [ 2145 | "#### 61. Find the nearest value from a given value in an array (★★☆)" 2146 | ] 2147 | }, 2148 | { 2149 | "cell_type": "code", 2150 | "execution_count": 64, 2151 | "metadata": {}, 2152 | "outputs": [ 2153 | { 2154 | "name": "stdout", 2155 | "output_type": "stream", 2156 | "text": [ 2157 | "0.4662687459965946\n" 2158 | ] 2159 | } 2160 | ], 2161 | "source": [ 2162 | "Z = np.random.uniform(0,1,10)\n", 2163 | "z = 0.5\n", 2164 | "m = Z.flat[np.abs(Z - z).argmin()]\n", 2165 | "print(m)" 2166 | ] 2167 | }, 2168 | { 2169 | "cell_type": "markdown", 2170 | "metadata": {}, 2171 | "source": [ 2172 | "#### 62. Considering two arrays with shape (1,3) and (3,1), how to compute their sum using an iterator? (★★☆)" 2173 | ] 2174 | }, 2175 | { 2176 | "cell_type": "code", 2177 | "execution_count": 65, 2178 | "metadata": {}, 2179 | "outputs": [ 2180 | { 2181 | "name": "stdout", 2182 | "output_type": "stream", 2183 | "text": [ 2184 | "[[0 1 2]\n", 2185 | " [1 2 3]\n", 2186 | " [2 3 4]]\n" 2187 | ] 2188 | } 2189 | ], 2190 | "source": [ 2191 | "A = np.arange(3).reshape(3,1)\n", 2192 | "B = np.arange(3).reshape(1,3)\n", 2193 | "it = np.nditer([A,B,None])\n", 2194 | "for x,y,z in it: z[...] = x + y\n", 2195 | "print(it.operands[2])" 2196 | ] 2197 | }, 2198 | { 2199 | "cell_type": "markdown", 2200 | "metadata": {}, 2201 | "source": [ 2202 | "#### 63. Create an array class that has a name attribute (★★☆)" 2203 | ] 2204 | }, 2205 | { 2206 | "cell_type": "code", 2207 | "execution_count": 66, 2208 | "metadata": {}, 2209 | "outputs": [ 2210 | { 2211 | "name": "stdout", 2212 | "output_type": "stream", 2213 | "text": [ 2214 | "range_10\n" 2215 | ] 2216 | } 2217 | ], 2218 | "source": [ 2219 | "class NamedArray(np.ndarray):\n", 2220 | " def __new__(cls, array, name=\"no name\"):\n", 2221 | " obj = np.asarray(array).view(cls)\n", 2222 | " obj.name = name\n", 2223 | " return obj\n", 2224 | " def __array_finalize__(self, obj):\n", 2225 | " if obj is None: return\n", 2226 | " self.info = getattr(obj, 'name', \"no name\")\n", 2227 | "\n", 2228 | "Z = NamedArray(np.arange(10), \"range_10\")\n", 2229 | "print (Z.name)" 2230 | ] 2231 | }, 2232 | { 2233 | "cell_type": "markdown", 2234 | "metadata": {}, 2235 | "source": [ 2236 | "#### 64. Consider a given vector, how to add 1 to each element indexed by a second vector (be careful with repeated indices)? (★★★)" 2237 | ] 2238 | }, 2239 | { 2240 | "cell_type": "code", 2241 | "execution_count": 67, 2242 | "metadata": {}, 2243 | "outputs": [ 2244 | { 2245 | "name": "stdout", 2246 | "output_type": "stream", 2247 | "text": [ 2248 | "[2. 1. 4. 4. 2. 3. 1. 4. 5. 4.]\n", 2249 | "[3. 1. 7. 7. 3. 5. 1. 7. 9. 7.]\n" 2250 | ] 2251 | } 2252 | ], 2253 | "source": [ 2254 | "Z = np.ones(10)\n", 2255 | "I = np.random.randint(0,len(Z),20)\n", 2256 | "Z += np.bincount(I, minlength=len(Z))\n", 2257 | "print(Z)\n", 2258 | "\n", 2259 | "# Another solution\n", 2260 | "np.add.at(Z, I, 1)\n", 2261 | "print(Z)" 2262 | ] 2263 | }, 2264 | { 2265 | "cell_type": "markdown", 2266 | "metadata": {}, 2267 | "source": [ 2268 | "#### 65. How to accumulate elements of a vector (X) to an array (F) based on an index list (I)? (★★★)" 2269 | ] 2270 | }, 2271 | { 2272 | "cell_type": "code", 2273 | "execution_count": 68, 2274 | "metadata": {}, 2275 | "outputs": [ 2276 | { 2277 | "name": "stdout", 2278 | "output_type": "stream", 2279 | "text": [ 2280 | "[0. 7. 0. 6. 5. 0. 0. 0. 0. 3.]\n" 2281 | ] 2282 | } 2283 | ], 2284 | "source": [ 2285 | "X = [1,2,3,4,5,6]\n", 2286 | "I = [1,3,9,3,4,1]\n", 2287 | "F = np.bincount(I,X)\n", 2288 | "print(F)" 2289 | ] 2290 | }, 2291 | { 2292 | "cell_type": "markdown", 2293 | "metadata": {}, 2294 | "source": [ 2295 | "#### 66. Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique colors (★★☆)" 2296 | ] 2297 | }, 2298 | { 2299 | "cell_type": "code", 2300 | "execution_count": 69, 2301 | "metadata": {}, 2302 | "outputs": [ 2303 | { 2304 | "name": "stdout", 2305 | "output_type": "stream", 2306 | "text": [ 2307 | "64\n", 2308 | "64\n" 2309 | ] 2310 | } 2311 | ], 2312 | "source": [ 2313 | "w, h = 256, 256\n", 2314 | "I = np.random.randint(0, 4, (h, w, 3)).astype(np.ubyte)\n", 2315 | "colors = np.unique(I.reshape(-1, 3), axis=0)\n", 2316 | "n = len(colors)\n", 2317 | "print(n)\n", 2318 | "\n", 2319 | "# Faster version\n", 2320 | "\n", 2321 | "w, h = 256, 256\n", 2322 | "I = np.random.randint(0,4,(h,w,3), dtype=np.uint8)\n", 2323 | "\n", 2324 | "# View each pixel as a single 24-bit integer, rather than three 8-bit bytes\n", 2325 | "I24 = np.dot(I.astype(np.uint32),[1,256,65536])\n", 2326 | "\n", 2327 | "# Count unique colours\n", 2328 | "n = len(np.unique(I24))\n", 2329 | "print(n)" 2330 | ] 2331 | }, 2332 | { 2333 | "cell_type": "markdown", 2334 | "metadata": {}, 2335 | "source": [ 2336 | "#### 67. Considering a four dimensions array, how to get sum over the last two axis at once? (★★★)" 2337 | ] 2338 | }, 2339 | { 2340 | "cell_type": "code", 2341 | "execution_count": 70, 2342 | "metadata": {}, 2343 | "outputs": [ 2344 | { 2345 | "name": "stdout", 2346 | "output_type": "stream", 2347 | "text": [ 2348 | "[[59 55 61 40]\n", 2349 | " [52 45 62 55]\n", 2350 | " [63 56 74 34]]\n", 2351 | "[[59 55 61 40]\n", 2352 | " [52 45 62 55]\n", 2353 | " [63 56 74 34]]\n" 2354 | ] 2355 | } 2356 | ], 2357 | "source": [ 2358 | "A = np.random.randint(0,10,(3,4,3,4))\n", 2359 | "# solution by passing a tuple of axes (introduced in numpy 1.7.0)\n", 2360 | "sum = A.sum(axis=(-2,-1))\n", 2361 | "print(sum)\n", 2362 | "# solution by flattening the last two dimensions into one\n", 2363 | "sum = A.reshape(A.shape[:-2] + (-1,)).sum(axis=-1)\n", 2364 | "print(sum)" 2365 | ] 2366 | }, 2367 | { 2368 | "cell_type": "markdown", 2369 | "metadata": {}, 2370 | "source": [ 2371 | "#### 68. Considering a one-dimensional vector D, how to compute means of subsets of D using a vector S of same size describing subset indices? (★★★)" 2372 | ] 2373 | }, 2374 | { 2375 | "cell_type": "code", 2376 | "execution_count": 71, 2377 | "metadata": {}, 2378 | "outputs": [ 2379 | { 2380 | "name": "stdout", 2381 | "output_type": "stream", 2382 | "text": [ 2383 | "[0.32405276 0.41632549 0.51352229 0.55773555 0.76342123 0.55102833\n", 2384 | " 0.55299829 0.38927183 0.51715287 0.38034321]\n", 2385 | "0 0.324053\n", 2386 | "1 0.416325\n", 2387 | "2 0.513522\n", 2388 | "3 0.557736\n", 2389 | "4 0.763421\n", 2390 | "5 0.551028\n", 2391 | "6 0.552998\n", 2392 | "7 0.389272\n", 2393 | "8 0.517153\n", 2394 | "9 0.380343\n", 2395 | "dtype: float64\n" 2396 | ] 2397 | } 2398 | ], 2399 | "source": [ 2400 | "\n", 2401 | "D = np.random.uniform(0,1,100)\n", 2402 | "S = np.random.randint(0,10,100)\n", 2403 | "D_sums = np.bincount(S, weights=D)\n", 2404 | "D_counts = np.bincount(S)\n", 2405 | "D_means = D_sums / D_counts\n", 2406 | "print(D_means)\n", 2407 | "\n", 2408 | "# Pandas solution as a reference due to more intuitive code\n", 2409 | "import pandas as pd\n", 2410 | "print(pd.Series(D).groupby(S).mean())" 2411 | ] 2412 | }, 2413 | { 2414 | "cell_type": "markdown", 2415 | "metadata": {}, 2416 | "source": [ 2417 | "#### 69. How to get the diagonal of a dot product? (★★★)" 2418 | ] 2419 | }, 2420 | { 2421 | "cell_type": "code", 2422 | "execution_count": 72, 2423 | "metadata": {}, 2424 | "outputs": [ 2425 | { 2426 | "data": { 2427 | "text/plain": [ 2428 | "array([1.04493741, 1.33813764, 1.09863653, 0.67494893, 0.81377143])" 2429 | ] 2430 | }, 2431 | "execution_count": 72, 2432 | "metadata": {}, 2433 | "output_type": "execute_result" 2434 | } 2435 | ], 2436 | "source": [ 2437 | "A = np.random.uniform(0,1,(5,5))\n", 2438 | "B = np.random.uniform(0,1,(5,5))\n", 2439 | "\n", 2440 | "# Slow version\n", 2441 | "np.diag(np.dot(A, B))\n", 2442 | "\n", 2443 | "# Fast version\n", 2444 | "np.sum(A * B.T, axis=1)\n", 2445 | "\n", 2446 | "# Faster version\n", 2447 | "np.einsum(\"ij,ji->i\", A, B)" 2448 | ] 2449 | }, 2450 | { 2451 | "cell_type": "markdown", 2452 | "metadata": {}, 2453 | "source": [ 2454 | "#### 70. Consider the vector [1, 2, 3, 4, 5], how to build a new vector with 3 consecutive zeros interleaved between each value? (★★★)" 2455 | ] 2456 | }, 2457 | { 2458 | "cell_type": "code", 2459 | "execution_count": 73, 2460 | "metadata": {}, 2461 | "outputs": [ 2462 | { 2463 | "name": "stdout", 2464 | "output_type": "stream", 2465 | "text": [ 2466 | "[1. 0. 0. 0. 2. 0. 0. 0. 3. 0. 0. 0. 4. 0. 0. 0. 5.]\n" 2467 | ] 2468 | } 2469 | ], 2470 | "source": [ 2471 | "Z = np.array([1,2,3,4,5])\n", 2472 | "nz = 3\n", 2473 | "Z0 = np.zeros(len(Z) + (len(Z)-1)*(nz))\n", 2474 | "Z0[::nz+1] = Z\n", 2475 | "print(Z0)" 2476 | ] 2477 | }, 2478 | { 2479 | "cell_type": "markdown", 2480 | "metadata": {}, 2481 | "source": [ 2482 | "#### 71. Consider an array of dimension (5,5,3), how to mulitply it by an array with dimensions (5,5)? (★★★)" 2483 | ] 2484 | }, 2485 | { 2486 | "cell_type": "code", 2487 | "execution_count": 74, 2488 | "metadata": {}, 2489 | "outputs": [ 2490 | { 2491 | "name": "stdout", 2492 | "output_type": "stream", 2493 | "text": [ 2494 | "[[[2. 2. 2.]\n", 2495 | " [2. 2. 2.]\n", 2496 | " [2. 2. 2.]\n", 2497 | " [2. 2. 2.]\n", 2498 | " [2. 2. 2.]]\n", 2499 | "\n", 2500 | " [[2. 2. 2.]\n", 2501 | " [2. 2. 2.]\n", 2502 | " [2. 2. 2.]\n", 2503 | " [2. 2. 2.]\n", 2504 | " [2. 2. 2.]]\n", 2505 | "\n", 2506 | " [[2. 2. 2.]\n", 2507 | " [2. 2. 2.]\n", 2508 | " [2. 2. 2.]\n", 2509 | " [2. 2. 2.]\n", 2510 | " [2. 2. 2.]]\n", 2511 | "\n", 2512 | " [[2. 2. 2.]\n", 2513 | " [2. 2. 2.]\n", 2514 | " [2. 2. 2.]\n", 2515 | " [2. 2. 2.]\n", 2516 | " [2. 2. 2.]]\n", 2517 | "\n", 2518 | " [[2. 2. 2.]\n", 2519 | " [2. 2. 2.]\n", 2520 | " [2. 2. 2.]\n", 2521 | " [2. 2. 2.]\n", 2522 | " [2. 2. 2.]]]\n" 2523 | ] 2524 | } 2525 | ], 2526 | "source": [ 2527 | "A = np.ones((5,5,3))\n", 2528 | "B = 2*np.ones((5,5))\n", 2529 | "print(A * B[:,:,None])" 2530 | ] 2531 | }, 2532 | { 2533 | "cell_type": "markdown", 2534 | "metadata": {}, 2535 | "source": [ 2536 | "#### 72. How to swap two rows of an array? (★★★)" 2537 | ] 2538 | }, 2539 | { 2540 | "cell_type": "code", 2541 | "execution_count": 75, 2542 | "metadata": {}, 2543 | "outputs": [ 2544 | { 2545 | "name": "stdout", 2546 | "output_type": "stream", 2547 | "text": [ 2548 | "[[ 5 6 7 8 9]\n", 2549 | " [ 0 1 2 3 4]\n", 2550 | " [10 11 12 13 14]\n", 2551 | " [15 16 17 18 19]\n", 2552 | " [20 21 22 23 24]]\n" 2553 | ] 2554 | } 2555 | ], 2556 | "source": [ 2557 | "A = np.arange(25).reshape(5,5)\n", 2558 | "A[[0,1]] = A[[1,0]]\n", 2559 | "print(A)" 2560 | ] 2561 | }, 2562 | { 2563 | "cell_type": "markdown", 2564 | "metadata": {}, 2565 | "source": [ 2566 | "#### 73. Consider a set of 10 triplets describing 10 triangles (with shared vertices), find the set of unique line segments composing all the triangles (★★★)" 2567 | ] 2568 | }, 2569 | { 2570 | "cell_type": "code", 2571 | "execution_count": 76, 2572 | "metadata": {}, 2573 | "outputs": [ 2574 | { 2575 | "name": "stdout", 2576 | "output_type": "stream", 2577 | "text": [ 2578 | "[( 1, 51) ( 1, 78) ( 2, 37) ( 2, 63) ( 3, 19) ( 3, 72) ( 5, 22) ( 5, 54)\n", 2579 | " ( 5, 61) ( 5, 68) (13, 55) (13, 92) (17, 57) (17, 58) (19, 72) (21, 59)\n", 2580 | " (21, 97) (22, 54) (29, 48) (29, 66) (34, 56) (34, 95) (37, 63) (48, 66)\n", 2581 | " (51, 78) (55, 92) (56, 95) (57, 58) (59, 97) (61, 68)]\n" 2582 | ] 2583 | } 2584 | ], 2585 | "source": [ 2586 | "faces = np.random.randint(0,100,(10,3))\n", 2587 | "F = np.roll(faces.repeat(2,axis=1),-1,axis=1)\n", 2588 | "F = F.reshape(len(F)*3,2)\n", 2589 | "F = np.sort(F,axis=1)\n", 2590 | "G = F.view( dtype=[('p0',F.dtype),('p1',F.dtype)] )\n", 2591 | "G = np.unique(G)\n", 2592 | "print(G)" 2593 | ] 2594 | }, 2595 | { 2596 | "cell_type": "markdown", 2597 | "metadata": {}, 2598 | "source": [ 2599 | "#### 74. Given a sorted array C that corresponds to a bincount, how to produce an array A such that np.bincount(A) == C? (★★★)" 2600 | ] 2601 | }, 2602 | { 2603 | "cell_type": "code", 2604 | "execution_count": 77, 2605 | "metadata": {}, 2606 | "outputs": [ 2607 | { 2608 | "name": "stdout", 2609 | "output_type": "stream", 2610 | "text": [ 2611 | "[1 1 2 3 4 4 6]\n" 2612 | ] 2613 | } 2614 | ], 2615 | "source": [ 2616 | "C = np.bincount([1,1,2,3,4,4,6])\n", 2617 | "A = np.repeat(np.arange(len(C)), C)\n", 2618 | "print(A)" 2619 | ] 2620 | }, 2621 | { 2622 | "cell_type": "markdown", 2623 | "metadata": {}, 2624 | "source": [ 2625 | "#### 75. How to compute averages using a sliding window over an array? (★★★)" 2626 | ] 2627 | }, 2628 | { 2629 | "cell_type": "code", 2630 | "execution_count": 78, 2631 | "metadata": {}, 2632 | "outputs": [ 2633 | { 2634 | "name": "stdout", 2635 | "output_type": "stream", 2636 | "text": [ 2637 | "[ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18.]\n", 2638 | "[ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18.]\n" 2639 | ] 2640 | } 2641 | ], 2642 | "source": [ 2643 | "def moving_average(a, n=3) :\n", 2644 | " ret = np.cumsum(a, dtype=float)\n", 2645 | " ret[n:] = ret[n:] - ret[:-n]\n", 2646 | " return ret[n - 1:] / n\n", 2647 | "Z = np.arange(20)\n", 2648 | "print(moving_average(Z, n=3))\n", 2649 | "\n", 2650 | "# make sure your NumPy >= 1.20.0\n", 2651 | "\n", 2652 | "from numpy.lib.stride_tricks import sliding_window_view\n", 2653 | "\n", 2654 | "Z = np.arange(20)\n", 2655 | "print(sliding_window_view(Z, window_shape=3).mean(axis=-1))" 2656 | ] 2657 | }, 2658 | { 2659 | "cell_type": "markdown", 2660 | "metadata": {}, 2661 | "source": [ 2662 | "#### 76. Consider a one-dimensional array Z, build a two-dimensional array whose first row is (Z[0],Z[1],Z[2]) and each subsequent row is shifted by 1 (last row should be (Z[-3],Z[-2],Z[-1]) (★★★)" 2663 | ] 2664 | }, 2665 | { 2666 | "cell_type": "code", 2667 | "execution_count": 79, 2668 | "metadata": {}, 2669 | "outputs": [ 2670 | { 2671 | "name": "stdout", 2672 | "output_type": "stream", 2673 | "text": [ 2674 | "[[0 1 2]\n", 2675 | " [1 2 3]\n", 2676 | " [2 3 4]\n", 2677 | " [3 4 5]\n", 2678 | " [4 5 6]\n", 2679 | " [5 6 7]\n", 2680 | " [6 7 8]\n", 2681 | " [7 8 9]]\n", 2682 | "[[0 1 2]\n", 2683 | " [1 2 3]\n", 2684 | " [2 3 4]\n", 2685 | " [3 4 5]\n", 2686 | " [4 5 6]\n", 2687 | " [5 6 7]\n", 2688 | " [6 7 8]\n", 2689 | " [7 8 9]]\n" 2690 | ] 2691 | } 2692 | ], 2693 | "source": [ 2694 | "from numpy.lib import stride_tricks\n", 2695 | "\n", 2696 | "def rolling(a, window):\n", 2697 | " shape = (a.size - window + 1, window)\n", 2698 | " strides = (a.strides[0], a.strides[0])\n", 2699 | " return stride_tricks.as_strided(a, shape=shape, strides=strides)\n", 2700 | "Z = rolling(np.arange(10), 3)\n", 2701 | "print(Z)\n", 2702 | "\n", 2703 | "\n", 2704 | "Z = np.arange(10)\n", 2705 | "print(sliding_window_view(Z, window_shape=3))" 2706 | ] 2707 | }, 2708 | { 2709 | "cell_type": "markdown", 2710 | "metadata": {}, 2711 | "source": [ 2712 | "#### 77. How to negate a boolean, or to change the sign of a float inplace? (★★★)" 2713 | ] 2714 | }, 2715 | { 2716 | "cell_type": "code", 2717 | "execution_count": 80, 2718 | "metadata": {}, 2719 | "outputs": [ 2720 | { 2721 | "data": { 2722 | "text/plain": [ 2723 | "array([ 0.09610829, -0.03366445, 0.02875353, 0.83955381, -0.2512913 ,\n", 2724 | " -0.33777552, -0.50403415, -0.66014346, -0.59279018, 0.17143888,\n", 2725 | " 0.7130721 , 0.45142917, -0.15279332, 0.89115151, 0.0363265 ,\n", 2726 | " -0.78649209, 0.59478596, -0.92013642, 0.79089732, -0.89876801,\n", 2727 | " -0.95960561, 0.38755602, -0.81660356, -0.09201927, -0.89362299,\n", 2728 | " 0.95476494, -0.58705587, 0.88165382, -0.82232139, -0.24093065,\n", 2729 | " 0.14167176, -0.59454688, -0.11881655, -0.28441004, 0.9415774 ,\n", 2730 | " -0.95935789, 0.18269575, 0.14016204, -0.10974478, -0.64244344,\n", 2731 | " 0.54940535, 0.06582425, 0.03529335, 0.27372821, 0.28675885,\n", 2732 | " 0.32140548, 0.94914643, -0.29698489, 0.68910384, -0.0040607 ,\n", 2733 | " -0.6937189 , 0.44175999, 0.40519328, -0.48714156, -0.92577781,\n", 2734 | " -0.12745138, -0.61984939, -0.33726885, 0.59499614, 0.41657643,\n", 2735 | " -0.60943803, -0.03184028, -0.92776321, -0.91937336, 0.73100227,\n", 2736 | " -0.81314806, -0.61977767, 0.7409286 , -0.9902232 , 0.08825108,\n", 2737 | " -0.87180266, -0.33711521, 0.63058635, -0.93743197, 0.23580376,\n", 2738 | " 0.67722444, -0.60493563, -0.56712087, 0.78882418, -0.34890061,\n", 2739 | " 0.46291042, -0.42244945, 0.13873838, -0.50829188, 0.11866282,\n", 2740 | " 0.15896536, -0.44650495, -0.30351031, 0.2674895 , 0.85956853,\n", 2741 | " 0.91587259, 0.76032414, 0.39140663, 0.70019019, 0.51562493,\n", 2742 | " -0.52673453, -0.95222454, -0.03963169, 0.33844008, 0.20609607])" 2743 | ] 2744 | }, 2745 | "execution_count": 80, 2746 | "metadata": {}, 2747 | "output_type": "execute_result" 2748 | } 2749 | ], 2750 | "source": [ 2751 | "\n", 2752 | "Z = np.random.randint(0,2,100)\n", 2753 | "np.logical_not(Z, out=Z)\n", 2754 | "\n", 2755 | "Z = np.random.uniform(-1.0,1.0,100)\n", 2756 | "np.negative(Z, out=Z)" 2757 | ] 2758 | }, 2759 | { 2760 | "cell_type": "markdown", 2761 | "metadata": {}, 2762 | "source": [ 2763 | "#### 78. Consider 2 sets of points P0,P1 describing lines (2d) and a point p, how to compute distance from p to each line i (P0[i],P1[i])? (★★★)" 2764 | ] 2765 | }, 2766 | { 2767 | "cell_type": "code", 2768 | "execution_count": 81, 2769 | "metadata": {}, 2770 | "outputs": [ 2771 | { 2772 | "name": "stdout", 2773 | "output_type": "stream", 2774 | "text": [ 2775 | "[2.52532091 7.96816615 0.54007088 1.98981265 0.1899908 0.79338195\n", 2776 | " 1.88741034 0.6640788 4.11331711 0.15419172]\n" 2777 | ] 2778 | } 2779 | ], 2780 | "source": [ 2781 | "def distance(P0, P1, p):\n", 2782 | " T = P1 - P0\n", 2783 | " L = (T**2).sum(axis=1)\n", 2784 | " U = -((P0[:,0]-p[...,0])*T[:,0] + (P0[:,1]-p[...,1])*T[:,1]) / L\n", 2785 | " U = U.reshape(len(U),1)\n", 2786 | " D = P0 + U*T - p\n", 2787 | " return np.sqrt((D**2).sum(axis=1))\n", 2788 | "\n", 2789 | "P0 = np.random.uniform(-10,10,(10,2))\n", 2790 | "P1 = np.random.uniform(-10,10,(10,2))\n", 2791 | "p = np.random.uniform(-10,10,( 1,2))\n", 2792 | "print(distance(P0, P1, p))" 2793 | ] 2794 | }, 2795 | { 2796 | "cell_type": "markdown", 2797 | "metadata": {}, 2798 | "source": [ 2799 | "#### 79. Consider 2 sets of points P0,P1 describing lines (2d) and a set of points P, how to compute distance from each point j (P[j]) to each line i (P0[i],P1[i])? (★★★)" 2800 | ] 2801 | }, 2802 | { 2803 | "cell_type": "code", 2804 | "execution_count": 82, 2805 | "metadata": {}, 2806 | "outputs": [ 2807 | { 2808 | "name": "stdout", 2809 | "output_type": "stream", 2810 | "text": [ 2811 | "[[ 1.25615892 10.67377232 7.20265757 0.1340666 4.30661495 2.66039773\n", 2812 | " 10.86721121 0.47686385 5.89023433 4.63344567]\n", 2813 | " [ 6.71462707 3.57891739 12.33290026 5.0721129 6.66299693 8.90578422\n", 2814 | " 3.75461735 0.96881575 4.2832034 6.04973081]\n", 2815 | " [ 6.27124168 3.85610086 11.8759887 5.2508583 6.16997643 8.97074756\n", 2816 | " 3.85882773 1.41030423 4.76986551 5.5655465 ]\n", 2817 | " [ 9.7489223 18.13383126 4.06019987 3.13551096 7.34540232 3.1628043\n", 2818 | " 14.3722863 10.50100383 17.27869946 6.66827164]\n", 2819 | " [ 7.31128463 2.91302361 12.90840951 5.42068074 7.03111747 9.37871046\n", 2820 | " 3.15071082 0.84612944 3.97701415 6.33984063]\n", 2821 | " [ 0.05315215 8.70521883 5.59743387 5.82297607 0.22567027 8.04160937\n", 2822 | " 6.84299709 6.05364089 10.44971638 0.03770025]\n", 2823 | " [ 1.59001376 8.32039242 7.2585588 4.04498908 2.51531838 6.7145608\n", 2824 | " 7.39355415 3.59635056 8.07715061 2.3513391 ]\n", 2825 | " [ 1.30436923 10.87947021 7.28402349 0.62784811 4.59802232 2.2021428\n", 2826 | " 11.22922919 0.04927326 5.55654083 4.97713211]\n", 2827 | " [ 3.7155359 7.94576332 1.29521108 13.60850452 7.106304 14.55413239\n", 2828 | " 2.79673305 14.82063638 18.2953888 7.99788009]\n", 2829 | " [ 8.38026346 17.84142916 2.57411903 1.45093324 5.2556206 1.88475096\n", 2830 | " 14.94222705 8.23168289 15.10590745 4.47861993]]\n" 2831 | ] 2832 | } 2833 | ], 2834 | "source": [ 2835 | "\n", 2836 | "# based on distance function from previous question\n", 2837 | "P0 = np.random.uniform(-10, 10, (10,2))\n", 2838 | "P1 = np.random.uniform(-10,10,(10,2))\n", 2839 | "p = np.random.uniform(-10, 10, (10,2))\n", 2840 | "print(np.array([distance(P0,P1,p_i) for p_i in p]))" 2841 | ] 2842 | }, 2843 | { 2844 | "cell_type": "markdown", 2845 | "metadata": {}, 2846 | "source": [ 2847 | "#### 80. Consider an arbitrary array, write a function that extract a subpart with a fixed shape and centered on a given element (pad with a `fill` value when necessary) (★★★)" 2848 | ] 2849 | }, 2850 | { 2851 | "cell_type": "code", 2852 | "execution_count": 83, 2853 | "metadata": {}, 2854 | "outputs": [ 2855 | { 2856 | "name": "stdout", 2857 | "output_type": "stream", 2858 | "text": [ 2859 | "[[1 7 0 8 7 1 4 6 9 5]\n", 2860 | " [9 2 0 2 7 9 8 7 5 0]\n", 2861 | " [3 4 0 3 7 1 0 3 5 4]\n", 2862 | " [8 6 0 2 4 9 7 0 0 7]\n", 2863 | " [1 4 4 9 3 5 2 7 7 1]\n", 2864 | " [0 3 7 6 4 6 2 4 1 1]\n", 2865 | " [2 4 1 7 9 6 3 4 4 5]\n", 2866 | " [0 3 0 7 0 3 5 6 2 3]\n", 2867 | " [3 4 2 6 4 6 0 3 7 4]\n", 2868 | " [3 6 3 2 3 3 1 9 5 8]]\n", 2869 | "[[0 0 0 0 0]\n", 2870 | " [0 1 7 0 8]\n", 2871 | " [0 9 2 0 2]\n", 2872 | " [0 3 4 0 3]\n", 2873 | " [0 8 6 0 2]]\n" 2874 | ] 2875 | }, 2876 | { 2877 | "name": "stderr", 2878 | "output_type": "stream", 2879 | "text": [ 2880 | "C:\\Users\\Devdatta Supnekar\\anaconda3\\lib\\site-packages\\ipykernel_launcher.py:23: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.\n" 2881 | ] 2882 | } 2883 | ], 2884 | "source": [ 2885 | "Z = np.random.randint(0,10,(10,10))\n", 2886 | "shape = (5,5)\n", 2887 | "fill = 0\n", 2888 | "position = (1,1)\n", 2889 | "\n", 2890 | "R = np.ones(shape, dtype=Z.dtype)*fill\n", 2891 | "P = np.array(list(position)).astype(int)\n", 2892 | "Rs = np.array(list(R.shape)).astype(int)\n", 2893 | "Zs = np.array(list(Z.shape)).astype(int)\n", 2894 | "\n", 2895 | "R_start = np.zeros((len(shape),)).astype(int)\n", 2896 | "R_stop = np.array(list(shape)).astype(int)\n", 2897 | "Z_start = (P-Rs//2)\n", 2898 | "Z_stop = (P+Rs//2)+Rs%2\n", 2899 | "\n", 2900 | "R_start = (R_start - np.minimum(Z_start,0)).tolist()\n", 2901 | "Z_start = (np.maximum(Z_start,0)).tolist()\n", 2902 | "R_stop = np.maximum(R_start, (R_stop - np.maximum(Z_stop-Zs,0))).tolist()\n", 2903 | "Z_stop = (np.minimum(Z_stop,Zs)).tolist()\n", 2904 | "\n", 2905 | "r = [slice(start,stop) for start,stop in zip(R_start,R_stop)]\n", 2906 | "z = [slice(start,stop) for start,stop in zip(Z_start,Z_stop)]\n", 2907 | "R[r] = Z[z]\n", 2908 | "print(Z)\n", 2909 | "print(R)" 2910 | ] 2911 | }, 2912 | { 2913 | "cell_type": "markdown", 2914 | "metadata": {}, 2915 | "source": [ 2916 | "#### 81. Consider an array Z = [1,2,3,4,5,6,7,8,9,10,11,12,13,14], how to generate an array R = [[1,2,3,4], [2,3,4,5], [3,4,5,6], ..., [11,12,13,14]]? (★★★)" 2917 | ] 2918 | }, 2919 | { 2920 | "cell_type": "code", 2921 | "execution_count": 84, 2922 | "metadata": {}, 2923 | "outputs": [ 2924 | { 2925 | "name": "stdout", 2926 | "output_type": "stream", 2927 | "text": [ 2928 | "[[ 1 2 3 4]\n", 2929 | " [ 2 3 4 5]\n", 2930 | " [ 3 4 5 6]\n", 2931 | " [ 4 5 6 7]\n", 2932 | " [ 5 6 7 8]\n", 2933 | " [ 6 7 8 9]\n", 2934 | " [ 7 8 9 10]\n", 2935 | " [ 8 9 10 11]\n", 2936 | " [ 9 10 11 12]\n", 2937 | " [10 11 12 13]\n", 2938 | " [11 12 13 14]]\n", 2939 | "[[ 1 2 3 4]\n", 2940 | " [ 2 3 4 5]\n", 2941 | " [ 3 4 5 6]\n", 2942 | " [ 4 5 6 7]\n", 2943 | " [ 5 6 7 8]\n", 2944 | " [ 6 7 8 9]\n", 2945 | " [ 7 8 9 10]\n", 2946 | " [ 8 9 10 11]\n", 2947 | " [ 9 10 11 12]\n", 2948 | " [10 11 12 13]\n", 2949 | " [11 12 13 14]]\n" 2950 | ] 2951 | } 2952 | ], 2953 | "source": [ 2954 | "Z = np.arange(1,15,dtype=np.uint32)\n", 2955 | "R = stride_tricks.as_strided(Z,(11,4),(4,4))\n", 2956 | "print(R)\n", 2957 | "\n", 2958 | "\n", 2959 | "Z = np.arange(1, 15, dtype=np.uint32)\n", 2960 | "print(sliding_window_view(Z, window_shape=4))" 2961 | ] 2962 | }, 2963 | { 2964 | "cell_type": "markdown", 2965 | "metadata": {}, 2966 | "source": [ 2967 | "#### 82. Compute a matrix rank (★★★)" 2968 | ] 2969 | }, 2970 | { 2971 | "cell_type": "code", 2972 | "execution_count": 85, 2973 | "metadata": {}, 2974 | "outputs": [ 2975 | { 2976 | "name": "stdout", 2977 | "output_type": "stream", 2978 | "text": [ 2979 | "10\n", 2980 | "10\n" 2981 | ] 2982 | } 2983 | ], 2984 | "source": [ 2985 | "Z = np.random.uniform(0,1,(10,10))\n", 2986 | "U, S, V = np.linalg.svd(Z) # Singular Value Decomposition\n", 2987 | "rank = np.sum(S > 1e-10)\n", 2988 | "print(rank)\n", 2989 | "\n", 2990 | "# alternative solution:\n", 2991 | "\n", 2992 | "rank = np.linalg.matrix_rank(Z)\n", 2993 | "print(rank)" 2994 | ] 2995 | }, 2996 | { 2997 | "cell_type": "markdown", 2998 | "metadata": {}, 2999 | "source": [ 3000 | "#### 83. How to find the most frequent value in an array?" 3001 | ] 3002 | }, 3003 | { 3004 | "cell_type": "code", 3005 | "execution_count": 86, 3006 | "metadata": {}, 3007 | "outputs": [ 3008 | { 3009 | "name": "stdout", 3010 | "output_type": "stream", 3011 | "text": [ 3012 | "3\n" 3013 | ] 3014 | } 3015 | ], 3016 | "source": [ 3017 | "Z = np.random.randint(0,10,50)\n", 3018 | "print(np.bincount(Z).argmax())" 3019 | ] 3020 | }, 3021 | { 3022 | "cell_type": "markdown", 3023 | "metadata": {}, 3024 | "source": [ 3025 | "#### 84. Extract all the contiguous 3x3 blocks from a random 10x10 matrix (★★★)" 3026 | ] 3027 | }, 3028 | { 3029 | "cell_type": "code", 3030 | "execution_count": 87, 3031 | "metadata": {}, 3032 | "outputs": [ 3033 | { 3034 | "name": "stdout", 3035 | "output_type": "stream", 3036 | "text": [ 3037 | "[[[[0 3 2]\n", 3038 | " [2 4 1]\n", 3039 | " [3 0 2]]\n", 3040 | "\n", 3041 | " [[3 2 0]\n", 3042 | " [4 1 0]\n", 3043 | " [0 2 3]]\n", 3044 | "\n", 3045 | " [[2 0 2]\n", 3046 | " [1 0 2]\n", 3047 | " [2 3 1]]\n", 3048 | "\n", 3049 | " [[0 2 0]\n", 3050 | " [0 2 1]\n", 3051 | " [3 1 1]]\n", 3052 | "\n", 3053 | " [[2 0 4]\n", 3054 | " [2 1 2]\n", 3055 | " [1 1 2]]\n", 3056 | "\n", 3057 | " [[0 4 3]\n", 3058 | " [1 2 4]\n", 3059 | " [1 2 3]]\n", 3060 | "\n", 3061 | " [[4 3 4]\n", 3062 | " [2 4 2]\n", 3063 | " [2 3 0]]\n", 3064 | "\n", 3065 | " [[3 4 4]\n", 3066 | " [4 2 2]\n", 3067 | " [3 0 1]]]\n", 3068 | "\n", 3069 | "\n", 3070 | " [[[2 4 1]\n", 3071 | " [3 0 2]\n", 3072 | " [2 2 1]]\n", 3073 | "\n", 3074 | " [[4 1 0]\n", 3075 | " [0 2 3]\n", 3076 | " [2 1 3]]\n", 3077 | "\n", 3078 | " [[1 0 2]\n", 3079 | " [2 3 1]\n", 3080 | " [1 3 4]]\n", 3081 | "\n", 3082 | " [[0 2 1]\n", 3083 | " [3 1 1]\n", 3084 | " [3 4 1]]\n", 3085 | "\n", 3086 | " [[2 1 2]\n", 3087 | " [1 1 2]\n", 3088 | " [4 1 1]]\n", 3089 | "\n", 3090 | " [[1 2 4]\n", 3091 | " [1 2 3]\n", 3092 | " [1 1 1]]\n", 3093 | "\n", 3094 | " [[2 4 2]\n", 3095 | " [2 3 0]\n", 3096 | " [1 1 2]]\n", 3097 | "\n", 3098 | " [[4 2 2]\n", 3099 | " [3 0 1]\n", 3100 | " [1 2 2]]]\n", 3101 | "\n", 3102 | "\n", 3103 | " [[[3 0 2]\n", 3104 | " [2 2 1]\n", 3105 | " [4 2 1]]\n", 3106 | "\n", 3107 | " [[0 2 3]\n", 3108 | " [2 1 3]\n", 3109 | " [2 1 4]]\n", 3110 | "\n", 3111 | " [[2 3 1]\n", 3112 | " [1 3 4]\n", 3113 | " [1 4 0]]\n", 3114 | "\n", 3115 | " [[3 1 1]\n", 3116 | " [3 4 1]\n", 3117 | " [4 0 4]]\n", 3118 | "\n", 3119 | " [[1 1 2]\n", 3120 | " [4 1 1]\n", 3121 | " [0 4 4]]\n", 3122 | "\n", 3123 | " [[1 2 3]\n", 3124 | " [1 1 1]\n", 3125 | " [4 4 1]]\n", 3126 | "\n", 3127 | " [[2 3 0]\n", 3128 | " [1 1 2]\n", 3129 | " [4 1 1]]\n", 3130 | "\n", 3131 | " [[3 0 1]\n", 3132 | " [1 2 2]\n", 3133 | " [1 1 3]]]\n", 3134 | "\n", 3135 | "\n", 3136 | " [[[2 2 1]\n", 3137 | " [4 2 1]\n", 3138 | " [2 4 1]]\n", 3139 | "\n", 3140 | " [[2 1 3]\n", 3141 | " [2 1 4]\n", 3142 | " [4 1 3]]\n", 3143 | "\n", 3144 | " [[1 3 4]\n", 3145 | " [1 4 0]\n", 3146 | " [1 3 0]]\n", 3147 | "\n", 3148 | " [[3 4 1]\n", 3149 | " [4 0 4]\n", 3150 | " [3 0 3]]\n", 3151 | "\n", 3152 | " [[4 1 1]\n", 3153 | " [0 4 4]\n", 3154 | " [0 3 2]]\n", 3155 | "\n", 3156 | " [[1 1 1]\n", 3157 | " [4 4 1]\n", 3158 | " [3 2 2]]\n", 3159 | "\n", 3160 | " [[1 1 2]\n", 3161 | " [4 1 1]\n", 3162 | " [2 2 3]]\n", 3163 | "\n", 3164 | " [[1 2 2]\n", 3165 | " [1 1 3]\n", 3166 | " [2 3 0]]]\n", 3167 | "\n", 3168 | "\n", 3169 | " [[[4 2 1]\n", 3170 | " [2 4 1]\n", 3171 | " [1 2 1]]\n", 3172 | "\n", 3173 | " [[2 1 4]\n", 3174 | " [4 1 3]\n", 3175 | " [2 1 1]]\n", 3176 | "\n", 3177 | " [[1 4 0]\n", 3178 | " [1 3 0]\n", 3179 | " [1 1 0]]\n", 3180 | "\n", 3181 | " [[4 0 4]\n", 3182 | " [3 0 3]\n", 3183 | " [1 0 4]]\n", 3184 | "\n", 3185 | " [[0 4 4]\n", 3186 | " [0 3 2]\n", 3187 | " [0 4 3]]\n", 3188 | "\n", 3189 | " [[4 4 1]\n", 3190 | " [3 2 2]\n", 3191 | " [4 3 3]]\n", 3192 | "\n", 3193 | " [[4 1 1]\n", 3194 | " [2 2 3]\n", 3195 | " [3 3 0]]\n", 3196 | "\n", 3197 | " [[1 1 3]\n", 3198 | " [2 3 0]\n", 3199 | " [3 0 3]]]\n", 3200 | "\n", 3201 | "\n", 3202 | " [[[2 4 1]\n", 3203 | " [1 2 1]\n", 3204 | " [2 3 1]]\n", 3205 | "\n", 3206 | " [[4 1 3]\n", 3207 | " [2 1 1]\n", 3208 | " [3 1 1]]\n", 3209 | "\n", 3210 | " [[1 3 0]\n", 3211 | " [1 1 0]\n", 3212 | " [1 1 4]]\n", 3213 | "\n", 3214 | " [[3 0 3]\n", 3215 | " [1 0 4]\n", 3216 | " [1 4 2]]\n", 3217 | "\n", 3218 | " [[0 3 2]\n", 3219 | " [0 4 3]\n", 3220 | " [4 2 1]]\n", 3221 | "\n", 3222 | " [[3 2 2]\n", 3223 | " [4 3 3]\n", 3224 | " [2 1 1]]\n", 3225 | "\n", 3226 | " [[2 2 3]\n", 3227 | " [3 3 0]\n", 3228 | " [1 1 3]]\n", 3229 | "\n", 3230 | " [[2 3 0]\n", 3231 | " [3 0 3]\n", 3232 | " [1 3 1]]]\n", 3233 | "\n", 3234 | "\n", 3235 | " [[[1 2 1]\n", 3236 | " [2 3 1]\n", 3237 | " [2 2 0]]\n", 3238 | "\n", 3239 | " [[2 1 1]\n", 3240 | " [3 1 1]\n", 3241 | " [2 0 2]]\n", 3242 | "\n", 3243 | " [[1 1 0]\n", 3244 | " [1 1 4]\n", 3245 | " [0 2 2]]\n", 3246 | "\n", 3247 | " [[1 0 4]\n", 3248 | " [1 4 2]\n", 3249 | " [2 2 4]]\n", 3250 | "\n", 3251 | " [[0 4 3]\n", 3252 | " [4 2 1]\n", 3253 | " [2 4 0]]\n", 3254 | "\n", 3255 | " [[4 3 3]\n", 3256 | " [2 1 1]\n", 3257 | " [4 0 1]]\n", 3258 | "\n", 3259 | " [[3 3 0]\n", 3260 | " [1 1 3]\n", 3261 | " [0 1 2]]\n", 3262 | "\n", 3263 | " [[3 0 3]\n", 3264 | " [1 3 1]\n", 3265 | " [1 2 2]]]\n", 3266 | "\n", 3267 | "\n", 3268 | " [[[2 3 1]\n", 3269 | " [2 2 0]\n", 3270 | " [2 3 2]]\n", 3271 | "\n", 3272 | " [[3 1 1]\n", 3273 | " [2 0 2]\n", 3274 | " [3 2 3]]\n", 3275 | "\n", 3276 | " [[1 1 4]\n", 3277 | " [0 2 2]\n", 3278 | " [2 3 3]]\n", 3279 | "\n", 3280 | " [[1 4 2]\n", 3281 | " [2 2 4]\n", 3282 | " [3 3 0]]\n", 3283 | "\n", 3284 | " [[4 2 1]\n", 3285 | " [2 4 0]\n", 3286 | " [3 0 0]]\n", 3287 | "\n", 3288 | " [[2 1 1]\n", 3289 | " [4 0 1]\n", 3290 | " [0 0 2]]\n", 3291 | "\n", 3292 | " [[1 1 3]\n", 3293 | " [0 1 2]\n", 3294 | " [0 2 3]]\n", 3295 | "\n", 3296 | " [[1 3 1]\n", 3297 | " [1 2 2]\n", 3298 | " [2 3 4]]]]\n", 3299 | "[[[[4 3 2]\n", 3300 | " [3 3 4]\n", 3301 | " [4 2 4]]\n", 3302 | "\n", 3303 | " [[3 2 1]\n", 3304 | " [3 4 4]\n", 3305 | " [2 4 3]]\n", 3306 | "\n", 3307 | " [[2 1 3]\n", 3308 | " [4 4 0]\n", 3309 | " [4 3 1]]\n", 3310 | "\n", 3311 | " [[1 3 4]\n", 3312 | " [4 0 3]\n", 3313 | " [3 1 1]]\n", 3314 | "\n", 3315 | " [[3 4 1]\n", 3316 | " [0 3 0]\n", 3317 | " [1 1 0]]\n", 3318 | "\n", 3319 | " [[4 1 0]\n", 3320 | " [3 0 4]\n", 3321 | " [1 0 0]]\n", 3322 | "\n", 3323 | " [[1 0 0]\n", 3324 | " [0 4 3]\n", 3325 | " [0 0 3]]\n", 3326 | "\n", 3327 | " [[0 0 2]\n", 3328 | " [4 3 1]\n", 3329 | " [0 3 3]]]\n", 3330 | "\n", 3331 | "\n", 3332 | " [[[3 3 4]\n", 3333 | " [4 2 4]\n", 3334 | " [3 4 1]]\n", 3335 | "\n", 3336 | " [[3 4 4]\n", 3337 | " [2 4 3]\n", 3338 | " [4 1 2]]\n", 3339 | "\n", 3340 | " [[4 4 0]\n", 3341 | " [4 3 1]\n", 3342 | " [1 2 0]]\n", 3343 | "\n", 3344 | " [[4 0 3]\n", 3345 | " [3 1 1]\n", 3346 | " [2 0 0]]\n", 3347 | "\n", 3348 | " [[0 3 0]\n", 3349 | " [1 1 0]\n", 3350 | " [0 0 2]]\n", 3351 | "\n", 3352 | " [[3 0 4]\n", 3353 | " [1 0 0]\n", 3354 | " [0 2 0]]\n", 3355 | "\n", 3356 | " [[0 4 3]\n", 3357 | " [0 0 3]\n", 3358 | " [2 0 2]]\n", 3359 | "\n", 3360 | " [[4 3 1]\n", 3361 | " [0 3 3]\n", 3362 | " [0 2 2]]]\n", 3363 | "\n", 3364 | "\n", 3365 | " [[[4 2 4]\n", 3366 | " [3 4 1]\n", 3367 | " [0 1 1]]\n", 3368 | "\n", 3369 | " [[2 4 3]\n", 3370 | " [4 1 2]\n", 3371 | " [1 1 2]]\n", 3372 | "\n", 3373 | " [[4 3 1]\n", 3374 | " [1 2 0]\n", 3375 | " [1 2 0]]\n", 3376 | "\n", 3377 | " [[3 1 1]\n", 3378 | " [2 0 0]\n", 3379 | " [2 0 1]]\n", 3380 | "\n", 3381 | " [[1 1 0]\n", 3382 | " [0 0 2]\n", 3383 | " [0 1 3]]\n", 3384 | "\n", 3385 | " [[1 0 0]\n", 3386 | " [0 2 0]\n", 3387 | " [1 3 1]]\n", 3388 | "\n", 3389 | " [[0 0 3]\n", 3390 | " [2 0 2]\n", 3391 | " [3 1 1]]\n", 3392 | "\n", 3393 | " [[0 3 3]\n", 3394 | " [0 2 2]\n", 3395 | " [1 1 4]]]\n", 3396 | "\n", 3397 | "\n", 3398 | " [[[3 4 1]\n", 3399 | " [0 1 1]\n", 3400 | " [0 0 1]]\n", 3401 | "\n", 3402 | " [[4 1 2]\n", 3403 | " [1 1 2]\n", 3404 | " [0 1 0]]\n", 3405 | "\n", 3406 | " [[1 2 0]\n", 3407 | " [1 2 0]\n", 3408 | " [1 0 1]]\n", 3409 | "\n", 3410 | " [[2 0 0]\n", 3411 | " [2 0 1]\n", 3412 | " [0 1 4]]\n", 3413 | "\n", 3414 | " [[0 0 2]\n", 3415 | " [0 1 3]\n", 3416 | " [1 4 3]]\n", 3417 | "\n", 3418 | " [[0 2 0]\n", 3419 | " [1 3 1]\n", 3420 | " [4 3 2]]\n", 3421 | "\n", 3422 | " [[2 0 2]\n", 3423 | " [3 1 1]\n", 3424 | " [3 2 0]]\n", 3425 | "\n", 3426 | " [[0 2 2]\n", 3427 | " [1 1 4]\n", 3428 | " [2 0 2]]]\n", 3429 | "\n", 3430 | "\n", 3431 | " [[[0 1 1]\n", 3432 | " [0 0 1]\n", 3433 | " [0 3 2]]\n", 3434 | "\n", 3435 | " [[1 1 2]\n", 3436 | " [0 1 0]\n", 3437 | " [3 2 1]]\n", 3438 | "\n", 3439 | " [[1 2 0]\n", 3440 | " [1 0 1]\n", 3441 | " [2 1 4]]\n", 3442 | "\n", 3443 | " [[2 0 1]\n", 3444 | " [0 1 4]\n", 3445 | " [1 4 4]]\n", 3446 | "\n", 3447 | " [[0 1 3]\n", 3448 | " [1 4 3]\n", 3449 | " [4 4 1]]\n", 3450 | "\n", 3451 | " [[1 3 1]\n", 3452 | " [4 3 2]\n", 3453 | " [4 1 1]]\n", 3454 | "\n", 3455 | " [[3 1 1]\n", 3456 | " [3 2 0]\n", 3457 | " [1 1 1]]\n", 3458 | "\n", 3459 | " [[1 1 4]\n", 3460 | " [2 0 2]\n", 3461 | " [1 1 1]]]\n", 3462 | "\n", 3463 | "\n", 3464 | " [[[0 0 1]\n", 3465 | " [0 3 2]\n", 3466 | " [2 3 0]]\n", 3467 | "\n", 3468 | " [[0 1 0]\n", 3469 | " [3 2 1]\n", 3470 | " [3 0 1]]\n", 3471 | "\n", 3472 | " [[1 0 1]\n", 3473 | " [2 1 4]\n", 3474 | " [0 1 1]]\n", 3475 | "\n", 3476 | " [[0 1 4]\n", 3477 | " [1 4 4]\n", 3478 | " [1 1 3]]\n", 3479 | "\n", 3480 | " [[1 4 3]\n", 3481 | " [4 4 1]\n", 3482 | " [1 3 1]]\n", 3483 | "\n", 3484 | " [[4 3 2]\n", 3485 | " [4 1 1]\n", 3486 | " [3 1 2]]\n", 3487 | "\n", 3488 | " [[3 2 0]\n", 3489 | " [1 1 1]\n", 3490 | " [1 2 0]]\n", 3491 | "\n", 3492 | " [[2 0 2]\n", 3493 | " [1 1 1]\n", 3494 | " [2 0 4]]]\n", 3495 | "\n", 3496 | "\n", 3497 | " [[[0 3 2]\n", 3498 | " [2 3 0]\n", 3499 | " [2 2 2]]\n", 3500 | "\n", 3501 | " [[3 2 1]\n", 3502 | " [3 0 1]\n", 3503 | " [2 2 2]]\n", 3504 | "\n", 3505 | " [[2 1 4]\n", 3506 | " [0 1 1]\n", 3507 | " [2 2 1]]\n", 3508 | "\n", 3509 | " [[1 4 4]\n", 3510 | " [1 1 3]\n", 3511 | " [2 1 4]]\n", 3512 | "\n", 3513 | " [[4 4 1]\n", 3514 | " [1 3 1]\n", 3515 | " [1 4 1]]\n", 3516 | "\n", 3517 | " [[4 1 1]\n", 3518 | " [3 1 2]\n", 3519 | " [4 1 0]]\n", 3520 | "\n", 3521 | " [[1 1 1]\n", 3522 | " [1 2 0]\n", 3523 | " [1 0 0]]\n", 3524 | "\n", 3525 | " [[1 1 1]\n", 3526 | " [2 0 4]\n", 3527 | " [0 0 1]]]\n", 3528 | "\n", 3529 | "\n", 3530 | " [[[2 3 0]\n", 3531 | " [2 2 2]\n", 3532 | " [2 1 4]]\n", 3533 | "\n", 3534 | " [[3 0 1]\n", 3535 | " [2 2 2]\n", 3536 | " [1 4 2]]\n", 3537 | "\n", 3538 | " [[0 1 1]\n", 3539 | " [2 2 1]\n", 3540 | " [4 2 1]]\n", 3541 | "\n", 3542 | " [[1 1 3]\n", 3543 | " [2 1 4]\n", 3544 | " [2 1 0]]\n", 3545 | "\n", 3546 | " [[1 3 1]\n", 3547 | " [1 4 1]\n", 3548 | " [1 0 4]]\n", 3549 | "\n", 3550 | " [[3 1 2]\n", 3551 | " [4 1 0]\n", 3552 | " [0 4 3]]\n", 3553 | "\n", 3554 | " [[1 2 0]\n", 3555 | " [1 0 0]\n", 3556 | " [4 3 2]]\n", 3557 | "\n", 3558 | " [[2 0 4]\n", 3559 | " [0 0 1]\n", 3560 | " [3 2 0]]]]\n" 3561 | ] 3562 | } 3563 | ], 3564 | "source": [ 3565 | "Z = np.random.randint(0,5,(10,10))\n", 3566 | "n = 3\n", 3567 | "i = 1 + (Z.shape[0]-3)\n", 3568 | "j = 1 + (Z.shape[1]-3)\n", 3569 | "C = stride_tricks.as_strided(Z, shape=(i, j, n, n), strides=Z.strides + Z.strides)\n", 3570 | "print(C)\n", 3571 | "\n", 3572 | "Z = np.random.randint(0,5,(10,10))\n", 3573 | "print(sliding_window_view(Z, window_shape=(3, 3)))" 3574 | ] 3575 | }, 3576 | { 3577 | "cell_type": "markdown", 3578 | "metadata": {}, 3579 | "source": [ 3580 | "#### 85. Create a 2D array subclass such that Z[i,j] == Z[j,i] (★★★)" 3581 | ] 3582 | }, 3583 | { 3584 | "cell_type": "code", 3585 | "execution_count": 88, 3586 | "metadata": {}, 3587 | "outputs": [ 3588 | { 3589 | "name": "stdout", 3590 | "output_type": "stream", 3591 | "text": [ 3592 | "[[ 1 15 4 6 11]\n", 3593 | " [15 5 12 16 9]\n", 3594 | " [ 4 12 6 42 7]\n", 3595 | " [ 6 16 42 1 8]\n", 3596 | " [11 9 7 8 7]]\n" 3597 | ] 3598 | } 3599 | ], 3600 | "source": [ 3601 | "# Note: only works for 2d array and value setting using indices\n", 3602 | "\n", 3603 | "class Symetric(np.ndarray):\n", 3604 | " def __setitem__(self, index, value):\n", 3605 | " i,j = index\n", 3606 | " super(Symetric, self).__setitem__((i,j), value)\n", 3607 | " super(Symetric, self).__setitem__((j,i), value)\n", 3608 | "\n", 3609 | "def symetric(Z):\n", 3610 | " return np.asarray(Z + Z.T - np.diag(Z.diagonal())).view(Symetric)\n", 3611 | "\n", 3612 | "S = symetric(np.random.randint(0,10,(5,5)))\n", 3613 | "S[2,3] = 42\n", 3614 | "print(S)" 3615 | ] 3616 | }, 3617 | { 3618 | "cell_type": "markdown", 3619 | "metadata": {}, 3620 | "source": [ 3621 | "#### 86. Consider a set of p matrices wich shape (n,n) and a set of p vectors with shape (n,1). How to compute the sum of of the p matrix products at once? (result has shape (n,1)) (★★★)" 3622 | ] 3623 | }, 3624 | { 3625 | "cell_type": "code", 3626 | "execution_count": 89, 3627 | "metadata": {}, 3628 | "outputs": [ 3629 | { 3630 | "name": "stdout", 3631 | "output_type": "stream", 3632 | "text": [ 3633 | "[[200.]\n", 3634 | " [200.]\n", 3635 | " [200.]\n", 3636 | " [200.]\n", 3637 | " [200.]\n", 3638 | " [200.]\n", 3639 | " [200.]\n", 3640 | " [200.]\n", 3641 | " [200.]\n", 3642 | " [200.]\n", 3643 | " [200.]\n", 3644 | " [200.]\n", 3645 | " [200.]\n", 3646 | " [200.]\n", 3647 | " [200.]\n", 3648 | " [200.]\n", 3649 | " [200.]\n", 3650 | " [200.]\n", 3651 | " [200.]\n", 3652 | " [200.]]\n" 3653 | ] 3654 | } 3655 | ], 3656 | "source": [ 3657 | "p, n = 10, 20\n", 3658 | "M = np.ones((p,n,n))\n", 3659 | "V = np.ones((p,n,1))\n", 3660 | "S = np.tensordot(M, V, axes=[[0, 2], [0, 1]])\n", 3661 | "print(S)\n", 3662 | "\n", 3663 | "# It works, because:\n", 3664 | "# M is (p,n,n)\n", 3665 | "# V is (p,n,1)\n", 3666 | "# Thus, summing over the paired axes 0 and 0 (of M and V independently),\n", 3667 | "# and 2 and 1, to remain with a (n,1) vector." 3668 | ] 3669 | }, 3670 | { 3671 | "cell_type": "markdown", 3672 | "metadata": {}, 3673 | "source": [ 3674 | "#### 87. Consider a 16x16 array, how to get the block-sum (block size is 4x4)? (★★★)" 3675 | ] 3676 | }, 3677 | { 3678 | "cell_type": "code", 3679 | "execution_count": 90, 3680 | "metadata": {}, 3681 | "outputs": [ 3682 | { 3683 | "name": "stdout", 3684 | "output_type": "stream", 3685 | "text": [ 3686 | "[[16. 16. 16. 16.]\n", 3687 | " [16. 16. 16. 16.]\n", 3688 | " [16. 16. 16. 16.]\n", 3689 | " [16. 16. 16. 16.]]\n", 3690 | "[[16. 16. 16. 16.]\n", 3691 | " [16. 16. 16. 16.]\n", 3692 | " [16. 16. 16. 16.]\n", 3693 | " [16. 16. 16. 16.]]\n" 3694 | ] 3695 | } 3696 | ], 3697 | "source": [ 3698 | "Z = np.ones((16,16))\n", 3699 | "k = 4\n", 3700 | "S = np.add.reduceat(np.add.reduceat(Z, np.arange(0, Z.shape[0], k), axis=0),\n", 3701 | " np.arange(0, Z.shape[1], k), axis=1)\n", 3702 | "print(S)\n", 3703 | "\n", 3704 | "# alternative solution:\n", 3705 | "\n", 3706 | "Z = np.ones((16,16))\n", 3707 | "k = 4\n", 3708 | "\n", 3709 | "windows = np.lib.stride_tricks.sliding_window_view(Z, (k, k))\n", 3710 | "S = windows[::k, ::k, ...].sum(axis=(-2, -1))\n", 3711 | "\n", 3712 | "Z = np.ones((16, 16))\n", 3713 | "k = 4\n", 3714 | "print(sliding_window_view(Z, window_shape=(k, k))[::k, ::k].sum(axis=(-2, -1)))" 3715 | ] 3716 | }, 3717 | { 3718 | "cell_type": "markdown", 3719 | "metadata": {}, 3720 | "source": [ 3721 | "#### 88. How to implement the Game of Life using numpy arrays? (★★★)" 3722 | ] 3723 | }, 3724 | { 3725 | "cell_type": "code", 3726 | "execution_count": 91, 3727 | "metadata": {}, 3728 | "outputs": [ 3729 | { 3730 | "name": "stdout", 3731 | "output_type": "stream", 3732 | "text": [ 3733 | "[[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", 3734 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3735 | " [0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", 3736 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3737 | " [0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", 3738 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3739 | " [0 0 0 0 0 0 0 0 0 1 1 0 0 1 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", 3740 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3741 | " [0 0 0 0 0 0 0 1 0 1 1 0 1 1 0 0 0 0 0 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0\n", 3742 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3743 | " [0 0 0 0 0 0 0 1 0 0 0 1 1 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", 3744 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3745 | " [0 0 0 0 0 0 1 1 0 0 0 0 1 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", 3746 | " 0 0 0 0 0 0 0 0 0 1 1 1 0 0]\n", 3747 | " [0 0 0 0 0 0 1 0 0 1 1 1 1 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0\n", 3748 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3749 | " [0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 1 1 0 1 0 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0\n", 3750 | " 0 0 0 0 0 1 1 1 0 0 0 0 0 0]\n", 3751 | " [0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", 3752 | " 0 0 0 0 0 1 0 0 1 0 0 0 0 0]\n", 3753 | " [0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0\n", 3754 | " 0 0 0 0 0 1 1 0 0 1 0 0 0 0]\n", 3755 | " [0 0 1 1 1 1 0 0 1 1 1 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0\n", 3756 | " 0 0 0 0 0 0 0 1 1 0 0 0 0 0]\n", 3757 | " [0 0 1 0 0 1 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0\n", 3758 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3759 | " [0 0 1 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0\n", 3760 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3761 | " [0 0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0\n", 3762 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3763 | " [0 0 0 0 0 0 1 1 1 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0\n", 3764 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3765 | " [0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0\n", 3766 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3767 | " [0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0\n", 3768 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3769 | " [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", 3770 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3771 | " [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", 3772 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3773 | " [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", 3774 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3775 | " [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0\n", 3776 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3777 | " [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0\n", 3778 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3779 | " [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 0 1 0 0 0 0 0 0 0 0\n", 3780 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3781 | " [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", 3782 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3783 | " [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", 3784 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3785 | " [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", 3786 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3787 | " [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", 3788 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3789 | " [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", 3790 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3791 | " [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", 3792 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3793 | " [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", 3794 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3795 | " [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", 3796 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3797 | " [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", 3798 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3799 | " [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", 3800 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3801 | " [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", 3802 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3803 | " [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 1\n", 3804 | " 1 1 0 1 1 0 0 0 0 0 0 0 0 0]\n", 3805 | " [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0\n", 3806 | " 0 1 0 0 0 1 0 0 0 0 0 0 0 0]\n", 3807 | " [0 0 0 0 0 0 1 1 1 0 0 0 0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1\n", 3808 | " 0 0 0 0 0 1 0 0 0 0 0 0 0 0]\n", 3809 | " [0 0 0 0 0 0 1 0 0 1 0 0 0 1 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1\n", 3810 | " 0 0 0 1 1 1 0 0 0 0 0 0 0 0]\n", 3811 | " [0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 1 1 0 1\n", 3812 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3813 | " [0 0 0 0 0 0 1 0 1 1 0 0 0 0 0 1 1 0 0 0 0 0 1 1 0 0 0 1 0 1 0 0 0 1 0 1\n", 3814 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3815 | " [0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 1 0\n", 3816 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3817 | " [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", 3818 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3819 | " [0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", 3820 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3821 | " [0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", 3822 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3823 | " [0 0 0 1 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", 3824 | " 0 0 1 1 0 0 0 0 0 0 0 0 0 0]\n", 3825 | " [0 0 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", 3826 | " 0 0 1 1 0 0 0 0 0 0 0 0 0 0]\n", 3827 | " [0 0 0 1 1 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", 3828 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3829 | " [0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", 3830 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 3831 | " [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", 3832 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]\n" 3833 | ] 3834 | } 3835 | ], 3836 | "source": [ 3837 | "def iterate(Z):\n", 3838 | " # Count neighbours\n", 3839 | " N = (Z[0:-2,0:-2] + Z[0:-2,1:-1] + Z[0:-2,2:] +\n", 3840 | " Z[1:-1,0:-2] + Z[1:-1,2:] +\n", 3841 | " Z[2: ,0:-2] + Z[2: ,1:-1] + Z[2: ,2:])\n", 3842 | "\n", 3843 | " # Apply rules\n", 3844 | " birth = (N==3) & (Z[1:-1,1:-1]==0)\n", 3845 | " survive = ((N==2) | (N==3)) & (Z[1:-1,1:-1]==1)\n", 3846 | " Z[...] = 0\n", 3847 | " Z[1:-1,1:-1][birth | survive] = 1\n", 3848 | " return Z\n", 3849 | "\n", 3850 | "Z = np.random.randint(0,2,(50,50))\n", 3851 | "for i in range(100): Z = iterate(Z)\n", 3852 | "print(Z)" 3853 | ] 3854 | }, 3855 | { 3856 | "cell_type": "markdown", 3857 | "metadata": {}, 3858 | "source": [ 3859 | "#### 89. How to get the n largest values of an array (★★★)" 3860 | ] 3861 | }, 3862 | { 3863 | "cell_type": "code", 3864 | "execution_count": 92, 3865 | "metadata": {}, 3866 | "outputs": [ 3867 | { 3868 | "name": "stdout", 3869 | "output_type": "stream", 3870 | "text": [ 3871 | "[9995 9996 9997 9998 9999]\n", 3872 | "[9999 9997 9996 9998 9995]\n" 3873 | ] 3874 | } 3875 | ], 3876 | "source": [ 3877 | "Z = np.arange(10000)\n", 3878 | "np.random.shuffle(Z)\n", 3879 | "n = 5\n", 3880 | "\n", 3881 | "# Slow\n", 3882 | "print (Z[np.argsort(Z)[-n:]])\n", 3883 | "\n", 3884 | "# Fast\n", 3885 | "print (Z[np.argpartition(-Z,n)[:n]])" 3886 | ] 3887 | }, 3888 | { 3889 | "cell_type": "markdown", 3890 | "metadata": {}, 3891 | "source": [ 3892 | "#### 90. Given an arbitrary number of vectors, build the cartesian product (every combinations of every item) (★★★)" 3893 | ] 3894 | }, 3895 | { 3896 | "cell_type": "code", 3897 | "execution_count": 93, 3898 | "metadata": {}, 3899 | "outputs": [ 3900 | { 3901 | "name": "stdout", 3902 | "output_type": "stream", 3903 | "text": [ 3904 | "[[1 4 6]\n", 3905 | " [1 4 7]\n", 3906 | " [1 5 6]\n", 3907 | " [1 5 7]\n", 3908 | " [2 4 6]\n", 3909 | " [2 4 7]\n", 3910 | " [2 5 6]\n", 3911 | " [2 5 7]\n", 3912 | " [3 4 6]\n", 3913 | " [3 4 7]\n", 3914 | " [3 5 6]\n", 3915 | " [3 5 7]]\n" 3916 | ] 3917 | } 3918 | ], 3919 | "source": [ 3920 | "def cartesian(arrays):\n", 3921 | " arrays = [np.asarray(a) for a in arrays]\n", 3922 | " shape = (len(x) for x in arrays)\n", 3923 | "\n", 3924 | " ix = np.indices(shape, dtype=int)\n", 3925 | " ix = ix.reshape(len(arrays), -1).T\n", 3926 | "\n", 3927 | " for n, arr in enumerate(arrays):\n", 3928 | " ix[:, n] = arrays[n][ix[:, n]]\n", 3929 | "\n", 3930 | " return ix\n", 3931 | "\n", 3932 | "print (cartesian(([1, 2, 3], [4, 5], [6, 7])))" 3933 | ] 3934 | }, 3935 | { 3936 | "cell_type": "markdown", 3937 | "metadata": {}, 3938 | "source": [ 3939 | "#### 91. How to create a record array from a regular array? (★★★)" 3940 | ] 3941 | }, 3942 | { 3943 | "cell_type": "code", 3944 | "execution_count": 94, 3945 | "metadata": {}, 3946 | "outputs": [ 3947 | { 3948 | "name": "stdout", 3949 | "output_type": "stream", 3950 | "text": [ 3951 | "[(b'Hello', 2.5, 3) (b'World', 3.6, 2)]\n" 3952 | ] 3953 | } 3954 | ], 3955 | "source": [ 3956 | "Z = np.array([(\"Hello\", 2.5, 3),\n", 3957 | " (\"World\", 3.6, 2)])\n", 3958 | "R = np.core.records.fromarrays(Z.T,\n", 3959 | " names='col1, col2, col3',\n", 3960 | " formats = 'S8, f8, i8')\n", 3961 | "print(R)" 3962 | ] 3963 | }, 3964 | { 3965 | "cell_type": "markdown", 3966 | "metadata": {}, 3967 | "source": [ 3968 | "#### 92. Consider a large vector Z, compute Z to the power of 3 using 3 different methods (★★★)" 3969 | ] 3970 | }, 3971 | { 3972 | "cell_type": "code", 3973 | "execution_count": 95, 3974 | "metadata": {}, 3975 | "outputs": [ 3976 | { 3977 | "name": "stdout", 3978 | "output_type": "stream", 3979 | "text": [ 3980 | "1.53 s ± 59.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n", 3981 | "389 ms ± 24.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n", 3982 | "222 ms ± 27 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" 3983 | ] 3984 | } 3985 | ], 3986 | "source": [ 3987 | "x = np.random.rand(int(5e7))\n", 3988 | "\n", 3989 | "%timeit np.power(x,3)\n", 3990 | "%timeit x*x*x\n", 3991 | "%timeit np.einsum('i,i,i->i',x,x,x)" 3992 | ] 3993 | }, 3994 | { 3995 | "cell_type": "markdown", 3996 | "metadata": {}, 3997 | "source": [ 3998 | "#### 93. Consider two arrays A and B of shape (8,3) and (2,2). How to find rows of A that contain elements of each row of B regardless of the order of the elements in B? (★★★)" 3999 | ] 4000 | }, 4001 | { 4002 | "cell_type": "code", 4003 | "execution_count": 96, 4004 | "metadata": {}, 4005 | "outputs": [ 4006 | { 4007 | "name": "stdout", 4008 | "output_type": "stream", 4009 | "text": [ 4010 | "[1 2 5 7]\n" 4011 | ] 4012 | } 4013 | ], 4014 | "source": [ 4015 | "A = np.random.randint(0,5,(8,3))\n", 4016 | "B = np.random.randint(0,5,(2,2))\n", 4017 | "\n", 4018 | "C = (A[..., np.newaxis, np.newaxis] == B)\n", 4019 | "rows = np.where(C.any((3,1)).all(1))[0]\n", 4020 | "print(rows)" 4021 | ] 4022 | }, 4023 | { 4024 | "cell_type": "markdown", 4025 | "metadata": {}, 4026 | "source": [ 4027 | "#### 94. Considering a 10x3 matrix, extract rows with unequal values (e.g. [2,2,3]) (★★★)" 4028 | ] 4029 | }, 4030 | { 4031 | "cell_type": "code", 4032 | "execution_count": 97, 4033 | "metadata": {}, 4034 | "outputs": [ 4035 | { 4036 | "name": "stdout", 4037 | "output_type": "stream", 4038 | "text": [ 4039 | "[[3 0 4]\n", 4040 | " [3 3 4]\n", 4041 | " [1 2 4]\n", 4042 | " [3 2 1]\n", 4043 | " [0 0 3]\n", 4044 | " [4 3 0]\n", 4045 | " [0 3 1]\n", 4046 | " [0 4 0]\n", 4047 | " [4 4 0]\n", 4048 | " [1 0 3]]\n", 4049 | "[[3 0 4]\n", 4050 | " [3 3 4]\n", 4051 | " [1 2 4]\n", 4052 | " [3 2 1]\n", 4053 | " [0 0 3]\n", 4054 | " [4 3 0]\n", 4055 | " [0 3 1]\n", 4056 | " [0 4 0]\n", 4057 | " [4 4 0]\n", 4058 | " [1 0 3]]\n", 4059 | "[[3 0 4]\n", 4060 | " [3 3 4]\n", 4061 | " [1 2 4]\n", 4062 | " [3 2 1]\n", 4063 | " [0 0 3]\n", 4064 | " [4 3 0]\n", 4065 | " [0 3 1]\n", 4066 | " [0 4 0]\n", 4067 | " [4 4 0]\n", 4068 | " [1 0 3]]\n" 4069 | ] 4070 | } 4071 | ], 4072 | "source": [ 4073 | "Z = np.random.randint(0,5,(10,3))\n", 4074 | "print(Z)\n", 4075 | "# solution for arrays of all dtypes (including string arrays and record arrays)\n", 4076 | "E = np.all(Z[:,1:] == Z[:,:-1], axis=1)\n", 4077 | "U = Z[~E]\n", 4078 | "print(U)\n", 4079 | "# soluiton for numerical arrays only, will work for any number of columns in Z\n", 4080 | "U = Z[Z.max(axis=1) != Z.min(axis=1),:]\n", 4081 | "print(U)" 4082 | ] 4083 | }, 4084 | { 4085 | "cell_type": "markdown", 4086 | "metadata": {}, 4087 | "source": [ 4088 | "#### 95. Convert a vector of ints into a matrix binary representation (★★★)" 4089 | ] 4090 | }, 4091 | { 4092 | "cell_type": "code", 4093 | "execution_count": 98, 4094 | "metadata": {}, 4095 | "outputs": [ 4096 | { 4097 | "name": "stdout", 4098 | "output_type": "stream", 4099 | "text": [ 4100 | "[[0 0 0 0 0 0 0 0]\n", 4101 | " [0 0 0 0 0 0 0 1]\n", 4102 | " [0 0 0 0 0 0 1 0]\n", 4103 | " [0 0 0 0 0 0 1 1]\n", 4104 | " [0 0 0 0 1 1 1 1]\n", 4105 | " [0 0 0 1 0 0 0 0]\n", 4106 | " [0 0 1 0 0 0 0 0]\n", 4107 | " [0 1 0 0 0 0 0 0]\n", 4108 | " [1 0 0 0 0 0 0 0]]\n", 4109 | "[[0 0 0 0 0 0 0 0]\n", 4110 | " [0 0 0 0 0 0 0 1]\n", 4111 | " [0 0 0 0 0 0 1 0]\n", 4112 | " [0 0 0 0 0 0 1 1]\n", 4113 | " [0 0 0 0 1 1 1 1]\n", 4114 | " [0 0 0 1 0 0 0 0]\n", 4115 | " [0 0 1 0 0 0 0 0]\n", 4116 | " [0 1 0 0 0 0 0 0]\n", 4117 | " [1 0 0 0 0 0 0 0]]\n" 4118 | ] 4119 | } 4120 | ], 4121 | "source": [ 4122 | "I = np.array([0, 1, 2, 3, 15, 16, 32, 64, 128])\n", 4123 | "B = ((I.reshape(-1,1) & (2**np.arange(8))) != 0).astype(int)\n", 4124 | "print(B[:,::-1])\n", 4125 | "\n", 4126 | "I = np.array([0, 1, 2, 3, 15, 16, 32, 64, 128], dtype=np.uint8)\n", 4127 | "print(np.unpackbits(I[:, np.newaxis], axis=1))" 4128 | ] 4129 | }, 4130 | { 4131 | "cell_type": "markdown", 4132 | "metadata": {}, 4133 | "source": [ 4134 | "#### 96. Given a two dimensional array, how to extract unique rows? (★★★)" 4135 | ] 4136 | }, 4137 | { 4138 | "cell_type": "code", 4139 | "execution_count": 99, 4140 | "metadata": {}, 4141 | "outputs": [ 4142 | { 4143 | "name": "stdout", 4144 | "output_type": "stream", 4145 | "text": [ 4146 | "[[0 0 1]\n", 4147 | " [1 0 0]\n", 4148 | " [1 0 1]\n", 4149 | " [1 1 1]]\n", 4150 | "[[0 0 1]\n", 4151 | " [1 0 0]\n", 4152 | " [1 0 1]\n", 4153 | " [1 1 1]]\n" 4154 | ] 4155 | } 4156 | ], 4157 | "source": [ 4158 | "Z = np.random.randint(0,2,(6,3))\n", 4159 | "T = np.ascontiguousarray(Z).view(np.dtype((np.void, Z.dtype.itemsize * Z.shape[1])))\n", 4160 | "_, idx = np.unique(T, return_index=True)\n", 4161 | "uZ = Z[idx]\n", 4162 | "print(uZ)\n", 4163 | "\n", 4164 | "# NumPy >= 1.13\n", 4165 | "uZ = np.unique(Z, axis=0)\n", 4166 | "print(uZ)" 4167 | ] 4168 | }, 4169 | { 4170 | "cell_type": "markdown", 4171 | "metadata": {}, 4172 | "source": [ 4173 | "#### 97. Considering 2 vectors A & B, write the einsum equivalent of inner, outer, sum, and mul function (★★★)" 4174 | ] 4175 | }, 4176 | { 4177 | "cell_type": "code", 4178 | "execution_count": 100, 4179 | "metadata": {}, 4180 | "outputs": [ 4181 | { 4182 | "data": { 4183 | "text/plain": [ 4184 | "array([[0.60526555, 0.16887694, 0.22900736, 0.23451826, 0.66688236,\n", 4185 | " 0.24630011, 0.26111032, 0.44050637, 0.21745296, 0.84205665],\n", 4186 | " [0.67396508, 0.188045 , 0.25500041, 0.26113681, 0.7425756 ,\n", 4187 | " 0.27425595, 0.29074715, 0.49050523, 0.24213455, 0.93763271],\n", 4188 | " [0.49767834, 0.13885871, 0.18830083, 0.19283215, 0.54834264,\n", 4189 | " 0.20251975, 0.21469741, 0.36220545, 0.17880024, 0.69237933],\n", 4190 | " [0.60838939, 0.16974853, 0.23018929, 0.23572863, 0.67032422,\n", 4191 | " 0.2475713 , 0.26245794, 0.44277988, 0.21857526, 0.8464026 ],\n", 4192 | " [0.37155136, 0.10366765, 0.14057961, 0.14396256, 0.40937577,\n", 4193 | " 0.15119503, 0.16028649, 0.27041146, 0.13348677, 0.51690914],\n", 4194 | " [0.69207301, 0.19309735, 0.2618517 , 0.26815297, 0.76252693,\n", 4195 | " 0.28162458, 0.29855888, 0.503684 , 0.24864016, 0.96282479],\n", 4196 | " [0.30608903, 0.08540281, 0.11581138, 0.1185983 , 0.33724929,\n", 4197 | " 0.12455651, 0.13204618, 0.22276862, 0.1099682 , 0.42583673],\n", 4198 | " [0.57797135, 0.1612615 , 0.21868037, 0.22394275, 0.63680958,\n", 4199 | " 0.23519331, 0.24933565, 0.42064192, 0.207647 , 0.80408445],\n", 4200 | " [0.32906809, 0.09181427, 0.12450571, 0.12750184, 0.36256764,\n", 4201 | " 0.13390735, 0.1419593 , 0.23949255, 0.11822386, 0.45780562],\n", 4202 | " [0.25004178, 0.0697649 , 0.09460543, 0.09688204, 0.27549635,\n", 4203 | " 0.10174925, 0.10786751, 0.18197797, 0.08983218, 0.34786275]])" 4204 | ] 4205 | }, 4206 | "execution_count": 100, 4207 | "metadata": {}, 4208 | "output_type": "execute_result" 4209 | } 4210 | ], 4211 | "source": [ 4212 | "\n", 4213 | "A = np.random.uniform(0,1,10)\n", 4214 | "B = np.random.uniform(0,1,10)\n", 4215 | "\n", 4216 | "np.einsum('i->', A) # np.sum(A)\n", 4217 | "np.einsum('i,i->i', A, B) # A * B\n", 4218 | "np.einsum('i,i', A, B) # np.inner(A, B)\n", 4219 | "np.einsum('i,j->ij', A, B) # np.outer(A, B)" 4220 | ] 4221 | }, 4222 | { 4223 | "cell_type": "markdown", 4224 | "metadata": {}, 4225 | "source": [ 4226 | "#### 98. Considering a path described by two vectors (X,Y), how to sample it using equidistant samples (★★★)?" 4227 | ] 4228 | }, 4229 | { 4230 | "cell_type": "code", 4231 | "execution_count": 101, 4232 | "metadata": {}, 4233 | "outputs": [], 4234 | "source": [ 4235 | "phi = np.arange(0, 10*np.pi, 0.1)\n", 4236 | "a = 1\n", 4237 | "x = a*phi*np.cos(phi)\n", 4238 | "y = a*phi*np.sin(phi)\n", 4239 | "\n", 4240 | "dr = (np.diff(x)**2 + np.diff(y)**2)**.5 # segment lengths\n", 4241 | "r = np.zeros_like(x)\n", 4242 | "r[1:] = np.cumsum(dr) # integrate path\n", 4243 | "r_int = np.linspace(0, r.max(), 200) # regular spaced path\n", 4244 | "x_int = np.interp(r_int, r, x) # integrate path\n", 4245 | "y_int = np.interp(r_int, r, y)" 4246 | ] 4247 | }, 4248 | { 4249 | "cell_type": "markdown", 4250 | "metadata": {}, 4251 | "source": [ 4252 | "#### 99. Given an integer n and a 2D array X, select from X the rows which can be interpreted as draws from a multinomial distribution with n degrees, i.e., the rows which only contain integers and which sum to n. (★★★)" 4253 | ] 4254 | }, 4255 | { 4256 | "cell_type": "code", 4257 | "execution_count": 102, 4258 | "metadata": {}, 4259 | "outputs": [ 4260 | { 4261 | "name": "stdout", 4262 | "output_type": "stream", 4263 | "text": [ 4264 | "[[2. 0. 1. 1.]]\n" 4265 | ] 4266 | } 4267 | ], 4268 | "source": [ 4269 | "X = np.asarray([[1.0, 0.0, 3.0, 8.0],\n", 4270 | " [2.0, 0.0, 1.0, 1.0],\n", 4271 | " [1.5, 2.5, 1.0, 0.0]])\n", 4272 | "n = 4\n", 4273 | "M = np.logical_and.reduce(np.mod(X, 1) == 0, axis=-1)\n", 4274 | "M &= (X.sum(axis=-1) == n)\n", 4275 | "print(X[M])" 4276 | ] 4277 | }, 4278 | { 4279 | "cell_type": "markdown", 4280 | "metadata": {}, 4281 | "source": [ 4282 | "#### 100. Compute bootstrapped 95% confidence intervals for the mean of a 1D array X (i.e., resample the elements of an array with replacement N times, compute the mean of each sample, and then compute percentiles over the means). (★★★)" 4283 | ] 4284 | }, 4285 | { 4286 | "cell_type": "code", 4287 | "execution_count": 103, 4288 | "metadata": {}, 4289 | "outputs": [ 4290 | { 4291 | "name": "stdout", 4292 | "output_type": "stream", 4293 | "text": [ 4294 | "[-0.17195547 0.21081716]\n" 4295 | ] 4296 | } 4297 | ], 4298 | "source": [ 4299 | "X = np.random.randn(100) # random 1D array\n", 4300 | "N = 1000 # number of bootstrap samples\n", 4301 | "idx = np.random.randint(0, X.size, (N, X.size))\n", 4302 | "means = X[idx].mean(axis=1)\n", 4303 | "confint = np.percentile(means, [2.5, 97.5])\n", 4304 | "print(confint)" 4305 | ] 4306 | }, 4307 | { 4308 | "cell_type": "code", 4309 | "execution_count": null, 4310 | "metadata": {}, 4311 | "outputs": [], 4312 | "source": [] 4313 | }, 4314 | { 4315 | "cell_type": "code", 4316 | "execution_count": null, 4317 | "metadata": {}, 4318 | "outputs": [], 4319 | "source": [] 4320 | } 4321 | ], 4322 | "metadata": { 4323 | "kernelspec": { 4324 | "display_name": "Python 3", 4325 | "language": "python", 4326 | "name": "python3" 4327 | }, 4328 | "language_info": { 4329 | "codemirror_mode": { 4330 | "name": "ipython", 4331 | "version": 3 4332 | }, 4333 | "file_extension": ".py", 4334 | "mimetype": "text/x-python", 4335 | "name": "python", 4336 | "nbconvert_exporter": "python", 4337 | "pygments_lexer": "ipython3", 4338 | "version": "3.7.6" 4339 | } 4340 | }, 4341 | "nbformat": 4, 4342 | "nbformat_minor": 5 4343 | } 4344 | -------------------------------------------------------------------------------- /100_Numpy_exercises.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 100 numpy exercises\n" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "#### 1. Import the numpy package under the name `np` (★☆☆)" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": null, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "#### 2. Print the numpy version and the configuration (★☆☆)" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": null, 34 | "metadata": {}, 35 | "outputs": [], 36 | "source": [] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "metadata": {}, 41 | "source": [ 42 | "#### 3. Create a null vector of size 10 (★☆☆)" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": {}, 55 | "source": [ 56 | "#### 4. How to find the memory size of any array (★☆☆)" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": null, 62 | "metadata": {}, 63 | "outputs": [], 64 | "source": [] 65 | }, 66 | { 67 | "cell_type": "markdown", 68 | "metadata": {}, 69 | "source": [ 70 | "#### 5. How to get the documentation of the numpy add function from the command line? (★☆☆)" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": null, 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [] 79 | }, 80 | { 81 | "cell_type": "markdown", 82 | "metadata": {}, 83 | "source": [ 84 | "#### 6. Create a null vector of size 10 but the fifth value which is 1 (★☆☆)" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": null, 90 | "metadata": {}, 91 | "outputs": [], 92 | "source": [] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "metadata": {}, 97 | "source": [ 98 | "#### 7. Create a vector with values ranging from 10 to 49 (★☆☆)" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": null, 104 | "metadata": {}, 105 | "outputs": [], 106 | "source": [] 107 | }, 108 | { 109 | "cell_type": "markdown", 110 | "metadata": {}, 111 | "source": [ 112 | "#### 8. Reverse a vector (first element becomes last) (★☆☆)" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": null, 118 | "metadata": {}, 119 | "outputs": [], 120 | "source": [] 121 | }, 122 | { 123 | "cell_type": "markdown", 124 | "metadata": {}, 125 | "source": [ 126 | "#### 9. Create a 3x3 matrix with values ranging from 0 to 8 (★☆☆)" 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": null, 132 | "metadata": {}, 133 | "outputs": [], 134 | "source": [] 135 | }, 136 | { 137 | "cell_type": "markdown", 138 | "metadata": {}, 139 | "source": [ 140 | "#### 10. Find indices of non-zero elements from [1,2,0,0,4,0] (★☆☆)" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": null, 146 | "metadata": {}, 147 | "outputs": [], 148 | "source": [] 149 | }, 150 | { 151 | "cell_type": "markdown", 152 | "metadata": {}, 153 | "source": [ 154 | "#### 11. Create a 3x3 identity matrix (★☆☆)" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": null, 160 | "metadata": {}, 161 | "outputs": [], 162 | "source": [] 163 | }, 164 | { 165 | "cell_type": "markdown", 166 | "metadata": {}, 167 | "source": [ 168 | "#### 12. Create a 3x3x3 array with random values (★☆☆)" 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": null, 174 | "metadata": {}, 175 | "outputs": [], 176 | "source": [] 177 | }, 178 | { 179 | "cell_type": "markdown", 180 | "metadata": {}, 181 | "source": [ 182 | "#### 13. Create a 10x10 array with random values and find the minimum and maximum values (★☆☆)" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": null, 188 | "metadata": {}, 189 | "outputs": [], 190 | "source": [] 191 | }, 192 | { 193 | "cell_type": "markdown", 194 | "metadata": {}, 195 | "source": [ 196 | "#### 14. Create a random vector of size 30 and find the mean value (★☆☆)" 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": null, 202 | "metadata": {}, 203 | "outputs": [], 204 | "source": [] 205 | }, 206 | { 207 | "cell_type": "markdown", 208 | "metadata": {}, 209 | "source": [ 210 | "#### 15. Create a 2d array with 1 on the border and 0 inside (★☆☆)" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": null, 216 | "metadata": {}, 217 | "outputs": [], 218 | "source": [] 219 | }, 220 | { 221 | "cell_type": "markdown", 222 | "metadata": {}, 223 | "source": [ 224 | "#### 16. How to add a border (filled with 0's) around an existing array? (★☆☆)" 225 | ] 226 | }, 227 | { 228 | "cell_type": "code", 229 | "execution_count": null, 230 | "metadata": {}, 231 | "outputs": [], 232 | "source": [] 233 | }, 234 | { 235 | "cell_type": "markdown", 236 | "metadata": {}, 237 | "source": [ 238 | "#### 17. What is the result of the following expression? (★☆☆)\n", 239 | "```python\n", 240 | "0 * np.nan\n", 241 | "np.nan == np.nan\n", 242 | "np.inf > np.nan\n", 243 | "np.nan - np.nan\n", 244 | "np.nan in set([np.nan])\n", 245 | "0.3 == 3 * 0.1\n", 246 | "```" 247 | ] 248 | }, 249 | { 250 | "cell_type": "code", 251 | "execution_count": null, 252 | "metadata": {}, 253 | "outputs": [], 254 | "source": [] 255 | }, 256 | { 257 | "cell_type": "markdown", 258 | "metadata": {}, 259 | "source": [ 260 | "#### 18. Create a 5x5 matrix with values 1,2,3,4 just below the diagonal (★☆☆)" 261 | ] 262 | }, 263 | { 264 | "cell_type": "code", 265 | "execution_count": null, 266 | "metadata": {}, 267 | "outputs": [], 268 | "source": [] 269 | }, 270 | { 271 | "cell_type": "markdown", 272 | "metadata": {}, 273 | "source": [ 274 | "#### 19. Create a 8x8 matrix and fill it with a checkerboard pattern (★☆☆)" 275 | ] 276 | }, 277 | { 278 | "cell_type": "code", 279 | "execution_count": null, 280 | "metadata": {}, 281 | "outputs": [], 282 | "source": [] 283 | }, 284 | { 285 | "cell_type": "markdown", 286 | "metadata": {}, 287 | "source": [ 288 | "#### 20. Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element? (★☆☆)" 289 | ] 290 | }, 291 | { 292 | "cell_type": "code", 293 | "execution_count": null, 294 | "metadata": {}, 295 | "outputs": [], 296 | "source": [] 297 | }, 298 | { 299 | "cell_type": "markdown", 300 | "metadata": {}, 301 | "source": [ 302 | "#### 21. Create a checkerboard 8x8 matrix using the tile function (★☆☆)" 303 | ] 304 | }, 305 | { 306 | "cell_type": "code", 307 | "execution_count": null, 308 | "metadata": {}, 309 | "outputs": [], 310 | "source": [] 311 | }, 312 | { 313 | "cell_type": "markdown", 314 | "metadata": {}, 315 | "source": [ 316 | "#### 22. Normalize a 5x5 random matrix (★☆☆)" 317 | ] 318 | }, 319 | { 320 | "cell_type": "code", 321 | "execution_count": null, 322 | "metadata": {}, 323 | "outputs": [], 324 | "source": [] 325 | }, 326 | { 327 | "cell_type": "markdown", 328 | "metadata": {}, 329 | "source": [ 330 | "#### 23. Create a custom dtype that describes a color as four unsigned bytes (RGBA) (★☆☆)" 331 | ] 332 | }, 333 | { 334 | "cell_type": "code", 335 | "execution_count": null, 336 | "metadata": {}, 337 | "outputs": [], 338 | "source": [] 339 | }, 340 | { 341 | "cell_type": "markdown", 342 | "metadata": {}, 343 | "source": [ 344 | "#### 24. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) (★☆☆)" 345 | ] 346 | }, 347 | { 348 | "cell_type": "code", 349 | "execution_count": null, 350 | "metadata": {}, 351 | "outputs": [], 352 | "source": [] 353 | }, 354 | { 355 | "cell_type": "markdown", 356 | "metadata": {}, 357 | "source": [ 358 | "#### 25. Given a 1D array, negate all elements which are between 3 and 8, in place. (★☆☆)" 359 | ] 360 | }, 361 | { 362 | "cell_type": "code", 363 | "execution_count": null, 364 | "metadata": {}, 365 | "outputs": [], 366 | "source": [] 367 | }, 368 | { 369 | "cell_type": "markdown", 370 | "metadata": {}, 371 | "source": [ 372 | "#### 26. What is the output of the following script? (★☆☆)\n", 373 | "```python\n", 374 | "\n", 375 | "print(sum(range(5),-1))\n", 376 | "from numpy import *\n", 377 | "print(sum(range(5),-1))\n", 378 | "```" 379 | ] 380 | }, 381 | { 382 | "cell_type": "code", 383 | "execution_count": null, 384 | "metadata": {}, 385 | "outputs": [], 386 | "source": [] 387 | }, 388 | { 389 | "cell_type": "markdown", 390 | "metadata": {}, 391 | "source": [ 392 | "#### 27. Consider an integer vector Z, which of these expressions are legal? (★☆☆)\n", 393 | "```python\n", 394 | "Z**Z\n", 395 | "2 << Z >> 2\n", 396 | "Z <- Z\n", 397 | "1j*Z\n", 398 | "Z/1/1\n", 399 | "ZZ\n", 400 | "```" 401 | ] 402 | }, 403 | { 404 | "cell_type": "code", 405 | "execution_count": null, 406 | "metadata": {}, 407 | "outputs": [], 408 | "source": [] 409 | }, 410 | { 411 | "cell_type": "markdown", 412 | "metadata": {}, 413 | "source": [ 414 | "#### 28. What are the result of the following expressions? (★☆☆)\n", 415 | "```python\n", 416 | "np.array(0) / np.array(0)\n", 417 | "np.array(0) // np.array(0)\n", 418 | "np.array([np.nan]).astype(int).astype(float)\n", 419 | "```" 420 | ] 421 | }, 422 | { 423 | "cell_type": "code", 424 | "execution_count": null, 425 | "metadata": {}, 426 | "outputs": [], 427 | "source": [] 428 | }, 429 | { 430 | "cell_type": "markdown", 431 | "metadata": {}, 432 | "source": [ 433 | "#### 29. How to round away from zero a float array ? (★☆☆)" 434 | ] 435 | }, 436 | { 437 | "cell_type": "code", 438 | "execution_count": null, 439 | "metadata": {}, 440 | "outputs": [], 441 | "source": [] 442 | }, 443 | { 444 | "cell_type": "markdown", 445 | "metadata": {}, 446 | "source": [ 447 | "#### 30. How to find common values between two arrays? (★☆☆)" 448 | ] 449 | }, 450 | { 451 | "cell_type": "code", 452 | "execution_count": null, 453 | "metadata": {}, 454 | "outputs": [], 455 | "source": [] 456 | }, 457 | { 458 | "cell_type": "markdown", 459 | "metadata": {}, 460 | "source": [ 461 | "#### 31. How to ignore all numpy warnings (not recommended)? (★☆☆)" 462 | ] 463 | }, 464 | { 465 | "cell_type": "code", 466 | "execution_count": null, 467 | "metadata": {}, 468 | "outputs": [], 469 | "source": [] 470 | }, 471 | { 472 | "cell_type": "markdown", 473 | "metadata": {}, 474 | "source": [ 475 | "#### 32. Is the following expressions true? (★☆☆)\n", 476 | "```python\n", 477 | "np.sqrt(-1) == np.emath.sqrt(-1)\n", 478 | "```" 479 | ] 480 | }, 481 | { 482 | "cell_type": "code", 483 | "execution_count": null, 484 | "metadata": {}, 485 | "outputs": [], 486 | "source": [] 487 | }, 488 | { 489 | "cell_type": "markdown", 490 | "metadata": {}, 491 | "source": [ 492 | "#### 33. How to get the dates of yesterday, today and tomorrow? (★☆☆)" 493 | ] 494 | }, 495 | { 496 | "cell_type": "code", 497 | "execution_count": null, 498 | "metadata": {}, 499 | "outputs": [], 500 | "source": [] 501 | }, 502 | { 503 | "cell_type": "markdown", 504 | "metadata": {}, 505 | "source": [ 506 | "#### 34. How to get all the dates corresponding to the month of July 2016? (★★☆)" 507 | ] 508 | }, 509 | { 510 | "cell_type": "code", 511 | "execution_count": null, 512 | "metadata": {}, 513 | "outputs": [], 514 | "source": [] 515 | }, 516 | { 517 | "cell_type": "markdown", 518 | "metadata": {}, 519 | "source": [ 520 | "#### 35. How to compute ((A+B)*(-A/2)) in place (without copy)? (★★☆)" 521 | ] 522 | }, 523 | { 524 | "cell_type": "code", 525 | "execution_count": null, 526 | "metadata": {}, 527 | "outputs": [], 528 | "source": [] 529 | }, 530 | { 531 | "cell_type": "markdown", 532 | "metadata": {}, 533 | "source": [ 534 | "#### 36. Extract the integer part of a random array of positive numbers using 4 different methods (★★☆)" 535 | ] 536 | }, 537 | { 538 | "cell_type": "code", 539 | "execution_count": null, 540 | "metadata": {}, 541 | "outputs": [], 542 | "source": [] 543 | }, 544 | { 545 | "cell_type": "markdown", 546 | "metadata": {}, 547 | "source": [ 548 | "#### 37. Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆)" 549 | ] 550 | }, 551 | { 552 | "cell_type": "code", 553 | "execution_count": null, 554 | "metadata": {}, 555 | "outputs": [], 556 | "source": [] 557 | }, 558 | { 559 | "cell_type": "markdown", 560 | "metadata": {}, 561 | "source": [ 562 | "#### 38. Consider a generator function that generates 10 integers and use it to build an array (★☆☆)" 563 | ] 564 | }, 565 | { 566 | "cell_type": "code", 567 | "execution_count": null, 568 | "metadata": {}, 569 | "outputs": [], 570 | "source": [] 571 | }, 572 | { 573 | "cell_type": "markdown", 574 | "metadata": {}, 575 | "source": [ 576 | "#### 39. Create a vector of size 10 with values ranging from 0 to 1, both excluded (★★☆)" 577 | ] 578 | }, 579 | { 580 | "cell_type": "code", 581 | "execution_count": null, 582 | "metadata": {}, 583 | "outputs": [], 584 | "source": [] 585 | }, 586 | { 587 | "cell_type": "markdown", 588 | "metadata": {}, 589 | "source": [ 590 | "#### 40. Create a random vector of size 10 and sort it (★★☆)" 591 | ] 592 | }, 593 | { 594 | "cell_type": "code", 595 | "execution_count": null, 596 | "metadata": {}, 597 | "outputs": [], 598 | "source": [] 599 | }, 600 | { 601 | "cell_type": "markdown", 602 | "metadata": {}, 603 | "source": [ 604 | "#### 41. How to sum a small array faster than np.sum? (★★☆)" 605 | ] 606 | }, 607 | { 608 | "cell_type": "code", 609 | "execution_count": null, 610 | "metadata": {}, 611 | "outputs": [], 612 | "source": [] 613 | }, 614 | { 615 | "cell_type": "markdown", 616 | "metadata": {}, 617 | "source": [ 618 | "#### 42. Consider two random array A and B, check if they are equal (★★☆)" 619 | ] 620 | }, 621 | { 622 | "cell_type": "code", 623 | "execution_count": null, 624 | "metadata": {}, 625 | "outputs": [], 626 | "source": [] 627 | }, 628 | { 629 | "cell_type": "markdown", 630 | "metadata": {}, 631 | "source": [ 632 | "#### 43. Make an array immutable (read-only) (★★☆)" 633 | ] 634 | }, 635 | { 636 | "cell_type": "code", 637 | "execution_count": null, 638 | "metadata": {}, 639 | "outputs": [], 640 | "source": [] 641 | }, 642 | { 643 | "cell_type": "markdown", 644 | "metadata": {}, 645 | "source": [ 646 | "#### 44. Consider a random 10x2 matrix representing cartesian coordinates, convert them to polar coordinates (★★☆)" 647 | ] 648 | }, 649 | { 650 | "cell_type": "code", 651 | "execution_count": null, 652 | "metadata": {}, 653 | "outputs": [], 654 | "source": [] 655 | }, 656 | { 657 | "cell_type": "markdown", 658 | "metadata": {}, 659 | "source": [ 660 | "#### 45. Create random vector of size 10 and replace the maximum value by 0 (★★☆)" 661 | ] 662 | }, 663 | { 664 | "cell_type": "code", 665 | "execution_count": null, 666 | "metadata": {}, 667 | "outputs": [], 668 | "source": [] 669 | }, 670 | { 671 | "cell_type": "markdown", 672 | "metadata": {}, 673 | "source": [ 674 | "#### 46. Create a structured array with `x` and `y` coordinates covering the [0,1]x[0,1] area (★★☆)" 675 | ] 676 | }, 677 | { 678 | "cell_type": "code", 679 | "execution_count": null, 680 | "metadata": {}, 681 | "outputs": [], 682 | "source": [] 683 | }, 684 | { 685 | "cell_type": "markdown", 686 | "metadata": {}, 687 | "source": [ 688 | "#### 47. Given two arrays, X and Y, construct the Cauchy matrix C (Cij =1/(xi - yj)) (★★☆)" 689 | ] 690 | }, 691 | { 692 | "cell_type": "code", 693 | "execution_count": null, 694 | "metadata": {}, 695 | "outputs": [], 696 | "source": [] 697 | }, 698 | { 699 | "cell_type": "markdown", 700 | "metadata": {}, 701 | "source": [ 702 | "#### 48. Print the minimum and maximum representable value for each numpy scalar type (★★☆)" 703 | ] 704 | }, 705 | { 706 | "cell_type": "code", 707 | "execution_count": null, 708 | "metadata": {}, 709 | "outputs": [], 710 | "source": [] 711 | }, 712 | { 713 | "cell_type": "markdown", 714 | "metadata": {}, 715 | "source": [ 716 | "#### 49. How to print all the values of an array? (★★☆)" 717 | ] 718 | }, 719 | { 720 | "cell_type": "code", 721 | "execution_count": null, 722 | "metadata": {}, 723 | "outputs": [], 724 | "source": [] 725 | }, 726 | { 727 | "cell_type": "markdown", 728 | "metadata": {}, 729 | "source": [ 730 | "#### 50. How to find the closest value (to a given scalar) in a vector? (★★☆)" 731 | ] 732 | }, 733 | { 734 | "cell_type": "code", 735 | "execution_count": null, 736 | "metadata": {}, 737 | "outputs": [], 738 | "source": [] 739 | }, 740 | { 741 | "cell_type": "markdown", 742 | "metadata": {}, 743 | "source": [ 744 | "#### 51. Create a structured array representing a position (x,y) and a color (r,g,b) (★★☆)" 745 | ] 746 | }, 747 | { 748 | "cell_type": "code", 749 | "execution_count": null, 750 | "metadata": {}, 751 | "outputs": [], 752 | "source": [] 753 | }, 754 | { 755 | "cell_type": "markdown", 756 | "metadata": {}, 757 | "source": [ 758 | "#### 52. Consider a random vector with shape (100,2) representing coordinates, find point by point distances (★★☆)" 759 | ] 760 | }, 761 | { 762 | "cell_type": "code", 763 | "execution_count": null, 764 | "metadata": {}, 765 | "outputs": [], 766 | "source": [] 767 | }, 768 | { 769 | "cell_type": "markdown", 770 | "metadata": {}, 771 | "source": [ 772 | "#### 53. How to convert a float (32 bits) array into an integer (32 bits) in place?" 773 | ] 774 | }, 775 | { 776 | "cell_type": "code", 777 | "execution_count": null, 778 | "metadata": {}, 779 | "outputs": [], 780 | "source": [] 781 | }, 782 | { 783 | "cell_type": "markdown", 784 | "metadata": {}, 785 | "source": [ 786 | "#### 54. How to read the following file? (★★☆)\n", 787 | "```\n", 788 | "1, 2, 3, 4, 5\n", 789 | "6, , , 7, 8\n", 790 | " , , 9,10,11\n", 791 | "```" 792 | ] 793 | }, 794 | { 795 | "cell_type": "code", 796 | "execution_count": null, 797 | "metadata": {}, 798 | "outputs": [], 799 | "source": [] 800 | }, 801 | { 802 | "cell_type": "markdown", 803 | "metadata": {}, 804 | "source": [ 805 | "#### 55. What is the equivalent of enumerate for numpy arrays? (★★☆)" 806 | ] 807 | }, 808 | { 809 | "cell_type": "code", 810 | "execution_count": null, 811 | "metadata": {}, 812 | "outputs": [], 813 | "source": [] 814 | }, 815 | { 816 | "cell_type": "markdown", 817 | "metadata": {}, 818 | "source": [ 819 | "#### 56. Generate a generic 2D Gaussian-like array (★★☆)" 820 | ] 821 | }, 822 | { 823 | "cell_type": "code", 824 | "execution_count": null, 825 | "metadata": {}, 826 | "outputs": [], 827 | "source": [] 828 | }, 829 | { 830 | "cell_type": "markdown", 831 | "metadata": {}, 832 | "source": [ 833 | "#### 57. How to randomly place p elements in a 2D array? (★★☆)" 834 | ] 835 | }, 836 | { 837 | "cell_type": "code", 838 | "execution_count": null, 839 | "metadata": {}, 840 | "outputs": [], 841 | "source": [] 842 | }, 843 | { 844 | "cell_type": "markdown", 845 | "metadata": {}, 846 | "source": [ 847 | "#### 58. Subtract the mean of each row of a matrix (★★☆)" 848 | ] 849 | }, 850 | { 851 | "cell_type": "code", 852 | "execution_count": null, 853 | "metadata": {}, 854 | "outputs": [], 855 | "source": [] 856 | }, 857 | { 858 | "cell_type": "markdown", 859 | "metadata": {}, 860 | "source": [ 861 | "#### 59. How to sort an array by the nth column? (★★☆)" 862 | ] 863 | }, 864 | { 865 | "cell_type": "code", 866 | "execution_count": null, 867 | "metadata": {}, 868 | "outputs": [], 869 | "source": [] 870 | }, 871 | { 872 | "cell_type": "markdown", 873 | "metadata": {}, 874 | "source": [ 875 | "#### 60. How to tell if a given 2D array has null columns? (★★☆)" 876 | ] 877 | }, 878 | { 879 | "cell_type": "code", 880 | "execution_count": null, 881 | "metadata": {}, 882 | "outputs": [], 883 | "source": [] 884 | }, 885 | { 886 | "cell_type": "markdown", 887 | "metadata": {}, 888 | "source": [ 889 | "#### 61. Find the nearest value from a given value in an array (★★☆)" 890 | ] 891 | }, 892 | { 893 | "cell_type": "code", 894 | "execution_count": null, 895 | "metadata": {}, 896 | "outputs": [], 897 | "source": [] 898 | }, 899 | { 900 | "cell_type": "markdown", 901 | "metadata": {}, 902 | "source": [ 903 | "#### 62. Considering two arrays with shape (1,3) and (3,1), how to compute their sum using an iterator? (★★☆)" 904 | ] 905 | }, 906 | { 907 | "cell_type": "code", 908 | "execution_count": null, 909 | "metadata": {}, 910 | "outputs": [], 911 | "source": [] 912 | }, 913 | { 914 | "cell_type": "markdown", 915 | "metadata": {}, 916 | "source": [ 917 | "#### 63. Create an array class that has a name attribute (★★☆)" 918 | ] 919 | }, 920 | { 921 | "cell_type": "code", 922 | "execution_count": null, 923 | "metadata": {}, 924 | "outputs": [], 925 | "source": [] 926 | }, 927 | { 928 | "cell_type": "markdown", 929 | "metadata": {}, 930 | "source": [ 931 | "#### 64. Consider a given vector, how to add 1 to each element indexed by a second vector (be careful with repeated indices)? (★★★)" 932 | ] 933 | }, 934 | { 935 | "cell_type": "code", 936 | "execution_count": null, 937 | "metadata": {}, 938 | "outputs": [], 939 | "source": [] 940 | }, 941 | { 942 | "cell_type": "markdown", 943 | "metadata": {}, 944 | "source": [ 945 | "#### 65. How to accumulate elements of a vector (X) to an array (F) based on an index list (I)? (★★★)" 946 | ] 947 | }, 948 | { 949 | "cell_type": "code", 950 | "execution_count": null, 951 | "metadata": {}, 952 | "outputs": [], 953 | "source": [] 954 | }, 955 | { 956 | "cell_type": "markdown", 957 | "metadata": {}, 958 | "source": [ 959 | "#### 66. Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique colors (★★☆)" 960 | ] 961 | }, 962 | { 963 | "cell_type": "code", 964 | "execution_count": null, 965 | "metadata": {}, 966 | "outputs": [], 967 | "source": [] 968 | }, 969 | { 970 | "cell_type": "markdown", 971 | "metadata": {}, 972 | "source": [ 973 | "#### 67. Considering a four dimensions array, how to get sum over the last two axis at once? (★★★)" 974 | ] 975 | }, 976 | { 977 | "cell_type": "code", 978 | "execution_count": null, 979 | "metadata": {}, 980 | "outputs": [], 981 | "source": [] 982 | }, 983 | { 984 | "cell_type": "markdown", 985 | "metadata": {}, 986 | "source": [ 987 | "#### 68. Considering a one-dimensional vector D, how to compute means of subsets of D using a vector S of same size describing subset indices? (★★★)" 988 | ] 989 | }, 990 | { 991 | "cell_type": "code", 992 | "execution_count": null, 993 | "metadata": {}, 994 | "outputs": [], 995 | "source": [] 996 | }, 997 | { 998 | "cell_type": "markdown", 999 | "metadata": {}, 1000 | "source": [ 1001 | "#### 69. How to get the diagonal of a dot product? (★★★)" 1002 | ] 1003 | }, 1004 | { 1005 | "cell_type": "code", 1006 | "execution_count": null, 1007 | "metadata": {}, 1008 | "outputs": [], 1009 | "source": [] 1010 | }, 1011 | { 1012 | "cell_type": "markdown", 1013 | "metadata": {}, 1014 | "source": [ 1015 | "#### 70. Consider the vector [1, 2, 3, 4, 5], how to build a new vector with 3 consecutive zeros interleaved between each value? (★★★)" 1016 | ] 1017 | }, 1018 | { 1019 | "cell_type": "code", 1020 | "execution_count": null, 1021 | "metadata": {}, 1022 | "outputs": [], 1023 | "source": [] 1024 | }, 1025 | { 1026 | "cell_type": "markdown", 1027 | "metadata": {}, 1028 | "source": [ 1029 | "#### 71. Consider an array of dimension (5,5,3), how to mulitply it by an array with dimensions (5,5)? (★★★)" 1030 | ] 1031 | }, 1032 | { 1033 | "cell_type": "code", 1034 | "execution_count": null, 1035 | "metadata": {}, 1036 | "outputs": [], 1037 | "source": [] 1038 | }, 1039 | { 1040 | "cell_type": "markdown", 1041 | "metadata": {}, 1042 | "source": [ 1043 | "#### 72. How to swap two rows of an array? (★★★)" 1044 | ] 1045 | }, 1046 | { 1047 | "cell_type": "code", 1048 | "execution_count": null, 1049 | "metadata": {}, 1050 | "outputs": [], 1051 | "source": [] 1052 | }, 1053 | { 1054 | "cell_type": "markdown", 1055 | "metadata": {}, 1056 | "source": [ 1057 | "#### 73. Consider a set of 10 triplets describing 10 triangles (with shared vertices), find the set of unique line segments composing all the triangles (★★★)" 1058 | ] 1059 | }, 1060 | { 1061 | "cell_type": "code", 1062 | "execution_count": null, 1063 | "metadata": {}, 1064 | "outputs": [], 1065 | "source": [] 1066 | }, 1067 | { 1068 | "cell_type": "markdown", 1069 | "metadata": {}, 1070 | "source": [ 1071 | "#### 74. Given a sorted array C that corresponds to a bincount, how to produce an array A such that np.bincount(A) == C? (★★★)" 1072 | ] 1073 | }, 1074 | { 1075 | "cell_type": "code", 1076 | "execution_count": null, 1077 | "metadata": {}, 1078 | "outputs": [], 1079 | "source": [] 1080 | }, 1081 | { 1082 | "cell_type": "markdown", 1083 | "metadata": {}, 1084 | "source": [ 1085 | "#### 75. How to compute averages using a sliding window over an array? (★★★)" 1086 | ] 1087 | }, 1088 | { 1089 | "cell_type": "code", 1090 | "execution_count": null, 1091 | "metadata": {}, 1092 | "outputs": [], 1093 | "source": [] 1094 | }, 1095 | { 1096 | "cell_type": "markdown", 1097 | "metadata": {}, 1098 | "source": [ 1099 | "#### 76. Consider a one-dimensional array Z, build a two-dimensional array whose first row is (Z[0],Z[1],Z[2]) and each subsequent row is shifted by 1 (last row should be (Z[-3],Z[-2],Z[-1]) (★★★)" 1100 | ] 1101 | }, 1102 | { 1103 | "cell_type": "code", 1104 | "execution_count": null, 1105 | "metadata": {}, 1106 | "outputs": [], 1107 | "source": [] 1108 | }, 1109 | { 1110 | "cell_type": "markdown", 1111 | "metadata": {}, 1112 | "source": [ 1113 | "#### 77. How to negate a boolean, or to change the sign of a float inplace? (★★★)" 1114 | ] 1115 | }, 1116 | { 1117 | "cell_type": "code", 1118 | "execution_count": null, 1119 | "metadata": {}, 1120 | "outputs": [], 1121 | "source": [] 1122 | }, 1123 | { 1124 | "cell_type": "markdown", 1125 | "metadata": {}, 1126 | "source": [ 1127 | "#### 78. Consider 2 sets of points P0,P1 describing lines (2d) and a point p, how to compute distance from p to each line i (P0[i],P1[i])? (★★★)" 1128 | ] 1129 | }, 1130 | { 1131 | "cell_type": "code", 1132 | "execution_count": null, 1133 | "metadata": {}, 1134 | "outputs": [], 1135 | "source": [] 1136 | }, 1137 | { 1138 | "cell_type": "markdown", 1139 | "metadata": {}, 1140 | "source": [ 1141 | "#### 79. Consider 2 sets of points P0,P1 describing lines (2d) and a set of points P, how to compute distance from each point j (P[j]) to each line i (P0[i],P1[i])? (★★★)" 1142 | ] 1143 | }, 1144 | { 1145 | "cell_type": "code", 1146 | "execution_count": null, 1147 | "metadata": {}, 1148 | "outputs": [], 1149 | "source": [] 1150 | }, 1151 | { 1152 | "cell_type": "markdown", 1153 | "metadata": {}, 1154 | "source": [ 1155 | "#### 80. Consider an arbitrary array, write a function that extract a subpart with a fixed shape and centered on a given element (pad with a `fill` value when necessary) (★★★)" 1156 | ] 1157 | }, 1158 | { 1159 | "cell_type": "code", 1160 | "execution_count": null, 1161 | "metadata": {}, 1162 | "outputs": [], 1163 | "source": [] 1164 | }, 1165 | { 1166 | "cell_type": "markdown", 1167 | "metadata": {}, 1168 | "source": [ 1169 | "#### 81. Consider an array Z = [1,2,3,4,5,6,7,8,9,10,11,12,13,14], how to generate an array R = [[1,2,3,4], [2,3,4,5], [3,4,5,6], ..., [11,12,13,14]]? (★★★)" 1170 | ] 1171 | }, 1172 | { 1173 | "cell_type": "code", 1174 | "execution_count": null, 1175 | "metadata": {}, 1176 | "outputs": [], 1177 | "source": [] 1178 | }, 1179 | { 1180 | "cell_type": "markdown", 1181 | "metadata": {}, 1182 | "source": [ 1183 | "#### 82. Compute a matrix rank (★★★)" 1184 | ] 1185 | }, 1186 | { 1187 | "cell_type": "code", 1188 | "execution_count": null, 1189 | "metadata": {}, 1190 | "outputs": [], 1191 | "source": [] 1192 | }, 1193 | { 1194 | "cell_type": "markdown", 1195 | "metadata": {}, 1196 | "source": [ 1197 | "#### 83. How to find the most frequent value in an array?" 1198 | ] 1199 | }, 1200 | { 1201 | "cell_type": "code", 1202 | "execution_count": null, 1203 | "metadata": {}, 1204 | "outputs": [], 1205 | "source": [] 1206 | }, 1207 | { 1208 | "cell_type": "markdown", 1209 | "metadata": {}, 1210 | "source": [ 1211 | "#### 84. Extract all the contiguous 3x3 blocks from a random 10x10 matrix (★★★)" 1212 | ] 1213 | }, 1214 | { 1215 | "cell_type": "code", 1216 | "execution_count": null, 1217 | "metadata": {}, 1218 | "outputs": [], 1219 | "source": [] 1220 | }, 1221 | { 1222 | "cell_type": "markdown", 1223 | "metadata": {}, 1224 | "source": [ 1225 | "#### 85. Create a 2D array subclass such that Z[i,j] == Z[j,i] (★★★)" 1226 | ] 1227 | }, 1228 | { 1229 | "cell_type": "code", 1230 | "execution_count": null, 1231 | "metadata": {}, 1232 | "outputs": [], 1233 | "source": [] 1234 | }, 1235 | { 1236 | "cell_type": "markdown", 1237 | "metadata": {}, 1238 | "source": [ 1239 | "#### 86. Consider a set of p matrices wich shape (n,n) and a set of p vectors with shape (n,1). How to compute the sum of of the p matrix products at once? (result has shape (n,1)) (★★★)" 1240 | ] 1241 | }, 1242 | { 1243 | "cell_type": "code", 1244 | "execution_count": null, 1245 | "metadata": {}, 1246 | "outputs": [], 1247 | "source": [] 1248 | }, 1249 | { 1250 | "cell_type": "markdown", 1251 | "metadata": {}, 1252 | "source": [ 1253 | "#### 87. Consider a 16x16 array, how to get the block-sum (block size is 4x4)? (★★★)" 1254 | ] 1255 | }, 1256 | { 1257 | "cell_type": "code", 1258 | "execution_count": null, 1259 | "metadata": {}, 1260 | "outputs": [], 1261 | "source": [] 1262 | }, 1263 | { 1264 | "cell_type": "markdown", 1265 | "metadata": {}, 1266 | "source": [ 1267 | "#### 88. How to implement the Game of Life using numpy arrays? (★★★)" 1268 | ] 1269 | }, 1270 | { 1271 | "cell_type": "code", 1272 | "execution_count": null, 1273 | "metadata": {}, 1274 | "outputs": [], 1275 | "source": [] 1276 | }, 1277 | { 1278 | "cell_type": "markdown", 1279 | "metadata": {}, 1280 | "source": [ 1281 | "#### 89. How to get the n largest values of an array (★★★)" 1282 | ] 1283 | }, 1284 | { 1285 | "cell_type": "code", 1286 | "execution_count": null, 1287 | "metadata": {}, 1288 | "outputs": [], 1289 | "source": [] 1290 | }, 1291 | { 1292 | "cell_type": "markdown", 1293 | "metadata": {}, 1294 | "source": [ 1295 | "#### 90. Given an arbitrary number of vectors, build the cartesian product (every combinations of every item) (★★★)" 1296 | ] 1297 | }, 1298 | { 1299 | "cell_type": "code", 1300 | "execution_count": null, 1301 | "metadata": {}, 1302 | "outputs": [], 1303 | "source": [] 1304 | }, 1305 | { 1306 | "cell_type": "markdown", 1307 | "metadata": {}, 1308 | "source": [ 1309 | "#### 91. How to create a record array from a regular array? (★★★)" 1310 | ] 1311 | }, 1312 | { 1313 | "cell_type": "code", 1314 | "execution_count": null, 1315 | "metadata": {}, 1316 | "outputs": [], 1317 | "source": [] 1318 | }, 1319 | { 1320 | "cell_type": "markdown", 1321 | "metadata": {}, 1322 | "source": [ 1323 | "#### 92. Consider a large vector Z, compute Z to the power of 3 using 3 different methods (★★★)" 1324 | ] 1325 | }, 1326 | { 1327 | "cell_type": "code", 1328 | "execution_count": null, 1329 | "metadata": {}, 1330 | "outputs": [], 1331 | "source": [] 1332 | }, 1333 | { 1334 | "cell_type": "markdown", 1335 | "metadata": {}, 1336 | "source": [ 1337 | "#### 93. Consider two arrays A and B of shape (8,3) and (2,2). How to find rows of A that contain elements of each row of B regardless of the order of the elements in B? (★★★)" 1338 | ] 1339 | }, 1340 | { 1341 | "cell_type": "code", 1342 | "execution_count": null, 1343 | "metadata": {}, 1344 | "outputs": [], 1345 | "source": [] 1346 | }, 1347 | { 1348 | "cell_type": "markdown", 1349 | "metadata": {}, 1350 | "source": [ 1351 | "#### 94. Considering a 10x3 matrix, extract rows with unequal values (e.g. [2,2,3]) (★★★)" 1352 | ] 1353 | }, 1354 | { 1355 | "cell_type": "code", 1356 | "execution_count": null, 1357 | "metadata": {}, 1358 | "outputs": [], 1359 | "source": [] 1360 | }, 1361 | { 1362 | "cell_type": "markdown", 1363 | "metadata": {}, 1364 | "source": [ 1365 | "#### 95. Convert a vector of ints into a matrix binary representation (★★★)" 1366 | ] 1367 | }, 1368 | { 1369 | "cell_type": "code", 1370 | "execution_count": null, 1371 | "metadata": {}, 1372 | "outputs": [], 1373 | "source": [] 1374 | }, 1375 | { 1376 | "cell_type": "markdown", 1377 | "metadata": {}, 1378 | "source": [ 1379 | "#### 96. Given a two dimensional array, how to extract unique rows? (★★★)" 1380 | ] 1381 | }, 1382 | { 1383 | "cell_type": "code", 1384 | "execution_count": null, 1385 | "metadata": {}, 1386 | "outputs": [], 1387 | "source": [] 1388 | }, 1389 | { 1390 | "cell_type": "markdown", 1391 | "metadata": {}, 1392 | "source": [ 1393 | "#### 97. Considering 2 vectors A & B, write the einsum equivalent of inner, outer, sum, and mul function (★★★)" 1394 | ] 1395 | }, 1396 | { 1397 | "cell_type": "code", 1398 | "execution_count": null, 1399 | "metadata": {}, 1400 | "outputs": [], 1401 | "source": [] 1402 | }, 1403 | { 1404 | "cell_type": "markdown", 1405 | "metadata": {}, 1406 | "source": [ 1407 | "#### 98. Considering a path described by two vectors (X,Y), how to sample it using equidistant samples (★★★)?" 1408 | ] 1409 | }, 1410 | { 1411 | "cell_type": "code", 1412 | "execution_count": null, 1413 | "metadata": {}, 1414 | "outputs": [], 1415 | "source": [] 1416 | }, 1417 | { 1418 | "cell_type": "markdown", 1419 | "metadata": {}, 1420 | "source": [ 1421 | "#### 99. Given an integer n and a 2D array X, select from X the rows which can be interpreted as draws from a multinomial distribution with n degrees, i.e., the rows which only contain integers and which sum to n. (★★★)" 1422 | ] 1423 | }, 1424 | { 1425 | "cell_type": "code", 1426 | "execution_count": null, 1427 | "metadata": {}, 1428 | "outputs": [], 1429 | "source": [] 1430 | }, 1431 | { 1432 | "cell_type": "markdown", 1433 | "metadata": {}, 1434 | "source": [ 1435 | "#### 100. Compute bootstrapped 95% confidence intervals for the mean of a 1D array X (i.e., resample the elements of an array with replacement N times, compute the mean of each sample, and then compute percentiles over the means). (★★★)" 1436 | ] 1437 | }, 1438 | { 1439 | "cell_type": "code", 1440 | "execution_count": null, 1441 | "metadata": {}, 1442 | "outputs": [], 1443 | "source": [] 1444 | } 1445 | ], 1446 | "metadata": { 1447 | "kernelspec": { 1448 | "display_name": "Python 3", 1449 | "language": "python", 1450 | "name": "python3" 1451 | }, 1452 | "language_info": { 1453 | "codemirror_mode": { 1454 | "name": "ipython", 1455 | "version": 3 1456 | }, 1457 | "file_extension": ".py", 1458 | "mimetype": "text/x-python", 1459 | "name": "python", 1460 | "nbconvert_exporter": "python", 1461 | "pygments_lexer": "ipython3", 1462 | "version": "3.7.6" 1463 | } 1464 | }, 1465 | "nbformat": 4, 1466 | "nbformat_minor": 5 1467 | } 1468 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Numpy_Exercise 2 | NumPy is a computational library that helps in speeding up Vector Algebra operations that involve Vectors (Distance between points, Cosine Similarity) and Matrices. Specifically, it helps in constructing powerful n-dimensional arrays that works smoothly with distributed and GPU systems. It is a very handy library and extensively used in the domains of Data Analytics and Machine Learning. Other than Python, it can also be used in tandem with languages like C and Fortran. Being an Open Source Library under a liberal BSD license, it is developed and maintained publicly on GitHub. 3 | --------------------------------------------------------------------------------