├── .idea ├── .gitignore ├── PDS-CSVTU-Sem2.iml ├── inspectionProfiles │ └── profiles_settings.xml ├── misc.xml ├── modules.xml └── vcs.xml ├── .vscode └── settings.json ├── Basics_Of_Tensorflow.ipynb ├── Computer_Vision_TensorFlow ├── .ipynb_checkpoints │ └── 01_Basics_Of_Tensorflow-checkpoint.ipynb └── 01_Basics_Of_Tensorflow.ipynb ├── Concept_Implementations ├── Ascii_Unicode.py ├── Operators.py ├── Taking_Input.py ├── capitalize_name.py ├── data_types.py ├── end_sep.py ├── f_string.py ├── f_strings.py ├── hello.py ├── hello2.py ├── ok.py ├── okay.txt ├── python_Learnings.ipynb ├── removing_whitespaces.py ├── test.py ├── typecasting.py └── variables_1.py ├── Keras_Basics ├── .ipynb_checkpoints │ └── Untitled-checkpoint.ipynb ├── 01_Basics.py └── Untitled.ipynb ├── LICENSE ├── Neural_Network_from_scratch.ipynb ├── Pandas └── 01_Pandas_basics.ipynb ├── Practice_Problems ├── Celcius_To_Faren.py └── Tut_py_cdh.ipynb ├── README.md ├── basic_problem_practices ├── file.txt └── problem_practices.ipynb └── numpy ├── 01_numpy_basics.ipynb ├── 02_numpy_advanced.ipynb └── 03_Numpy_Tricks.ipynb /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | # Editor-based HTTP Client requests 5 | /httpRequests/ 6 | # Datasource local storage ignored files 7 | /dataSources/ 8 | /dataSources.local.xml 9 | -------------------------------------------------------------------------------- /.idea/PDS-CSVTU-Sem2.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "python.testing.unittestArgs": [ 3 | "-v", 4 | "-s", 5 | ".", 6 | "-p", 7 | "*test.py" 8 | ], 9 | "python.testing.pytestEnabled": false, 10 | "python.testing.unittestEnabled": true 11 | } -------------------------------------------------------------------------------- /Basics_Of_Tensorflow.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text", 7 | "id": "view-in-github" 8 | }, 9 | "source": [ 10 | "\"Open" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 1, 16 | "id": "initial_id", 17 | "metadata": { 18 | "ExecuteTime": { 19 | "end_time": "2024-06-09T14:10:46.770853Z", 20 | "start_time": "2024-06-09T14:10:46.648525Z" 21 | }, 22 | "collapsed": true, 23 | "id": "initial_id" 24 | }, 25 | "outputs": [], 26 | "source": [ 27 | "import tensorflow as tf" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 2, 33 | "id": "1a56e97d3f7b7d11", 34 | "metadata": { 35 | "colab": { 36 | "base_uri": "https://localhost:8080/" 37 | }, 38 | "id": "1a56e97d3f7b7d11", 39 | "outputId": "79f59a24-3ce6-4647-a48d-66e7782d289d" 40 | }, 41 | "outputs": [ 42 | { 43 | "name": "stdout", 44 | "output_type": "stream", 45 | "text": [ 46 | "tf.Tensor(8, shape=(), dtype=int32)\n" 47 | ] 48 | } 49 | ], 50 | "source": [ 51 | "tensor_zero_d = tf.constant(8)\n", 52 | "print(tensor_zero_d)" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 3, 58 | "id": "qZ0x0rYqYO2J", 59 | "metadata": { 60 | "colab": { 61 | "base_uri": "https://localhost:8080/" 62 | }, 63 | "id": "qZ0x0rYqYO2J", 64 | "outputId": "72ee9286-aca9-4469-d8f3-cc16a24a62d7" 65 | }, 66 | "outputs": [ 67 | { 68 | "name": "stdout", 69 | "output_type": "stream", 70 | "text": [ 71 | "tf.Tensor([ 2 0 -3], shape=(3,), dtype=int32)\n" 72 | ] 73 | } 74 | ], 75 | "source": [ 76 | "tensor_one_d = tf.constant([2,0,-3])\n", 77 | "print(tensor_one_d)" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 4, 83 | "id": "qydCUy6GYs1w", 84 | "metadata": { 85 | "colab": { 86 | "base_uri": "https://localhost:8080/" 87 | }, 88 | "id": "qydCUy6GYs1w", 89 | "outputId": "de1a0aac-8f9f-4b36-f461-009ed584b6fe" 90 | }, 91 | "outputs": [ 92 | { 93 | "name": "stdout", 94 | "output_type": "stream", 95 | "text": [ 96 | "tf.Tensor(\n", 97 | "[[1 2 3]\n", 98 | " [4 5 6]\n", 99 | " [7 8 9]], shape=(3, 3), dtype=int32)\n" 100 | ] 101 | } 102 | ], 103 | "source": [ 104 | "tensor_two_d = tf.constant([\n", 105 | " [1,2,3],\n", 106 | " [4,5,6],\n", 107 | " [7,8,9]\n", 108 | "])\n", 109 | "print(tensor_two_d)" 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": 5, 115 | "id": "gXv7d0EgZHki", 116 | "metadata": { 117 | "id": "gXv7d0EgZHki" 118 | }, 119 | "outputs": [], 120 | "source": [ 121 | "tensor_three_d = tf.constant([\n", 122 | "\n", 123 | " [[1,2,3],\n", 124 | " [3,5,-1]],\n", 125 | "\n", 126 | " [[10,3,4],\n", 127 | " [1,5,6]],\n", 128 | "\n", 129 | " [[6,5,3],\n", 130 | " [45,76,9]],\n", 131 | "\n", 132 | " [[2,5,3],\n", 133 | " [5,4,5]]\n", 134 | "])" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": 6, 140 | "id": "nfAEckN5OMIo", 141 | "metadata": { 142 | "id": "nfAEckN5OMIo" 143 | }, 144 | "outputs": [], 145 | "source": [ 146 | "tensor_four_d = tf.constant([\n", 147 | " [\n", 148 | "\n", 149 | " [[1,2,3],\n", 150 | " [3,5,-1]],\n", 151 | "\n", 152 | " [[10,3,4],\n", 153 | " [1,5,6]],\n", 154 | "\n", 155 | " [[6,5,3],\n", 156 | " [45,76,9]],\n", 157 | "\n", 158 | " [[2,5,3],\n", 159 | " [5,4,5]]\n", 160 | "],\n", 161 | " [\n", 162 | "\n", 163 | " [[1,2,3],\n", 164 | " [3,5,-1]],\n", 165 | "\n", 166 | " [[10,3,4],\n", 167 | " [1,5,6]],\n", 168 | "\n", 169 | " [[6,5,3],\n", 170 | " [45,76,9]],\n", 171 | "\n", 172 | " [[2,5,3],\n", 173 | " [5,4,5]]\n", 174 | "],\n", 175 | " [\n", 176 | "\n", 177 | " [[1,2,3],\n", 178 | " [3,5,-1]],\n", 179 | "\n", 180 | " [[10,3,4],\n", 181 | " [1,5,6]],\n", 182 | "\n", 183 | " [[6,5,3],\n", 184 | " [45,76,9]],\n", 185 | "\n", 186 | " [[2,5,3],\n", 187 | " [5,4,5]]\n", 188 | "]\n", 189 | "])" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": 7, 195 | "id": "46cc0f50", 196 | "metadata": {}, 197 | "outputs": [], 198 | "source": [ 199 | "import numpy as np\n" 200 | ] 201 | }, 202 | { 203 | "cell_type": "code", 204 | "execution_count": 8, 205 | "id": "d5711469", 206 | "metadata": {}, 207 | "outputs": [], 208 | "source": [ 209 | "arr = np.array([1,2,3,4,5,6,7,8,9])\n", 210 | "coverted_tensor = tf.convert_to_tensor(arr)" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": 9, 216 | "id": "5d321f95", 217 | "metadata": {}, 218 | "outputs": [ 219 | { 220 | "name": "stdout", 221 | "output_type": "stream", 222 | "text": [ 223 | "tf.Tensor([1 2 3 4 5 6 7 8 9], shape=(9,), dtype=int32)\n" 224 | ] 225 | } 226 | ], 227 | "source": [ 228 | "print(coverted_tensor)" 229 | ] 230 | }, 231 | { 232 | "cell_type": "code", 233 | "execution_count": 10, 234 | "id": "7bf34052", 235 | "metadata": {}, 236 | "outputs": [], 237 | "source": [ 238 | "eye_tensor = tf.eye(\n", 239 | " num_rows=4,\n", 240 | " num_columns=None,\n", 241 | " batch_shape=None,\n", 242 | " dtype=tf.dtypes.float32,\n", 243 | " name=None\n", 244 | ")" 245 | ] 246 | }, 247 | { 248 | "cell_type": "code", 249 | "execution_count": 11, 250 | "id": "4544f2ef", 251 | "metadata": {}, 252 | "outputs": [ 253 | { 254 | "name": "stdout", 255 | "output_type": "stream", 256 | "text": [ 257 | "tf.Tensor(\n", 258 | "[[1. 0. 0. 0.]\n", 259 | " [0. 1. 0. 0.]\n", 260 | " [0. 0. 1. 0.]\n", 261 | " [0. 0. 0. 1.]], shape=(4, 4), dtype=float32)\n" 262 | ] 263 | } 264 | ], 265 | "source": [ 266 | "print(eye_tensor)" 267 | ] 268 | }, 269 | { 270 | "cell_type": "code", 271 | "execution_count": 12, 272 | "id": "abb07c3c", 273 | "metadata": {}, 274 | "outputs": [ 275 | { 276 | "name": "stdout", 277 | "output_type": "stream", 278 | "text": [ 279 | "tf.Tensor(\n", 280 | "[[1. 0. 0. 0. 0. 0.]\n", 281 | " [0. 1. 0. 0. 0. 0.]\n", 282 | " [0. 0. 1. 0. 0. 0.]\n", 283 | " [0. 0. 0. 1. 0. 0.]], shape=(4, 6), dtype=float32)\n" 284 | ] 285 | } 286 | ], 287 | "source": [ 288 | "eye_tensor1 = tf.eye(\n", 289 | " num_rows=4,\n", 290 | " num_columns=6,\n", 291 | " batch_shape=None,\n", 292 | " dtype=tf.dtypes.float32,\n", 293 | " name=None\n", 294 | ")\n", 295 | "print(eye_tensor1)" 296 | ] 297 | }, 298 | { 299 | "cell_type": "code", 300 | "execution_count": 13, 301 | "id": "0103e9d5", 302 | "metadata": {}, 303 | "outputs": [ 304 | { 305 | "name": "stdout", 306 | "output_type": "stream", 307 | "text": [ 308 | "tf.Tensor(\n", 309 | "[[[1. 0. 0. 0.]\n", 310 | " [0. 1. 0. 0.]\n", 311 | " [0. 0. 1. 0.]\n", 312 | " [0. 0. 0. 1.]]\n", 313 | "\n", 314 | " [[1. 0. 0. 0.]\n", 315 | " [0. 1. 0. 0.]\n", 316 | " [0. 0. 1. 0.]\n", 317 | " [0. 0. 0. 1.]]], shape=(2, 4, 4), dtype=float32)\n" 318 | ] 319 | } 320 | ], 321 | "source": [ 322 | "eye_tensor2 = tf.eye(\n", 323 | " num_rows=4,\n", 324 | " num_columns=None,\n", 325 | " batch_shape=[2],\n", 326 | " dtype=tf.dtypes.float32,\n", 327 | " name=None\n", 328 | ")\n", 329 | "print(eye_tensor2)" 330 | ] 331 | }, 332 | { 333 | "cell_type": "code", 334 | "execution_count": 14, 335 | "id": "39d7dac2", 336 | "metadata": {}, 337 | "outputs": [], 338 | "source": [ 339 | "filled_tensor = tf.fill([2,3], 8)" 340 | ] 341 | }, 342 | { 343 | "cell_type": "code", 344 | "execution_count": 15, 345 | "id": "4cc9ec02", 346 | "metadata": {}, 347 | "outputs": [ 348 | { 349 | "name": "stdout", 350 | "output_type": "stream", 351 | "text": [ 352 | "tf.Tensor(\n", 353 | "[[8 8 8]\n", 354 | " [8 8 8]], shape=(2, 3), dtype=int32)\n" 355 | ] 356 | } 357 | ], 358 | "source": [ 359 | "print(filled_tensor)" 360 | ] 361 | }, 362 | { 363 | "cell_type": "code", 364 | "execution_count": 16, 365 | "id": "793f5e60", 366 | "metadata": {}, 367 | "outputs": [ 368 | { 369 | "data": { 370 | "text/plain": [ 371 | "" 372 | ] 373 | }, 374 | "execution_count": 16, 375 | "metadata": {}, 376 | "output_type": "execute_result" 377 | } 378 | ], 379 | "source": [ 380 | "tf.shape(tensor_four_d)" 381 | ] 382 | }, 383 | { 384 | "cell_type": "code", 385 | "execution_count": 17, 386 | "id": "6e6ea28b", 387 | "metadata": {}, 388 | "outputs": [ 389 | { 390 | "data": { 391 | "text/plain": [ 392 | "" 393 | ] 394 | }, 395 | "execution_count": 17, 396 | "metadata": {}, 397 | "output_type": "execute_result" 398 | } 399 | ], 400 | "source": [ 401 | "tf.size(tensor_four_d)" 402 | ] 403 | }, 404 | { 405 | "cell_type": "code", 406 | "execution_count": 20, 407 | "id": "13451544", 408 | "metadata": {}, 409 | "outputs": [], 410 | "source": [ 411 | "# random tensors\n", 412 | "\n", 413 | "randome_tensor = tf.random.normal(\n", 414 | " [3,2,2],\n", 415 | " mean =0.0,\n", 416 | " stddev=1.0,\n", 417 | " dtype=tf.dtypes.float32,\n", 418 | " seed=None,\n", 419 | " name=None\n", 420 | ")" 421 | ] 422 | }, 423 | { 424 | "cell_type": "code", 425 | "execution_count": 21, 426 | "id": "396737c0", 427 | "metadata": {}, 428 | "outputs": [ 429 | { 430 | "name": "stdout", 431 | "output_type": "stream", 432 | "text": [ 433 | "tf.Tensor(\n", 434 | "[[[-0.94215214 -0.68370026]\n", 435 | " [ 0.15898167 -0.9476472 ]]\n", 436 | "\n", 437 | " [[ 0.5433289 -0.74997306]\n", 438 | " [ 0.7603781 0.90746987]]\n", 439 | "\n", 440 | " [[ 0.47381338 -0.22523715]\n", 441 | " [-0.25446403 0.23084885]]], shape=(3, 2, 2), dtype=float32)\n" 442 | ] 443 | } 444 | ], 445 | "source": [ 446 | "print(randome_tensor)" 447 | ] 448 | }, 449 | { 450 | "cell_type": "code", 451 | "execution_count": 22, 452 | "id": "0acbe1b7", 453 | "metadata": {}, 454 | "outputs": [], 455 | "source": [ 456 | "#uniform tensor\n", 457 | "uniform_tensor = tf.random.uniform(\n", 458 | " [2,4,5],\n", 459 | " minval = 0,\n", 460 | " maxval = None,\n", 461 | " dtype = tf.dtypes.float32,\n", 462 | " seed = None,\n", 463 | " name = None\n", 464 | ")" 465 | ] 466 | }, 467 | { 468 | "cell_type": "code", 469 | "execution_count": 23, 470 | "id": "d891b113", 471 | "metadata": {}, 472 | "outputs": [ 473 | { 474 | "name": "stdout", 475 | "output_type": "stream", 476 | "text": [ 477 | "tf.Tensor(\n", 478 | "[[[0.5950475 0.664094 0.8105799 0.7654351 0.0633949 ]\n", 479 | " [0.05833912 0.13866317 0.17369008 0.06138539 0.25493038]\n", 480 | " [0.5600575 0.15159655 0.27926755 0.9447837 0.486354 ]\n", 481 | " [0.10908127 0.7079065 0.0742588 0.9124538 0.348966 ]]\n", 482 | "\n", 483 | " [[0.07500505 0.50155103 0.8346634 0.42695498 0.9723712 ]\n", 484 | " [0.01290166 0.93380535 0.0856235 0.9937459 0.25540137]\n", 485 | " [0.6791761 0.38062 0.726624 0.80450785 0.3083142 ]\n", 486 | " [0.5216892 0.08864832 0.02486145 0.4966612 0.9902508 ]]], shape=(2, 4, 5), dtype=float32)\n" 487 | ] 488 | } 489 | ], 490 | "source": [ 491 | "print(uniform_tensor)" 492 | ] 493 | }, 494 | { 495 | "cell_type": "markdown", 496 | "id": "44da8a8c", 497 | "metadata": {}, 498 | "source": [ 499 | "## Tensor Indexing" 500 | ] 501 | }, 502 | { 503 | "cell_type": "code", 504 | "execution_count": 25, 505 | "id": "85ef8057", 506 | "metadata": {}, 507 | "outputs": [ 508 | { 509 | "name": "stdout", 510 | "output_type": "stream", 511 | "text": [ 512 | "tf.Tensor([1 2 3 4 5 6 7 8 9], shape=(9,), dtype=int32)\n", 513 | "tf.Tensor(1, shape=(), dtype=int32)\n", 514 | "tf.Tensor(2, shape=(), dtype=int32)\n", 515 | "tf.Tensor(3, shape=(), dtype=int32)\n", 516 | "tf.Tensor(4, shape=(), dtype=int32)\n", 517 | "tf.Tensor(5, shape=(), dtype=int32)\n", 518 | "tf.Tensor(6, shape=(), dtype=int32)\n", 519 | "tf.Tensor(7, shape=(), dtype=int32)\n", 520 | "tf.Tensor(8, shape=(), dtype=int32)\n", 521 | "tf.Tensor(9, shape=(), dtype=int32)\n" 522 | ] 523 | } 524 | ], 525 | "source": [ 526 | "tensor_indexed = tf.constant([1,2,3,4,5,6,7,8,9])\n", 527 | "print(tensor_indexed)\n", 528 | "print(tensor_indexed[0])\n", 529 | "print(tensor_indexed[1])\n", 530 | "print(tensor_indexed[2])\n", 531 | "print(tensor_indexed[3])\n", 532 | "print(tensor_indexed[4])\n", 533 | "print(tensor_indexed[5])\n", 534 | "print(tensor_indexed[6])\n", 535 | "print(tensor_indexed[7])\n", 536 | "print(tensor_indexed[8])" 537 | ] 538 | }, 539 | { 540 | "cell_type": "code", 541 | "execution_count": 26, 542 | "id": "f8c1596f", 543 | "metadata": {}, 544 | "outputs": [ 545 | { 546 | "name": "stdout", 547 | "output_type": "stream", 548 | "text": [ 549 | "tf.Tensor([1 2 3], shape=(3,), dtype=int32)\n" 550 | ] 551 | } 552 | ], 553 | "source": [ 554 | "print(tensor_indexed[0:3])" 555 | ] 556 | }, 557 | { 558 | "cell_type": "code", 559 | "execution_count": 27, 560 | "id": "50779865", 561 | "metadata": {}, 562 | "outputs": [ 563 | { 564 | "name": "stdout", 565 | "output_type": "stream", 566 | "text": [ 567 | "tf.Tensor([1 3 5 7], shape=(4,), dtype=int32)\n" 568 | ] 569 | } 570 | ], 571 | "source": [ 572 | "print(tensor_indexed[0:8:2])" 573 | ] 574 | }, 575 | { 576 | "cell_type": "code", 577 | "execution_count": null, 578 | "id": "7ecf0fd0", 579 | "metadata": {}, 580 | "outputs": [], 581 | "source": [ 582 | "#timestamp = 1:22:00" 583 | ] 584 | } 585 | ], 586 | "metadata": { 587 | "colab": { 588 | "include_colab_link": true, 589 | "provenance": [] 590 | }, 591 | "kernelspec": { 592 | "display_name": "Python 3", 593 | "language": "python", 594 | "name": "python3" 595 | }, 596 | "language_info": { 597 | "codemirror_mode": { 598 | "name": "ipython", 599 | "version": 3 600 | }, 601 | "file_extension": ".py", 602 | "mimetype": "text/x-python", 603 | "name": "python", 604 | "nbconvert_exporter": "python", 605 | "pygments_lexer": "ipython3", 606 | "version": "3.12.4" 607 | } 608 | }, 609 | "nbformat": 4, 610 | "nbformat_minor": 5 611 | } 612 | -------------------------------------------------------------------------------- /Computer_Vision_TensorFlow/.ipynb_checkpoints/01_Basics_Of_Tensorflow-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "id": "initial_id", 7 | "metadata": { 8 | "ExecuteTime": { 9 | "end_time": "2024-06-11T14:39:05.184563Z", 10 | "start_time": "2024-06-11T14:39:04.499328Z" 11 | } 12 | }, 13 | "outputs": [], 14 | "source": [ 15 | "import tensorflow as tf" 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": null, 21 | "id": "1a56e97d3f7b7d11", 22 | "metadata": {}, 23 | "outputs": [], 24 | "source": [] 25 | } 26 | ], 27 | "metadata": { 28 | "kernelspec": { 29 | "display_name": "Python 3 (ipykernel)", 30 | "language": "python", 31 | "name": "python3" 32 | }, 33 | "language_info": { 34 | "codemirror_mode": { 35 | "name": "ipython", 36 | "version": 3 37 | }, 38 | "file_extension": ".py", 39 | "mimetype": "text/x-python", 40 | "name": "python", 41 | "nbconvert_exporter": "python", 42 | "pygments_lexer": "ipython3", 43 | "version": "3.11.5" 44 | } 45 | }, 46 | "nbformat": 4, 47 | "nbformat_minor": 5 48 | } 49 | -------------------------------------------------------------------------------- /Computer_Vision_TensorFlow/01_Basics_Of_Tensorflow.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "id": "initial_id", 7 | "metadata": { 8 | "ExecuteTime": { 9 | "end_time": "2024-06-11T14:39:05.184563Z", 10 | "start_time": "2024-06-11T14:39:04.499328Z" 11 | } 12 | }, 13 | "outputs": [], 14 | "source": [ 15 | "import tensorflow as tf" 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": null, 21 | "id": "1a56e97d3f7b7d11", 22 | "metadata": {}, 23 | "outputs": [], 24 | "source": [] 25 | } 26 | ], 27 | "metadata": { 28 | "kernelspec": { 29 | "display_name": "Python 3 (ipykernel)", 30 | "language": "python", 31 | "name": "python3" 32 | }, 33 | "language_info": { 34 | "codemirror_mode": { 35 | "name": "ipython", 36 | "version": 3 37 | }, 38 | "file_extension": ".py", 39 | "mimetype": "text/x-python", 40 | "name": "python", 41 | "nbconvert_exporter": "python", 42 | "pygments_lexer": "ipython3", 43 | "version": "3.11.5" 44 | } 45 | }, 46 | "nbformat": 4, 47 | "nbformat_minor": 5 48 | } 49 | -------------------------------------------------------------------------------- /Concept_Implementations/Ascii_Unicode.py: -------------------------------------------------------------------------------- 1 | #Ascii and Unicode values of a character 2 | # Ascii values of a character 3 | print(ord('a')) 4 | print(ord('A')) 5 | # Unicode values of a character 6 | print(ord('€')) 7 | print(ord('¥')) 8 | # Character of an Ascii value 9 | print(chr(97)) 10 | print(chr(65)) 11 | # Character of a Unicode value 12 | print(chr(8364)) 13 | print(chr(165)) 14 | # Output: 15 | # 97 16 | # 65 17 | # 8364 18 | # 165 19 | # a 20 | # A 21 | # € 22 | # ¥ 23 | -------------------------------------------------------------------------------- /Concept_Implementations/Operators.py: -------------------------------------------------------------------------------- 1 | #Operators in Python :- 2 | 3 | #Arithmetic Operators :- 4 | #Addition :- 5 | a = 10 6 | b = 20 7 | c = a + b 8 | print("Addition of a and b is : ", c) 9 | #Subtraction :- 10 | c = a - b 11 | print("Subtraction of a and b is : ", c) 12 | #Multiplication :- 13 | c = a * b 14 | print("Multiplication of a and b is : ", c) 15 | #Division :- 16 | c = a / b 17 | print("Division of a and b is : ", c) 18 | #Modulus :- 19 | c = a % b 20 | print("Modulus of a and b is : ", c) 21 | #Floor Division :- 22 | c = a // b 23 | print("Floor Division of a and b is : ", c) 24 | #Exponentiation :- 25 | c = a ** b 26 | print("Exponentiation of a and b is : ", c) 27 | #Comparison Operators :- 28 | #Equal to :- 29 | a = 10 30 | b = 20 31 | if a == b: 32 | print("a is equal to b") 33 | else: 34 | print("a is not equal to b") 35 | #Not Equal to :- 36 | if a != b: 37 | print("a is not equal to b") 38 | else: 39 | print("a is equal to b") 40 | #Greater than :- 41 | if a > b: 42 | print("a is greater than b") 43 | else: 44 | print("a is not greater than b") 45 | #Less than :- 46 | if a < b: 47 | print("a is less than b") 48 | else: 49 | print("a is not less than b") 50 | #Greater than or equal to :- 51 | if a >= b: 52 | print("a is greater than or equal to b") 53 | else: 54 | print("a is not greater than or equal to b") 55 | #Less than or equal to :- 56 | if a <= b: 57 | print("a is less than or equal to b") 58 | else: 59 | print("a is not less than or equal to b") 60 | #Logical Operators :- 61 | #AND :- 62 | a = 10 63 | b = 20 64 | c = 30 65 | if a > b and a > c: 66 | print("a is the greatest") 67 | elif b > a and b > c: 68 | print("b is the greatest") 69 | else: 70 | print("c is the greatest") 71 | #OR :- 72 | if a > b or a > c: 73 | print("a is the greatest") 74 | elif b > a or b > c: 75 | print("b is the greatest") 76 | else: 77 | print("c is the greatest") 78 | #NOT :- 79 | if not a > b: 80 | print("a is not greater than b") 81 | else: 82 | print("a is greater than b") 83 | #Assignment Operators :- 84 | #Equal to :- 85 | a = 10 86 | b = 20 87 | c = a 88 | print("c is : ", c) 89 | #Addition :- 90 | c += a 91 | print("c is : ", c) 92 | #Subtraction :- 93 | c -= a 94 | print("c is : ", c) 95 | #Multiplication :- 96 | c *= a 97 | print("c is : ", c) 98 | #Division :- 99 | c /= a 100 | print("c is : ", c) 101 | #Modulus :- 102 | c %= a 103 | print("c is : ", c) 104 | #Floor Division :- 105 | c //= a 106 | print("c is : ", c) 107 | #Exponentiation :- 108 | c **= a 109 | print("c is : ", c) 110 | #Bitwise Operators :- 111 | #AND :- 112 | a = 10 113 | b = 20 114 | c = a & b 115 | print("c is : ", c) 116 | #OR :- 117 | c = a | b 118 | print("c is : ", c) 119 | #XOR :- 120 | c = a ^ b 121 | print("c is : ", c) 122 | #NOT :- 123 | c = ~a 124 | print("c is : ", c) 125 | #Left Shift :- 126 | c = a << 2 127 | print("c is : ", c) 128 | #Right Shift :- 129 | c = a >> 2 130 | print("c is : ", c) 131 | #Identity Operators :- 132 | #is :- 133 | a = 10 134 | b = 20 135 | if a is b: 136 | print("a and b are same") 137 | else: 138 | print("a and b are not same") 139 | #is not :- 140 | if a is not b: 141 | print("a and b are not same") 142 | else: 143 | print("a and b are same") 144 | #Membership Operators :- 145 | #in :- 146 | a = [10, 20, 30, 40, 50] 147 | if 10 in a: 148 | print("10 is in a") 149 | else: 150 | print("10 is not in a") 151 | #not in :- 152 | if 10 not in a: 153 | print("10 is not in a") 154 | else: 155 | print("10 is in a") 156 | #Ternary Operator :- 157 | a = 10 158 | b = 20 159 | c = a if a > b else b 160 | print("c is : ", c) 161 | #Operator Precedence :- 162 | #Precedence of Operators :- 163 | #1. Parentheses () 164 | #2. Exponentiation ** 165 | #3. Complement ~, Unary +, Unary - 166 | #4. Multiplication *, Division /, Floor Division //, Modulus % 167 | #5. Addition +, Subtraction - 168 | #6. Right to Left 169 | #7. Left Shift <<, Right Shift >> 170 | #8. Bitwise AND & 171 | #9. Bitwise XOR ^ 172 | #10. Bitwise OR | 173 | #11. Comparison Operators 174 | #12. Logical Operators 175 | #13. Assignment Operators 176 | #14. Identity Operators 177 | #15. Membership Operators 178 | #16. Ternary Operator 179 | #17. Comma , 180 | -------------------------------------------------------------------------------- /Concept_Implementations/Taking_Input.py: -------------------------------------------------------------------------------- 1 | #Taking input from user 2 | name = input("What's your name ? ") 3 | print("Hello ", name) 4 | #Taking integer input from user 5 | age = int(input("What's your age ? ")) 6 | print("Your age is ", age) 7 | #Taking float input from user 8 | height = float(input("What's your height ? ")) 9 | print("Your height is ", height) 10 | #Taking boolean input from user 11 | is_student = bool(input("Are you a student ? ")) 12 | print("Are you a student ? ", is_student) 13 | #Taking complex input from user 14 | complex_number = complex(input("Enter a complex number : ")) 15 | print("Complex number is ", complex_number) 16 | #Taking list input from user 17 | list = list(input("Enter a list : ")) 18 | print("List is ", list) 19 | #Taking tuple input from user 20 | tuple = tuple(input("Enter a tuple : ")) 21 | print("Tuple is ", tuple) 22 | #Taking set input from user 23 | set = set(input("Enter a set : ")) 24 | print("Set is ", set) 25 | #Taking dictionary input from user 26 | dict = dict(input("Enter a dictionary : ")) 27 | print("Dictionary is ", dict) 28 | #Taking string input from user 29 | string = str(input("Enter a string : ")) 30 | print("String is ", string) 31 | #Taking multiple inputs from user 32 | name, age = input("Enter your name and age : ").split() 33 | print("Your name is ", name) 34 | print("Your age is ", age) 35 | #Taking multiple integer inputs from user 36 | x, y = map(int, input("Enter two numbers : ").split()) 37 | print("Sum is ", x+y) 38 | #Taking multiple float inputs from user 39 | x, y = map(float, input("Enter two numbers : ").split()) 40 | print("Sum is ", x+y) 41 | #Taking multiple boolean inputs from user 42 | x, y = map(bool, input("Enter two boolean values : ").split()) 43 | print("Sum is ", x+y) 44 | #Taking multiple complex inputs from user 45 | x, y = map(complex, input("Enter two complex numbers : ").split()) 46 | print("Sum is ", x+y) 47 | #Taking multiple list inputs from user 48 | x, y = map(list, input("Enter two lists : ").split()) 49 | print("Sum is ", x+y) 50 | #Taking multiple tuple inputs from user 51 | x, y = map(tuple, input("Enter two tuples : ").split()) 52 | print("Sum is ", x+y) 53 | #Taking multiple set inputs from user 54 | x, y = map(set, input("Enter two sets : ").split()) 55 | print("Sum is ", x+y) 56 | #Taking multiple dictionary inputs from user 57 | x, y = map(dict, input("Enter two dictionaries : ").split()) 58 | print("Sum is ", x+y) 59 | #Taking multiple string inputs from user 60 | x, y = map(str, input("Enter two strings : ").split()) 61 | print("Sum is ", x+y) -------------------------------------------------------------------------------- /Concept_Implementations/capitalize_name.py: -------------------------------------------------------------------------------- 1 | # ASk user input 2 | name = input("What is your name deer ?") 3 | 4 | # Capitalize the name 5 | 6 | name = name.capitalize() 7 | 8 | # capitalize() capitalizes the first letter of the string 9 | 10 | 11 | print(f"Hello {name}!") 12 | -------------------------------------------------------------------------------- /Concept_Implementations/data_types.py: -------------------------------------------------------------------------------- 1 | #Numeric Data Types 2 | #int 3 | int = 100 4 | print("This is ",type(int)) 5 | #float 6 | float = 100.0 7 | print("This is ",type(float)) 8 | #complex 9 | complex = 1+2j 10 | print("This is ",type(complex)) 11 | #Sequence Data Types 12 | #String 13 | string = "Hello" 14 | print("This is ",type(string)) 15 | #List 16 | list = [1,2,3,4,5] 17 | print("This is ",type(list)) 18 | #Tuple 19 | tuple = (1,2,3,4,5) 20 | print("This is ",type(tuple)) 21 | #Set 22 | set = {1,2,3,4,5} 23 | print("This is ",type(set)) 24 | #Dictionary 25 | dict = {"name":"John", "age":25} 26 | print("This is ",type(dict)) 27 | #Boolean Data Type 28 | boolean = True 29 | print("This is ",type(boolean)) 30 | 31 | -------------------------------------------------------------------------------- /Concept_Implementations/end_sep.py: -------------------------------------------------------------------------------- 1 | #printing with separator 2 | print("Hello", "World", sep="***") 3 | # printing with end 4 | print("Hello", end=" ") 5 | print("World") 6 | # printing with end and separator 7 | print("Hello", "World", sep="***", end="!!!") 8 | -------------------------------------------------------------------------------- /Concept_Implementations/f_string.py: -------------------------------------------------------------------------------- 1 | # Ask user for there name and age and print it using f-string 2 | 3 | name = input("Enter your name: ") 4 | age = input("Enter your age: ") 5 | print(f"Hello {name}, you are {age} years old.") 6 | 7 | # Output: 8 | # Enter your name: John 9 | # Enter your age: 25 10 | # Hello John, you are 25 years old. 11 | -------------------------------------------------------------------------------- /Concept_Implementations/f_strings.py: -------------------------------------------------------------------------------- 1 | # Ask user for name and age and print a message with that information 2 | name = input("What is your name? ") 3 | age = input("What is your age? ") 4 | print(f"Hello {name}! You are {age} years old.") 5 | -------------------------------------------------------------------------------- /Concept_Implementations/hello.py: -------------------------------------------------------------------------------- 1 | print("Hello World") -------------------------------------------------------------------------------- /Concept_Implementations/hello2.py: -------------------------------------------------------------------------------- 1 | name = input("WHat's your name ? ") 2 | print("Hello ", name) -------------------------------------------------------------------------------- /Concept_Implementations/ok.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import argparse 4 | 5 | def resize_image(image, height): 6 | """Resize the image while maintaining aspect ratio.""" 7 | ratio = height / image.shape[0] 8 | return cv2.resize(image, (int(ratio * image.shape[1]), height)) 9 | 10 | def preprocess_image(image): 11 | """Preprocess the image for document detection.""" 12 | # Convert to grayscale 13 | gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 14 | 15 | # Bilateral Filter to reduce noise and preserve edges 16 | filtered = cv2.bilateralFilter(gray, 11, 50, 50) 17 | 18 | # Adaptive Thresholding to create binary image 19 | binary = cv2.adaptiveThreshold(filtered, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 115, 4) 20 | 21 | # Median Blur to further remove noise 22 | blurred = cv2.medianBlur(binary, 11) 23 | 24 | # Add a black border to handle edge cases 25 | bordered = cv2.copyMakeBorder(blurred, 4, 4, 4, 4, cv2.BORDER_CONSTANT, value=[0, 0, 0]) 26 | 27 | return bordered 28 | 29 | def detect_document(image): 30 | """Detect document corners in the preprocessed image.""" 31 | # Detect edges using Canny edge detector 32 | edges = cv2.Canny(image, 200, 250) 33 | 34 | # Find contours in the edge-detected image 35 | _, contours, _ = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) 36 | 37 | # Sort contours by area and select the largest 10 38 | contours = sorted(contours, key=cv2.contourArea, reverse=True)[:10] 39 | 40 | # Iterate through contours and approximate polygon 41 | for cnt in contours: 42 | perimeter = cv2.arcLength(cnt, True) 43 | approx = cv2.approxPolyDP(cnt, 0.03 * perimeter, True) 44 | 45 | # If the contour has 4 corners, it's likely the document 46 | if len(approx) == 4: 47 | return approx.reshape(4, 2) 48 | 49 | return None 50 | 51 | def transform_image(image, corners): 52 | """Transform the image to correct perspective.""" 53 | # Define target points for perspective transformation 54 | height = max(np.linalg.norm(corners[0] - corners[1]), np.linalg.norm(corners[2] - corners[3])) 55 | width = max(np.linalg.norm(corners[1] - corners[2]), np.linalg.norm(corners[3] - corners[0])) 56 | target = np.array([[0, 0], [0, height], [width, height], [width, 0]], np.float32) 57 | 58 | # Calculate perspective transformation matrix 59 | M = cv2.getPerspectiveTransform(corners, target) 60 | 61 | # Warp the image to correct perspective 62 | return cv2.warpPerspective(image, M, (int(width), int(height))) 63 | 64 | def main(): 65 | # Construct the argument parser and parse the arguments 66 | ap = argparse.ArgumentParser() 67 | ap.add_argument("-img", "--image", required=True, help="Path to the image") 68 | args = vars(ap.parse_args()) 69 | 70 | # Load the image 71 | image = cv2.imread(args['image']) 72 | 73 | # Resize the image 74 | image = resize_image(image, 800) 75 | 76 | # Preprocess the image 77 | preprocessed_image = preprocess_image(image.copy()) 78 | 79 | # Detect document corners 80 | corners = detect_document(preprocessed_image) 81 | if corners is None: 82 | print("No document found.") 83 | return 84 | 85 | # Transform the image 86 | transformed_image = transform_image(image, corners) 87 | 88 | # Display the original and transformed images 89 | cv2.imshow("Original Image", image) 90 | cv2.imshow("Transformed Image", transformed_image) 91 | cv2.waitKey(0) 92 | cv2.destroyAllWindows() 93 | 94 | if __name__ == "__main__": 95 | main() 96 | -------------------------------------------------------------------------------- /Concept_Implementations/okay.txt: -------------------------------------------------------------------------------- 1 | Hello, World!Hello, World! -------------------------------------------------------------------------------- /Concept_Implementations/python_Learnings.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 7, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "HEllo, World!\n" 13 | ] 14 | } 15 | ], 16 | "source": [ 17 | "print(\"HEllo, World!\")" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 8, 23 | "metadata": {}, 24 | "outputs": [], 25 | "source": [ 26 | "import numpy as np " 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 9, 32 | "metadata": {}, 33 | "outputs": [], 34 | "source": [ 35 | "import pandas as pd" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 10, 41 | "metadata": {}, 42 | "outputs": [ 43 | { 44 | "name": "stdout", 45 | "output_type": "stream", 46 | "text": [ 47 | "Requirement already satisfied: matplotlib in c:\\users\\abhi\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (3.9.1)\n", 48 | "Requirement already satisfied: contourpy>=1.0.1 in c:\\users\\abhi\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from matplotlib) (1.2.1)\n", 49 | "Requirement already satisfied: cycler>=0.10 in c:\\users\\abhi\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from matplotlib) (0.12.1)\n", 50 | "Requirement already satisfied: fonttools>=4.22.0 in c:\\users\\abhi\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from matplotlib) (4.53.1)\n", 51 | "Requirement already satisfied: kiwisolver>=1.3.1 in c:\\users\\abhi\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from matplotlib) (1.4.5)\n", 52 | "Requirement already satisfied: numpy>=1.23 in c:\\users\\abhi\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from matplotlib) (2.0.1)\n", 53 | "Requirement already satisfied: packaging>=20.0 in c:\\users\\abhi\\appdata\\roaming\\python\\python312\\site-packages (from matplotlib) (24.1)\n", 54 | "Requirement already satisfied: pillow>=8 in c:\\users\\abhi\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from matplotlib) (10.4.0)\n", 55 | "Requirement already satisfied: pyparsing>=2.3.1 in c:\\users\\abhi\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from matplotlib) (3.1.2)\n", 56 | "Requirement already satisfied: python-dateutil>=2.7 in c:\\users\\abhi\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from matplotlib) (2.9.0.post0)\n", 57 | "Requirement already satisfied: six>=1.5 in c:\\users\\abhi\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from python-dateutil>=2.7->matplotlib) (1.16.0)\n" 58 | ] 59 | }, 60 | { 61 | "name": "stderr", 62 | "output_type": "stream", 63 | "text": [ 64 | "\n", 65 | "[notice] A new release of pip is available: 24.0 -> 24.2\n", 66 | "[notice] To update, run: python.exe -m pip install --upgrade pip\n" 67 | ] 68 | } 69 | ], 70 | "source": [ 71 | "!pip install matplotlib" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": 11, 77 | "metadata": {}, 78 | "outputs": [], 79 | "source": [ 80 | "import matplotlib.pyplot as plt" 81 | ] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "metadata": {}, 86 | "source": [ 87 | "## Variables" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": 12, 93 | "metadata": {}, 94 | "outputs": [], 95 | "source": [ 96 | "a = 1\n", 97 | "b ='1'\n", 98 | "c = \"1\"\n", 99 | "d = 1.0" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 13, 105 | "metadata": {}, 106 | "outputs": [ 107 | { 108 | "name": "stdout", 109 | "output_type": "stream", 110 | "text": [ 111 | "\n", 112 | "\n", 113 | "\n", 114 | "\n" 115 | ] 116 | } 117 | ], 118 | "source": [ 119 | "print(type(a))\n", 120 | "print(type(b))\n", 121 | "print(type(c))\n", 122 | "print(type(d))" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 14, 128 | "metadata": {}, 129 | "outputs": [ 130 | { 131 | "name": "stdout", 132 | "output_type": "stream", 133 | "text": [ 134 | "1\n", 135 | "1\n", 136 | "1\n", 137 | "1.0\n" 138 | ] 139 | } 140 | ], 141 | "source": [ 142 | "print(a)\n", 143 | "print(b)\n", 144 | "print(c)\n", 145 | "print(d)" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 15, 151 | "metadata": {}, 152 | "outputs": [ 153 | { 154 | "name": "stdout", 155 | "output_type": "stream", 156 | "text": [ 157 | "Python\n", 158 | "P\n", 159 | "y\n", 160 | "t\n", 161 | "h\n", 162 | "o\n", 163 | "n\n" 164 | ] 165 | } 166 | ], 167 | "source": [ 168 | "x ='Python'\n", 169 | "print(x)\n", 170 | "print(x[0])\n", 171 | "print(x[1])\n", 172 | "print(x[2])\n", 173 | "print(x[3])\n", 174 | "print(x[4])\n", 175 | "print(x[5])\n" 176 | ] 177 | }, 178 | { 179 | "cell_type": "code", 180 | "execution_count": 16, 181 | "metadata": {}, 182 | "outputs": [], 183 | "source": [ 184 | "var_1, var_2, var_3 = 10, 20, 30" 185 | ] 186 | }, 187 | { 188 | "cell_type": "code", 189 | "execution_count": 17, 190 | "metadata": {}, 191 | "outputs": [], 192 | "source": [ 193 | "var_1 = var_2 = var_3 = 40" 194 | ] 195 | }, 196 | { 197 | "cell_type": "markdown", 198 | "metadata": {}, 199 | "source": [ 200 | "## Comments" 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": 18, 206 | "metadata": {}, 207 | "outputs": [], 208 | "source": [ 209 | "# This is example of single line comment" 210 | ] 211 | }, 212 | { 213 | "cell_type": "code", 214 | "execution_count": 19, 215 | "metadata": {}, 216 | "outputs": [ 217 | { 218 | "data": { 219 | "text/plain": [ 220 | "'This is example of multi line comment'" 221 | ] 222 | }, 223 | "execution_count": 19, 224 | "metadata": {}, 225 | "output_type": "execute_result" 226 | } 227 | ], 228 | "source": [ 229 | "'''This is example of multi line comment'''" 230 | ] 231 | }, 232 | { 233 | "cell_type": "markdown", 234 | "metadata": {}, 235 | "source": [ 236 | "## Datatype" 237 | ] 238 | }, 239 | { 240 | "cell_type": "code", 241 | "execution_count": 20, 242 | "metadata": {}, 243 | "outputs": [], 244 | "source": [ 245 | "d1 = 5\n", 246 | "d2 = 'abhinav'\n", 247 | "d3 = 5.0\n", 248 | "d4 = True\n", 249 | "d5 = 5+3j\n", 250 | "d6 = [1,2,3,4,5]\n", 251 | "d7 = (1,2,3,4,5)\n", 252 | "d8 = {1:'one', 2:'two', 3:'three'}\n", 253 | "d9 = {1,2,3,4,5}\n", 254 | "d10 = None" 255 | ] 256 | }, 257 | { 258 | "cell_type": "code", 259 | "execution_count": 21, 260 | "metadata": {}, 261 | "outputs": [ 262 | { 263 | "name": "stdout", 264 | "output_type": "stream", 265 | "text": [ 266 | "\n", 267 | "\n", 268 | "\n", 269 | "\n", 270 | "\n", 271 | "\n", 272 | "\n", 273 | "\n", 274 | "\n", 275 | "\n" 276 | ] 277 | } 278 | ], 279 | "source": [ 280 | "print(type(d1))\n", 281 | "print(type(d2))\n", 282 | "print(type(d3))\n", 283 | "print(type(d4))\n", 284 | "print(type(d5))\n", 285 | "print(type(d6))\n", 286 | "print(type(d7))\n", 287 | "print(type(d8))\n", 288 | "print(type(d9))\n", 289 | "print(type(d10))" 290 | ] 291 | }, 292 | { 293 | "cell_type": "markdown", 294 | "metadata": {}, 295 | "source": [ 296 | "## String" 297 | ] 298 | }, 299 | { 300 | "cell_type": "code", 301 | "execution_count": 22, 302 | "metadata": {}, 303 | "outputs": [ 304 | { 305 | "name": "stdout", 306 | "output_type": "stream", 307 | "text": [ 308 | "This is example of multi line string\n" 309 | ] 310 | } 311 | ], 312 | "source": [ 313 | "A = '''This is example of multi line string'''\n", 314 | "print(A)" 315 | ] 316 | }, 317 | { 318 | "cell_type": "code", 319 | "execution_count": 23, 320 | "metadata": {}, 321 | "outputs": [ 322 | { 323 | "name": "stdout", 324 | "output_type": "stream", 325 | "text": [ 326 | "36\n" 327 | ] 328 | } 329 | ], 330 | "source": [ 331 | "print(len(A))" 332 | ] 333 | }, 334 | { 335 | "cell_type": "markdown", 336 | "metadata": {}, 337 | "source": [ 338 | "## Type Conversion" 339 | ] 340 | }, 341 | { 342 | "cell_type": "code", 343 | "execution_count": 24, 344 | "metadata": {}, 345 | "outputs": [ 346 | { 347 | "name": "stdout", 348 | "output_type": "stream", 349 | "text": [ 350 | "30.0\n" 351 | ] 352 | } 353 | ], 354 | "source": [ 355 | "## implicit type conversion\n", 356 | "a = 10\n", 357 | "b = 20.0\n", 358 | "c = a + b\n", 359 | "print(c)" 360 | ] 361 | }, 362 | { 363 | "cell_type": "code", 364 | "execution_count": 25, 365 | "metadata": {}, 366 | "outputs": [ 367 | { 368 | "name": "stdout", 369 | "output_type": "stream", 370 | "text": [ 371 | "30\n", 372 | "\n" 373 | ] 374 | } 375 | ], 376 | "source": [ 377 | "## explicit type conversion\n", 378 | "a = 10\n", 379 | "b = '20'\n", 380 | "c = a + int(b)\n", 381 | "print(c)" 382 | ] 383 | }, 384 | { 385 | "cell_type": "markdown", 386 | "metadata": {}, 387 | "source": [ 388 | "## Operators" 389 | ] 390 | }, 391 | { 392 | "cell_type": "code", 393 | "execution_count": 26, 394 | "metadata": {}, 395 | "outputs": [], 396 | "source": [ 397 | "# Arithmetic Operators\n", 398 | "a = 10\n", 399 | "b = 20" 400 | ] 401 | }, 402 | { 403 | "cell_type": "code", 404 | "execution_count": 27, 405 | "metadata": {}, 406 | "outputs": [ 407 | { 408 | "name": "stdout", 409 | "output_type": "stream", 410 | "text": [ 411 | "30\n", 412 | "-10\n", 413 | "200\n", 414 | "0.5\n", 415 | "10\n", 416 | "100000000000000000000\n", 417 | "0\n" 418 | ] 419 | } 420 | ], 421 | "source": [ 422 | "print(a+b)\n", 423 | "print(a-b)\n", 424 | "print(a*b)\n", 425 | "print(a/b)\n", 426 | "print(a%b)\n", 427 | "print(a**b)\n", 428 | "print(a//b)\n" 429 | ] 430 | }, 431 | { 432 | "cell_type": "code", 433 | "execution_count": 28, 434 | "metadata": {}, 435 | "outputs": [], 436 | "source": [ 437 | "# Comparison Operators\n", 438 | "a = 10\n", 439 | "b = 20" 440 | ] 441 | }, 442 | { 443 | "cell_type": "code", 444 | "execution_count": 29, 445 | "metadata": {}, 446 | "outputs": [ 447 | { 448 | "name": "stdout", 449 | "output_type": "stream", 450 | "text": [ 451 | "False\n", 452 | "True\n", 453 | "False\n", 454 | "True\n", 455 | "False\n", 456 | "True\n" 457 | ] 458 | } 459 | ], 460 | "source": [ 461 | "print(a==b)\n", 462 | "print(a!=b)\n", 463 | "print(a>b)\n", 464 | "print(a=b)\n", 466 | "print(a<=b)\n" 467 | ] 468 | }, 469 | { 470 | "cell_type": "code", 471 | "execution_count": 30, 472 | "metadata": {}, 473 | "outputs": [], 474 | "source": [ 475 | "# assignment operators\n", 476 | "a = 10\n", 477 | "b = 20" 478 | ] 479 | }, 480 | { 481 | "cell_type": "code", 482 | "execution_count": 31, 483 | "metadata": {}, 484 | "outputs": [ 485 | { 486 | "name": "stdout", 487 | "output_type": "stream", 488 | "text": [ 489 | "30\n", 490 | "10\n", 491 | "200\n", 492 | "10.0\n", 493 | "10.0\n", 494 | "1e+20\n", 495 | "5e+18\n" 496 | ] 497 | } 498 | ], 499 | "source": [ 500 | "a += b\n", 501 | "print(a)\n", 502 | "a -= b \n", 503 | "print(a)\n", 504 | "a *= b\n", 505 | "print(a)\n", 506 | "a /= b\n", 507 | "print(a) \n", 508 | "a %= b\n", 509 | "print(a)\n", 510 | "a **= b\n", 511 | "print(a)\n", 512 | "a //= b\n", 513 | "print(a)\n" 514 | ] 515 | }, 516 | { 517 | "cell_type": "code", 518 | "execution_count": 32, 519 | "metadata": {}, 520 | "outputs": [ 521 | { 522 | "data": { 523 | "text/plain": [ 524 | "False" 525 | ] 526 | }, 527 | "execution_count": 32, 528 | "metadata": {}, 529 | "output_type": "execute_result" 530 | } 531 | ], 532 | "source": [ 533 | "# Logical Operators\n", 534 | "a = True\n", 535 | "b = False\n", 536 | "a and b\n", 537 | "a or b\n", 538 | "not a\n" 539 | ] 540 | }, 541 | { 542 | "cell_type": "code", 543 | "execution_count": 33, 544 | "metadata": {}, 545 | "outputs": [ 546 | { 547 | "name": "stdout", 548 | "output_type": "stream", 549 | "text": [ 550 | "False\n" 551 | ] 552 | } 553 | ], 554 | "source": [ 555 | "a = 10\n", 556 | "b = 20\n", 557 | "print(a<2 and b>3)" 558 | ] 559 | }, 560 | { 561 | "cell_type": "markdown", 562 | "metadata": {}, 563 | "source": [ 564 | "## Bitwise Ops" 565 | ] 566 | }, 567 | { 568 | "cell_type": "code", 569 | "execution_count": 34, 570 | "metadata": {}, 571 | "outputs": [ 572 | { 573 | "name": "stdout", 574 | "output_type": "stream", 575 | "text": [ 576 | "0\n", 577 | "30\n", 578 | "30\n" 579 | ] 580 | } 581 | ], 582 | "source": [ 583 | "# Bitwise Operators\n", 584 | "a = 10 \n", 585 | "b = 20\n", 586 | "print(a & b)\n", 587 | "print(a | b)\n", 588 | "print(a ^ b)\n" 589 | ] 590 | }, 591 | { 592 | "cell_type": "markdown", 593 | "metadata": {}, 594 | "source": [ 595 | "## Input and Output" 596 | ] 597 | }, 598 | { 599 | "cell_type": "code", 600 | "execution_count": 35, 601 | "metadata": {}, 602 | "outputs": [ 603 | { 604 | "name": "stdout", 605 | "output_type": "stream", 606 | "text": [ 607 | "1\n", 608 | "\n" 609 | ] 610 | } 611 | ], 612 | "source": [ 613 | "num = input(\"Enter the number: \")\n", 614 | "print(num)\n", 615 | "print(type(num))" 616 | ] 617 | }, 618 | { 619 | "cell_type": "code", 620 | "execution_count": 36, 621 | "metadata": {}, 622 | "outputs": [ 623 | { 624 | "name": "stdout", 625 | "output_type": "stream", 626 | "text": [ 627 | "1\n", 628 | "\n" 629 | ] 630 | } 631 | ], 632 | "source": [ 633 | "num = int(input(\"Enter the number: \"))\n", 634 | "print(num)\n", 635 | "print(type(num))" 636 | ] 637 | }, 638 | { 639 | "cell_type": "markdown", 640 | "metadata": {}, 641 | "source": [ 642 | "## If...Else" 643 | ] 644 | }, 645 | { 646 | "cell_type": "code", 647 | "execution_count": 37, 648 | "metadata": {}, 649 | "outputs": [], 650 | "source": [ 651 | "var1 = 10\n", 652 | "var2 = 20" 653 | ] 654 | }, 655 | { 656 | "cell_type": "code", 657 | "execution_count": 38, 658 | "metadata": {}, 659 | "outputs": [ 660 | { 661 | "name": "stdout", 662 | "output_type": "stream", 663 | "text": [ 664 | "var2 is greater than var1\n" 665 | ] 666 | } 667 | ], 668 | "source": [ 669 | "if var1 > var2:\n", 670 | " print(\"var1 is greater than var2\")\n", 671 | "else:\n", 672 | " print(\"var2 is greater than var1\")" 673 | ] 674 | }, 675 | { 676 | "cell_type": "code", 677 | "execution_count": 39, 678 | "metadata": {}, 679 | "outputs": [ 680 | { 681 | "name": "stdout", 682 | "output_type": "stream", 683 | "text": [ 684 | "var1 is not equal to var2\n" 685 | ] 686 | } 687 | ], 688 | "source": [ 689 | "if var1 != var2:\n", 690 | " print(\"var1 is not equal to var2\")\n", 691 | "else:\n", 692 | " print(\"var1 is equal to var2\")" 693 | ] 694 | }, 695 | { 696 | "cell_type": "code", 697 | "execution_count": 40, 698 | "metadata": {}, 699 | "outputs": [ 700 | { 701 | "name": "stdout", 702 | "output_type": "stream", 703 | "text": [ 704 | "var2 is greater than var1\n" 705 | ] 706 | } 707 | ], 708 | "source": [ 709 | "if var1 == var2:\n", 710 | " print(\"var1 is equal to var2\")\n", 711 | "elif var1 > var2:\n", 712 | " print(\"var1 is greater than var2\")\n", 713 | "else:\n", 714 | " print(\"var2 is greater than var1\")" 715 | ] 716 | }, 717 | { 718 | "cell_type": "markdown", 719 | "metadata": {}, 720 | "source": [ 721 | "## Loops" 722 | ] 723 | }, 724 | { 725 | "cell_type": "code", 726 | "execution_count": 41, 727 | "metadata": {}, 728 | "outputs": [ 729 | { 730 | "name": "stdout", 731 | "output_type": "stream", 732 | "text": [ 733 | "P\n", 734 | "y\n", 735 | "t\n", 736 | "h\n", 737 | "o\n", 738 | "n\n" 739 | ] 740 | } 741 | ], 742 | "source": [ 743 | "## For loop\n", 744 | "abc = 'Python'\n", 745 | "for i in abc:\n", 746 | " print(i)\n", 747 | " " 748 | ] 749 | }, 750 | { 751 | "cell_type": "code", 752 | "execution_count": 42, 753 | "metadata": {}, 754 | "outputs": [ 755 | { 756 | "name": "stdout", 757 | "output_type": "stream", 758 | "text": [ 759 | "Python\n", 760 | "Java\n", 761 | "C++\n" 762 | ] 763 | } 764 | ], 765 | "source": [ 766 | "abc = ['Python', 'Java', 'C++']\n", 767 | "for i in abc:\n", 768 | " print(i)" 769 | ] 770 | }, 771 | { 772 | "cell_type": "code", 773 | "execution_count": 43, 774 | "metadata": {}, 775 | "outputs": [ 776 | { 777 | "name": "stdout", 778 | "output_type": "stream", 779 | "text": [ 780 | "Python\n", 781 | "Java\n", 782 | "C++\n" 783 | ] 784 | } 785 | ], 786 | "source": [ 787 | "abc = ('Python', 'Java', 'C++') \n", 788 | "for i in abc:\n", 789 | " print(i)" 790 | ] 791 | }, 792 | { 793 | "cell_type": "code", 794 | "execution_count": 44, 795 | "metadata": {}, 796 | "outputs": [ 797 | { 798 | "name": "stdout", 799 | "output_type": "stream", 800 | "text": [ 801 | "Python\n", 802 | "C++\n", 803 | "Java\n" 804 | ] 805 | } 806 | ], 807 | "source": [ 808 | "abc = {'Python', 'Java', 'C++'}\n", 809 | "for i in abc:\n", 810 | " print(i)" 811 | ] 812 | }, 813 | { 814 | "cell_type": "code", 815 | "execution_count": 45, 816 | "metadata": {}, 817 | "outputs": [ 818 | { 819 | "name": "stdout", 820 | "output_type": "stream", 821 | "text": [ 822 | "name\n", 823 | "age\n", 824 | "city\n" 825 | ] 826 | } 827 | ], 828 | "source": [ 829 | "abc = {'name':'Abhinav', 'age':25, 'city':'Delhi'}\n", 830 | "for i in abc:\n", 831 | " print(i)" 832 | ] 833 | }, 834 | { 835 | "cell_type": "code", 836 | "execution_count": 46, 837 | "metadata": {}, 838 | "outputs": [ 839 | { 840 | "name": "stdout", 841 | "output_type": "stream", 842 | "text": [ 843 | "1\n", 844 | "2\n", 845 | "3\n", 846 | "4\n", 847 | "5\n" 848 | ] 849 | } 850 | ], 851 | "source": [ 852 | "abc = (1,2,3,4,5) \n", 853 | "for i in abc:\n", 854 | " print(i)" 855 | ] 856 | }, 857 | { 858 | "cell_type": "code", 859 | "execution_count": 47, 860 | "metadata": {}, 861 | "outputs": [ 862 | { 863 | "name": "stdout", 864 | "output_type": "stream", 865 | "text": [ 866 | "1\n", 867 | "2\n" 868 | ] 869 | } 870 | ], 871 | "source": [ 872 | "for i in abc:\n", 873 | " if i == 3:\n", 874 | " break\n", 875 | " print(i)" 876 | ] 877 | }, 878 | { 879 | "cell_type": "code", 880 | "execution_count": 48, 881 | "metadata": {}, 882 | "outputs": [ 883 | { 884 | "name": "stdout", 885 | "output_type": "stream", 886 | "text": [ 887 | "1\n", 888 | "2\n", 889 | "4\n", 890 | "5\n" 891 | ] 892 | } 893 | ], 894 | "source": [ 895 | "for i in abc:\n", 896 | " if i == 3:\n", 897 | " continue\n", 898 | " print(i)" 899 | ] 900 | }, 901 | { 902 | "cell_type": "code", 903 | "execution_count": 1, 904 | "metadata": {}, 905 | "outputs": [ 906 | { 907 | "name": "stdout", 908 | "output_type": "stream", 909 | "text": [ 910 | "0\n", 911 | "1\n", 912 | "2\n", 913 | "3\n", 914 | "4\n", 915 | "5\n", 916 | "6\n", 917 | "7\n", 918 | "8\n", 919 | "9\n" 920 | ] 921 | } 922 | ], 923 | "source": [ 924 | "for i in range(10):\n", 925 | " print(i)" 926 | ] 927 | }, 928 | { 929 | "cell_type": "code", 930 | "execution_count": 2, 931 | "metadata": {}, 932 | "outputs": [ 933 | { 934 | "name": "stdout", 935 | "output_type": "stream", 936 | "text": [ 937 | "1\n", 938 | "2\n", 939 | "3\n", 940 | "4\n", 941 | "5\n", 942 | "6\n", 943 | "7\n", 944 | "8\n", 945 | "9\n" 946 | ] 947 | } 948 | ], 949 | "source": [ 950 | "for i in range(1,10):\n", 951 | " print(i)" 952 | ] 953 | }, 954 | { 955 | "cell_type": "code", 956 | "execution_count": 3, 957 | "metadata": {}, 958 | "outputs": [ 959 | { 960 | "name": "stdout", 961 | "output_type": "stream", 962 | "text": [ 963 | "1\n", 964 | "3\n", 965 | "5\n", 966 | "7\n", 967 | "9\n" 968 | ] 969 | } 970 | ], 971 | "source": [ 972 | "for i in range(1,10,2):\n", 973 | " print(i)" 974 | ] 975 | }, 976 | { 977 | "cell_type": "code", 978 | "execution_count": 4, 979 | "metadata": {}, 980 | "outputs": [ 981 | { 982 | "name": "stdout", 983 | "output_type": "stream", 984 | "text": [ 985 | "0 0\n", 986 | "0 1\n", 987 | "0 2\n", 988 | "1 0\n", 989 | "1 1\n", 990 | "1 2\n", 991 | "2 0\n", 992 | "2 1\n", 993 | "2 2\n" 994 | ] 995 | } 996 | ], 997 | "source": [ 998 | "# nested loop\n", 999 | "for i in range(3):\n", 1000 | " for j in range(3):\n", 1001 | " print(i,j)" 1002 | ] 1003 | }, 1004 | { 1005 | "cell_type": "code", 1006 | "execution_count": 5, 1007 | "metadata": {}, 1008 | "outputs": [ 1009 | { 1010 | "name": "stdout", 1011 | "output_type": "stream", 1012 | "text": [ 1013 | "0 0\n", 1014 | "1 1\n", 1015 | "2 2\n" 1016 | ] 1017 | } 1018 | ], 1019 | "source": [ 1020 | "for i in range(3):\n", 1021 | " for j in range(3):\n", 1022 | " if i == j:\n", 1023 | " print(i,j)" 1024 | ] 1025 | }, 1026 | { 1027 | "cell_type": "code", 1028 | "execution_count": 6, 1029 | "metadata": {}, 1030 | "outputs": [ 1031 | { 1032 | "name": "stdout", 1033 | "output_type": "stream", 1034 | "text": [ 1035 | "1\n", 1036 | "2\n", 1037 | "3\n", 1038 | "4\n", 1039 | "5\n", 1040 | "6\n", 1041 | "7\n", 1042 | "8\n", 1043 | "9\n", 1044 | "10\n" 1045 | ] 1046 | } 1047 | ], 1048 | "source": [ 1049 | "# While loop\n", 1050 | "i = 1\n", 1051 | "while i<=10:\n", 1052 | " print(i)\n", 1053 | " i += 1" 1054 | ] 1055 | }, 1056 | { 1057 | "cell_type": "code", 1058 | "execution_count": 7, 1059 | "metadata": {}, 1060 | "outputs": [ 1061 | { 1062 | "name": "stdout", 1063 | "output_type": "stream", 1064 | "text": [ 1065 | "1\n", 1066 | "2\n", 1067 | "3\n", 1068 | "4\n" 1069 | ] 1070 | } 1071 | ], 1072 | "source": [ 1073 | "i = 1 \n", 1074 | "while i<=10:\n", 1075 | " if i == 5:\n", 1076 | " break\n", 1077 | " print(i)\n", 1078 | " i += 1" 1079 | ] 1080 | }, 1081 | { 1082 | "cell_type": "code", 1083 | "execution_count": 8, 1084 | "metadata": {}, 1085 | "outputs": [ 1086 | { 1087 | "name": "stdout", 1088 | "output_type": "stream", 1089 | "text": [ 1090 | "1\n", 1091 | "2\n", 1092 | "3\n", 1093 | "4\n", 1094 | "6\n", 1095 | "7\n", 1096 | "8\n", 1097 | "9\n", 1098 | "10\n" 1099 | ] 1100 | } 1101 | ], 1102 | "source": [ 1103 | "i = 1\n", 1104 | "while i<=10:\n", 1105 | " if i == 5:\n", 1106 | " i += 1\n", 1107 | " continue\n", 1108 | " print(i)\n", 1109 | " i += 1" 1110 | ] 1111 | }, 1112 | { 1113 | "cell_type": "markdown", 1114 | "metadata": {}, 1115 | "source": [ 1116 | "## List" 1117 | ] 1118 | }, 1119 | { 1120 | "cell_type": "code", 1121 | "execution_count": 11, 1122 | "metadata": {}, 1123 | "outputs": [ 1124 | { 1125 | "name": "stdout", 1126 | "output_type": "stream", 1127 | "text": [ 1128 | "[1, 2, 3, 4, 5]\n", 1129 | "\n" 1130 | ] 1131 | } 1132 | ], 1133 | "source": [ 1134 | "# List\n", 1135 | "abc = [1,2,3,4,5]\n", 1136 | "print(abc)\n", 1137 | "print(type(abc))\n" 1138 | ] 1139 | }, 1140 | { 1141 | "cell_type": "code", 1142 | "execution_count": 12, 1143 | "metadata": {}, 1144 | "outputs": [ 1145 | { 1146 | "name": "stdout", 1147 | "output_type": "stream", 1148 | "text": [ 1149 | "1\n", 1150 | "2\n", 1151 | "3\n", 1152 | "4\n", 1153 | "5\n" 1154 | ] 1155 | } 1156 | ], 1157 | "source": [ 1158 | "#accessing elements\n", 1159 | "print(abc[0])\n", 1160 | "print(abc[1])\n", 1161 | "print(abc[2])\n", 1162 | "print(abc[3])\n", 1163 | "print(abc[4])\n" 1164 | ] 1165 | }, 1166 | { 1167 | "cell_type": "code", 1168 | "execution_count": 13, 1169 | "metadata": {}, 1170 | "outputs": [ 1171 | { 1172 | "name": "stdout", 1173 | "output_type": "stream", 1174 | "text": [ 1175 | "[10, 2, 3, 4, 5]\n" 1176 | ] 1177 | } 1178 | ], 1179 | "source": [ 1180 | "# updating elements\n", 1181 | "abc[0] = 10\n", 1182 | "print(abc)" 1183 | ] 1184 | }, 1185 | { 1186 | "cell_type": "code", 1187 | "execution_count": 14, 1188 | "metadata": {}, 1189 | "outputs": [ 1190 | { 1191 | "name": "stdout", 1192 | "output_type": "stream", 1193 | "text": [ 1194 | "[10, 2, 3, 4, 5, 6]\n" 1195 | ] 1196 | } 1197 | ], 1198 | "source": [ 1199 | "abc.append(6)\n", 1200 | "print(abc)" 1201 | ] 1202 | }, 1203 | { 1204 | "cell_type": "code", 1205 | "execution_count": 18, 1206 | "metadata": {}, 1207 | "outputs": [ 1208 | { 1209 | "name": "stdout", 1210 | "output_type": "stream", 1211 | "text": [ 1212 | "[10, 2, 3, 4, 5, 6]\n" 1213 | ] 1214 | } 1215 | ], 1216 | "source": [ 1217 | "defi = abc.copy()\n", 1218 | "print(defi)" 1219 | ] 1220 | }, 1221 | { 1222 | "cell_type": "markdown", 1223 | "metadata": {}, 1224 | "source": [ 1225 | "## Tuple" 1226 | ] 1227 | }, 1228 | { 1229 | "cell_type": "code", 1230 | "execution_count": 19, 1231 | "metadata": {}, 1232 | "outputs": [ 1233 | { 1234 | "name": "stdout", 1235 | "output_type": "stream", 1236 | "text": [ 1237 | "(1, 2, 3, 4, 5)\n", 1238 | "\n" 1239 | ] 1240 | } 1241 | ], 1242 | "source": [ 1243 | "# tuple\n", 1244 | "abc = (1,2,3,4,5)\n", 1245 | "print(abc)\n", 1246 | "print(type(abc))" 1247 | ] 1248 | }, 1249 | { 1250 | "cell_type": "code", 1251 | "execution_count": 20, 1252 | "metadata": {}, 1253 | "outputs": [ 1254 | { 1255 | "name": "stdout", 1256 | "output_type": "stream", 1257 | "text": [ 1258 | "1\n", 1259 | "2\n", 1260 | "3\n", 1261 | "4\n", 1262 | "5\n" 1263 | ] 1264 | } 1265 | ], 1266 | "source": [ 1267 | "#accessing elements\n", 1268 | "print(abc[0])\n", 1269 | "print(abc[1])\n", 1270 | "print(abc[2])\n", 1271 | "print(abc[3])\n", 1272 | "print(abc[4])\n" 1273 | ] 1274 | }, 1275 | { 1276 | "cell_type": "markdown", 1277 | "metadata": {}, 1278 | "source": [ 1279 | "## Set" 1280 | ] 1281 | }, 1282 | { 1283 | "cell_type": "code", 1284 | "execution_count": 21, 1285 | "metadata": {}, 1286 | "outputs": [ 1287 | { 1288 | "name": "stdout", 1289 | "output_type": "stream", 1290 | "text": [ 1291 | "{1, 2, 3, 4, 5}\n", 1292 | "\n" 1293 | ] 1294 | } 1295 | ], 1296 | "source": [ 1297 | "# Set\n", 1298 | "abc = {1,2,3,4,5}\n", 1299 | "print(abc)\n", 1300 | "print(type(abc))\n" 1301 | ] 1302 | }, 1303 | { 1304 | "cell_type": "markdown", 1305 | "metadata": {}, 1306 | "source": [ 1307 | "## Function" 1308 | ] 1309 | }, 1310 | { 1311 | "cell_type": "code", 1312 | "execution_count": 22, 1313 | "metadata": {}, 1314 | "outputs": [], 1315 | "source": [ 1316 | "def function_name():\n", 1317 | " print(\"Hello, World!\")" 1318 | ] 1319 | }, 1320 | { 1321 | "cell_type": "code", 1322 | "execution_count": 23, 1323 | "metadata": {}, 1324 | "outputs": [ 1325 | { 1326 | "name": "stdout", 1327 | "output_type": "stream", 1328 | "text": [ 1329 | "Hello, World!\n" 1330 | ] 1331 | } 1332 | ], 1333 | "source": [ 1334 | "function_name()\n" 1335 | ] 1336 | }, 1337 | { 1338 | "cell_type": "code", 1339 | "execution_count": 24, 1340 | "metadata": {}, 1341 | "outputs": [], 1342 | "source": [ 1343 | "def function_op(fname, lname):\n", 1344 | " print(fname + \" \" + lname)" 1345 | ] 1346 | }, 1347 | { 1348 | "cell_type": "code", 1349 | "execution_count": 25, 1350 | "metadata": {}, 1351 | "outputs": [ 1352 | { 1353 | "name": "stdout", 1354 | "output_type": "stream", 1355 | "text": [ 1356 | "Abhinav shukla\n" 1357 | ] 1358 | } 1359 | ], 1360 | "source": [ 1361 | "function_op(\"Abhinav\", \"shukla\")" 1362 | ] 1363 | }, 1364 | { 1365 | "cell_type": "code", 1366 | "execution_count": 26, 1367 | "metadata": {}, 1368 | "outputs": [], 1369 | "source": [ 1370 | "def function2(*names):\n", 1371 | " print(\"The name is \" + names[2])\n", 1372 | " " 1373 | ] 1374 | }, 1375 | { 1376 | "cell_type": "code", 1377 | "execution_count": 27, 1378 | "metadata": {}, 1379 | "outputs": [ 1380 | { 1381 | "name": "stdout", 1382 | "output_type": "stream", 1383 | "text": [ 1384 | "The name is Rajesh\n" 1385 | ] 1386 | } 1387 | ], 1388 | "source": [ 1389 | "function2(\"Abhinav\", \"Shukla\", \"Rajesh\", \"Rahul\")" 1390 | ] 1391 | }, 1392 | { 1393 | "cell_type": "code", 1394 | "execution_count": 28, 1395 | "metadata": {}, 1396 | "outputs": [], 1397 | "source": [ 1398 | "def function3(name = \"Abhinav\"):\n", 1399 | " print(\"The name is \" + name)" 1400 | ] 1401 | }, 1402 | { 1403 | "cell_type": "code", 1404 | "execution_count": 29, 1405 | "metadata": {}, 1406 | "outputs": [], 1407 | "source": [ 1408 | "def fun(**kwargs):\n", 1409 | " print(\"The name is \" + kwargs[\"name\"])" 1410 | ] 1411 | }, 1412 | { 1413 | "cell_type": "code", 1414 | "execution_count": 30, 1415 | "metadata": {}, 1416 | "outputs": [ 1417 | { 1418 | "name": "stdout", 1419 | "output_type": "stream", 1420 | "text": [ 1421 | "The name is Abhinav\n" 1422 | ] 1423 | } 1424 | ], 1425 | "source": [ 1426 | "fun(name = \"Abhinav\")" 1427 | ] 1428 | }, 1429 | { 1430 | "cell_type": "code", 1431 | "execution_count": 31, 1432 | "metadata": {}, 1433 | "outputs": [], 1434 | "source": [ 1435 | "def sum(a,b):\n", 1436 | " return a+b\n" 1437 | ] 1438 | }, 1439 | { 1440 | "cell_type": "code", 1441 | "execution_count": 32, 1442 | "metadata": {}, 1443 | "outputs": [ 1444 | { 1445 | "data": { 1446 | "text/plain": [ 1447 | "30" 1448 | ] 1449 | }, 1450 | "execution_count": 32, 1451 | "metadata": {}, 1452 | "output_type": "execute_result" 1453 | } 1454 | ], 1455 | "source": [ 1456 | "sum(10,20)" 1457 | ] 1458 | }, 1459 | { 1460 | "cell_type": "code", 1461 | "execution_count": 33, 1462 | "metadata": {}, 1463 | "outputs": [], 1464 | "source": [ 1465 | "# Recursion\n", 1466 | "def fact(n):\n", 1467 | " if n == 1:\n", 1468 | " return 1\n", 1469 | " else:\n", 1470 | " return n*fact(n-1)" 1471 | ] 1472 | }, 1473 | { 1474 | "cell_type": "code", 1475 | "execution_count": 34, 1476 | "metadata": {}, 1477 | "outputs": [ 1478 | { 1479 | "data": { 1480 | "text/plain": [ 1481 | "120" 1482 | ] 1483 | }, 1484 | "execution_count": 34, 1485 | "metadata": {}, 1486 | "output_type": "execute_result" 1487 | } 1488 | ], 1489 | "source": [ 1490 | "fact(5)\n" 1491 | ] 1492 | }, 1493 | { 1494 | "cell_type": "code", 1495 | "execution_count": 35, 1496 | "metadata": {}, 1497 | "outputs": [], 1498 | "source": [ 1499 | "def myfunc(n):\n", 1500 | " return lambda a: a*n" 1501 | ] 1502 | }, 1503 | { 1504 | "cell_type": "code", 1505 | "execution_count": 36, 1506 | "metadata": {}, 1507 | "outputs": [ 1508 | { 1509 | "data": { 1510 | "text/plain": [ 1511 | "22" 1512 | ] 1513 | }, 1514 | "execution_count": 36, 1515 | "metadata": {}, 1516 | "output_type": "execute_result" 1517 | } 1518 | ], 1519 | "source": [ 1520 | "myfunc(2)(11)" 1521 | ] 1522 | }, 1523 | { 1524 | "cell_type": "code", 1525 | "execution_count": 38, 1526 | "metadata": {}, 1527 | "outputs": [ 1528 | { 1529 | "data": { 1530 | "text/plain": [ 1531 | "33" 1532 | ] 1533 | }, 1534 | "execution_count": 38, 1535 | "metadata": {}, 1536 | "output_type": "execute_result" 1537 | } 1538 | ], 1539 | "source": [ 1540 | "myfunc(3)(11)" 1541 | ] 1542 | }, 1543 | { 1544 | "cell_type": "code", 1545 | "execution_count": 39, 1546 | "metadata": {}, 1547 | "outputs": [ 1548 | { 1549 | "data": { 1550 | "text/plain": [ 1551 | "44" 1552 | ] 1553 | }, 1554 | "execution_count": 39, 1555 | "metadata": {}, 1556 | "output_type": "execute_result" 1557 | } 1558 | ], 1559 | "source": [ 1560 | "def myfunc(n):\n", 1561 | " return lambda a: a*n*n \n", 1562 | "myfunc(2)(11)" 1563 | ] 1564 | }, 1565 | { 1566 | "cell_type": "code", 1567 | "execution_count": 42, 1568 | "metadata": {}, 1569 | "outputs": [], 1570 | "source": [ 1571 | "def fact(n):\n", 1572 | " if n == 1:\n", 1573 | " print(n)\n", 1574 | " return 1\n", 1575 | " else:\n", 1576 | " print(n,'*',end=' ')\n", 1577 | " return n*fact(n-1)" 1578 | ] 1579 | }, 1580 | { 1581 | "cell_type": "code", 1582 | "execution_count": 43, 1583 | "metadata": {}, 1584 | "outputs": [ 1585 | { 1586 | "name": "stdout", 1587 | "output_type": "stream", 1588 | "text": [ 1589 | "5 * 4 * 3 * 2 * 1\n" 1590 | ] 1591 | }, 1592 | { 1593 | "data": { 1594 | "text/plain": [ 1595 | "120" 1596 | ] 1597 | }, 1598 | "execution_count": 43, 1599 | "metadata": {}, 1600 | "output_type": "execute_result" 1601 | } 1602 | ], 1603 | "source": [ 1604 | "fact(5)" 1605 | ] 1606 | }, 1607 | { 1608 | "cell_type": "code", 1609 | "execution_count": 44, 1610 | "metadata": {}, 1611 | "outputs": [], 1612 | "source": [ 1613 | "# fibonacci series\n", 1614 | "def fib(n):\n", 1615 | " a = 0\n", 1616 | " b = 1\n", 1617 | " if n == 1:\n", 1618 | " print(a)\n", 1619 | " else:\n", 1620 | " print(a)\n", 1621 | " print(b)\n", 1622 | " for i in range(2,n):\n", 1623 | " c = a+b\n", 1624 | " a = b\n", 1625 | " b = c\n", 1626 | " print(c)" 1627 | ] 1628 | }, 1629 | { 1630 | "cell_type": "code", 1631 | "execution_count": 45, 1632 | "metadata": {}, 1633 | "outputs": [ 1634 | { 1635 | "name": "stdout", 1636 | "output_type": "stream", 1637 | "text": [ 1638 | "0\n", 1639 | "1\n", 1640 | "1\n", 1641 | "2\n", 1642 | "3\n" 1643 | ] 1644 | } 1645 | ], 1646 | "source": [ 1647 | "fib(5)" 1648 | ] 1649 | }, 1650 | { 1651 | "cell_type": "code", 1652 | "execution_count": 46, 1653 | "metadata": {}, 1654 | "outputs": [], 1655 | "source": [ 1656 | "# using recursion for fib" 1657 | ] 1658 | }, 1659 | { 1660 | "cell_type": "code", 1661 | "execution_count": 47, 1662 | "metadata": {}, 1663 | "outputs": [], 1664 | "source": [ 1665 | "def fib_recursion(n):\n", 1666 | " if n <= 1:\n", 1667 | " return n\n", 1668 | " else:\n", 1669 | " return fib_recursion(n-1) + fib_recursion(n-2)" 1670 | ] 1671 | }, 1672 | { 1673 | "cell_type": "code", 1674 | "execution_count": 48, 1675 | "metadata": {}, 1676 | "outputs": [ 1677 | { 1678 | "data": { 1679 | "text/plain": [ 1680 | "5" 1681 | ] 1682 | }, 1683 | "execution_count": 48, 1684 | "metadata": {}, 1685 | "output_type": "execute_result" 1686 | } 1687 | ], 1688 | "source": [ 1689 | "fib_recursion(5)" 1690 | ] 1691 | }, 1692 | { 1693 | "cell_type": "markdown", 1694 | "metadata": {}, 1695 | "source": [ 1696 | "# File Handling" 1697 | ] 1698 | }, 1699 | { 1700 | "cell_type": "code", 1701 | "execution_count": 1, 1702 | "metadata": {}, 1703 | "outputs": [ 1704 | { 1705 | "name": "stdout", 1706 | "output_type": "stream", 1707 | "text": [ 1708 | "Hello I am Learning Python File handling !!! hue ue !!!\n", 1709 | "\n" 1710 | ] 1711 | } 1712 | ], 1713 | "source": [ 1714 | "f = open(\"okay.txt\", \"r\")\n", 1715 | "data = f.read()\n", 1716 | "print(data)\n", 1717 | "print(type(data))" 1718 | ] 1719 | }, 1720 | { 1721 | "cell_type": "code", 1722 | "execution_count": 2, 1723 | "metadata": {}, 1724 | "outputs": [ 1725 | { 1726 | "data": { 1727 | "text/plain": [ 1728 | "13" 1729 | ] 1730 | }, 1731 | "execution_count": 2, 1732 | "metadata": {}, 1733 | "output_type": "execute_result" 1734 | } 1735 | ], 1736 | "source": [ 1737 | "f = open(\"okay.txt\", \"w\")\n", 1738 | "f.write(\"Hello, World!\") " 1739 | ] 1740 | }, 1741 | { 1742 | "cell_type": "code", 1743 | "execution_count": 6, 1744 | "metadata": {}, 1745 | "outputs": [ 1746 | { 1747 | "name": "stdout", 1748 | "output_type": "stream", 1749 | "text": [ 1750 | "Hello, World!\n" 1751 | ] 1752 | } 1753 | ], 1754 | "source": [ 1755 | "f = open(\"okay.txt\", \"r\")\n", 1756 | "data = f.read()\n", 1757 | "print(data)" 1758 | ] 1759 | }, 1760 | { 1761 | "cell_type": "code", 1762 | "execution_count": 7, 1763 | "metadata": {}, 1764 | "outputs": [ 1765 | { 1766 | "name": "stdout", 1767 | "output_type": "stream", 1768 | "text": [ 1769 | "Hello, World!Hello, World!\n" 1770 | ] 1771 | } 1772 | ], 1773 | "source": [ 1774 | "f = open(\"okay.txt\", \"a\")\n", 1775 | "f.write(\"Hello, World!\")\n", 1776 | "f = open(\"okay.txt\", \"r\")\n", 1777 | "data = f.read() \n", 1778 | "print(data)" 1779 | ] 1780 | }, 1781 | { 1782 | "cell_type": "code", 1783 | "execution_count": 8, 1784 | "metadata": {}, 1785 | "outputs": [ 1786 | { 1787 | "name": "stdout", 1788 | "output_type": "stream", 1789 | "text": [ 1790 | "Hello, World!Hello, World!\n" 1791 | ] 1792 | } 1793 | ], 1794 | "source": [ 1795 | "# using with statement\n", 1796 | "with open(\"okay.txt\", \"r\") as f:\n", 1797 | " data = f.read()\n", 1798 | " print(data)" 1799 | ] 1800 | }, 1801 | { 1802 | "cell_type": "code", 1803 | "execution_count": null, 1804 | "metadata": {}, 1805 | "outputs": [], 1806 | "source": [] 1807 | } 1808 | ], 1809 | "metadata": { 1810 | "kernelspec": { 1811 | "display_name": "base", 1812 | "language": "python", 1813 | "name": "python3" 1814 | }, 1815 | "language_info": { 1816 | "codemirror_mode": { 1817 | "name": "ipython", 1818 | "version": 3 1819 | }, 1820 | "file_extension": ".py", 1821 | "mimetype": "text/x-python", 1822 | "name": "python", 1823 | "nbconvert_exporter": "python", 1824 | "pygments_lexer": "ipython3", 1825 | "version": "3.12.4" 1826 | }, 1827 | "orig_nbformat": 4 1828 | }, 1829 | "nbformat": 4, 1830 | "nbformat_minor": 2 1831 | } 1832 | -------------------------------------------------------------------------------- /Concept_Implementations/removing_whitespaces.py: -------------------------------------------------------------------------------- 1 | # Ask user for the input 2 | 3 | name = input("what is your name ? ") 4 | 5 | # remove whitespace from the str 6 | 7 | name = name.strip() 8 | 9 | # strip() removes whitespaces from the beginning and end of the string 10 | 11 | print(f"Hello {name}!") 12 | # The output will be "Hello name!" without any whitespaces in the beginning or end of the string -------------------------------------------------------------------------------- /Concept_Implementations/test.py: -------------------------------------------------------------------------------- 1 | name, age = input("Enter your name and age : ").split() 2 | print("Your name is ", name) 3 | print("Your age is ", age) -------------------------------------------------------------------------------- /Concept_Implementations/typecasting.py: -------------------------------------------------------------------------------- 1 | #Typecasting in python 2 | #Typecasting is the process of converting one data type to another data type. 3 | #There are two types of typecasting: 4 | #Implicit Typecasting 5 | #Explicit Typecasting 6 | #Implicit Typecasting 7 | #Implicit typecasting is done automatically by the compiler. 8 | #In this type of typecasting, the compiler automatically converts one data type to another data type. 9 | #Implicit typecasting is also known as automatic typecasting. 10 | #Implicit typecasting is done when: 11 | #A smaller data type is converted to a larger data type. 12 | #For example, converting an integer to a float. 13 | #Explicit Typecasting 14 | #Explicit typecasting is done manually by the programmer. 15 | #In this type of typecasting, the programmer manually converts one data type to another data type. 16 | #Explicit typecasting is also known as manual typecasting. 17 | #Explicit typecasting is done when: 18 | #A larger data type is converted to a smaller data type. 19 | #For example, converting a float to an integer. 20 | #Typecasting functions in python 21 | #There are several typecasting functions in python that are used to convert one data type to another data type. 22 | #The following are the typecasting functions in python: 23 | #int(): This function is used to convert a data type to an integer data type. 24 | #float(): This function is used to convert a data type to a float data type. 25 | #str(): This function is used to convert a data type to a string data type. 26 | #bool(): This function is used to convert a data type to a boolean data type. 27 | #list(): This function is used to convert a data type to a list data type. 28 | #tuple(): This function is used to convert a data type to a tuple data type. 29 | #set(): This function is used to convert a data type to a set data type. 30 | #dict(): This function is used to convert a data type to a dictionary data type. 31 | #complex(): This function is used to convert a data type to a complex data type. 32 | #Typecasting examples 33 | #The following are the examples of typecasting in python: 34 | #Converting an integer to a float 35 | x = 10 36 | y = float(x) 37 | print(y) 38 | #Converting a float to an integer 39 | x = 10.5 40 | y = int(x) 41 | print(y) 42 | #Converting a string to an integer 43 | x = "10" 44 | y = int(x) 45 | print(y) 46 | #Converting an integer to a string 47 | x = 10 48 | y = str(x) 49 | print(y) 50 | #Converting a string to a float 51 | x = "10.5" 52 | y = float(x) 53 | print(y) 54 | #Converting a float to a string 55 | x = 10.5 56 | y = str(x) 57 | print(y) 58 | #Converting a string to a boolean 59 | x = "True" 60 | y = bool(x) 61 | print(y) 62 | #Converting a boolean to a string 63 | x = True 64 | y = str(x) 65 | print(y) 66 | #Converting a list to a tuple 67 | x = [10, 20, 30, 40, 50] 68 | y = tuple(x) 69 | print(y) 70 | #Converting a tuple to a list 71 | x = (10, 20, 30, 40, 50) 72 | y = list(x) 73 | print(y) 74 | #Converting a string to a set 75 | x = "hello" 76 | y = set(x) 77 | print(y) 78 | #Converting a set to a string 79 | x = {"h", "e", "l", "l", "o"} 80 | y = str(x) 81 | print(y) 82 | #Converting a dictionary to a string 83 | x = {'name': 'John', 'age': 25} 84 | y = str(x) 85 | print(y) 86 | #Converting a string to a complex number 87 | x = "10+5j" 88 | y = complex(x) 89 | print(y) 90 | #Converting a complex number to a string 91 | x = 10+5j 92 | y = str(x) 93 | print(y) 94 | #Converting a string to a list 95 | x = "hello" 96 | y = list(x) 97 | print(y) 98 | #Converting a list to a string 99 | x = ['h', 'e', 'l', 'l', 'o'] 100 | y = str(x) 101 | print(y) 102 | #Converting a string to a tuple 103 | x = "hello" 104 | y = tuple(x) 105 | print(y) 106 | #Converting a tuple to a string 107 | x = ('h', 'e', 'l', 'l', 'o') 108 | y = str(x) 109 | print(y) 110 | #Converting a string to a set 111 | x = "hello" 112 | y = set(x) 113 | print(y) 114 | #Converting a set to a string 115 | x = {'h', 'e', 'l', 'l', 'o'} 116 | y = str(x) 117 | print(y) 118 | #Converting a dictionary to a string 119 | x = {'name': 'John', 'age': 25} 120 | y = str(x) 121 | print(y) 122 | #Converting a string to a complex number 123 | x = "10+5j" 124 | y = complex(x) 125 | print(y) -------------------------------------------------------------------------------- /Concept_Implementations/variables_1.py: -------------------------------------------------------------------------------- 1 | x = 5 2 | print("This is ",type(x)) 3 | y = 5.2 4 | print("This is ",type(y)) 5 | z = "Hello" 6 | print("This is ",type(z)) 7 | a = True 8 | print("This is ",type(a)) 9 | b = False 10 | print("This is ",type(b)) -------------------------------------------------------------------------------- /Keras_Basics/.ipynb_checkpoints/Untitled-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "a32c6c80", 6 | "metadata": {}, 7 | "source": [ 8 | "# Data Preparation and Processing" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 2, 14 | "id": "90b8fb03", 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "import numpy as np \n", 19 | "from random import randint\n", 20 | "from sklearn.utils import shuffle\n", 21 | "from sklearn.preprocessing import MinMaxScaler" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 3, 27 | "id": "0940225d", 28 | "metadata": {}, 29 | "outputs": [], 30 | "source": [ 31 | "train_labels = []\n", 32 | "train_samples = []\n" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "id": "20e0e534", 38 | "metadata": {}, 39 | "source": [ 40 | "Example data :\\\n", 41 | " -> An Experimental drug was tested on individual from ages 13 to 100 in a clinical trial.\\\n", 42 | " -> The trial had 2100 participants.Half were under 65 years , half were 65 years or older.\\\n", 43 | " -> Around 95 of patients 65 or older experienced side effects.\\\n", 44 | " -> Around 95 of patents under 65 experienced no side effects." 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": null, 50 | "id": "587b653a", 51 | "metadata": {}, 52 | "outputs": [], 53 | "source": [ 54 | "for i in range(50):\n", 55 | " # The ~5% of younger individuals who did experiece side effets\n", 56 | " random_younger = randint(13,64)\n", 57 | " train_samples.append(random_younger)\n", 58 | " train_labels.append(1)\n", 59 | " \n", 60 | " # The ~5% of the older population who did not experience side effects \n", 61 | " random_older = randint(65,100)\n", 62 | " train_samples.append(random_older)\n", 63 | " train_labels.append(0)\n" 64 | ] 65 | } 66 | ], 67 | "metadata": { 68 | "kernelspec": { 69 | "display_name": "Python 3 (ipykernel)", 70 | "language": "python", 71 | "name": "python3" 72 | }, 73 | "language_info": { 74 | "codemirror_mode": { 75 | "name": "ipython", 76 | "version": 3 77 | }, 78 | "file_extension": ".py", 79 | "mimetype": "text/x-python", 80 | "name": "python", 81 | "nbconvert_exporter": "python", 82 | "pygments_lexer": "ipython3", 83 | "version": "3.11.5" 84 | } 85 | }, 86 | "nbformat": 4, 87 | "nbformat_minor": 5 88 | } 89 | -------------------------------------------------------------------------------- /Keras_Basics/01_Basics.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maxprogrammer007/PDS-CSVTU-Sem2/b8ef4e1c6df34a90d669228f846de2d222646467/Keras_Basics/01_Basics.py -------------------------------------------------------------------------------- /Keras_Basics/Untitled.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "a32c6c80", 6 | "metadata": {}, 7 | "source": [ 8 | "# Data Preparation and Processing" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 2, 14 | "id": "90b8fb03", 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "import numpy as np \n", 19 | "from random import randint\n", 20 | "from sklearn.utils import shuffle\n", 21 | "from sklearn.preprocessing import MinMaxScaler" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 3, 27 | "id": "0940225d", 28 | "metadata": {}, 29 | "outputs": [], 30 | "source": [ 31 | "train_labels = []\n", 32 | "train_samples = []\n" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "id": "20e0e534", 38 | "metadata": {}, 39 | "source": [ 40 | "Example data :\\\n", 41 | " -> An Experimental drug was tested on individual from ages 13 to 100 in a clinical trial.\\\n", 42 | " -> The trial had 2100 participants.Half were under 65 years , half were 65 years or older.\\\n", 43 | " -> Around 95 of patients 65 or older experienced side effects.\\\n", 44 | " -> Around 95 of patents under 65 experienced no side effects." 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": null, 50 | "id": "587b653a", 51 | "metadata": {}, 52 | "outputs": [], 53 | "source": [ 54 | "for i in range(50):\n", 55 | " # The ~5% of younger individuals who did experiece side effets\n", 56 | " random_younger = randint(13,64)\n", 57 | " train_samples.append(random_younger)\n", 58 | " train_labels.append(1)\n", 59 | " \n", 60 | " # The ~5% of the older population who did not experience side effects \n", 61 | " random_older = randint(65,100)\n", 62 | " train_samples.append(random_older)\n", 63 | " train_labels.append(0)\n" 64 | ] 65 | } 66 | ], 67 | "metadata": { 68 | "kernelspec": { 69 | "display_name": "Python 3 (ipykernel)", 70 | "language": "python", 71 | "name": "python3" 72 | }, 73 | "language_info": { 74 | "codemirror_mode": { 75 | "name": "ipython", 76 | "version": 3 77 | }, 78 | "file_extension": ".py", 79 | "mimetype": "text/x-python", 80 | "name": "python", 81 | "nbconvert_exporter": "python", 82 | "pygments_lexer": "ipython3", 83 | "version": "3.11.5" 84 | } 85 | }, 86 | "nbformat": 4, 87 | "nbformat_minor": 5 88 | } 89 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /Pandas/01_Pandas_basics.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "f9a55b53ccb4113b", 6 | "metadata": {}, 7 | "source": [ 8 | "### What is Pandas\n", 9 | "\n", 10 | "Pandas is a fast, powerful, flexible and easy to use open source data analysis and manipulation tool,\n", 11 | "built on top of the Python programming language." 12 | ] 13 | }, 14 | { 15 | "cell_type": "markdown", 16 | "id": "79adff8e219cf00c", 17 | "metadata": {}, 18 | "source": [ 19 | "### Pandas Series\n", 20 | "\n", 21 | "A Pandas Series is like a column in a table. It is a 1-D array holding data of any type." 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 1, 27 | "id": "7f417016cc90a87a", 28 | "metadata": { 29 | "ExecuteTime": { 30 | "end_time": "2024-06-12T10:41:05.612645Z", 31 | "start_time": "2024-06-12T10:41:04.259822Z" 32 | } 33 | }, 34 | "outputs": [], 35 | "source": [ 36 | "import pandas as pd\n", 37 | "import numpy as np" 38 | ] 39 | }, 40 | { 41 | "cell_type": "markdown", 42 | "id": "c9bf59a3ea2f359e", 43 | "metadata": {}, 44 | "source": [ 45 | "### Creating a Pandas Series using List" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 2, 51 | "id": "d203d858a4e4de4f", 52 | "metadata": { 53 | "ExecuteTime": { 54 | "end_time": "2024-06-12T15:43:56.907539Z", 55 | "start_time": "2024-06-12T15:43:56.893979Z" 56 | } 57 | }, 58 | "outputs": [], 59 | "source": [ 60 | "# string\n", 61 | "s = pd.Series(['a', 'b', 'c', 'd'])" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 6, 67 | "id": "e6ca67f08a60a0a0", 68 | "metadata": { 69 | "ExecuteTime": { 70 | "end_time": "2024-06-12T15:46:08.451225Z", 71 | "start_time": "2024-06-12T15:46:08.441918Z" 72 | } 73 | }, 74 | "outputs": [ 75 | { 76 | "name": "stdout", 77 | "output_type": "stream", 78 | "text": [ 79 | "0 USA\n", 80 | "1 Canada\n", 81 | "2 Germany\n", 82 | "3 UK\n", 83 | "dtype: object\n" 84 | ] 85 | } 86 | ], 87 | "source": [ 88 | "country = ['USA', 'Canada', 'Germany', 'UK']\n", 89 | "s = pd.Series(country)\n", 90 | "print(s)" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 7, 96 | "id": "20f0a5f8184b2774", 97 | "metadata": { 98 | "ExecuteTime": { 99 | "end_time": "2024-06-12T15:46:12.077931Z", 100 | "start_time": "2024-06-12T15:46:12.070254Z" 101 | } 102 | }, 103 | "outputs": [], 104 | "source": [ 105 | "# integer\n", 106 | "s = pd.Series([1, 2, 3, 4])\n" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 8, 112 | "id": "e53fc2e6637d4d4", 113 | "metadata": { 114 | "ExecuteTime": { 115 | "end_time": "2024-06-12T15:46:13.070008Z", 116 | "start_time": "2024-06-12T15:46:13.065592Z" 117 | } 118 | }, 119 | "outputs": [ 120 | { 121 | "name": "stdout", 122 | "output_type": "stream", 123 | "text": [ 124 | "0 1\n", 125 | "1 2\n", 126 | "2 3\n", 127 | "3 4\n", 128 | "dtype: int64\n" 129 | ] 130 | } 131 | ], 132 | "source": [ 133 | "print(s)" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 9, 139 | "id": "ebfb8933d5147890", 140 | "metadata": { 141 | "ExecuteTime": { 142 | "end_time": "2024-06-12T16:00:49.693157Z", 143 | "start_time": "2024-06-12T16:00:49.672280Z" 144 | } 145 | }, 146 | "outputs": [ 147 | { 148 | "name": "stdout", 149 | "output_type": "stream", 150 | "text": [ 151 | "Maths 90\n", 152 | "Science 80\n", 153 | "English 70\n", 154 | "History 60\n", 155 | "dtype: int64\n" 156 | ] 157 | } 158 | ], 159 | "source": [ 160 | "# custom index\n", 161 | "marks = [90, 80, 70, 60]\n", 162 | "subject = ['Maths', 'Science', 'English', 'History']\n", 163 | "s = pd.Series(marks, index=subject)\n", 164 | "print(s)" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": 10, 170 | "id": "e2214d8549f1d225", 171 | "metadata": { 172 | "ExecuteTime": { 173 | "end_time": "2024-06-12T16:03:38.466234Z", 174 | "start_time": "2024-06-12T16:03:38.461750Z" 175 | } 176 | }, 177 | "outputs": [ 178 | { 179 | "name": "stdout", 180 | "output_type": "stream", 181 | "text": [ 182 | "Maths 90\n", 183 | "Science 80\n", 184 | "English 70\n", 185 | "History 60\n", 186 | "Name: Marks hai bhai mere, dtype: int64\n" 187 | ] 188 | } 189 | ], 190 | "source": [ 191 | "# Setting a name to the Series\n", 192 | "s = pd.Series(marks, index=subject, name='Marks hai bhai mere')\n", 193 | "print(s)" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": 11, 199 | "id": "4e3359df340ae030", 200 | "metadata": { 201 | "ExecuteTime": { 202 | "end_time": "2024-06-12T16:04:08.272265Z", 203 | "start_time": "2024-06-12T16:04:08.266077Z" 204 | } 205 | }, 206 | "outputs": [ 207 | { 208 | "name": "stdout", 209 | "output_type": "stream", 210 | "text": [ 211 | "0 10\n", 212 | "1 20\n", 213 | "2 30\n", 214 | "3 40\n", 215 | "dtype: int32\n" 216 | ] 217 | } 218 | ], 219 | "source": [ 220 | "# Creating a Series from a numpy array\n", 221 | "arr = np.array([10, 20, 30, 40])\n", 222 | "s = pd.Series(arr)\n", 223 | "print(s)" 224 | ] 225 | }, 226 | { 227 | "cell_type": "code", 228 | "execution_count": 12, 229 | "id": "ae5976da73e6c665", 230 | "metadata": { 231 | "ExecuteTime": { 232 | "end_time": "2024-06-12T16:07:24.418476Z", 233 | "start_time": "2024-06-12T16:07:24.397733Z" 234 | } 235 | }, 236 | "outputs": [ 237 | { 238 | "data": { 239 | "text/plain": [ 240 | "Maths 90\n", 241 | "Science 80\n", 242 | "English 70\n", 243 | "History 60\n", 244 | "Name: Marks hai bhai mere, dtype: int64" 245 | ] 246 | }, 247 | "execution_count": 12, 248 | "metadata": {}, 249 | "output_type": "execute_result" 250 | } 251 | ], 252 | "source": [ 253 | "marks = pd.Series(marks, index=subject, name='Marks hai bhai mere')\n", 254 | "marks\n" 255 | ] 256 | }, 257 | { 258 | "cell_type": "code", 259 | "execution_count": 13, 260 | "id": "c7ae7f64df065aac", 261 | "metadata": { 262 | "ExecuteTime": { 263 | "end_time": "2024-06-12T16:09:15.304393Z", 264 | "start_time": "2024-06-12T16:09:15.287525Z" 265 | } 266 | }, 267 | "outputs": [ 268 | { 269 | "name": "stdout", 270 | "output_type": "stream", 271 | "text": [ 272 | "Maths 90\n", 273 | "Science 80\n", 274 | "English 70\n", 275 | "History 60\n", 276 | "dtype: int64\n" 277 | ] 278 | } 279 | ], 280 | "source": [ 281 | "# Series from a dictionary\n", 282 | "marks = {\n", 283 | " 'Maths': 90,\n", 284 | " 'Science': 80,\n", 285 | " 'English': 70,\n", 286 | " 'History': 60\n", 287 | " }\n", 288 | "marks_series = pd.Series(marks)\n", 289 | "print(marks_series)" 290 | ] 291 | }, 292 | { 293 | "cell_type": "code", 294 | "execution_count": null, 295 | "id": "efbdf7f5af8f503", 296 | "metadata": {}, 297 | "outputs": [], 298 | "source": [] 299 | } 300 | ], 301 | "metadata": { 302 | "kernelspec": { 303 | "display_name": "Python 3", 304 | "language": "python", 305 | "name": "python3" 306 | }, 307 | "language_info": { 308 | "codemirror_mode": { 309 | "name": "ipython", 310 | "version": 2 311 | }, 312 | "file_extension": ".py", 313 | "mimetype": "text/x-python", 314 | "name": "python", 315 | "nbconvert_exporter": "python", 316 | "pygments_lexer": "ipython2", 317 | "version": "3.12.4" 318 | } 319 | }, 320 | "nbformat": 4, 321 | "nbformat_minor": 5 322 | } 323 | -------------------------------------------------------------------------------- /Practice_Problems/Celcius_To_Faren.py: -------------------------------------------------------------------------------- 1 | # WAP to convert temperature from Celsius to Fahrenheit 2 | # Formula: F = (C * 9/5) + 32 3 | 4 | def celcius_to_farenheit(celcius): 5 | farenheit = (celcius * 9/5) + 32 6 | return farenheit 7 | 8 | celcius = int(input("Enter temperature in Celcius : ")) 9 | farenheit = celcius_to_farenheit(celcius) 10 | print("Temperature in Farenheit is : ", farenheit) 11 | 12 | -------------------------------------------------------------------------------- /Practice_Problems/Tut_py_cdh.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "metadata": { 5 | "ExecuteTime": { 6 | "end_time": "2024-06-17T00:11:04.300489Z", 7 | "start_time": "2024-06-17T00:11:04.288305Z" 8 | } 9 | }, 10 | "cell_type": "code", 11 | "source": [ 12 | "import os\n", 13 | "print(\"Hello: \", os.getcwd())\n", 14 | "print(\"Hello\")" 15 | ], 16 | "id": "413c98e88bb0a202", 17 | "outputs": [ 18 | { 19 | "name": "stdout", 20 | "output_type": "stream", 21 | "text": [ 22 | "Hello: C:\\Users\\Administrator\\Documents\\GitHub\\PDS-CSVTU-Sem2\\Practice_Problems\n", 23 | "Hello\n" 24 | ] 25 | } 26 | ], 27 | "execution_count": 3 28 | }, 29 | { 30 | "metadata": { 31 | "ExecuteTime": { 32 | "end_time": "2024-06-17T08:27:56.629815Z", 33 | "start_time": "2024-06-17T08:27:56.622377Z" 34 | } 35 | }, 36 | "cell_type": "code", 37 | "source": [ 38 | "print(\"5\")\n", 39 | "print(5)\n", 40 | "# printing data type\n", 41 | "print(type(5))\n", 42 | "print(type(\"5\"))\n" 43 | ], 44 | "id": "d0bb4dba44be833b", 45 | "outputs": [ 46 | { 47 | "name": "stdout", 48 | "output_type": "stream", 49 | "text": [ 50 | "5\n", 51 | "5\n", 52 | "\n", 53 | "\n" 54 | ] 55 | } 56 | ], 57 | "execution_count": 3 58 | }, 59 | { 60 | "metadata": {}, 61 | "cell_type": "code", 62 | "outputs": [], 63 | "execution_count": null, 64 | "source": "#variables and data types\n", 65 | "id": "533b6443c60d898c" 66 | } 67 | ], 68 | "metadata": { 69 | "kernelspec": { 70 | "display_name": "Python 3", 71 | "language": "python", 72 | "name": "python3" 73 | }, 74 | "language_info": { 75 | "codemirror_mode": { 76 | "name": "ipython", 77 | "version": 2 78 | }, 79 | "file_extension": ".py", 80 | "mimetype": "text/x-python", 81 | "name": "python", 82 | "nbconvert_exporter": "python", 83 | "pygments_lexer": "ipython2", 84 | "version": "2.7.6" 85 | } 86 | }, 87 | "nbformat": 4, 88 | "nbformat_minor": 5 89 | } 90 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PDS-CSVTU-Sem2 2 | I am storing everything which i did in my python for data science course at UTD , CSVTU 3 | -------------------------------------------------------------------------------- /basic_problem_practices/file.txt: -------------------------------------------------------------------------------- 1 | hello 2 | hi 3 | bye 4 | kkrh 5 | boom boom -------------------------------------------------------------------------------- /basic_problem_practices/problem_practices.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# Portfolio Courses - Practice Problems" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stdout", 19 | "output_type": "stream", 20 | "text": [ 21 | "hi\n" 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "#Problem 1 - Reading a specific line from a file\n", 27 | "\n", 28 | "filename = input(\"Enter the file name: \")\n", 29 | "line_number = int(input(\"Enter the line number: \"))\n", 30 | "\n", 31 | "file = open(filename, \"r\")\n", 32 | "lines = file.readlines() # read all lines into a list\n", 33 | "file.close()\n", 34 | "\n", 35 | "total_lines = len(lines) # get the total number of lines in the file\n", 36 | "if line_number > total_lines: # check if the specified line number is greater than the total number of lines\n", 37 | " print(\"The file does not have that many lines.\")\n", 38 | " exit()\n", 39 | "else:\n", 40 | " line = lines[line_number - 1].rstrip('\\n') # get the line at the specified index and remove the newline character\n", 41 | " print(line)" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 2, 47 | "metadata": {}, 48 | "outputs": [ 49 | { 50 | "name": "stdout", 51 | "output_type": "stream", 52 | "text": [ 53 | "The area of the circle is 78.5397\n" 54 | ] 55 | } 56 | ], 57 | "source": [ 58 | "#problem 2 = computing area of a cirlce\n", 59 | "\n", 60 | "input_radius = float(input(\"enter the radius of the circle :\"))\n", 61 | "area = round(3.14159 * input_radius ** 2,4)\n", 62 | "print(\"The area of the circle is\", area)\n" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": null, 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [] 71 | } 72 | ], 73 | "metadata": { 74 | "kernelspec": { 75 | "display_name": "Python 3", 76 | "language": "python", 77 | "name": "python3" 78 | }, 79 | "language_info": { 80 | "codemirror_mode": { 81 | "name": "ipython", 82 | "version": 3 83 | }, 84 | "file_extension": ".py", 85 | "mimetype": "text/x-python", 86 | "name": "python", 87 | "nbconvert_exporter": "python", 88 | "pygments_lexer": "ipython3", 89 | "version": "3.12.4" 90 | } 91 | }, 92 | "nbformat": 4, 93 | "nbformat_minor": 2 94 | } 95 | -------------------------------------------------------------------------------- /numpy/01_numpy_basics.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "metadata": {}, 5 | "cell_type": "markdown", 6 | "source": [ 7 | "# Numpy\n", 8 | "## Numpy is a library for the Python programming language, adding support for large, Multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.\n", 9 | "##\n", 10 | "## Numpy is the core library for scientific computing in Python. It provides a high-performance multidimensional array object, and tools for working with these arrays. If you are already familiar with MATLAB, you might find this tutorial useful to get started with Numpy.\n", 11 | "##\n", 12 | "## Numpy is a general-purpose array-processing package. It provides a high-performance multidimensional array object, and tools for working with these arrays. It is the fundamental package for scientific computing with Python.\n", 13 | "### also has functions for working in domain of linear algebra, fourier transform, and matrices. \n", 14 | " " 15 | ], 16 | "id": "36b008e704dbf7eb" 17 | }, 18 | { 19 | "metadata": { 20 | "ExecuteTime": { 21 | "end_time": "2024-05-28T08:52:03.040075Z", 22 | "start_time": "2024-05-28T08:52:03.036014Z" 23 | } 24 | }, 25 | "cell_type": "code", 26 | "source": "import numpy as np", 27 | "id": "ad251f14638628de", 28 | "outputs": [], 29 | "execution_count": 68 30 | }, 31 | { 32 | "metadata": {}, 33 | "cell_type": "markdown", 34 | "source": [ 35 | "## We have imported the numpy library as np. Now we can use the functions in the library by using np.function_name.\n", 36 | "## Arrays are stored in contiguous memory locations. They are faster than lists.\n", 37 | "## Arrays are mutable. We can change the values of the array.\n", 38 | "## Arrays are homogeneous. All the elements of the array should be of the same data type.]\n", 39 | "## Arrays are of fixed size. We cannot increase or decrease the size of the array.\n" 40 | ], 41 | "id": "5c91126c60ffa846" 42 | }, 43 | { 44 | "metadata": { 45 | "ExecuteTime": { 46 | "end_time": "2024-05-28T08:52:03.559343Z", 47 | "start_time": "2024-05-28T08:52:03.554015Z" 48 | } 49 | }, 50 | "cell_type": "code", 51 | "source": [ 52 | "# Creating an array\n", 53 | "a = np.array([1,2,3,4,5])\n", 54 | "print(a) \n", 55 | "print(type(a)) # type of the array\n", 56 | "print(a.shape) # shape of the array \n", 57 | "print(a.dtype) # data type of the array\n", 58 | "print(a.size) # number of elements in the array\n", 59 | "print(a.ndim) # number of dimensions of the array\n", 60 | "print(a.itemsize) # size of each element in the array\n" 61 | ], 62 | "id": "bbb1675fa6a51052", 63 | "outputs": [ 64 | { 65 | "name": "stdout", 66 | "output_type": "stream", 67 | "text": [ 68 | "[1 2 3 4 5]\n", 69 | "\n", 70 | "(5,)\n", 71 | "int32\n", 72 | "5\n", 73 | "1\n", 74 | "4\n" 75 | ] 76 | } 77 | ], 78 | "execution_count": 69 79 | }, 80 | { 81 | "metadata": { 82 | "ExecuteTime": { 83 | "end_time": "2024-05-28T08:52:03.581332Z", 84 | "start_time": "2024-05-28T08:52:03.569972Z" 85 | } 86 | }, 87 | "cell_type": "code", 88 | "source": [ 89 | "# Addition of arrays\n", 90 | "a = np.array([1,2,3,4,5])\n", 91 | "b = np.array([6,7,8,9,10])\n", 92 | "c = a + b\n", 93 | "print(c)" 94 | ], 95 | "id": "bc7a571526c65c8", 96 | "outputs": [ 97 | { 98 | "name": "stdout", 99 | "output_type": "stream", 100 | "text": [ 101 | "[ 7 9 11 13 15]\n" 102 | ] 103 | } 104 | ], 105 | "execution_count": 70 106 | }, 107 | { 108 | "metadata": { 109 | "ExecuteTime": { 110 | "end_time": "2024-05-28T08:52:03.624131Z", 111 | "start_time": "2024-05-28T08:52:03.619953Z" 112 | } 113 | }, 114 | "cell_type": "code", 115 | "source": [ 116 | "# Subtraction of arrays\n", 117 | "a = np.array([1,2,3,4,5])\n", 118 | "b = np.array([6,7,8,9,10])\n", 119 | "c = a - b\n", 120 | "print(c)" 121 | ], 122 | "id": "d361ec9ca7450a04", 123 | "outputs": [ 124 | { 125 | "name": "stdout", 126 | "output_type": "stream", 127 | "text": [ 128 | "[-5 -5 -5 -5 -5]\n" 129 | ] 130 | } 131 | ], 132 | "execution_count": 71 133 | }, 134 | { 135 | "metadata": { 136 | "ExecuteTime": { 137 | "end_time": "2024-05-28T08:52:03.678785Z", 138 | "start_time": "2024-05-28T08:52:03.673667Z" 139 | } 140 | }, 141 | "cell_type": "code", 142 | "source": [ 143 | "# Multiplication of arrays\n", 144 | "a = np.array([1,2,3,4,5])\n", 145 | "b = np.array([6,7,8,9,10])\n", 146 | "c = a * b\n", 147 | "print(c)" 148 | ], 149 | "id": "26d74dce47b598fc", 150 | "outputs": [ 151 | { 152 | "name": "stdout", 153 | "output_type": "stream", 154 | "text": [ 155 | "[ 6 14 24 36 50]\n" 156 | ] 157 | } 158 | ], 159 | "execution_count": 72 160 | }, 161 | { 162 | "metadata": { 163 | "ExecuteTime": { 164 | "end_time": "2024-05-28T08:52:03.719453Z", 165 | "start_time": "2024-05-28T08:52:03.715237Z" 166 | } 167 | }, 168 | "cell_type": "code", 169 | "source": [ 170 | "# Division of arrays\n", 171 | "a = np.array([1,2,3,4,5])\n", 172 | "b = np.array([6,7,8,9,10])\n", 173 | "c = a / b\n", 174 | "print(c)" 175 | ], 176 | "id": "db4035467cf03053", 177 | "outputs": [ 178 | { 179 | "name": "stdout", 180 | "output_type": "stream", 181 | "text": [ 182 | "[0.16666667 0.28571429 0.375 0.44444444 0.5 ]\n" 183 | ] 184 | } 185 | ], 186 | "execution_count": 73 187 | }, 188 | { 189 | "metadata": { 190 | "ExecuteTime": { 191 | "end_time": "2024-05-28T08:52:03.780625Z", 192 | "start_time": "2024-05-28T08:52:03.776929Z" 193 | } 194 | }, 195 | "cell_type": "code", 196 | "source": [ 197 | "# Dot product of arrays\n", 198 | "a = np.array([1,2,3,4,5])\n", 199 | "b = np.array([6,7,8,9,10])\n", 200 | "c = np.dot(a,b)\n", 201 | "print(c)" 202 | ], 203 | "id": "f021506251d240d", 204 | "outputs": [ 205 | { 206 | "name": "stdout", 207 | "output_type": "stream", 208 | "text": [ 209 | "130\n" 210 | ] 211 | } 212 | ], 213 | "execution_count": 74 214 | }, 215 | { 216 | "metadata": { 217 | "ExecuteTime": { 218 | "end_time": "2024-05-28T08:52:03.846475Z", 219 | "start_time": "2024-05-28T08:52:03.842187Z" 220 | } 221 | }, 222 | "cell_type": "code", 223 | "source": [ 224 | "# Creating a 2D array\n", 225 | "a = np.array([[1,2,3],[4,5,6],[7,8,9]])\n", 226 | "print(a)\n" 227 | ], 228 | "id": "660ca08b9717c2bf", 229 | "outputs": [ 230 | { 231 | "name": "stdout", 232 | "output_type": "stream", 233 | "text": [ 234 | "[[1 2 3]\n", 235 | " [4 5 6]\n", 236 | " [7 8 9]]\n" 237 | ] 238 | } 239 | ], 240 | "execution_count": 75 241 | }, 242 | { 243 | "metadata": { 244 | "ExecuteTime": { 245 | "end_time": "2024-05-28T08:52:03.958622Z", 246 | "start_time": "2024-05-28T08:52:03.954950Z" 247 | } 248 | }, 249 | "cell_type": "code", 250 | "source": [ 251 | "# Accessing elements of a 2D array\n", 252 | "a = np.array([[1,2,3],[4,5,6],[7,8,9]])\n", 253 | "print(a[0,0]) # first element\n", 254 | "print(a[0,1]) # second element\n" 255 | ], 256 | "id": "e1cd81e91e876486", 257 | "outputs": [ 258 | { 259 | "name": "stdout", 260 | "output_type": "stream", 261 | "text": [ 262 | "1\n", 263 | "2\n" 264 | ] 265 | } 266 | ], 267 | "execution_count": 76 268 | }, 269 | { 270 | "metadata": { 271 | "ExecuteTime": { 272 | "end_time": "2024-05-28T08:52:03.996614Z", 273 | "start_time": "2024-05-28T08:52:03.991908Z" 274 | } 275 | }, 276 | "cell_type": "code", 277 | "source": [ 278 | "# Slicing of a 2D array\n", 279 | "a = np.array([[1,2,3],[4,5,6],[7,8,9]])\n", 280 | "print(a[0:2,0:2]) #irst two rows and first two columns" 281 | ], 282 | "id": "afd150b06d804d4b", 283 | "outputs": [ 284 | { 285 | "name": "stdout", 286 | "output_type": "stream", 287 | "text": [ 288 | "[[1 2]\n", 289 | " [4 5]]\n" 290 | ] 291 | } 292 | ], 293 | "execution_count": 77 294 | }, 295 | { 296 | "metadata": { 297 | "ExecuteTime": { 298 | "end_time": "2024-05-28T08:52:04.077410Z", 299 | "start_time": "2024-05-28T08:52:04.072492Z" 300 | } 301 | }, 302 | "cell_type": "code", 303 | "source": [ 304 | "# Creating a 4 x 2 matrix\n", 305 | "a = np.array([[1,2],[3,4],[5,6],[7,8]])\n", 306 | "print(a)" 307 | ], 308 | "id": "f509e379e16c222c", 309 | "outputs": [ 310 | { 311 | "name": "stdout", 312 | "output_type": "stream", 313 | "text": [ 314 | "[[1 2]\n", 315 | " [3 4]\n", 316 | " [5 6]\n", 317 | " [7 8]]\n" 318 | ] 319 | } 320 | ], 321 | "execution_count": 78 322 | }, 323 | { 324 | "metadata": { 325 | "ExecuteTime": { 326 | "end_time": "2024-05-28T08:52:04.100456Z", 327 | "start_time": "2024-05-28T08:52:04.094495Z" 328 | } 329 | }, 330 | "cell_type": "code", 331 | "source": [ 332 | "# 3d Array\n", 333 | "c = np.array([[[1,2,3],[4,5,6]],[[11,12,13],[14,15,16]]])\n", 334 | "print(c)" 335 | ], 336 | "id": "f61d2959b6fb778f", 337 | "outputs": [ 338 | { 339 | "name": "stdout", 340 | "output_type": "stream", 341 | "text": [ 342 | "[[[ 1 2 3]\n", 343 | " [ 4 5 6]]\n", 344 | "\n", 345 | " [[11 12 13]\n", 346 | " [14 15 16]]]\n" 347 | ] 348 | } 349 | ], 350 | "execution_count": 79 351 | }, 352 | { 353 | "metadata": { 354 | "ExecuteTime": { 355 | "end_time": "2024-05-28T08:52:04.160102Z", 356 | "start_time": "2024-05-28T08:52:04.153427Z" 357 | } 358 | }, 359 | "cell_type": "code", 360 | "source": [ 361 | "#dtype\n", 362 | "\n", 363 | "a = np.array([0,2,3],dtype=bool)\n", 364 | "print(a)\n", 365 | "b = np.array([0,2,3],dtype=complex)\n", 366 | "print(b)" 367 | ], 368 | "id": "87acd9ba181eaa43", 369 | "outputs": [ 370 | { 371 | "name": "stdout", 372 | "output_type": "stream", 373 | "text": [ 374 | "[False True True]\n", 375 | "[0.+0.j 2.+0.j 3.+0.j]\n" 376 | ] 377 | } 378 | ], 379 | "execution_count": 80 380 | }, 381 | { 382 | "metadata": { 383 | "ExecuteTime": { 384 | "end_time": "2024-05-28T08:52:04.216486Z", 385 | "start_time": "2024-05-28T08:52:04.211538Z" 386 | } 387 | }, 388 | "cell_type": "code", 389 | "source": [ 390 | "# np.arange\n", 391 | "a = np.arange(1,11)\n", 392 | "print(a)\n", 393 | "b = np.arange(1,11,2)\n", 394 | "print(b)" 395 | ], 396 | "id": "5d4829575bb5b5aa", 397 | "outputs": [ 398 | { 399 | "name": "stdout", 400 | "output_type": "stream", 401 | "text": [ 402 | "[ 1 2 3 4 5 6 7 8 9 10]\n", 403 | "[1 3 5 7 9]\n" 404 | ] 405 | } 406 | ], 407 | "execution_count": 81 408 | }, 409 | { 410 | "metadata": { 411 | "ExecuteTime": { 412 | "end_time": "2024-05-28T08:52:04.272997Z", 413 | "start_time": "2024-05-28T08:52:04.268575Z" 414 | } 415 | }, 416 | "cell_type": "code", 417 | "source": [ 418 | "# with reshape\n", 419 | "\n", 420 | "np.arange(1,11).reshape(2,5)\n" 421 | ], 422 | "id": "e4d2752902602832", 423 | "outputs": [ 424 | { 425 | "data": { 426 | "text/plain": [ 427 | "array([[ 1, 2, 3, 4, 5],\n", 428 | " [ 6, 7, 8, 9, 10]])" 429 | ] 430 | }, 431 | "execution_count": 82, 432 | "metadata": {}, 433 | "output_type": "execute_result" 434 | } 435 | ], 436 | "execution_count": 82 437 | }, 438 | { 439 | "metadata": { 440 | "ExecuteTime": { 441 | "end_time": "2024-05-28T08:52:04.309947Z", 442 | "start_time": "2024-05-28T08:52:04.306325Z" 443 | } 444 | }, 445 | "cell_type": "code", 446 | "source": [ 447 | "a =np.arange(1,11).reshape(5,2)\n", 448 | "print(a)\n" 449 | ], 450 | "id": "c49d6f15e1d1e3aa", 451 | "outputs": [ 452 | { 453 | "name": "stdout", 454 | "output_type": "stream", 455 | "text": [ 456 | "[[ 1 2]\n", 457 | " [ 3 4]\n", 458 | " [ 5 6]\n", 459 | " [ 7 8]\n", 460 | " [ 9 10]]\n" 461 | ] 462 | } 463 | ], 464 | "execution_count": 83 465 | }, 466 | { 467 | "metadata": { 468 | "ExecuteTime": { 469 | "end_time": "2024-05-28T08:52:04.349384Z", 470 | "start_time": "2024-05-28T08:52:04.346312Z" 471 | } 472 | }, 473 | "cell_type": "code", 474 | "source": [ 475 | "# np.ones and np.zeros\n", 476 | "a = np.ones((3,3))\n", 477 | "print(a)" 478 | ], 479 | "id": "ad8ad0006bd04e20", 480 | "outputs": [ 481 | { 482 | "name": "stdout", 483 | "output_type": "stream", 484 | "text": [ 485 | "[[1. 1. 1.]\n", 486 | " [1. 1. 1.]\n", 487 | " [1. 1. 1.]]\n" 488 | ] 489 | } 490 | ], 491 | "execution_count": 84 492 | }, 493 | { 494 | "metadata": { 495 | "ExecuteTime": { 496 | "end_time": "2024-05-28T08:52:04.430532Z", 497 | "start_time": "2024-05-28T08:52:04.427226Z" 498 | } 499 | }, 500 | "cell_type": "code", 501 | "source": [ 502 | "b = np.zeros((3,3))\n", 503 | "print(b)\n" 504 | ], 505 | "id": "de8aba0fbc37222d", 506 | "outputs": [ 507 | { 508 | "name": "stdout", 509 | "output_type": "stream", 510 | "text": [ 511 | "[[0. 0. 0.]\n", 512 | " [0. 0. 0.]\n", 513 | " [0. 0. 0.]]\n" 514 | ] 515 | } 516 | ], 517 | "execution_count": 85 518 | }, 519 | { 520 | "metadata": { 521 | "ExecuteTime": { 522 | "end_time": "2024-05-28T08:52:04.511259Z", 523 | "start_time": "2024-05-28T08:52:04.507311Z" 524 | } 525 | }, 526 | "cell_type": "code", 527 | "source": [ 528 | "# np.random\n", 529 | "a = np.random.random((3,3))" 530 | ], 531 | "id": "a46982be62b0d6f9", 532 | "outputs": [], 533 | "execution_count": 86 534 | }, 535 | { 536 | "metadata": { 537 | "ExecuteTime": { 538 | "end_time": "2024-05-28T08:52:04.537073Z", 539 | "start_time": "2024-05-28T08:52:04.533651Z" 540 | } 541 | }, 542 | "cell_type": "code", 543 | "source": "print(a)", 544 | "id": "8fcb803c0c6b7b0a", 545 | "outputs": [ 546 | { 547 | "name": "stdout", 548 | "output_type": "stream", 549 | "text": [ 550 | "[[0.6728589 0.09794667 0.2942666 ]\n", 551 | " [0.24164844 0.85593302 0.09477387]\n", 552 | " [0.51547219 0.37220659 0.87810424]]\n" 553 | ] 554 | } 555 | ], 556 | "execution_count": 87 557 | }, 558 | { 559 | "metadata": { 560 | "ExecuteTime": { 561 | "end_time": "2024-05-28T08:52:04.576312Z", 562 | "start_time": "2024-05-28T08:52:04.563173Z" 563 | } 564 | }, 565 | "cell_type": "code", 566 | "source": [ 567 | "# np.linspace\n", 568 | "a = np.linspace(1,10,5) # 5 numbers between 1 and 10\n", 569 | "print(a)" 570 | ], 571 | "id": "4a2c9eea3359082a", 572 | "outputs": [ 573 | { 574 | "name": "stdout", 575 | "output_type": "stream", 576 | "text": [ 577 | "[ 1. 3.25 5.5 7.75 10. ]\n" 578 | ] 579 | } 580 | ], 581 | "execution_count": 88 582 | }, 583 | { 584 | "metadata": { 585 | "ExecuteTime": { 586 | "end_time": "2024-05-28T08:52:04.613912Z", 587 | "start_time": "2024-05-28T08:52:04.610721Z" 588 | } 589 | }, 590 | "cell_type": "code", 591 | "source": [ 592 | "# np.eye\n", 593 | "a = np.eye(3) # 3 x 3 identity matrix\n", 594 | "print(a)" 595 | ], 596 | "id": "ed89b4894de163d5", 597 | "outputs": [ 598 | { 599 | "name": "stdout", 600 | "output_type": "stream", 601 | "text": [ 602 | "[[1. 0. 0.]\n", 603 | " [0. 1. 0.]\n", 604 | " [0. 0. 1.]]\n" 605 | ] 606 | } 607 | ], 608 | "execution_count": 89 609 | }, 610 | { 611 | "metadata": { 612 | "ExecuteTime": { 613 | "end_time": "2024-05-28T08:52:04.659910Z", 614 | "start_time": "2024-05-28T08:52:04.656458Z" 615 | } 616 | }, 617 | "cell_type": "code", 618 | "source": [ 619 | "# np.full\n", 620 | "a = np.full((3,3),5) # 3 x 3 matrix with all elements as 5\n", 621 | "print(a)" 622 | ], 623 | "id": "c935d26f21f8075f", 624 | "outputs": [ 625 | { 626 | "name": "stdout", 627 | "output_type": "stream", 628 | "text": [ 629 | "[[5 5 5]\n", 630 | " [5 5 5]\n", 631 | " [5 5 5]]\n" 632 | ] 633 | } 634 | ], 635 | "execution_count": 90 636 | }, 637 | { 638 | "metadata": { 639 | "ExecuteTime": { 640 | "end_time": "2024-05-28T08:52:04.705301Z", 641 | "start_time": "2024-05-28T08:52:04.700504Z" 642 | } 643 | }, 644 | "cell_type": "code", 645 | "source": [ 646 | "# np.empty\n", 647 | "a = np.empty((3,3)) # 3 x 3 matrix with random values\n", 648 | "print(a)" 649 | ], 650 | "id": "3b6f721e0c3ce53e", 651 | "outputs": [ 652 | { 653 | "name": "stdout", 654 | "output_type": "stream", 655 | "text": [ 656 | "[[1. 0. 0.]\n", 657 | " [0. 1. 0.]\n", 658 | " [0. 0. 1.]]\n" 659 | ] 660 | } 661 | ], 662 | "execution_count": 91 663 | }, 664 | { 665 | "metadata": {}, 666 | "cell_type": "markdown", 667 | "source": "# Numpy Attributes\n", 668 | "id": "729feff63e3f3894" 669 | }, 670 | { 671 | "metadata": { 672 | "ExecuteTime": { 673 | "end_time": "2024-05-28T08:52:04.756267Z", 674 | "start_time": "2024-05-28T08:52:04.752655Z" 675 | } 676 | }, 677 | "cell_type": "code", 678 | "source": [ 679 | "a1 = np.arange(10) # 1D array\n", 680 | "print(a1)\n", 681 | "a2 = np.arange(12).reshape(3,4) # 2D array\n", 682 | "print(a2)\n", 683 | "a3 = np.arange(24).reshape(2,3,4) # 3D array\n", 684 | "print(a3)" 685 | ], 686 | "id": "2a25b1c81974fbfc", 687 | "outputs": [ 688 | { 689 | "name": "stdout", 690 | "output_type": "stream", 691 | "text": [ 692 | "[0 1 2 3 4 5 6 7 8 9]\n", 693 | "[[ 0 1 2 3]\n", 694 | " [ 4 5 6 7]\n", 695 | " [ 8 9 10 11]]\n", 696 | "[[[ 0 1 2 3]\n", 697 | " [ 4 5 6 7]\n", 698 | " [ 8 9 10 11]]\n", 699 | "\n", 700 | " [[12 13 14 15]\n", 701 | " [16 17 18 19]\n", 702 | " [20 21 22 23]]]\n" 703 | ] 704 | } 705 | ], 706 | "execution_count": 92 707 | }, 708 | { 709 | "metadata": { 710 | "ExecuteTime": { 711 | "end_time": "2024-05-28T08:52:04.788978Z", 712 | "start_time": "2024-05-28T08:52:04.785345Z" 713 | } 714 | }, 715 | "cell_type": "code", 716 | "source": [ 717 | "# ndim\n", 718 | "print(a1.ndim) # number of dimensions\n", 719 | "print(a2.ndim)\n", 720 | "print(a3.ndim)" 721 | ], 722 | "id": "981c4443135d39b8", 723 | "outputs": [ 724 | { 725 | "name": "stdout", 726 | "output_type": "stream", 727 | "text": [ 728 | "1\n", 729 | "2\n", 730 | "3\n" 731 | ] 732 | } 733 | ], 734 | "execution_count": 93 735 | }, 736 | { 737 | "metadata": { 738 | "ExecuteTime": { 739 | "end_time": "2024-05-28T08:52:04.865839Z", 740 | "start_time": "2024-05-28T08:52:04.862544Z" 741 | } 742 | }, 743 | "cell_type": "code", 744 | "source": [ 745 | "# shape\n", 746 | "print(a1.shape) # number of elements in each dimension\n", 747 | "print(a2.shape)\n", 748 | "print(a3.shape)" 749 | ], 750 | "id": "61b08403f451ff5b", 751 | "outputs": [ 752 | { 753 | "name": "stdout", 754 | "output_type": "stream", 755 | "text": [ 756 | "(10,)\n", 757 | "(3, 4)\n", 758 | "(2, 3, 4)\n" 759 | ] 760 | } 761 | ], 762 | "execution_count": 94 763 | }, 764 | { 765 | "metadata": { 766 | "ExecuteTime": { 767 | "end_time": "2024-05-28T08:52:04.893807Z", 768 | "start_time": "2024-05-28T08:52:04.884104Z" 769 | } 770 | }, 771 | "cell_type": "code", 772 | "source": [ 773 | "# size\n", 774 | "print(a1.size) # number of elements in the array\n", 775 | "print(a2.size)\n", 776 | "print(a3.size)" 777 | ], 778 | "id": "5fab764508b9b168", 779 | "outputs": [ 780 | { 781 | "name": "stdout", 782 | "output_type": "stream", 783 | "text": [ 784 | "10\n", 785 | "12\n", 786 | "24\n" 787 | ] 788 | } 789 | ], 790 | "execution_count": 95 791 | }, 792 | { 793 | "metadata": { 794 | "ExecuteTime": { 795 | "end_time": "2024-05-28T08:52:04.960887Z", 796 | "start_time": "2024-05-28T08:52:04.956726Z" 797 | } 798 | }, 799 | "cell_type": "code", 800 | "source": [ 801 | "# itemsize\n", 802 | "print(a1.itemsize) # size of each element in the array\n", 803 | "print(a2.itemsize)\n", 804 | "print(a3.itemsize)" 805 | ], 806 | "id": "e40eb03d90696043", 807 | "outputs": [ 808 | { 809 | "name": "stdout", 810 | "output_type": "stream", 811 | "text": [ 812 | "4\n", 813 | "4\n", 814 | "4\n" 815 | ] 816 | } 817 | ], 818 | "execution_count": 96 819 | }, 820 | { 821 | "metadata": { 822 | "ExecuteTime": { 823 | "end_time": "2024-05-28T08:52:05.027806Z", 824 | "start_time": "2024-05-28T08:52:05.024464Z" 825 | } 826 | }, 827 | "cell_type": "code", 828 | "source": [ 829 | "# dtype\n", 830 | "print(a1.dtype) # data type of the array\n", 831 | "print(a2.dtype)\n", 832 | "print(a3.dtype)" 833 | ], 834 | "id": "3b1d5157a8174410", 835 | "outputs": [ 836 | { 837 | "name": "stdout", 838 | "output_type": "stream", 839 | "text": [ 840 | "int32\n", 841 | "int32\n", 842 | "int32\n" 843 | ] 844 | } 845 | ], 846 | "execution_count": 97 847 | }, 848 | { 849 | "metadata": {}, 850 | "cell_type": "markdown", 851 | "source": "# Changing Datatype of Array", 852 | "id": "bf807f6e11a2acae" 853 | }, 854 | { 855 | "metadata": { 856 | "ExecuteTime": { 857 | "end_time": "2024-05-28T08:52:05.083397Z", 858 | "start_time": "2024-05-28T08:52:05.080331Z" 859 | } 860 | }, 861 | "cell_type": "code", 862 | "source": [ 863 | "# astype\n", 864 | "a3.dtype" 865 | ], 866 | "id": "cbe5df8117678818", 867 | "outputs": [ 868 | { 869 | "data": { 870 | "text/plain": [ 871 | "dtype('int32')" 872 | ] 873 | }, 874 | "execution_count": 98, 875 | "metadata": {}, 876 | "output_type": "execute_result" 877 | } 878 | ], 879 | "execution_count": 98 880 | }, 881 | { 882 | "metadata": { 883 | "ExecuteTime": { 884 | "end_time": "2024-05-28T08:52:05.124700Z", 885 | "start_time": "2024-05-28T08:52:05.120739Z" 886 | } 887 | }, 888 | "cell_type": "code", 889 | "source": "a3 = a3.astype(float)", 890 | "id": "aed19e8306c8b27f", 891 | "outputs": [], 892 | "execution_count": 99 893 | }, 894 | { 895 | "metadata": { 896 | "ExecuteTime": { 897 | "end_time": "2024-05-28T08:52:05.187051Z", 898 | "start_time": "2024-05-28T08:52:05.183351Z" 899 | } 900 | }, 901 | "cell_type": "code", 902 | "source": "a3.dtype", 903 | "id": "67ab6f3de84f09b7", 904 | "outputs": [ 905 | { 906 | "data": { 907 | "text/plain": [ 908 | "dtype('float64')" 909 | ] 910 | }, 911 | "execution_count": 100, 912 | "metadata": {}, 913 | "output_type": "execute_result" 914 | } 915 | ], 916 | "execution_count": 100 917 | }, 918 | { 919 | "metadata": {}, 920 | "cell_type": "markdown", 921 | "source": "# Array Operations", 922 | "id": "af722b622f4eb509" 923 | }, 924 | { 925 | "metadata": { 926 | "ExecuteTime": { 927 | "end_time": "2024-05-28T08:52:05.215231Z", 928 | "start_time": "2024-05-28T08:52:05.211381Z" 929 | } 930 | }, 931 | "cell_type": "code", 932 | "source": [ 933 | "a1 = np.arange(12).reshape(3,4)\n", 934 | "a2 = np.arange(12,24).reshape(3,4)\n", 935 | "\n", 936 | "print(a1)\n", 937 | "print(a2)" 938 | ], 939 | "id": "507703f36620d267", 940 | "outputs": [ 941 | { 942 | "name": "stdout", 943 | "output_type": "stream", 944 | "text": [ 945 | "[[ 0 1 2 3]\n", 946 | " [ 4 5 6 7]\n", 947 | " [ 8 9 10 11]]\n", 948 | "[[12 13 14 15]\n", 949 | " [16 17 18 19]\n", 950 | " [20 21 22 23]]\n" 951 | ] 952 | } 953 | ], 954 | "execution_count": 101 955 | }, 956 | { 957 | "metadata": { 958 | "ExecuteTime": { 959 | "end_time": "2024-05-28T08:52:05.268157Z", 960 | "start_time": "2024-05-28T08:52:05.265011Z" 961 | } 962 | }, 963 | "cell_type": "code", 964 | "source": [ 965 | "# Scalar operations\n", 966 | "\n", 967 | "# arithmetic operations\n", 968 | "print(a1 + 2)\n", 969 | "print(a1 - 2)\n", 970 | "print(a1 * 2)\n", 971 | "print(a1 / 2)\n" 972 | ], 973 | "id": "f46ba05a28ccccb1", 974 | "outputs": [ 975 | { 976 | "name": "stdout", 977 | "output_type": "stream", 978 | "text": [ 979 | "[[ 2 3 4 5]\n", 980 | " [ 6 7 8 9]\n", 981 | " [10 11 12 13]]\n", 982 | "[[-2 -1 0 1]\n", 983 | " [ 2 3 4 5]\n", 984 | " [ 6 7 8 9]]\n", 985 | "[[ 0 2 4 6]\n", 986 | " [ 8 10 12 14]\n", 987 | " [16 18 20 22]]\n", 988 | "[[0. 0.5 1. 1.5]\n", 989 | " [2. 2.5 3. 3.5]\n", 990 | " [4. 4.5 5. 5.5]]\n" 991 | ] 992 | } 993 | ], 994 | "execution_count": 102 995 | }, 996 | { 997 | "metadata": { 998 | "ExecuteTime": { 999 | "end_time": "2024-05-28T08:52:05.294153Z", 1000 | "start_time": "2024-05-28T08:52:05.290501Z" 1001 | } 1002 | }, 1003 | "cell_type": "code", 1004 | "source": [ 1005 | "# relational operations\n", 1006 | "print(a1 > 5)\n", 1007 | "print(a1 < 5)\n", 1008 | "print(a1 == 5)\n", 1009 | "print(a1 != 5)\n" 1010 | ], 1011 | "id": "cd4b1ad14cbab764", 1012 | "outputs": [ 1013 | { 1014 | "name": "stdout", 1015 | "output_type": "stream", 1016 | "text": [ 1017 | "[[False False False False]\n", 1018 | " [False False True True]\n", 1019 | " [ True True True True]]\n", 1020 | "[[ True True True True]\n", 1021 | " [ True False False False]\n", 1022 | " [False False False False]]\n", 1023 | "[[False False False False]\n", 1024 | " [False True False False]\n", 1025 | " [False False False False]]\n", 1026 | "[[ True True True True]\n", 1027 | " [ True False True True]\n", 1028 | " [ True True True True]]\n" 1029 | ] 1030 | } 1031 | ], 1032 | "execution_count": 103 1033 | }, 1034 | { 1035 | "metadata": { 1036 | "ExecuteTime": { 1037 | "end_time": "2024-05-28T08:52:05.339764Z", 1038 | "start_time": "2024-05-28T08:52:05.336594Z" 1039 | } 1040 | }, 1041 | "cell_type": "code", 1042 | "source": [ 1043 | "# Vector operations\n", 1044 | "\n", 1045 | "# arithmetic operations\n", 1046 | "print(a1 + a2)\n", 1047 | "print(a1 - a2)\n", 1048 | "print(a1 * a2)\n", 1049 | "print(a1 / a2)\n", 1050 | " " 1051 | ], 1052 | "id": "eb42d59cad53066d", 1053 | "outputs": [ 1054 | { 1055 | "name": "stdout", 1056 | "output_type": "stream", 1057 | "text": [ 1058 | "[[12 14 16 18]\n", 1059 | " [20 22 24 26]\n", 1060 | " [28 30 32 34]]\n", 1061 | "[[-12 -12 -12 -12]\n", 1062 | " [-12 -12 -12 -12]\n", 1063 | " [-12 -12 -12 -12]]\n", 1064 | "[[ 0 13 28 45]\n", 1065 | " [ 64 85 108 133]\n", 1066 | " [160 189 220 253]]\n", 1067 | "[[0. 0.07692308 0.14285714 0.2 ]\n", 1068 | " [0.25 0.29411765 0.33333333 0.36842105]\n", 1069 | " [0.4 0.42857143 0.45454545 0.47826087]]\n" 1070 | ] 1071 | } 1072 | ], 1073 | "execution_count": 104 1074 | }, 1075 | { 1076 | "metadata": {}, 1077 | "cell_type": "markdown", 1078 | "source": "# Numpy Functions", 1079 | "id": "2417331d42e53ac0" 1080 | }, 1081 | { 1082 | "metadata": { 1083 | "ExecuteTime": { 1084 | "end_time": "2024-05-28T08:52:05.388151Z", 1085 | "start_time": "2024-05-28T08:52:05.383537Z" 1086 | } 1087 | }, 1088 | "cell_type": "code", 1089 | "source": [ 1090 | "a1 = np.random.random((3,3))\n", 1091 | "a1 =np.round(a1*100) # round off the elements of the array \n", 1092 | "print(a1)" 1093 | ], 1094 | "id": "6061e7f961d76e43", 1095 | "outputs": [ 1096 | { 1097 | "name": "stdout", 1098 | "output_type": "stream", 1099 | "text": [ 1100 | "[[65. 24. 58.]\n", 1101 | " [97. 52. 7.]\n", 1102 | " [44. 15. 15.]]\n" 1103 | ] 1104 | } 1105 | ], 1106 | "execution_count": 105 1107 | }, 1108 | { 1109 | "metadata": { 1110 | "ExecuteTime": { 1111 | "end_time": "2024-05-28T08:52:05.459233Z", 1112 | "start_time": "2024-05-28T08:52:05.455817Z" 1113 | } 1114 | }, 1115 | "cell_type": "code", 1116 | "source": [ 1117 | "# max / min / sum / prod\n", 1118 | "print(a1.max()) # maximum element\n", 1119 | "print(a1.min()) # minimum element\n", 1120 | "print(a1.sum()) # sum of all elements\n", 1121 | "print(a1.prod()) # product of all elements" 1122 | ], 1123 | "id": "c0cb8c4248300816", 1124 | "outputs": [ 1125 | { 1126 | "name": "stdout", 1127 | "output_type": "stream", 1128 | "text": [ 1129 | "97.0\n", 1130 | "7.0\n", 1131 | "377.0\n", 1132 | "31627211616000.0\n" 1133 | ] 1134 | } 1135 | ], 1136 | "execution_count": 106 1137 | }, 1138 | { 1139 | "metadata": { 1140 | "ExecuteTime": { 1141 | "end_time": "2024-05-28T08:52:05.537967Z", 1142 | "start_time": "2024-05-28T08:52:05.534021Z" 1143 | } 1144 | }, 1145 | "cell_type": "code", 1146 | "source": [ 1147 | "# mean / median / std / var\n", 1148 | "print(a1.mean()) # mean\n", 1149 | "print(np.median(a1)) # median\n", 1150 | "print(a1.std()) # standard deviation\n", 1151 | "print(a1.var()) # variance" 1152 | ], 1153 | "id": "3d86bfee4c7a6c4e", 1154 | "outputs": [ 1155 | { 1156 | "name": "stdout", 1157 | "output_type": "stream", 1158 | "text": [ 1159 | "41.888888888888886\n", 1160 | "44.0\n", 1161 | "27.730626648544884\n", 1162 | "768.9876543209878\n" 1163 | ] 1164 | } 1165 | ], 1166 | "execution_count": 107 1167 | }, 1168 | { 1169 | "metadata": { 1170 | "ExecuteTime": { 1171 | "end_time": "2024-05-28T08:52:05.612525Z", 1172 | "start_time": "2024-05-28T08:52:05.607675Z" 1173 | } 1174 | }, 1175 | "cell_type": "code", 1176 | "source": [ 1177 | "# For each row\n", 1178 | "print(a1.max(axis=1)) # maximum element in each row\n", 1179 | "# 1 -> row" 1180 | ], 1181 | "id": "687cc2eceecb6f26", 1182 | "outputs": [ 1183 | { 1184 | "name": "stdout", 1185 | "output_type": "stream", 1186 | "text": [ 1187 | "[65. 97. 44.]\n" 1188 | ] 1189 | } 1190 | ], 1191 | "execution_count": 108 1192 | }, 1193 | { 1194 | "metadata": { 1195 | "ExecuteTime": { 1196 | "end_time": "2024-05-28T08:52:05.638531Z", 1197 | "start_time": "2024-05-28T08:52:05.634817Z" 1198 | } 1199 | }, 1200 | "cell_type": "code", 1201 | "source": [ 1202 | "# For each column\n", 1203 | "print(a1.max(axis=0)) # maximum element in each column\n", 1204 | "# 0 -> column" 1205 | ], 1206 | "id": "4628f4bc56145ec4", 1207 | "outputs": [ 1208 | { 1209 | "name": "stdout", 1210 | "output_type": "stream", 1211 | "text": [ 1212 | "[97. 52. 58.]\n" 1213 | ] 1214 | } 1215 | ], 1216 | "execution_count": 109 1217 | }, 1218 | { 1219 | "metadata": { 1220 | "ExecuteTime": { 1221 | "end_time": "2024-05-28T08:52:05.688431Z", 1222 | "start_time": "2024-05-28T08:52:05.684967Z" 1223 | } 1224 | }, 1225 | "cell_type": "code", 1226 | "source": [ 1227 | "# trigonometric functions\n", 1228 | "print(np.sin(a1))\n", 1229 | "print(np.cos(a1))\n", 1230 | "print(np.tan(a1))\n" 1231 | ], 1232 | "id": "d79acac3ca909b8", 1233 | "outputs": [ 1234 | { 1235 | "name": "stdout", 1236 | "output_type": "stream", 1237 | "text": [ 1238 | "[[ 0.82682868 -0.90557836 0.99287265]\n", 1239 | " [ 0.37960774 0.98662759 0.6569866 ]\n", 1240 | " [ 0.01770193 0.65028784 0.65028784]]\n", 1241 | "[[-0.56245385 0.42417901 0.11918014]\n", 1242 | " [-0.92514754 -0.16299078 0.75390225]\n", 1243 | " [ 0.99984331 -0.75968791 -0.75968791]]\n", 1244 | "[[-1.47003826 -2.1348967 8.33085685]\n", 1245 | " [-0.4103213 -6.05327238 0.87144798]\n", 1246 | " [ 0.0177047 -0.8559934 -0.8559934 ]]\n" 1247 | ] 1248 | } 1249 | ], 1250 | "execution_count": 110 1251 | }, 1252 | { 1253 | "metadata": { 1254 | "ExecuteTime": { 1255 | "end_time": "2024-05-28T08:52:05.749362Z", 1256 | "start_time": "2024-05-28T08:52:05.746124Z" 1257 | } 1258 | }, 1259 | "cell_type": "code", 1260 | "source": [ 1261 | "# exponential functions\n", 1262 | "print(np.exp(a1))\n", 1263 | "print(np.log(a1))\n", 1264 | "print(np.log10(a1)) # log base 10\n" 1265 | ], 1266 | "id": "8da85b776a95827e", 1267 | "outputs": [ 1268 | { 1269 | "name": "stdout", 1270 | "output_type": "stream", 1271 | "text": [ 1272 | "[[1.69488924e+28 2.64891221e+10 1.54553894e+25]\n", 1273 | " [1.33833472e+42 3.83100800e+22 1.09663316e+03]\n", 1274 | " [1.28516001e+19 3.26901737e+06 3.26901737e+06]]\n", 1275 | "[[4.17438727 3.17805383 4.06044301]\n", 1276 | " [4.57471098 3.95124372 1.94591015]\n", 1277 | " [3.78418963 2.7080502 2.7080502 ]]\n", 1278 | "[[1.81291336 1.38021124 1.76342799]\n", 1279 | " [1.98677173 1.71600334 0.84509804]\n", 1280 | " [1.64345268 1.17609126 1.17609126]]\n" 1281 | ] 1282 | } 1283 | ], 1284 | "execution_count": 111 1285 | }, 1286 | { 1287 | "metadata": { 1288 | "ExecuteTime": { 1289 | "end_time": "2024-05-28T08:52:05.810072Z", 1290 | "start_time": "2024-05-28T08:52:05.804104Z" 1291 | } 1292 | }, 1293 | "cell_type": "code", 1294 | "source": [ 1295 | "# Dot Products\n", 1296 | "a2 = np.arange(12).reshape(3,4)\n", 1297 | "a3 = np.arange(12,24).reshape(3,4)\n", 1298 | "np.dot(a2,a3.T) # dot product of two arrays a2 and a3 \n" 1299 | ], 1300 | "id": "3710080946844b62", 1301 | "outputs": [ 1302 | { 1303 | "data": { 1304 | "text/plain": [ 1305 | "array([[ 86, 110, 134],\n", 1306 | " [302, 390, 478],\n", 1307 | " [518, 670, 822]])" 1308 | ] 1309 | }, 1310 | "execution_count": 112, 1311 | "metadata": {}, 1312 | "output_type": "execute_result" 1313 | } 1314 | ], 1315 | "execution_count": 112 1316 | }, 1317 | { 1318 | "metadata": { 1319 | "ExecuteTime": { 1320 | "end_time": "2024-05-28T08:52:05.843040Z", 1321 | "start_time": "2024-05-28T08:52:05.838427Z" 1322 | } 1323 | }, 1324 | "cell_type": "code", 1325 | "source": [ 1326 | "# Transpose\n", 1327 | "a1.T\n" 1328 | ], 1329 | "id": "82edefb69bd2329a", 1330 | "outputs": [ 1331 | { 1332 | "data": { 1333 | "text/plain": [ 1334 | "array([[65., 97., 44.],\n", 1335 | " [24., 52., 15.],\n", 1336 | " [58., 7., 15.]])" 1337 | ] 1338 | }, 1339 | "execution_count": 113, 1340 | "metadata": {}, 1341 | "output_type": "execute_result" 1342 | } 1343 | ], 1344 | "execution_count": 113 1345 | }, 1346 | { 1347 | "metadata": { 1348 | "ExecuteTime": { 1349 | "end_time": "2024-05-28T08:52:05.899764Z", 1350 | "start_time": "2024-05-28T08:52:05.891402Z" 1351 | } 1352 | }, 1353 | "cell_type": "code", 1354 | "source": [ 1355 | "# Inverse\n", 1356 | "np.linalg.inv(a1)\n" 1357 | ], 1358 | "id": "543b9927ab877765", 1359 | "outputs": [ 1360 | { 1361 | "data": { 1362 | "text/plain": [ 1363 | "array([[-0.02111553, -0.01595395, 0.08909188],\n", 1364 | " [ 0.03588075, 0.04933212, -0.16176057],\n", 1365 | " [ 0.02605812, -0.00253386, -0.03290894]])" 1366 | ] 1367 | }, 1368 | "execution_count": 114, 1369 | "metadata": {}, 1370 | "output_type": "execute_result" 1371 | } 1372 | ], 1373 | "execution_count": 114 1374 | }, 1375 | { 1376 | "metadata": { 1377 | "ExecuteTime": { 1378 | "end_time": "2024-05-28T08:52:05.933515Z", 1379 | "start_time": "2024-05-28T08:52:05.930112Z" 1380 | } 1381 | }, 1382 | "cell_type": "code", 1383 | "source": [ 1384 | "# round / floor / ceil\n", 1385 | "print(np.round(3.14))\n", 1386 | "print(np.floor(3.14))\n", 1387 | "print(np.ceil(3.14))" 1388 | ], 1389 | "id": "a58c499363acbb4d", 1390 | "outputs": [ 1391 | { 1392 | "name": "stdout", 1393 | "output_type": "stream", 1394 | "text": [ 1395 | "3.0\n", 1396 | "3.0\n", 1397 | "4.0\n" 1398 | ] 1399 | } 1400 | ], 1401 | "execution_count": 115 1402 | }, 1403 | { 1404 | "metadata": { 1405 | "ExecuteTime": { 1406 | "end_time": "2024-05-28T08:52:05.981477Z", 1407 | "start_time": "2024-05-28T08:52:05.977799Z" 1408 | } 1409 | }, 1410 | "cell_type": "code", 1411 | "source": [ 1412 | "# unique\n", 1413 | "a1 = np.array([1,2,3,4,5,1,2,3,4,5])\n", 1414 | "print(np.unique(a1)) # unique elements in the array\n" 1415 | ], 1416 | "id": "14520231246c2e28", 1417 | "outputs": [ 1418 | { 1419 | "name": "stdout", 1420 | "output_type": "stream", 1421 | "text": [ 1422 | "[1 2 3 4 5]\n" 1423 | ] 1424 | } 1425 | ], 1426 | "execution_count": 116 1427 | }, 1428 | { 1429 | "metadata": {}, 1430 | "cell_type": "markdown", 1431 | "source": "# Numpy Indexing and Slicing", 1432 | "id": "504ed6c76483fce6" 1433 | }, 1434 | { 1435 | "metadata": { 1436 | "ExecuteTime": { 1437 | "end_time": "2024-05-28T08:52:06.037224Z", 1438 | "start_time": "2024-05-28T08:52:06.034157Z" 1439 | } 1440 | }, 1441 | "cell_type": "code", 1442 | "source": [ 1443 | "a1 = np.arange(10)\n", 1444 | "a2 = np.arange(12).reshape(3,4)\n", 1445 | "a3 = np.arange(24).reshape(2,3,4)" 1446 | ], 1447 | "id": "c19abe7635bc720e", 1448 | "outputs": [], 1449 | "execution_count": 117 1450 | }, 1451 | { 1452 | "metadata": { 1453 | "ExecuteTime": { 1454 | "end_time": "2024-05-28T08:52:06.104413Z", 1455 | "start_time": "2024-05-28T08:52:06.100188Z" 1456 | } 1457 | }, 1458 | "cell_type": "code", 1459 | "source": [ 1460 | "print(a1[0]) # first element\n", 1461 | "print(a1[-1]) # last element" 1462 | ], 1463 | "id": "adf6da4b28dddaa4", 1464 | "outputs": [ 1465 | { 1466 | "name": "stdout", 1467 | "output_type": "stream", 1468 | "text": [ 1469 | "0\n", 1470 | "9\n" 1471 | ] 1472 | } 1473 | ], 1474 | "execution_count": 118 1475 | }, 1476 | { 1477 | "metadata": { 1478 | "ExecuteTime": { 1479 | "end_time": "2024-05-28T08:52:06.135066Z", 1480 | "start_time": "2024-05-28T08:52:06.132112Z" 1481 | } 1482 | }, 1483 | "cell_type": "code", 1484 | "source": "print(a3[0,0,0]) # first element", 1485 | "id": "14e7c180ee2ea97f", 1486 | "outputs": [ 1487 | { 1488 | "name": "stdout", 1489 | "output_type": "stream", 1490 | "text": [ 1491 | "0\n" 1492 | ] 1493 | } 1494 | ], 1495 | "execution_count": 119 1496 | }, 1497 | { 1498 | "metadata": { 1499 | "ExecuteTime": { 1500 | "end_time": "2024-05-28T08:52:06.166963Z", 1501 | "start_time": "2024-05-28T08:52:06.163342Z" 1502 | } 1503 | }, 1504 | "cell_type": "code", 1505 | "source": [ 1506 | "# Slicing \n", 1507 | "print(a1[0:5]) # first five elements\n", 1508 | "print(a1[:5]) # first five elements\n", 1509 | "print(a1[5:]) # elements from index 5\n", 1510 | "print(a1[::2]) # every second element\n" 1511 | ], 1512 | "id": "1b125f9e8c9eef56", 1513 | "outputs": [ 1514 | { 1515 | "name": "stdout", 1516 | "output_type": "stream", 1517 | "text": [ 1518 | "[0 1 2 3 4]\n", 1519 | "[0 1 2 3 4]\n", 1520 | "[5 6 7 8 9]\n", 1521 | "[0 2 4 6 8]\n" 1522 | ] 1523 | } 1524 | ], 1525 | "execution_count": 120 1526 | }, 1527 | { 1528 | "metadata": { 1529 | "ExecuteTime": { 1530 | "end_time": "2024-05-28T08:52:06.191737Z", 1531 | "start_time": "2024-05-28T08:52:06.188407Z" 1532 | } 1533 | }, 1534 | "cell_type": "code", 1535 | "source": [ 1536 | "# Slicing 2D array\n", 1537 | "print(a2[0,0]) # first element\n", 1538 | "print(a2[0,0:2]) # first row and first two columns\n" 1539 | ], 1540 | "id": "a105cc622aa0d842", 1541 | "outputs": [ 1542 | { 1543 | "name": "stdout", 1544 | "output_type": "stream", 1545 | "text": [ 1546 | "0\n", 1547 | "[0 1]\n" 1548 | ] 1549 | } 1550 | ], 1551 | "execution_count": 121 1552 | }, 1553 | { 1554 | "metadata": { 1555 | "ExecuteTime": { 1556 | "end_time": "2024-05-28T08:52:06.254547Z", 1557 | "start_time": "2024-05-28T08:52:06.250432Z" 1558 | } 1559 | }, 1560 | "cell_type": "code", 1561 | "source": [ 1562 | "# Slicing 3D array\n", 1563 | "print(a3[0,0,0]) # first element\n", 1564 | "print(a3[0,0,0:2]) # first row and first two columns\n", 1565 | "print(a3[0,0:2,0:2]) # first two rows and first two columns\n" 1566 | ], 1567 | "id": "3a1b0a3747dae5f", 1568 | "outputs": [ 1569 | { 1570 | "name": "stdout", 1571 | "output_type": "stream", 1572 | "text": [ 1573 | "0\n", 1574 | "[0 1]\n", 1575 | "[[0 1]\n", 1576 | " [4 5]]\n" 1577 | ] 1578 | } 1579 | ], 1580 | "execution_count": 122 1581 | }, 1582 | { 1583 | "metadata": { 1584 | "ExecuteTime": { 1585 | "end_time": "2024-05-28T08:52:06.278595Z", 1586 | "start_time": "2024-05-28T08:52:06.273906Z" 1587 | } 1588 | }, 1589 | "cell_type": "code", 1590 | "source": [ 1591 | "# Printing corner elements of a 2D array\n", 1592 | "print(a2[0:1,0:2]) # first two rows and first two columns\n" 1593 | ], 1594 | "id": "81cb3440af4f3c4", 1595 | "outputs": [ 1596 | { 1597 | "name": "stdout", 1598 | "output_type": "stream", 1599 | "text": [ 1600 | "[[0 1]]\n" 1601 | ] 1602 | } 1603 | ], 1604 | "execution_count": 123 1605 | }, 1606 | { 1607 | "metadata": {}, 1608 | "cell_type": "markdown", 1609 | "source": "# Iterating over Arrays", 1610 | "id": "2147b5334d81dc4e" 1611 | }, 1612 | { 1613 | "metadata": { 1614 | "ExecuteTime": { 1615 | "end_time": "2024-05-28T08:52:06.332988Z", 1616 | "start_time": "2024-05-28T08:52:06.328386Z" 1617 | } 1618 | }, 1619 | "cell_type": "code", 1620 | "source": "a1", 1621 | "id": "4903361d0bf348e0", 1622 | "outputs": [ 1623 | { 1624 | "data": { 1625 | "text/plain": [ 1626 | "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])" 1627 | ] 1628 | }, 1629 | "execution_count": 124, 1630 | "metadata": {}, 1631 | "output_type": "execute_result" 1632 | } 1633 | ], 1634 | "execution_count": 124 1635 | }, 1636 | { 1637 | "metadata": { 1638 | "ExecuteTime": { 1639 | "end_time": "2024-05-28T08:52:06.347223Z", 1640 | "start_time": "2024-05-28T08:52:06.343056Z" 1641 | } 1642 | }, 1643 | "cell_type": "code", 1644 | "source": "a2", 1645 | "id": "486da72639ec5ce2", 1646 | "outputs": [ 1647 | { 1648 | "data": { 1649 | "text/plain": [ 1650 | "array([[ 0, 1, 2, 3],\n", 1651 | " [ 4, 5, 6, 7],\n", 1652 | " [ 8, 9, 10, 11]])" 1653 | ] 1654 | }, 1655 | "execution_count": 125, 1656 | "metadata": {}, 1657 | "output_type": "execute_result" 1658 | } 1659 | ], 1660 | "execution_count": 125 1661 | }, 1662 | { 1663 | "metadata": { 1664 | "ExecuteTime": { 1665 | "end_time": "2024-05-28T08:52:06.368082Z", 1666 | "start_time": "2024-05-28T08:52:06.364505Z" 1667 | } 1668 | }, 1669 | "cell_type": "code", 1670 | "source": [ 1671 | "for i in a1:\n", 1672 | " print(i)" 1673 | ], 1674 | "id": "2210b6325269578c", 1675 | "outputs": [ 1676 | { 1677 | "name": "stdout", 1678 | "output_type": "stream", 1679 | "text": [ 1680 | "0\n", 1681 | "1\n", 1682 | "2\n", 1683 | "3\n", 1684 | "4\n", 1685 | "5\n", 1686 | "6\n", 1687 | "7\n", 1688 | "8\n", 1689 | "9\n" 1690 | ] 1691 | } 1692 | ], 1693 | "execution_count": 126 1694 | }, 1695 | { 1696 | "metadata": { 1697 | "ExecuteTime": { 1698 | "end_time": "2024-05-28T08:52:06.430399Z", 1699 | "start_time": "2024-05-28T08:52:06.426672Z" 1700 | } 1701 | }, 1702 | "cell_type": "code", 1703 | "source": [ 1704 | "for i in a2: \n", 1705 | " print(i) # row by row" 1706 | ], 1707 | "id": "de1ac71ecd381c9e", 1708 | "outputs": [ 1709 | { 1710 | "name": "stdout", 1711 | "output_type": "stream", 1712 | "text": [ 1713 | "[0 1 2 3]\n", 1714 | "[4 5 6 7]\n", 1715 | "[ 8 9 10 11]\n" 1716 | ] 1717 | } 1718 | ], 1719 | "execution_count": 127 1720 | }, 1721 | { 1722 | "metadata": { 1723 | "ExecuteTime": { 1724 | "end_time": "2024-05-28T08:52:06.478605Z", 1725 | "start_time": "2024-05-28T08:52:06.475707Z" 1726 | } 1727 | }, 1728 | "cell_type": "code", 1729 | "source": [ 1730 | "for i in a2.flat: # flatten the array\n", 1731 | " print(i) # element by element" 1732 | ], 1733 | "id": "c60cc9bbec982857", 1734 | "outputs": [ 1735 | { 1736 | "name": "stdout", 1737 | "output_type": "stream", 1738 | "text": [ 1739 | "0\n", 1740 | "1\n", 1741 | "2\n", 1742 | "3\n", 1743 | "4\n", 1744 | "5\n", 1745 | "6\n", 1746 | "7\n", 1747 | "8\n", 1748 | "9\n", 1749 | "10\n", 1750 | "11\n" 1751 | ] 1752 | } 1753 | ], 1754 | "execution_count": 128 1755 | }, 1756 | { 1757 | "metadata": { 1758 | "ExecuteTime": { 1759 | "end_time": "2024-05-28T08:52:06.532035Z", 1760 | "start_time": "2024-05-28T08:52:06.528986Z" 1761 | } 1762 | }, 1763 | "cell_type": "code", 1764 | "source": [ 1765 | "for i in np.nditer(a2): # default order\n", 1766 | " print(i) # element by element" 1767 | ], 1768 | "id": "2ae39765c4b62f5", 1769 | "outputs": [ 1770 | { 1771 | "name": "stdout", 1772 | "output_type": "stream", 1773 | "text": [ 1774 | "0\n", 1775 | "1\n", 1776 | "2\n", 1777 | "3\n", 1778 | "4\n", 1779 | "5\n", 1780 | "6\n", 1781 | "7\n", 1782 | "8\n", 1783 | "9\n", 1784 | "10\n", 1785 | "11\n" 1786 | ] 1787 | } 1788 | ], 1789 | "execution_count": 129 1790 | }, 1791 | { 1792 | "metadata": { 1793 | "ExecuteTime": { 1794 | "end_time": "2024-05-28T08:52:06.608156Z", 1795 | "start_time": "2024-05-28T08:52:06.602602Z" 1796 | } 1797 | }, 1798 | "cell_type": "code", 1799 | "source": [ 1800 | "for i in np.nditer(a2,order='F'): # Fortran order\n", 1801 | " print(i) # column by column" 1802 | ], 1803 | "id": "bbf3f403fa9e6bc1", 1804 | "outputs": [ 1805 | { 1806 | "name": "stdout", 1807 | "output_type": "stream", 1808 | "text": [ 1809 | "0\n", 1810 | "4\n", 1811 | "8\n", 1812 | "1\n", 1813 | "5\n", 1814 | "9\n", 1815 | "2\n", 1816 | "6\n", 1817 | "10\n", 1818 | "3\n", 1819 | "7\n", 1820 | "11\n" 1821 | ] 1822 | } 1823 | ], 1824 | "execution_count": 130 1825 | }, 1826 | { 1827 | "metadata": { 1828 | "ExecuteTime": { 1829 | "end_time": "2024-05-28T08:52:06.636870Z", 1830 | "start_time": "2024-05-28T08:52:06.633460Z" 1831 | } 1832 | }, 1833 | "cell_type": "code", 1834 | "source": [ 1835 | "for i in np.nditer(a2,order='C'): # C order\n", 1836 | " print(i) # row by row" 1837 | ], 1838 | "id": "134d68b3d05f38a8", 1839 | "outputs": [ 1840 | { 1841 | "name": "stdout", 1842 | "output_type": "stream", 1843 | "text": [ 1844 | "0\n", 1845 | "1\n", 1846 | "2\n", 1847 | "3\n", 1848 | "4\n", 1849 | "5\n", 1850 | "6\n", 1851 | "7\n", 1852 | "8\n", 1853 | "9\n", 1854 | "10\n", 1855 | "11\n" 1856 | ] 1857 | } 1858 | ], 1859 | "execution_count": 131 1860 | }, 1861 | { 1862 | "metadata": { 1863 | "ExecuteTime": { 1864 | "end_time": "2024-05-28T08:52:06.676830Z", 1865 | "start_time": "2024-05-28T08:52:06.673421Z" 1866 | } 1867 | }, 1868 | "cell_type": "code", 1869 | "source": [ 1870 | "for i in np.nditer(a2,flags=['external_loop']): # external loop \n", 1871 | " print(i) # row by row" 1872 | ], 1873 | "id": "34cf95d6fe093239", 1874 | "outputs": [ 1875 | { 1876 | "name": "stdout", 1877 | "output_type": "stream", 1878 | "text": [ 1879 | "[ 0 1 2 3 4 5 6 7 8 9 10 11]\n" 1880 | ] 1881 | } 1882 | ], 1883 | "execution_count": 132 1884 | }, 1885 | { 1886 | "metadata": {}, 1887 | "cell_type": "markdown", 1888 | "source": "# Reshaping Arrays\n", 1889 | "id": "ab83d4ecf4450da3" 1890 | }, 1891 | { 1892 | "metadata": { 1893 | "ExecuteTime": { 1894 | "end_time": "2024-05-28T08:52:06.719016Z", 1895 | "start_time": "2024-05-28T08:52:06.705158Z" 1896 | } 1897 | }, 1898 | "cell_type": "code", 1899 | "source": "a3", 1900 | "id": "891dec7624718f2e", 1901 | "outputs": [ 1902 | { 1903 | "data": { 1904 | "text/plain": [ 1905 | "array([[[ 0, 1, 2, 3],\n", 1906 | " [ 4, 5, 6, 7],\n", 1907 | " [ 8, 9, 10, 11]],\n", 1908 | "\n", 1909 | " [[12, 13, 14, 15],\n", 1910 | " [16, 17, 18, 19],\n", 1911 | " [20, 21, 22, 23]]])" 1912 | ] 1913 | }, 1914 | "execution_count": 133, 1915 | "metadata": {}, 1916 | "output_type": "execute_result" 1917 | } 1918 | ], 1919 | "execution_count": 133 1920 | }, 1921 | { 1922 | "metadata": { 1923 | "ExecuteTime": { 1924 | "end_time": "2024-05-28T08:52:33.412972Z", 1925 | "start_time": "2024-05-28T08:52:33.396557Z" 1926 | } 1927 | }, 1928 | "cell_type": "code", 1929 | "source": "a2.T # transpose the array ", 1930 | "id": "a34d975eb754114a", 1931 | "outputs": [ 1932 | { 1933 | "data": { 1934 | "text/plain": [ 1935 | "array([[ 0, 4, 8],\n", 1936 | " [ 1, 5, 9],\n", 1937 | " [ 2, 6, 10],\n", 1938 | " [ 3, 7, 11]])" 1939 | ] 1940 | }, 1941 | "execution_count": 135, 1942 | "metadata": {}, 1943 | "output_type": "execute_result" 1944 | } 1945 | ], 1946 | "execution_count": 135 1947 | }, 1948 | { 1949 | "metadata": { 1950 | "ExecuteTime": { 1951 | "end_time": "2024-05-28T08:52:40.838345Z", 1952 | "start_time": "2024-05-28T08:52:40.831508Z" 1953 | } 1954 | }, 1955 | "cell_type": "code", 1956 | "source": "a2.reshape(4,3) # reshape the array to 4 x 3", 1957 | "id": "b148895e084780bf", 1958 | "outputs": [ 1959 | { 1960 | "data": { 1961 | "text/plain": [ 1962 | "array([[ 0, 1, 2],\n", 1963 | " [ 3, 4, 5],\n", 1964 | " [ 6, 7, 8],\n", 1965 | " [ 9, 10, 11]])" 1966 | ] 1967 | }, 1968 | "execution_count": 136, 1969 | "metadata": {}, 1970 | "output_type": "execute_result" 1971 | } 1972 | ], 1973 | "execution_count": 136 1974 | }, 1975 | { 1976 | "metadata": { 1977 | "ExecuteTime": { 1978 | "end_time": "2024-05-28T08:53:51.286478Z", 1979 | "start_time": "2024-05-28T08:53:51.273205Z" 1980 | } 1981 | }, 1982 | "cell_type": "code", 1983 | "source": [ 1984 | "# ravel\n", 1985 | "\n", 1986 | "a2.ravel() # flatten the array" 1987 | ], 1988 | "id": "811501fc05227300", 1989 | "outputs": [ 1990 | { 1991 | "data": { 1992 | "text/plain": [ 1993 | "array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])" 1994 | ] 1995 | }, 1996 | "execution_count": 137, 1997 | "metadata": {}, 1998 | "output_type": "execute_result" 1999 | } 2000 | ], 2001 | "execution_count": 137 2002 | }, 2003 | { 2004 | "metadata": {}, 2005 | "cell_type": "markdown", 2006 | "source": "# Stack Arrays", 2007 | "id": "4fb2cc73573a4b05" 2008 | }, 2009 | { 2010 | "metadata": { 2011 | "ExecuteTime": { 2012 | "end_time": "2024-05-28T08:55:38.974024Z", 2013 | "start_time": "2024-05-28T08:55:38.961254Z" 2014 | } 2015 | }, 2016 | "cell_type": "code", 2017 | "source": [ 2018 | "# Horizontal Stack\n", 2019 | "a1 = np.array([1,2,3])\n", 2020 | "a2 = np.array([4,5,6])\n", 2021 | "np.hstack((a1,a2))\n" 2022 | ], 2023 | "id": "dfce2a360d233602", 2024 | "outputs": [ 2025 | { 2026 | "data": { 2027 | "text/plain": [ 2028 | "array([1, 2, 3, 4, 5, 6])" 2029 | ] 2030 | }, 2031 | "execution_count": 138, 2032 | "metadata": {}, 2033 | "output_type": "execute_result" 2034 | } 2035 | ], 2036 | "execution_count": 138 2037 | }, 2038 | { 2039 | "metadata": { 2040 | "ExecuteTime": { 2041 | "end_time": "2024-05-28T08:56:01.025860Z", 2042 | "start_time": "2024-05-28T08:56:01.021379Z" 2043 | } 2044 | }, 2045 | "cell_type": "code", 2046 | "source": [ 2047 | "# Vertical Stack\n", 2048 | "a1 = np.array([1,2,3])\n", 2049 | "a2 = np.array([4,5,6])\n", 2050 | "np.vstack((a1,a2))\n" 2051 | ], 2052 | "id": "c7a3691076753534", 2053 | "outputs": [ 2054 | { 2055 | "data": { 2056 | "text/plain": [ 2057 | "array([[1, 2, 3],\n", 2058 | " [4, 5, 6]])" 2059 | ] 2060 | }, 2061 | "execution_count": 139, 2062 | "metadata": {}, 2063 | "output_type": "execute_result" 2064 | } 2065 | ], 2066 | "execution_count": 139 2067 | }, 2068 | { 2069 | "metadata": {}, 2070 | "cell_type": "markdown", 2071 | "source": "# Splitting Arrays", 2072 | "id": "a641d52bfb1189e6" 2073 | }, 2074 | { 2075 | "metadata": { 2076 | "ExecuteTime": { 2077 | "end_time": "2024-05-28T08:59:07.640736Z", 2078 | "start_time": "2024-05-28T08:59:07.632969Z" 2079 | } 2080 | }, 2081 | "cell_type": "code", 2082 | "source": [ 2083 | "a1 = np.arange(10)\n", 2084 | "np.split(a1,2) # split the array into two equal parts\n" 2085 | ], 2086 | "id": "eba0225f774cf7af", 2087 | "outputs": [ 2088 | { 2089 | "data": { 2090 | "text/plain": [ 2091 | "[array([0, 1, 2, 3, 4]), array([5, 6, 7, 8, 9])]" 2092 | ] 2093 | }, 2094 | "execution_count": 140, 2095 | "metadata": {}, 2096 | "output_type": "execute_result" 2097 | } 2098 | ], 2099 | "execution_count": 140 2100 | }, 2101 | { 2102 | "metadata": { 2103 | "ExecuteTime": { 2104 | "end_time": "2024-05-28T08:59:54.846217Z", 2105 | "start_time": "2024-05-28T08:59:54.841988Z" 2106 | } 2107 | }, 2108 | "cell_type": "code", 2109 | "source": [ 2110 | "a2 = np.arange(12).reshape(3,4)\n", 2111 | "np.vsplit(a2,3) # split the array into three equal parts" 2112 | ], 2113 | "id": "7ecd99457691ab5f", 2114 | "outputs": [ 2115 | { 2116 | "data": { 2117 | "text/plain": [ 2118 | "[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]" 2119 | ] 2120 | }, 2121 | "execution_count": 141, 2122 | "metadata": {}, 2123 | "output_type": "execute_result" 2124 | } 2125 | ], 2126 | "execution_count": 141 2127 | }, 2128 | { 2129 | "metadata": { 2130 | "ExecuteTime": { 2131 | "end_time": "2024-05-28T09:00:12.325792Z", 2132 | "start_time": "2024-05-28T09:00:12.322507Z" 2133 | } 2134 | }, 2135 | "cell_type": "code", 2136 | "source": "np.hsplit(a2,2) # split the array into two equal parts", 2137 | "id": "3666172d4df067ea", 2138 | "outputs": [ 2139 | { 2140 | "data": { 2141 | "text/plain": [ 2142 | "[array([[0, 1],\n", 2143 | " [4, 5],\n", 2144 | " [8, 9]]),\n", 2145 | " array([[ 2, 3],\n", 2146 | " [ 6, 7],\n", 2147 | " [10, 11]])]" 2148 | ] 2149 | }, 2150 | "execution_count": 142, 2151 | "metadata": {}, 2152 | "output_type": "execute_result" 2153 | } 2154 | ], 2155 | "execution_count": 142 2156 | }, 2157 | { 2158 | "metadata": {}, 2159 | "cell_type": "markdown", 2160 | "source": [ 2161 | "# End of the Notebook\n", 2162 | "\n", 2163 | "## Notebook by - Abhinav Shukla ( @Maxprogrammer007 )" 2164 | ], 2165 | "id": "ed63895e85a51e50" 2166 | } 2167 | ], 2168 | "metadata": { 2169 | "kernelspec": { 2170 | "display_name": "Python 3", 2171 | "language": "python", 2172 | "name": "python3" 2173 | }, 2174 | "language_info": { 2175 | "codemirror_mode": { 2176 | "name": "ipython", 2177 | "version": 2 2178 | }, 2179 | "file_extension": ".py", 2180 | "mimetype": "text/x-python", 2181 | "name": "python", 2182 | "nbconvert_exporter": "python", 2183 | "pygments_lexer": "ipython2", 2184 | "version": "2.7.6" 2185 | } 2186 | }, 2187 | "nbformat": 4, 2188 | "nbformat_minor": 5 2189 | } 2190 | --------------------------------------------------------------------------------