├── .ipynb_checkpoints ├── 1. Introduction to Data Science-checkpoint.ipynb ├── 2. NumPy-checkpoint.ipynb ├── 3. NumPy exercises-checkpoint.ipynb ├── Untitled-checkpoint.ipynb └── untitled-checkpoint.txt ├── 1. Introduction to Data Science.ipynb ├── 2. NumPy.ipynb ├── 3. NumPy exercises.ipynb ├── README.md ├── Untitled.ipynb ├── numpy.png └── untitled.txt /.ipynb_checkpoints/1. Introduction to Data Science-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "![rmotr](https://user-images.githubusercontent.com/7065401/52071918-bda15380-2562-11e9-828c-7f95297e4a82.png)\n", 8 | "
\n", 9 | "\n", 10 | "\n", 12 | " \n", 13 | "# Intro to Data Science\n", 14 | "\n", 15 | "What is _Data Science_? The only thing we know for sure is that, _Data Science_ is a broad term. It depends on the organization/company, the people involved, etc. This is an good representation:\n", 16 | "\n", 17 | "\n", 18 | "\n", 19 | "> [Source](http://drewconway.com/zia/2013/3/26/the-data-science-venn-diagram)\n" 20 | ] 21 | }, 22 | { 23 | "cell_type": "markdown", 24 | "metadata": {}, 25 | "source": [ 26 | "\n", 27 | "Although, this one might be a little bit more accurate:\n", 28 | "\n", 29 | "\n", 30 | "\n", 31 | "As a \"Data Scientist\" you'll be combining your domain knowledge \"science\", with large amounts of data, using programming and computers. Your tasks as Data Scientists will involve, highly technical tasks like Machine Learning to boring and repetitive operative tasks like scraping a website from data and importing it in a Database.\n" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 39 | "\n", 40 | "### What does a Data Scientist do?\n", 41 | "\n", 42 | "It's probably easier to define the tasks that are most common to Data Scientists that to give a general definition that applies to everybody.\n", 43 | "\n", 44 | "\n", 45 | "\n", 46 | "> [Source from recommended article](https://cacm.acm.org/blogs/blog-cacm/169199-data-science-workflow-overview-and-challenges/fulltext)\n" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "\n", 54 | "**Getting the data**\n", 55 | "\n", 56 | "Depending on your company/organization, getting the data can be as simple as a SQL query or as difficult as scraping entire websites. The problem with this task is that it's not standardized.\n", 57 | "\n", 58 | "**Parsing and Cleaning the Data**\n", 59 | "\n", 60 | "Depending on your sources, you'll need to do a little bit of preparation. Excluding outliers, filling null values, translating values, etc.\n", 61 | "\n", 62 | "**Merging, combining data**\n", 63 | "\n", 64 | "If the data comes from different sources, merging it can be tedious. Specially if it's hard to define that piece of information that relates different data sources.\n", 65 | "\n", 66 | "**Doing the analysis**\n", 67 | "\n", 68 | "This involves your own domain expertise + the tools available for the job. For example, you need to know the principles of statistics and you can also use [`statsmodels`](https://www.statsmodels.org) to simplify your job. The analysis part is usually iterative and involves other subtasks as visualizations, validation testing, etc.\n", 69 | "\n", 70 | "**Building models**\n", 71 | "\n", 72 | "The whole point of the analysis part is finding patterns in particular cases to build general models. Your models can be predictions, clusterings, or just automated reports. In a general sense, it's the result of all the previous phases.\n", 73 | "\n", 74 | "**Deploying it**\n", 75 | "\n", 76 | "Perfect analyses and models are useless if they're not repeatable and scalable. This phase depends on your own models, but it'll usually imply a cloud provider. From simple reports (emailed every day at 3AM from an AWS Lambda) to a Machine Learning Model (built on top of Tensor Flow and deployed on Google's cloud infrastructure)." 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "metadata": {}, 82 | "source": [ 83 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 84 | "\n", 85 | "### The only thing that's certain\n", 86 | "\n", 87 | "We're sorry we can't give you better answers to the question \"What is Data Science?\", but this is a new discipline and we're putting together definitions, scopes and responsibilities of different actors. But there's one thing that's certain: **You need to know how to code...**\n", 88 | "\n", 89 | "\n", 90 | "\n", 91 | "> [Source](https://twitter.com/josh_wills/status/198093512149958656?lang=en)\n" 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "metadata": {}, 97 | "source": [ 98 | "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)" 99 | ] 100 | } 101 | ], 102 | "metadata": { 103 | "kernelspec": { 104 | "display_name": "Python 3", 105 | "language": "python", 106 | "name": "python3" 107 | }, 108 | "language_info": { 109 | "codemirror_mode": { 110 | "name": "ipython", 111 | "version": 3 112 | }, 113 | "file_extension": ".py", 114 | "mimetype": "text/x-python", 115 | "name": "python", 116 | "nbconvert_exporter": "python", 117 | "pygments_lexer": "ipython3", 118 | "version": "3.7.4" 119 | } 120 | }, 121 | "nbformat": 4, 122 | "nbformat_minor": 2 123 | } 124 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/2. NumPy-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "![rmotr](https://user-images.githubusercontent.com/7065401/52071918-bda15380-2562-11e9-828c-7f95297e4a82.png)\n", 8 | "
\n", 9 | "\n", 10 | "\n", 12 | "\n", 13 | "# Numpy: Numeric computing library\n", 14 | "\n", 15 | "NumPy (Numerical Python) is one of the core packages for numerical computing in Python. Pandas, Matplotlib, Statmodels and many other Scientific libraries rely on NumPy.\n", 16 | "\n", 17 | "NumPy major contributions are:\n", 18 | "\n", 19 | "* Efficient numeric computation with C primitives\n", 20 | "* Efficient collections with vectorized operations\n", 21 | "* An integrated and natural Linear Algebra API\n", 22 | "* A C API for connecting NumPy with libraries written in C, C++, or FORTRAN.\n", 23 | "\n", 24 | "Let's develop on efficiency. In Python, **everything is an object**, which means that even simple ints are also objects, with all the required machinery to make object work. We call them \"Boxed Ints\". In contrast, NumPy uses primitive numeric types (floats, ints) which makes storing and computation efficient." 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "metadata": {}, 30 | "source": [ 31 | "\n" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", 39 | "\n", 40 | "## Hands on! " 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 1, 46 | "metadata": {}, 47 | "outputs": [], 48 | "source": [ 49 | "import sys\n", 50 | "import numpy as np" 51 | ] 52 | }, 53 | { 54 | "cell_type": "markdown", 55 | "metadata": {}, 56 | "source": [ 57 | "## Basic Numpy Arrays" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 2, 63 | "metadata": {}, 64 | "outputs": [ 65 | { 66 | "data": { 67 | "text/plain": [ 68 | "array([1, 2, 3, 4])" 69 | ] 70 | }, 71 | "execution_count": 2, 72 | "metadata": {}, 73 | "output_type": "execute_result" 74 | } 75 | ], 76 | "source": [ 77 | "np.array([1, 2, 3, 4])" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 3, 83 | "metadata": {}, 84 | "outputs": [], 85 | "source": [ 86 | "a = np.array([1, 2, 3, 4])" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": 4, 92 | "metadata": {}, 93 | "outputs": [], 94 | "source": [ 95 | "b = np.array([0, .5, 1, 1.5, 2])" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": 5, 101 | "metadata": {}, 102 | "outputs": [ 103 | { 104 | "data": { 105 | "text/plain": [ 106 | "(1, 2)" 107 | ] 108 | }, 109 | "execution_count": 5, 110 | "metadata": {}, 111 | "output_type": "execute_result" 112 | } 113 | ], 114 | "source": [ 115 | "a[0], a[1]" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": 6, 121 | "metadata": {}, 122 | "outputs": [ 123 | { 124 | "data": { 125 | "text/plain": [ 126 | "array([1, 2, 3, 4])" 127 | ] 128 | }, 129 | "execution_count": 6, 130 | "metadata": {}, 131 | "output_type": "execute_result" 132 | } 133 | ], 134 | "source": [ 135 | "a[0:]" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": 7, 141 | "metadata": {}, 142 | "outputs": [ 143 | { 144 | "data": { 145 | "text/plain": [ 146 | "array([2, 3])" 147 | ] 148 | }, 149 | "execution_count": 7, 150 | "metadata": {}, 151 | "output_type": "execute_result" 152 | } 153 | ], 154 | "source": [ 155 | "a[1:3]" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": 8, 161 | "metadata": { 162 | "scrolled": true 163 | }, 164 | "outputs": [ 165 | { 166 | "data": { 167 | "text/plain": [ 168 | "array([2, 3])" 169 | ] 170 | }, 171 | "execution_count": 8, 172 | "metadata": {}, 173 | "output_type": "execute_result" 174 | } 175 | ], 176 | "source": [ 177 | "a[1:-1]" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": 9, 183 | "metadata": {}, 184 | "outputs": [ 185 | { 186 | "data": { 187 | "text/plain": [ 188 | "array([1, 3])" 189 | ] 190 | }, 191 | "execution_count": 9, 192 | "metadata": {}, 193 | "output_type": "execute_result" 194 | } 195 | ], 196 | "source": [ 197 | "a[::2]" 198 | ] 199 | }, 200 | { 201 | "cell_type": "code", 202 | "execution_count": 10, 203 | "metadata": {}, 204 | "outputs": [ 205 | { 206 | "data": { 207 | "text/plain": [ 208 | "array([0. , 0.5, 1. , 1.5, 2. ])" 209 | ] 210 | }, 211 | "execution_count": 10, 212 | "metadata": {}, 213 | "output_type": "execute_result" 214 | } 215 | ], 216 | "source": [ 217 | "b" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": 11, 223 | "metadata": { 224 | "scrolled": true 225 | }, 226 | "outputs": [ 227 | { 228 | "data": { 229 | "text/plain": [ 230 | "(0.0, 1.0, 2.0)" 231 | ] 232 | }, 233 | "execution_count": 11, 234 | "metadata": {}, 235 | "output_type": "execute_result" 236 | } 237 | ], 238 | "source": [ 239 | "b[0], b[2], b[-1]" 240 | ] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "execution_count": 12, 245 | "metadata": {}, 246 | "outputs": [ 247 | { 248 | "data": { 249 | "text/plain": [ 250 | "array([0., 1., 2.])" 251 | ] 252 | }, 253 | "execution_count": 12, 254 | "metadata": {}, 255 | "output_type": "execute_result" 256 | } 257 | ], 258 | "source": [ 259 | "b[[0, 2, -1]]" 260 | ] 261 | }, 262 | { 263 | "cell_type": "markdown", 264 | "metadata": {}, 265 | "source": [ 266 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 267 | "\n", 268 | "## Array Types" 269 | ] 270 | }, 271 | { 272 | "cell_type": "code", 273 | "execution_count": 13, 274 | "metadata": {}, 275 | "outputs": [ 276 | { 277 | "data": { 278 | "text/plain": [ 279 | "array([1, 2, 3, 4])" 280 | ] 281 | }, 282 | "execution_count": 13, 283 | "metadata": {}, 284 | "output_type": "execute_result" 285 | } 286 | ], 287 | "source": [ 288 | "a" 289 | ] 290 | }, 291 | { 292 | "cell_type": "code", 293 | "execution_count": 14, 294 | "metadata": {}, 295 | "outputs": [ 296 | { 297 | "data": { 298 | "text/plain": [ 299 | "dtype('int64')" 300 | ] 301 | }, 302 | "execution_count": 14, 303 | "metadata": {}, 304 | "output_type": "execute_result" 305 | } 306 | ], 307 | "source": [ 308 | "a.dtype" 309 | ] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "execution_count": 15, 314 | "metadata": {}, 315 | "outputs": [ 316 | { 317 | "data": { 318 | "text/plain": [ 319 | "array([0. , 0.5, 1. , 1.5, 2. ])" 320 | ] 321 | }, 322 | "execution_count": 15, 323 | "metadata": {}, 324 | "output_type": "execute_result" 325 | } 326 | ], 327 | "source": [ 328 | "b" 329 | ] 330 | }, 331 | { 332 | "cell_type": "code", 333 | "execution_count": 16, 334 | "metadata": {}, 335 | "outputs": [ 336 | { 337 | "data": { 338 | "text/plain": [ 339 | "dtype('float64')" 340 | ] 341 | }, 342 | "execution_count": 16, 343 | "metadata": {}, 344 | "output_type": "execute_result" 345 | } 346 | ], 347 | "source": [ 348 | "b.dtype" 349 | ] 350 | }, 351 | { 352 | "cell_type": "code", 353 | "execution_count": 17, 354 | "metadata": { 355 | "scrolled": true 356 | }, 357 | "outputs": [ 358 | { 359 | "data": { 360 | "text/plain": [ 361 | "array([1., 2., 3., 4.])" 362 | ] 363 | }, 364 | "execution_count": 17, 365 | "metadata": {}, 366 | "output_type": "execute_result" 367 | } 368 | ], 369 | "source": [ 370 | "np.array([1, 2, 3, 4], dtype=np.float)" 371 | ] 372 | }, 373 | { 374 | "cell_type": "code", 375 | "execution_count": 18, 376 | "metadata": { 377 | "scrolled": true 378 | }, 379 | "outputs": [ 380 | { 381 | "data": { 382 | "text/plain": [ 383 | "array([1, 2, 3, 4], dtype=int8)" 384 | ] 385 | }, 386 | "execution_count": 18, 387 | "metadata": {}, 388 | "output_type": "execute_result" 389 | } 390 | ], 391 | "source": [ 392 | "np.array([1, 2, 3, 4], dtype=np.int8)" 393 | ] 394 | }, 395 | { 396 | "cell_type": "code", 397 | "execution_count": 19, 398 | "metadata": {}, 399 | "outputs": [], 400 | "source": [ 401 | "c = np.array(['a', 'b', 'c'])" 402 | ] 403 | }, 404 | { 405 | "cell_type": "code", 406 | "execution_count": 20, 407 | "metadata": {}, 408 | "outputs": [ 409 | { 410 | "data": { 411 | "text/plain": [ 412 | "dtype('= 2" 1483 | ] 1484 | }, 1485 | { 1486 | "cell_type": "code", 1487 | "execution_count": null, 1488 | "metadata": {}, 1489 | "outputs": [], 1490 | "source": [ 1491 | "a[a >= 2]" 1492 | ] 1493 | }, 1494 | { 1495 | "cell_type": "code", 1496 | "execution_count": null, 1497 | "metadata": {}, 1498 | "outputs": [], 1499 | "source": [ 1500 | "a.mean()" 1501 | ] 1502 | }, 1503 | { 1504 | "cell_type": "code", 1505 | "execution_count": null, 1506 | "metadata": { 1507 | "scrolled": true 1508 | }, 1509 | "outputs": [], 1510 | "source": [ 1511 | "a[a > a.mean()]" 1512 | ] 1513 | }, 1514 | { 1515 | "cell_type": "code", 1516 | "execution_count": null, 1517 | "metadata": {}, 1518 | "outputs": [], 1519 | "source": [ 1520 | "a[~(a > a.mean())]" 1521 | ] 1522 | }, 1523 | { 1524 | "cell_type": "code", 1525 | "execution_count": null, 1526 | "metadata": {}, 1527 | "outputs": [], 1528 | "source": [ 1529 | "a[(a == 0) | (a == 1)]" 1530 | ] 1531 | }, 1532 | { 1533 | "cell_type": "code", 1534 | "execution_count": null, 1535 | "metadata": {}, 1536 | "outputs": [], 1537 | "source": [ 1538 | "a[(a <= 2) & (a % 2 == 0)]" 1539 | ] 1540 | }, 1541 | { 1542 | "cell_type": "code", 1543 | "execution_count": null, 1544 | "metadata": {}, 1545 | "outputs": [], 1546 | "source": [ 1547 | "A = np.random.randint(100, size=(3, 3))" 1548 | ] 1549 | }, 1550 | { 1551 | "cell_type": "code", 1552 | "execution_count": null, 1553 | "metadata": {}, 1554 | "outputs": [], 1555 | "source": [ 1556 | "A" 1557 | ] 1558 | }, 1559 | { 1560 | "cell_type": "code", 1561 | "execution_count": null, 1562 | "metadata": {}, 1563 | "outputs": [], 1564 | "source": [ 1565 | "A[np.array([\n", 1566 | " [True, False, True],\n", 1567 | " [False, True, False],\n", 1568 | " [True, False, True]\n", 1569 | "])]" 1570 | ] 1571 | }, 1572 | { 1573 | "cell_type": "code", 1574 | "execution_count": null, 1575 | "metadata": {}, 1576 | "outputs": [], 1577 | "source": [ 1578 | "A > 30" 1579 | ] 1580 | }, 1581 | { 1582 | "cell_type": "code", 1583 | "execution_count": null, 1584 | "metadata": {}, 1585 | "outputs": [], 1586 | "source": [ 1587 | "A[A > 30]" 1588 | ] 1589 | }, 1590 | { 1591 | "cell_type": "markdown", 1592 | "metadata": {}, 1593 | "source": [ 1594 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1595 | "\n", 1596 | "## Linear Algebra" 1597 | ] 1598 | }, 1599 | { 1600 | "cell_type": "code", 1601 | "execution_count": null, 1602 | "metadata": {}, 1603 | "outputs": [], 1604 | "source": [ 1605 | "A = np.array([\n", 1606 | " [1, 2, 3],\n", 1607 | " [4, 5, 6],\n", 1608 | " [7, 8, 9]\n", 1609 | "])" 1610 | ] 1611 | }, 1612 | { 1613 | "cell_type": "code", 1614 | "execution_count": null, 1615 | "metadata": {}, 1616 | "outputs": [], 1617 | "source": [ 1618 | "B = np.array([\n", 1619 | " [6, 5],\n", 1620 | " [4, 3],\n", 1621 | " [2, 1]\n", 1622 | "])" 1623 | ] 1624 | }, 1625 | { 1626 | "cell_type": "code", 1627 | "execution_count": null, 1628 | "metadata": {}, 1629 | "outputs": [], 1630 | "source": [ 1631 | "A.dot(B)" 1632 | ] 1633 | }, 1634 | { 1635 | "cell_type": "code", 1636 | "execution_count": null, 1637 | "metadata": {}, 1638 | "outputs": [], 1639 | "source": [ 1640 | "A @ B" 1641 | ] 1642 | }, 1643 | { 1644 | "cell_type": "code", 1645 | "execution_count": null, 1646 | "metadata": {}, 1647 | "outputs": [], 1648 | "source": [ 1649 | "B.T" 1650 | ] 1651 | }, 1652 | { 1653 | "cell_type": "code", 1654 | "execution_count": null, 1655 | "metadata": {}, 1656 | "outputs": [], 1657 | "source": [ 1658 | "A" 1659 | ] 1660 | }, 1661 | { 1662 | "cell_type": "code", 1663 | "execution_count": null, 1664 | "metadata": { 1665 | "scrolled": true 1666 | }, 1667 | "outputs": [], 1668 | "source": [ 1669 | "B.T @ A" 1670 | ] 1671 | }, 1672 | { 1673 | "cell_type": "markdown", 1674 | "metadata": {}, 1675 | "source": [ 1676 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1677 | "\n", 1678 | "## Size of objects in Memory" 1679 | ] 1680 | }, 1681 | { 1682 | "cell_type": "markdown", 1683 | "metadata": {}, 1684 | "source": [ 1685 | "### Int, floats" 1686 | ] 1687 | }, 1688 | { 1689 | "cell_type": "code", 1690 | "execution_count": null, 1691 | "metadata": {}, 1692 | "outputs": [], 1693 | "source": [ 1694 | "# An integer in Python is > 24bytes\n", 1695 | "sys.getsizeof(1)" 1696 | ] 1697 | }, 1698 | { 1699 | "cell_type": "code", 1700 | "execution_count": null, 1701 | "metadata": { 1702 | "scrolled": true 1703 | }, 1704 | "outputs": [], 1705 | "source": [ 1706 | "# Longs are even larger\n", 1707 | "sys.getsizeof(10**100)" 1708 | ] 1709 | }, 1710 | { 1711 | "cell_type": "code", 1712 | "execution_count": null, 1713 | "metadata": { 1714 | "scrolled": true 1715 | }, 1716 | "outputs": [], 1717 | "source": [ 1718 | "# Numpy size is much smaller\n", 1719 | "np.dtype(int).itemsize" 1720 | ] 1721 | }, 1722 | { 1723 | "cell_type": "code", 1724 | "execution_count": null, 1725 | "metadata": { 1726 | "scrolled": true 1727 | }, 1728 | "outputs": [], 1729 | "source": [ 1730 | "np.dtype(float).itemsize" 1731 | ] 1732 | }, 1733 | { 1734 | "cell_type": "markdown", 1735 | "metadata": {}, 1736 | "source": [ 1737 | "### Lists are even larger" 1738 | ] 1739 | }, 1740 | { 1741 | "cell_type": "code", 1742 | "execution_count": null, 1743 | "metadata": {}, 1744 | "outputs": [], 1745 | "source": [ 1746 | "# A one-element list\n", 1747 | "sys.getsizeof([1])" 1748 | ] 1749 | }, 1750 | { 1751 | "cell_type": "code", 1752 | "execution_count": null, 1753 | "metadata": { 1754 | "scrolled": true 1755 | }, 1756 | "outputs": [], 1757 | "source": [ 1758 | "# An array of one element in numpy\n", 1759 | "np.array([1]).nbytes" 1760 | ] 1761 | }, 1762 | { 1763 | "cell_type": "markdown", 1764 | "metadata": {}, 1765 | "source": [ 1766 | "### And performance is also important" 1767 | ] 1768 | }, 1769 | { 1770 | "cell_type": "code", 1771 | "execution_count": null, 1772 | "metadata": {}, 1773 | "outputs": [], 1774 | "source": [ 1775 | "l = list(range(1000))" 1776 | ] 1777 | }, 1778 | { 1779 | "cell_type": "code", 1780 | "execution_count": null, 1781 | "metadata": {}, 1782 | "outputs": [], 1783 | "source": [ 1784 | "a = np.arange(1000)" 1785 | ] 1786 | }, 1787 | { 1788 | "cell_type": "code", 1789 | "execution_count": null, 1790 | "metadata": { 1791 | "scrolled": true 1792 | }, 1793 | "outputs": [], 1794 | "source": [ 1795 | "%time np.sum(a ** 2)" 1796 | ] 1797 | }, 1798 | { 1799 | "cell_type": "code", 1800 | "execution_count": null, 1801 | "metadata": { 1802 | "scrolled": true 1803 | }, 1804 | "outputs": [], 1805 | "source": [ 1806 | "%time sum([x ** 2 for x in l])" 1807 | ] 1808 | }, 1809 | { 1810 | "cell_type": "markdown", 1811 | "metadata": {}, 1812 | "source": [ 1813 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1814 | "\n", 1815 | "## Useful Numpy functions" 1816 | ] 1817 | }, 1818 | { 1819 | "cell_type": "markdown", 1820 | "metadata": {}, 1821 | "source": [ 1822 | "### `random` " 1823 | ] 1824 | }, 1825 | { 1826 | "cell_type": "code", 1827 | "execution_count": null, 1828 | "metadata": {}, 1829 | "outputs": [], 1830 | "source": [ 1831 | "np.random.random(size=2)" 1832 | ] 1833 | }, 1834 | { 1835 | "cell_type": "code", 1836 | "execution_count": null, 1837 | "metadata": {}, 1838 | "outputs": [], 1839 | "source": [ 1840 | "np.random.normal(size=2)" 1841 | ] 1842 | }, 1843 | { 1844 | "cell_type": "code", 1845 | "execution_count": null, 1846 | "metadata": {}, 1847 | "outputs": [], 1848 | "source": [ 1849 | "np.random.rand(2, 4)" 1850 | ] 1851 | }, 1852 | { 1853 | "cell_type": "markdown", 1854 | "metadata": {}, 1855 | "source": [ 1856 | "---\n", 1857 | "### `arange`" 1858 | ] 1859 | }, 1860 | { 1861 | "cell_type": "code", 1862 | "execution_count": null, 1863 | "metadata": {}, 1864 | "outputs": [], 1865 | "source": [ 1866 | "np.arange(10)" 1867 | ] 1868 | }, 1869 | { 1870 | "cell_type": "code", 1871 | "execution_count": null, 1872 | "metadata": {}, 1873 | "outputs": [], 1874 | "source": [ 1875 | "np.arange(5, 10)" 1876 | ] 1877 | }, 1878 | { 1879 | "cell_type": "code", 1880 | "execution_count": null, 1881 | "metadata": {}, 1882 | "outputs": [], 1883 | "source": [ 1884 | "np.arange(0, 1, .1)" 1885 | ] 1886 | }, 1887 | { 1888 | "cell_type": "markdown", 1889 | "metadata": {}, 1890 | "source": [ 1891 | "---\n", 1892 | "### `reshape`" 1893 | ] 1894 | }, 1895 | { 1896 | "cell_type": "code", 1897 | "execution_count": null, 1898 | "metadata": {}, 1899 | "outputs": [], 1900 | "source": [ 1901 | "np.arange(10).reshape(2, 5)" 1902 | ] 1903 | }, 1904 | { 1905 | "cell_type": "code", 1906 | "execution_count": null, 1907 | "metadata": {}, 1908 | "outputs": [], 1909 | "source": [ 1910 | "np.arange(10).reshape(5, 2)" 1911 | ] 1912 | }, 1913 | { 1914 | "cell_type": "markdown", 1915 | "metadata": {}, 1916 | "source": [ 1917 | "---\n", 1918 | "### `linspace`" 1919 | ] 1920 | }, 1921 | { 1922 | "cell_type": "code", 1923 | "execution_count": null, 1924 | "metadata": {}, 1925 | "outputs": [], 1926 | "source": [ 1927 | "np.linspace(0, 1, 5)" 1928 | ] 1929 | }, 1930 | { 1931 | "cell_type": "code", 1932 | "execution_count": null, 1933 | "metadata": {}, 1934 | "outputs": [], 1935 | "source": [ 1936 | "np.linspace(0, 1, 20)" 1937 | ] 1938 | }, 1939 | { 1940 | "cell_type": "code", 1941 | "execution_count": null, 1942 | "metadata": { 1943 | "scrolled": true 1944 | }, 1945 | "outputs": [], 1946 | "source": [ 1947 | "np.linspace(0, 1, 20, False)" 1948 | ] 1949 | }, 1950 | { 1951 | "cell_type": "markdown", 1952 | "metadata": {}, 1953 | "source": [ 1954 | "---\n", 1955 | "### `zeros`, `ones`, `empty`" 1956 | ] 1957 | }, 1958 | { 1959 | "cell_type": "code", 1960 | "execution_count": null, 1961 | "metadata": {}, 1962 | "outputs": [], 1963 | "source": [ 1964 | "np.zeros(5)" 1965 | ] 1966 | }, 1967 | { 1968 | "cell_type": "code", 1969 | "execution_count": null, 1970 | "metadata": { 1971 | "scrolled": true 1972 | }, 1973 | "outputs": [], 1974 | "source": [ 1975 | "np.zeros((3, 3))" 1976 | ] 1977 | }, 1978 | { 1979 | "cell_type": "code", 1980 | "execution_count": null, 1981 | "metadata": { 1982 | "scrolled": true 1983 | }, 1984 | "outputs": [], 1985 | "source": [ 1986 | "np.zeros((3, 3), dtype=np.int)" 1987 | ] 1988 | }, 1989 | { 1990 | "cell_type": "code", 1991 | "execution_count": null, 1992 | "metadata": {}, 1993 | "outputs": [], 1994 | "source": [ 1995 | "np.ones(5)" 1996 | ] 1997 | }, 1998 | { 1999 | "cell_type": "code", 2000 | "execution_count": null, 2001 | "metadata": {}, 2002 | "outputs": [], 2003 | "source": [ 2004 | "np.ones((3, 3))" 2005 | ] 2006 | }, 2007 | { 2008 | "cell_type": "code", 2009 | "execution_count": null, 2010 | "metadata": {}, 2011 | "outputs": [], 2012 | "source": [ 2013 | "np.empty(5)" 2014 | ] 2015 | }, 2016 | { 2017 | "cell_type": "code", 2018 | "execution_count": null, 2019 | "metadata": { 2020 | "scrolled": true 2021 | }, 2022 | "outputs": [], 2023 | "source": [ 2024 | "np.empty((2, 2))" 2025 | ] 2026 | }, 2027 | { 2028 | "cell_type": "markdown", 2029 | "metadata": {}, 2030 | "source": [ 2031 | "---\n", 2032 | "### `identity` and `eye`" 2033 | ] 2034 | }, 2035 | { 2036 | "cell_type": "code", 2037 | "execution_count": null, 2038 | "metadata": {}, 2039 | "outputs": [], 2040 | "source": [ 2041 | "np.identity(3)" 2042 | ] 2043 | }, 2044 | { 2045 | "cell_type": "code", 2046 | "execution_count": null, 2047 | "metadata": {}, 2048 | "outputs": [], 2049 | "source": [ 2050 | "np.eye(3, 3)" 2051 | ] 2052 | }, 2053 | { 2054 | "cell_type": "code", 2055 | "execution_count": null, 2056 | "metadata": {}, 2057 | "outputs": [], 2058 | "source": [ 2059 | "np.eye(8, 4)" 2060 | ] 2061 | }, 2062 | { 2063 | "cell_type": "code", 2064 | "execution_count": null, 2065 | "metadata": { 2066 | "scrolled": true 2067 | }, 2068 | "outputs": [], 2069 | "source": [ 2070 | "np.eye(8, 4, k=1)" 2071 | ] 2072 | }, 2073 | { 2074 | "cell_type": "code", 2075 | "execution_count": null, 2076 | "metadata": {}, 2077 | "outputs": [], 2078 | "source": [ 2079 | "np.eye(8, 4, k=-3)" 2080 | ] 2081 | }, 2082 | { 2083 | "cell_type": "code", 2084 | "execution_count": null, 2085 | "metadata": { 2086 | "scrolled": true 2087 | }, 2088 | "outputs": [], 2089 | "source": [ 2090 | "\"Hello World\"[6]" 2091 | ] 2092 | }, 2093 | { 2094 | "cell_type": "markdown", 2095 | "metadata": {}, 2096 | "source": [ 2097 | "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)" 2098 | ] 2099 | } 2100 | ], 2101 | "metadata": { 2102 | "kernelspec": { 2103 | "display_name": "Python 3", 2104 | "language": "python", 2105 | "name": "python3" 2106 | }, 2107 | "language_info": { 2108 | "codemirror_mode": { 2109 | "name": "ipython", 2110 | "version": 3 2111 | }, 2112 | "file_extension": ".py", 2113 | "mimetype": "text/x-python", 2114 | "name": "python", 2115 | "nbconvert_exporter": "python", 2116 | "pygments_lexer": "ipython3", 2117 | "version": "3.8.1" 2118 | } 2119 | }, 2120 | "nbformat": 4, 2121 | "nbformat_minor": 4 2122 | } 2123 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/3. NumPy exercises-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "![rmotr](https://user-images.githubusercontent.com/7065401/52071918-bda15380-2562-11e9-828c-7f95297e4a82.png)\n", 8 | "
\n", 9 | "\n", 10 | "# NumPy exercises\n" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": null, 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "# Import the numpy package under the name np\n", 20 | "import numpy as np\n", 21 | "\n", 22 | "# Print the numpy version and the configuration\n", 23 | "print(np.__version__)" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", 31 | "\n", 32 | "## Array creation" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "metadata": {}, 38 | "source": [ 39 | "### Create a numpy array of size 10, filled with zeros." 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": null, 45 | "metadata": {}, 46 | "outputs": [], 47 | "source": [ 48 | "# your code goes here\n" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": null, 54 | "metadata": { 55 | "cell_type": "solution" 56 | }, 57 | "outputs": [], 58 | "source": [ 59 | "#np.array([0] * 10)\n", 60 | "np.zeros(10)" 61 | ] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "metadata": {}, 66 | "source": [ 67 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 68 | "\n", 69 | "### Create a numpy array with values ranging from 10 to 49" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": null, 75 | "metadata": {}, 76 | "outputs": [], 77 | "source": [ 78 | "# your code goes here\n" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": null, 84 | "metadata": { 85 | "cell_type": "solution" 86 | }, 87 | "outputs": [], 88 | "source": [ 89 | "np.arange(10,50)" 90 | ] 91 | }, 92 | { 93 | "cell_type": "markdown", 94 | "metadata": {}, 95 | "source": [ 96 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 97 | "\n", 98 | "### Create a numpy matrix of 2*2 integers, filled with ones." 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": null, 104 | "metadata": {}, 105 | "outputs": [], 106 | "source": [ 107 | "# your code goes here\n" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": null, 113 | "metadata": { 114 | "cell_type": "solution" 115 | }, 116 | "outputs": [], 117 | "source": [ 118 | "np.ones([2,2], dtype=np.int)" 119 | ] 120 | }, 121 | { 122 | "cell_type": "markdown", 123 | "metadata": {}, 124 | "source": [ 125 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 126 | "\n", 127 | "### Create a numpy matrix of 3*2 float numbers, filled with ones." 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": null, 133 | "metadata": {}, 134 | "outputs": [], 135 | "source": [ 136 | "# your code goes here\n" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": null, 142 | "metadata": { 143 | "cell_type": "solution" 144 | }, 145 | "outputs": [], 146 | "source": [ 147 | "np.ones([3,2], dtype=np.float)" 148 | ] 149 | }, 150 | { 151 | "cell_type": "markdown", 152 | "metadata": {}, 153 | "source": [ 154 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 155 | "\n", 156 | "### Given the X numpy array, create a new numpy array with the same shape and type as X, filled with ones." 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": null, 162 | "metadata": {}, 163 | "outputs": [], 164 | "source": [ 165 | "# your code goes here\n" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": null, 171 | "metadata": { 172 | "cell_type": "solution" 173 | }, 174 | "outputs": [], 175 | "source": [ 176 | "X = np.arange(4, dtype=np.int)\n", 177 | "\n", 178 | "np.ones_like(X)" 179 | ] 180 | }, 181 | { 182 | "cell_type": "markdown", 183 | "metadata": {}, 184 | "source": [ 185 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 186 | "\n", 187 | "### Given the X numpy matrix, create a new numpy matrix with the same shape and type as X, filled with zeros." 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": null, 193 | "metadata": {}, 194 | "outputs": [], 195 | "source": [ 196 | "# your code goes here\n" 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": null, 202 | "metadata": { 203 | "cell_type": "solution" 204 | }, 205 | "outputs": [], 206 | "source": [ 207 | "X = np.array([[1,2,3], [4,5,6]], dtype=np.int)\n", 208 | "\n", 209 | "np.zeros_like(X)" 210 | ] 211 | }, 212 | { 213 | "cell_type": "markdown", 214 | "metadata": {}, 215 | "source": [ 216 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 217 | "\n", 218 | "### Create a numpy matrix of 4*4 integers, filled with fives." 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": null, 224 | "metadata": {}, 225 | "outputs": [], 226 | "source": [ 227 | "# your code goes here\n" 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": null, 233 | "metadata": { 234 | "cell_type": "solution" 235 | }, 236 | "outputs": [], 237 | "source": [ 238 | "np.ones([4,4], dtype=np.int) * 5" 239 | ] 240 | }, 241 | { 242 | "cell_type": "markdown", 243 | "metadata": {}, 244 | "source": [ 245 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 246 | "\n", 247 | "### Given the X numpy matrix, create a new numpy matrix with the same shape and type as X, filled with sevens." 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": null, 253 | "metadata": {}, 254 | "outputs": [], 255 | "source": [ 256 | "# your code goes here\n" 257 | ] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "execution_count": null, 262 | "metadata": { 263 | "cell_type": "solution" 264 | }, 265 | "outputs": [], 266 | "source": [ 267 | "X = np.array([[2,3], [6,2]], dtype=np.int)\n", 268 | "\n", 269 | "np.ones_like(X) * 7" 270 | ] 271 | }, 272 | { 273 | "cell_type": "markdown", 274 | "metadata": {}, 275 | "source": [ 276 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 277 | "\n", 278 | "### Create a 3*3 identity numpy matrix with ones on the diagonal and zeros elsewhere." 279 | ] 280 | }, 281 | { 282 | "cell_type": "code", 283 | "execution_count": null, 284 | "metadata": {}, 285 | "outputs": [], 286 | "source": [ 287 | "# your code goes here" 288 | ] 289 | }, 290 | { 291 | "cell_type": "code", 292 | "execution_count": null, 293 | "metadata": { 294 | "cell_type": "solution" 295 | }, 296 | "outputs": [], 297 | "source": [ 298 | "#np.eye(3)\n", 299 | "np.identity(3)" 300 | ] 301 | }, 302 | { 303 | "cell_type": "markdown", 304 | "metadata": {}, 305 | "source": [ 306 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 307 | "\n", 308 | "### Create a numpy array, filled with 3 random integer values between 1 and 10." 309 | ] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "execution_count": null, 314 | "metadata": {}, 315 | "outputs": [], 316 | "source": [ 317 | "# your code goes here\n" 318 | ] 319 | }, 320 | { 321 | "cell_type": "code", 322 | "execution_count": null, 323 | "metadata": { 324 | "cell_type": "solution" 325 | }, 326 | "outputs": [], 327 | "source": [ 328 | "np.random.randint(10, size=3)" 329 | ] 330 | }, 331 | { 332 | "cell_type": "markdown", 333 | "metadata": {}, 334 | "source": [ 335 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 336 | "\n", 337 | "### Create a 3\\*3\\*3 numpy matrix, filled with random float values." 338 | ] 339 | }, 340 | { 341 | "cell_type": "code", 342 | "execution_count": null, 343 | "metadata": {}, 344 | "outputs": [], 345 | "source": [ 346 | "# your code goes here\n" 347 | ] 348 | }, 349 | { 350 | "cell_type": "code", 351 | "execution_count": null, 352 | "metadata": { 353 | "cell_type": "solution" 354 | }, 355 | "outputs": [], 356 | "source": [ 357 | "#np.random.random((3,3,3)) \n", 358 | "np.random.randn(3,3,3) # 0 to 1 floats" 359 | ] 360 | }, 361 | { 362 | "cell_type": "markdown", 363 | "metadata": {}, 364 | "source": [ 365 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 366 | "\n", 367 | "### Given the X python list convert it to an Y numpy array" 368 | ] 369 | }, 370 | { 371 | "cell_type": "code", 372 | "execution_count": null, 373 | "metadata": {}, 374 | "outputs": [], 375 | "source": [ 376 | "# your code goes here\n" 377 | ] 378 | }, 379 | { 380 | "cell_type": "code", 381 | "execution_count": null, 382 | "metadata": { 383 | "cell_type": "solution" 384 | }, 385 | "outputs": [], 386 | "source": [ 387 | "X = [1, 2, 3]\n", 388 | "print(X, type(X))\n", 389 | "\n", 390 | "Y = np.array(X)\n", 391 | "print(Y, type(Y)) # different type" 392 | ] 393 | }, 394 | { 395 | "cell_type": "markdown", 396 | "metadata": {}, 397 | "source": [ 398 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 399 | "\n", 400 | "### Given the X numpy array, make a copy and store it on Y." 401 | ] 402 | }, 403 | { 404 | "cell_type": "code", 405 | "execution_count": null, 406 | "metadata": {}, 407 | "outputs": [], 408 | "source": [ 409 | "# your code goes here\n" 410 | ] 411 | }, 412 | { 413 | "cell_type": "code", 414 | "execution_count": null, 415 | "metadata": { 416 | "cell_type": "solution" 417 | }, 418 | "outputs": [], 419 | "source": [ 420 | "X = np.array([5,2,3], dtype=np.int)\n", 421 | "print(X, id(X))\n", 422 | "\n", 423 | "Y = np.copy(X)\n", 424 | "print(Y, id(Y)) # different id" 425 | ] 426 | }, 427 | { 428 | "cell_type": "markdown", 429 | "metadata": {}, 430 | "source": [ 431 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 432 | "\n", 433 | "### Create a numpy array with numbers from 1 to 10" 434 | ] 435 | }, 436 | { 437 | "cell_type": "code", 438 | "execution_count": null, 439 | "metadata": {}, 440 | "outputs": [], 441 | "source": [ 442 | "# your code goes here\n" 443 | ] 444 | }, 445 | { 446 | "cell_type": "code", 447 | "execution_count": null, 448 | "metadata": { 449 | "cell_type": "solution" 450 | }, 451 | "outputs": [], 452 | "source": [ 453 | "np.arange(1, 11)" 454 | ] 455 | }, 456 | { 457 | "cell_type": "markdown", 458 | "metadata": {}, 459 | "source": [ 460 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 461 | "\n", 462 | "### Create a numpy array with the odd numbers between 1 to 10" 463 | ] 464 | }, 465 | { 466 | "cell_type": "code", 467 | "execution_count": null, 468 | "metadata": {}, 469 | "outputs": [], 470 | "source": [ 471 | "# your code goes here\n" 472 | ] 473 | }, 474 | { 475 | "cell_type": "code", 476 | "execution_count": null, 477 | "metadata": { 478 | "cell_type": "solution" 479 | }, 480 | "outputs": [], 481 | "source": [ 482 | "np.arange(1, 11, 2)" 483 | ] 484 | }, 485 | { 486 | "cell_type": "markdown", 487 | "metadata": {}, 488 | "source": [ 489 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 490 | "\n", 491 | "### Create a numpy array with numbers from 1 to 10, in descending order." 492 | ] 493 | }, 494 | { 495 | "cell_type": "code", 496 | "execution_count": null, 497 | "metadata": {}, 498 | "outputs": [], 499 | "source": [ 500 | "# your code goes here\n" 501 | ] 502 | }, 503 | { 504 | "cell_type": "code", 505 | "execution_count": null, 506 | "metadata": { 507 | "cell_type": "solution" 508 | }, 509 | "outputs": [], 510 | "source": [ 511 | "np.arange(1, 11)[::-1]" 512 | ] 513 | }, 514 | { 515 | "cell_type": "markdown", 516 | "metadata": {}, 517 | "source": [ 518 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 519 | "\n", 520 | "### Create a 3*3 numpy matrix, filled with values ranging from 0 to 8" 521 | ] 522 | }, 523 | { 524 | "cell_type": "code", 525 | "execution_count": null, 526 | "metadata": {}, 527 | "outputs": [], 528 | "source": [ 529 | "# your code goes here\n" 530 | ] 531 | }, 532 | { 533 | "cell_type": "code", 534 | "execution_count": null, 535 | "metadata": { 536 | "cell_type": "solution" 537 | }, 538 | "outputs": [], 539 | "source": [ 540 | "np.arange(9).reshape(3,3)" 541 | ] 542 | }, 543 | { 544 | "cell_type": "markdown", 545 | "metadata": {}, 546 | "source": [ 547 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 548 | "\n", 549 | "### Show the memory size of the given Z numpy matrix" 550 | ] 551 | }, 552 | { 553 | "cell_type": "code", 554 | "execution_count": null, 555 | "metadata": {}, 556 | "outputs": [], 557 | "source": [ 558 | "# your code goes here\n" 559 | ] 560 | }, 561 | { 562 | "cell_type": "code", 563 | "execution_count": null, 564 | "metadata": { 565 | "cell_type": "solution", 566 | "scrolled": true 567 | }, 568 | "outputs": [], 569 | "source": [ 570 | "Z = np.zeros((10,10))\n", 571 | "\n", 572 | "print(\"%d bytes\" % (Z.size * Z.itemsize))" 573 | ] 574 | }, 575 | { 576 | "cell_type": "markdown", 577 | "metadata": {}, 578 | "source": [ 579 | "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", 580 | "\n", 581 | "## Array indexation\n" 582 | ] 583 | }, 584 | { 585 | "cell_type": "markdown", 586 | "metadata": {}, 587 | "source": [ 588 | "### Given the X numpy array, show it's first element" 589 | ] 590 | }, 591 | { 592 | "cell_type": "code", 593 | "execution_count": null, 594 | "metadata": {}, 595 | "outputs": [], 596 | "source": [ 597 | "# your code goes here\n" 598 | ] 599 | }, 600 | { 601 | "cell_type": "code", 602 | "execution_count": null, 603 | "metadata": { 604 | "cell_type": "solution" 605 | }, 606 | "outputs": [], 607 | "source": [ 608 | "X = np.array(['A','B','C','D','E'])\n", 609 | "\n", 610 | "X[0]" 611 | ] 612 | }, 613 | { 614 | "cell_type": "markdown", 615 | "metadata": {}, 616 | "source": [ 617 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 618 | "\n", 619 | "### Given the X numpy array, show it's last element" 620 | ] 621 | }, 622 | { 623 | "cell_type": "code", 624 | "execution_count": null, 625 | "metadata": {}, 626 | "outputs": [], 627 | "source": [ 628 | "# your code goes here\n" 629 | ] 630 | }, 631 | { 632 | "cell_type": "code", 633 | "execution_count": null, 634 | "metadata": { 635 | "cell_type": "solution" 636 | }, 637 | "outputs": [], 638 | "source": [ 639 | "X = np.array(['A','B','C','D','E'])\n", 640 | "\n", 641 | "#X[len(X)-1]\n", 642 | "X[-1]" 643 | ] 644 | }, 645 | { 646 | "cell_type": "markdown", 647 | "metadata": {}, 648 | "source": [ 649 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 650 | "\n", 651 | "### Given the X numpy array, show it's first three elements" 652 | ] 653 | }, 654 | { 655 | "cell_type": "code", 656 | "execution_count": null, 657 | "metadata": {}, 658 | "outputs": [], 659 | "source": [ 660 | "# your code goes here\n" 661 | ] 662 | }, 663 | { 664 | "cell_type": "code", 665 | "execution_count": null, 666 | "metadata": { 667 | "cell_type": "solution" 668 | }, 669 | "outputs": [], 670 | "source": [ 671 | "X = np.array(['A','B','C','D','E'])\n", 672 | "\n", 673 | "X[0:3] # remember! elements start at zero index" 674 | ] 675 | }, 676 | { 677 | "cell_type": "markdown", 678 | "metadata": {}, 679 | "source": [ 680 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 681 | "\n", 682 | "### Given the X numpy array, show all middle elements" 683 | ] 684 | }, 685 | { 686 | "cell_type": "code", 687 | "execution_count": null, 688 | "metadata": {}, 689 | "outputs": [], 690 | "source": [ 691 | "# your code goes here\n" 692 | ] 693 | }, 694 | { 695 | "cell_type": "code", 696 | "execution_count": null, 697 | "metadata": { 698 | "cell_type": "solution" 699 | }, 700 | "outputs": [], 701 | "source": [ 702 | "X = np.array(['A','B','C','D','E'])\n", 703 | "\n", 704 | "X[1:-1]" 705 | ] 706 | }, 707 | { 708 | "cell_type": "markdown", 709 | "metadata": {}, 710 | "source": [ 711 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 712 | "\n", 713 | "### Given the X numpy array, show the elements in reverse position" 714 | ] 715 | }, 716 | { 717 | "cell_type": "code", 718 | "execution_count": null, 719 | "metadata": {}, 720 | "outputs": [], 721 | "source": [ 722 | "# your code goes here\n" 723 | ] 724 | }, 725 | { 726 | "cell_type": "code", 727 | "execution_count": null, 728 | "metadata": { 729 | "cell_type": "solution" 730 | }, 731 | "outputs": [], 732 | "source": [ 733 | "X = np.array(['A','B','C','D','E'])\n", 734 | "\n", 735 | "X[::-1]" 736 | ] 737 | }, 738 | { 739 | "cell_type": "markdown", 740 | "metadata": {}, 741 | "source": [ 742 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 743 | "\n", 744 | "### Given the X numpy array, show the elements in an odd position" 745 | ] 746 | }, 747 | { 748 | "cell_type": "code", 749 | "execution_count": null, 750 | "metadata": {}, 751 | "outputs": [], 752 | "source": [ 753 | "# your code goes here\n" 754 | ] 755 | }, 756 | { 757 | "cell_type": "code", 758 | "execution_count": null, 759 | "metadata": { 760 | "cell_type": "solution" 761 | }, 762 | "outputs": [], 763 | "source": [ 764 | "X = np.array(['A','B','C','D','E'])\n", 765 | "\n", 766 | "#X[[0, 2, -1]]\n", 767 | "X[::2]" 768 | ] 769 | }, 770 | { 771 | "cell_type": "markdown", 772 | "metadata": {}, 773 | "source": [ 774 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 775 | "\n", 776 | "### Given the X numpy matrix, show the first row elements" 777 | ] 778 | }, 779 | { 780 | "cell_type": "code", 781 | "execution_count": null, 782 | "metadata": {}, 783 | "outputs": [], 784 | "source": [ 785 | "# your code goes here\n" 786 | ] 787 | }, 788 | { 789 | "cell_type": "code", 790 | "execution_count": null, 791 | "metadata": { 792 | "cell_type": "solution" 793 | }, 794 | "outputs": [], 795 | "source": [ 796 | "X = np.array([\n", 797 | " [1, 2, 3, 4],\n", 798 | " [5, 6, 7, 8],\n", 799 | " [9, 10, 11, 12],\n", 800 | " [13, 14, 15, 16]\n", 801 | "])\n", 802 | "\n", 803 | "X[0]" 804 | ] 805 | }, 806 | { 807 | "cell_type": "markdown", 808 | "metadata": {}, 809 | "source": [ 810 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 811 | "\n", 812 | "### Given the X numpy matrix, show the last row elements" 813 | ] 814 | }, 815 | { 816 | "cell_type": "code", 817 | "execution_count": null, 818 | "metadata": {}, 819 | "outputs": [], 820 | "source": [ 821 | "# your code goes here\n" 822 | ] 823 | }, 824 | { 825 | "cell_type": "code", 826 | "execution_count": null, 827 | "metadata": { 828 | "cell_type": "solution" 829 | }, 830 | "outputs": [], 831 | "source": [ 832 | "X = np.array([\n", 833 | " [1, 2, 3, 4],\n", 834 | " [5, 6, 7, 8],\n", 835 | " [9, 10, 11, 12],\n", 836 | " [13, 14, 15, 16]\n", 837 | "])\n", 838 | "\n", 839 | "X[-1]" 840 | ] 841 | }, 842 | { 843 | "cell_type": "markdown", 844 | "metadata": {}, 845 | "source": [ 846 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 847 | "\n", 848 | "### Given the X numpy matrix, show the first element on first row" 849 | ] 850 | }, 851 | { 852 | "cell_type": "code", 853 | "execution_count": null, 854 | "metadata": {}, 855 | "outputs": [], 856 | "source": [ 857 | "# your code goes here\n" 858 | ] 859 | }, 860 | { 861 | "cell_type": "code", 862 | "execution_count": null, 863 | "metadata": { 864 | "cell_type": "solution" 865 | }, 866 | "outputs": [], 867 | "source": [ 868 | "X = np.array([\n", 869 | " [1, 2, 3, 4],\n", 870 | " [5, 6, 7, 8],\n", 871 | " [9, 10, 11, 12],\n", 872 | " [13, 14, 15, 16]\n", 873 | "])\n", 874 | "\n", 875 | "#X[0][0]\n", 876 | "X[0, 0]" 877 | ] 878 | }, 879 | { 880 | "cell_type": "markdown", 881 | "metadata": {}, 882 | "source": [ 883 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 884 | "\n", 885 | "### Given the X numpy matrix, show the last element on last row" 886 | ] 887 | }, 888 | { 889 | "cell_type": "code", 890 | "execution_count": null, 891 | "metadata": {}, 892 | "outputs": [], 893 | "source": [ 894 | "# your code goes here\n" 895 | ] 896 | }, 897 | { 898 | "cell_type": "code", 899 | "execution_count": null, 900 | "metadata": { 901 | "cell_type": "solution" 902 | }, 903 | "outputs": [], 904 | "source": [ 905 | "X = np.array([\n", 906 | " [1, 2, 3, 4],\n", 907 | " [5, 6, 7, 8],\n", 908 | " [9, 10, 11, 12],\n", 909 | " [13, 14, 15, 16]\n", 910 | "])\n", 911 | "\n", 912 | "#X[-1][-1]\n", 913 | "X[-1, -1]" 914 | ] 915 | }, 916 | { 917 | "cell_type": "markdown", 918 | "metadata": {}, 919 | "source": [ 920 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 921 | "\n", 922 | "### Given the X numpy matrix, show the middle row elements" 923 | ] 924 | }, 925 | { 926 | "cell_type": "code", 927 | "execution_count": null, 928 | "metadata": {}, 929 | "outputs": [], 930 | "source": [ 931 | "# your code goes here\n" 932 | ] 933 | }, 934 | { 935 | "cell_type": "code", 936 | "execution_count": null, 937 | "metadata": { 938 | "cell_type": "solution" 939 | }, 940 | "outputs": [], 941 | "source": [ 942 | "X = np.array([\n", 943 | " [1, 2, 3, 4],\n", 944 | " [5, 6, 7, 8],\n", 945 | " [9, 10, 11, 12],\n", 946 | " [13, 14, 15, 16]\n", 947 | "])\n", 948 | "\n", 949 | "#X[1:-1][1:-1] wrong!\n", 950 | "X[1:-1, 1:-1]" 951 | ] 952 | }, 953 | { 954 | "cell_type": "markdown", 955 | "metadata": {}, 956 | "source": [ 957 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 958 | "\n", 959 | "### Given the X numpy matrix, show the first two elements on the first two rows" 960 | ] 961 | }, 962 | { 963 | "cell_type": "code", 964 | "execution_count": null, 965 | "metadata": {}, 966 | "outputs": [], 967 | "source": [ 968 | "# your code goes here\n" 969 | ] 970 | }, 971 | { 972 | "cell_type": "code", 973 | "execution_count": null, 974 | "metadata": { 975 | "cell_type": "solution" 976 | }, 977 | "outputs": [], 978 | "source": [ 979 | "X = np.array([\n", 980 | " [1, 2, 3, 4],\n", 981 | " [5, 6, 7, 8],\n", 982 | " [9, 10, 11, 12],\n", 983 | " [13, 14, 15, 16]\n", 984 | "])\n", 985 | "\n", 986 | "#X[:2][:2] wrong!\n", 987 | "#X[0:2, 0:2]\n", 988 | "X[:2, :2]" 989 | ] 990 | }, 991 | { 992 | "cell_type": "markdown", 993 | "metadata": {}, 994 | "source": [ 995 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 996 | "\n", 997 | "### Given the X numpy matrix, show the last two elements on the last two rows" 998 | ] 999 | }, 1000 | { 1001 | "cell_type": "code", 1002 | "execution_count": null, 1003 | "metadata": {}, 1004 | "outputs": [], 1005 | "source": [ 1006 | "# your code goes here\n" 1007 | ] 1008 | }, 1009 | { 1010 | "cell_type": "code", 1011 | "execution_count": null, 1012 | "metadata": { 1013 | "cell_type": "solution" 1014 | }, 1015 | "outputs": [], 1016 | "source": [ 1017 | "X = np.array([\n", 1018 | " [1, 2, 3, 4],\n", 1019 | " [5, 6, 7, 8],\n", 1020 | " [9, 10, 11, 12],\n", 1021 | " [13, 14, 15, 16]\n", 1022 | "])\n", 1023 | "\n", 1024 | "X[2:, 2:]" 1025 | ] 1026 | }, 1027 | { 1028 | "cell_type": "markdown", 1029 | "metadata": {}, 1030 | "source": [ 1031 | "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", 1032 | "\n", 1033 | "## Array manipulation\n" 1034 | ] 1035 | }, 1036 | { 1037 | "cell_type": "markdown", 1038 | "metadata": {}, 1039 | "source": [ 1040 | "### Convert the given integer numpy array to float" 1041 | ] 1042 | }, 1043 | { 1044 | "cell_type": "code", 1045 | "execution_count": null, 1046 | "metadata": {}, 1047 | "outputs": [], 1048 | "source": [ 1049 | "# your code goes here\n" 1050 | ] 1051 | }, 1052 | { 1053 | "cell_type": "code", 1054 | "execution_count": null, 1055 | "metadata": { 1056 | "cell_type": "solution" 1057 | }, 1058 | "outputs": [], 1059 | "source": [ 1060 | "X = [-5, -3, 0, 10, 40]\n", 1061 | "\n", 1062 | "np.array(X, np.float)" 1063 | ] 1064 | }, 1065 | { 1066 | "cell_type": "markdown", 1067 | "metadata": {}, 1068 | "source": [ 1069 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1070 | "\n", 1071 | "### Reverse the given numpy array (first element becomes last)" 1072 | ] 1073 | }, 1074 | { 1075 | "cell_type": "code", 1076 | "execution_count": null, 1077 | "metadata": {}, 1078 | "outputs": [], 1079 | "source": [ 1080 | "# your code goes here\n" 1081 | ] 1082 | }, 1083 | { 1084 | "cell_type": "code", 1085 | "execution_count": null, 1086 | "metadata": { 1087 | "cell_type": "solution" 1088 | }, 1089 | "outputs": [], 1090 | "source": [ 1091 | "X = [-5, -3, 0, 10, 40]\n", 1092 | "\n", 1093 | "X[::-1]" 1094 | ] 1095 | }, 1096 | { 1097 | "cell_type": "markdown", 1098 | "metadata": {}, 1099 | "source": [ 1100 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1101 | "\n", 1102 | "### Order (sort) the given numpy array" 1103 | ] 1104 | }, 1105 | { 1106 | "cell_type": "code", 1107 | "execution_count": null, 1108 | "metadata": {}, 1109 | "outputs": [], 1110 | "source": [ 1111 | "# your code goes here\n" 1112 | ] 1113 | }, 1114 | { 1115 | "cell_type": "code", 1116 | "execution_count": null, 1117 | "metadata": { 1118 | "cell_type": "solution" 1119 | }, 1120 | "outputs": [], 1121 | "source": [ 1122 | "X = [0, 10, -5, 40, -3]\n", 1123 | "\n", 1124 | "X.sort()\n", 1125 | "X" 1126 | ] 1127 | }, 1128 | { 1129 | "cell_type": "markdown", 1130 | "metadata": {}, 1131 | "source": [ 1132 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1133 | "\n", 1134 | "### Given the X numpy array, set the fifth element equal to 1" 1135 | ] 1136 | }, 1137 | { 1138 | "cell_type": "code", 1139 | "execution_count": null, 1140 | "metadata": {}, 1141 | "outputs": [], 1142 | "source": [ 1143 | "# your code goes here\n" 1144 | ] 1145 | }, 1146 | { 1147 | "cell_type": "code", 1148 | "execution_count": null, 1149 | "metadata": { 1150 | "cell_type": "solution" 1151 | }, 1152 | "outputs": [], 1153 | "source": [ 1154 | "X = np.zeros(10)\n", 1155 | "\n", 1156 | "X[4] = 1\n", 1157 | "X" 1158 | ] 1159 | }, 1160 | { 1161 | "cell_type": "markdown", 1162 | "metadata": {}, 1163 | "source": [ 1164 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1165 | "\n", 1166 | "### Given the X numpy array, change the 50 with a 40" 1167 | ] 1168 | }, 1169 | { 1170 | "cell_type": "code", 1171 | "execution_count": null, 1172 | "metadata": {}, 1173 | "outputs": [], 1174 | "source": [ 1175 | "# your code goes here\n" 1176 | ] 1177 | }, 1178 | { 1179 | "cell_type": "code", 1180 | "execution_count": null, 1181 | "metadata": { 1182 | "cell_type": "solution" 1183 | }, 1184 | "outputs": [], 1185 | "source": [ 1186 | "X = np.array([10, 20, 30, 50])\n", 1187 | "\n", 1188 | "X[3] = 40\n", 1189 | "X" 1190 | ] 1191 | }, 1192 | { 1193 | "cell_type": "markdown", 1194 | "metadata": {}, 1195 | "source": [ 1196 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1197 | "\n", 1198 | "### Given the X numpy matrix, change the last row with all 1" 1199 | ] 1200 | }, 1201 | { 1202 | "cell_type": "code", 1203 | "execution_count": null, 1204 | "metadata": {}, 1205 | "outputs": [], 1206 | "source": [ 1207 | "# your code goes here\n" 1208 | ] 1209 | }, 1210 | { 1211 | "cell_type": "code", 1212 | "execution_count": null, 1213 | "metadata": { 1214 | "cell_type": "solution" 1215 | }, 1216 | "outputs": [], 1217 | "source": [ 1218 | "X = np.array([\n", 1219 | " [1, 2, 3, 4],\n", 1220 | " [5, 6, 7, 8],\n", 1221 | " [9, 10, 11, 12],\n", 1222 | " [13, 14, 15, 16]\n", 1223 | "])\n", 1224 | "\n", 1225 | "X[-1] = np.array([1, 1, 1, 1])\n", 1226 | "X" 1227 | ] 1228 | }, 1229 | { 1230 | "cell_type": "markdown", 1231 | "metadata": {}, 1232 | "source": [ 1233 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1234 | "\n", 1235 | "### Given the X numpy matrix, change the last item on the last row with a 0" 1236 | ] 1237 | }, 1238 | { 1239 | "cell_type": "code", 1240 | "execution_count": null, 1241 | "metadata": {}, 1242 | "outputs": [], 1243 | "source": [ 1244 | "# your code goes here\n" 1245 | ] 1246 | }, 1247 | { 1248 | "cell_type": "code", 1249 | "execution_count": null, 1250 | "metadata": { 1251 | "cell_type": "solution" 1252 | }, 1253 | "outputs": [], 1254 | "source": [ 1255 | "X = np.array([\n", 1256 | " [1, 2, 3, 4],\n", 1257 | " [5, 6, 7, 8],\n", 1258 | " [9, 10, 11, 12],\n", 1259 | " [13, 14, 15, 16]\n", 1260 | "])\n", 1261 | "\n", 1262 | "X[-1, -1] = 0\n", 1263 | "X" 1264 | ] 1265 | }, 1266 | { 1267 | "cell_type": "markdown", 1268 | "metadata": {}, 1269 | "source": [ 1270 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1271 | "\n", 1272 | "### Given the X numpy matrix, add 5 to every element" 1273 | ] 1274 | }, 1275 | { 1276 | "cell_type": "code", 1277 | "execution_count": null, 1278 | "metadata": {}, 1279 | "outputs": [], 1280 | "source": [ 1281 | "# your code goes here\n" 1282 | ] 1283 | }, 1284 | { 1285 | "cell_type": "code", 1286 | "execution_count": null, 1287 | "metadata": { 1288 | "cell_type": "solution" 1289 | }, 1290 | "outputs": [], 1291 | "source": [ 1292 | "X = np.array([\n", 1293 | " [1, 2, 3, 4],\n", 1294 | " [5, 6, 7, 8],\n", 1295 | " [9, 10, 11, 12],\n", 1296 | " [13, 14, 15, 16]\n", 1297 | "])\n", 1298 | "\n", 1299 | "X + 5" 1300 | ] 1301 | }, 1302 | { 1303 | "cell_type": "markdown", 1304 | "metadata": {}, 1305 | "source": [ 1306 | "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", 1307 | "\n", 1308 | "## Boolean arrays _(also called masks)_\n" 1309 | ] 1310 | }, 1311 | { 1312 | "cell_type": "markdown", 1313 | "metadata": {}, 1314 | "source": [ 1315 | "### Given the X numpy array, make a mask showing negative elements" 1316 | ] 1317 | }, 1318 | { 1319 | "cell_type": "code", 1320 | "execution_count": null, 1321 | "metadata": {}, 1322 | "outputs": [], 1323 | "source": [ 1324 | "# your code goes here\n" 1325 | ] 1326 | }, 1327 | { 1328 | "cell_type": "code", 1329 | "execution_count": null, 1330 | "metadata": { 1331 | "cell_type": "solution" 1332 | }, 1333 | "outputs": [], 1334 | "source": [ 1335 | "X = np.array([-1,2,0,-4,5,6,0,0,-9,10])\n", 1336 | "\n", 1337 | "mask = X <= 0\n", 1338 | "mask" 1339 | ] 1340 | }, 1341 | { 1342 | "cell_type": "markdown", 1343 | "metadata": {}, 1344 | "source": [ 1345 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1346 | "\n", 1347 | "### Given the X numpy array, get the negative elements" 1348 | ] 1349 | }, 1350 | { 1351 | "cell_type": "code", 1352 | "execution_count": null, 1353 | "metadata": {}, 1354 | "outputs": [], 1355 | "source": [ 1356 | "# your code goes here\n" 1357 | ] 1358 | }, 1359 | { 1360 | "cell_type": "code", 1361 | "execution_count": null, 1362 | "metadata": { 1363 | "cell_type": "solution" 1364 | }, 1365 | "outputs": [], 1366 | "source": [ 1367 | "X = np.array([-1, 2, 0, -4, 5, 6, 0, 0, -9, 10])\n", 1368 | "\n", 1369 | "mask = X <= 0\n", 1370 | "X[mask]" 1371 | ] 1372 | }, 1373 | { 1374 | "cell_type": "markdown", 1375 | "metadata": {}, 1376 | "source": [ 1377 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1378 | "\n", 1379 | "### Given the X numpy array, get numbers higher than 5" 1380 | ] 1381 | }, 1382 | { 1383 | "cell_type": "code", 1384 | "execution_count": null, 1385 | "metadata": {}, 1386 | "outputs": [], 1387 | "source": [ 1388 | "# your code goes here\n" 1389 | ] 1390 | }, 1391 | { 1392 | "cell_type": "code", 1393 | "execution_count": null, 1394 | "metadata": { 1395 | "cell_type": "solution" 1396 | }, 1397 | "outputs": [], 1398 | "source": [ 1399 | "X = np.array([-1, 2, 0, -4, 5, 6, 0, 0, -9, 10])\n", 1400 | "\n", 1401 | "mask = X > 5\n", 1402 | "X[mask]" 1403 | ] 1404 | }, 1405 | { 1406 | "cell_type": "markdown", 1407 | "metadata": {}, 1408 | "source": [ 1409 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1410 | "\n", 1411 | "### Given the X numpy array, get numbers higher than the elements mean" 1412 | ] 1413 | }, 1414 | { 1415 | "cell_type": "code", 1416 | "execution_count": null, 1417 | "metadata": {}, 1418 | "outputs": [], 1419 | "source": [ 1420 | "# your code goes here\n" 1421 | ] 1422 | }, 1423 | { 1424 | "cell_type": "code", 1425 | "execution_count": null, 1426 | "metadata": { 1427 | "cell_type": "solution" 1428 | }, 1429 | "outputs": [], 1430 | "source": [ 1431 | "X = np.array([-1, 2, 0, -4, 5, 6, 0, 0, -9, 10])\n", 1432 | "\n", 1433 | "mask = X > X.mean()\n", 1434 | "X[mask]" 1435 | ] 1436 | }, 1437 | { 1438 | "cell_type": "markdown", 1439 | "metadata": {}, 1440 | "source": [ 1441 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1442 | "\n", 1443 | "### Given the X numpy array, get numbers equal to 2 or 10" 1444 | ] 1445 | }, 1446 | { 1447 | "cell_type": "code", 1448 | "execution_count": null, 1449 | "metadata": {}, 1450 | "outputs": [], 1451 | "source": [ 1452 | "# your code goes here\n" 1453 | ] 1454 | }, 1455 | { 1456 | "cell_type": "code", 1457 | "execution_count": null, 1458 | "metadata": { 1459 | "cell_type": "solution", 1460 | "scrolled": true 1461 | }, 1462 | "outputs": [], 1463 | "source": [ 1464 | "X = np.array([-1, 2, 0, -4, 5, 6, 0, 0, -9, 10])\n", 1465 | "\n", 1466 | "mask = (X == 2) | (X == 10)\n", 1467 | "X[mask]" 1468 | ] 1469 | }, 1470 | { 1471 | "cell_type": "markdown", 1472 | "metadata": {}, 1473 | "source": [ 1474 | "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", 1475 | "\n", 1476 | "## Logic functions\n" 1477 | ] 1478 | }, 1479 | { 1480 | "cell_type": "markdown", 1481 | "metadata": {}, 1482 | "source": [ 1483 | "### Given the X numpy array, return True if none of its elements is zero" 1484 | ] 1485 | }, 1486 | { 1487 | "cell_type": "code", 1488 | "execution_count": null, 1489 | "metadata": {}, 1490 | "outputs": [], 1491 | "source": [ 1492 | "# your code goes here\n" 1493 | ] 1494 | }, 1495 | { 1496 | "cell_type": "code", 1497 | "execution_count": null, 1498 | "metadata": { 1499 | "cell_type": "solution" 1500 | }, 1501 | "outputs": [], 1502 | "source": [ 1503 | "X = np.array([-1, 2, 0, -4, 5, 6, 0, 0, -9, 10])\n", 1504 | "\n", 1505 | "X.all()" 1506 | ] 1507 | }, 1508 | { 1509 | "cell_type": "markdown", 1510 | "metadata": {}, 1511 | "source": [ 1512 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1513 | "\n", 1514 | "### Given the X numpy array, return True if any of its elements is zero" 1515 | ] 1516 | }, 1517 | { 1518 | "cell_type": "code", 1519 | "execution_count": null, 1520 | "metadata": {}, 1521 | "outputs": [], 1522 | "source": [ 1523 | "# your code goes here\n" 1524 | ] 1525 | }, 1526 | { 1527 | "cell_type": "code", 1528 | "execution_count": null, 1529 | "metadata": { 1530 | "cell_type": "solution" 1531 | }, 1532 | "outputs": [], 1533 | "source": [ 1534 | "X = np.array([-1, 2, 0, -4, 5, 6, 0, 0, -9, 10])\n", 1535 | "\n", 1536 | "X.any()" 1537 | ] 1538 | }, 1539 | { 1540 | "cell_type": "markdown", 1541 | "metadata": {}, 1542 | "source": [ 1543 | "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", 1544 | "\n", 1545 | "## Summary statistics" 1546 | ] 1547 | }, 1548 | { 1549 | "cell_type": "markdown", 1550 | "metadata": {}, 1551 | "source": [ 1552 | "### Given the X numpy array, show the sum of its elements" 1553 | ] 1554 | }, 1555 | { 1556 | "cell_type": "code", 1557 | "execution_count": null, 1558 | "metadata": {}, 1559 | "outputs": [], 1560 | "source": [ 1561 | "# your code goes here\n" 1562 | ] 1563 | }, 1564 | { 1565 | "cell_type": "code", 1566 | "execution_count": null, 1567 | "metadata": { 1568 | "cell_type": "solution" 1569 | }, 1570 | "outputs": [], 1571 | "source": [ 1572 | "X = np.array([3, 5, 6, 7, 2, 3, 4, 9, 4])\n", 1573 | "\n", 1574 | "#np.sum(X)\n", 1575 | "X.sum()" 1576 | ] 1577 | }, 1578 | { 1579 | "cell_type": "markdown", 1580 | "metadata": {}, 1581 | "source": [ 1582 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1583 | "\n", 1584 | "### Given the X numpy array, show the mean value of its elements" 1585 | ] 1586 | }, 1587 | { 1588 | "cell_type": "code", 1589 | "execution_count": null, 1590 | "metadata": {}, 1591 | "outputs": [], 1592 | "source": [ 1593 | "# your code goes here\n" 1594 | ] 1595 | }, 1596 | { 1597 | "cell_type": "code", 1598 | "execution_count": null, 1599 | "metadata": { 1600 | "cell_type": "solution" 1601 | }, 1602 | "outputs": [], 1603 | "source": [ 1604 | "X = np.array([1, 2, 0, 4, 5, 6, 0, 0, 9, 10])\n", 1605 | "\n", 1606 | "#np.mean(X)\n", 1607 | "X.mean()" 1608 | ] 1609 | }, 1610 | { 1611 | "cell_type": "markdown", 1612 | "metadata": {}, 1613 | "source": [ 1614 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1615 | "\n", 1616 | "### Given the X numpy matrix, show the sum of its columns" 1617 | ] 1618 | }, 1619 | { 1620 | "cell_type": "code", 1621 | "execution_count": null, 1622 | "metadata": {}, 1623 | "outputs": [], 1624 | "source": [ 1625 | "# your code goes here\n" 1626 | ] 1627 | }, 1628 | { 1629 | "cell_type": "code", 1630 | "execution_count": null, 1631 | "metadata": { 1632 | "cell_type": "solution" 1633 | }, 1634 | "outputs": [], 1635 | "source": [ 1636 | "X = np.array([\n", 1637 | " [1, 2, 3, 4],\n", 1638 | " [5, 6, 7, 8],\n", 1639 | " [9, 10, 11, 12],\n", 1640 | " [13, 14, 15, 16]\n", 1641 | "])\n", 1642 | "\n", 1643 | "X.sum(axis=0) # remember: axis=0 columns; axis=1 rows" 1644 | ] 1645 | }, 1646 | { 1647 | "cell_type": "markdown", 1648 | "metadata": {}, 1649 | "source": [ 1650 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1651 | "\n", 1652 | "### Given the X numpy matrix, show the mean value of its rows" 1653 | ] 1654 | }, 1655 | { 1656 | "cell_type": "code", 1657 | "execution_count": null, 1658 | "metadata": {}, 1659 | "outputs": [], 1660 | "source": [ 1661 | "# your code goes here\n" 1662 | ] 1663 | }, 1664 | { 1665 | "cell_type": "code", 1666 | "execution_count": null, 1667 | "metadata": { 1668 | "cell_type": "solution" 1669 | }, 1670 | "outputs": [], 1671 | "source": [ 1672 | "X = np.array([\n", 1673 | " [1, 2, 3, 4],\n", 1674 | " [5, 6, 7, 8],\n", 1675 | " [9, 10, 11, 12],\n", 1676 | " [13, 14, 15, 16]\n", 1677 | "])\n", 1678 | "\n", 1679 | "X.mean(axis=1) # remember: axis=0 columns; axis=1 rows" 1680 | ] 1681 | }, 1682 | { 1683 | "cell_type": "markdown", 1684 | "metadata": {}, 1685 | "source": [ 1686 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1687 | "\n", 1688 | "### Given the X numpy array, show the max value of its elements" 1689 | ] 1690 | }, 1691 | { 1692 | "cell_type": "code", 1693 | "execution_count": null, 1694 | "metadata": {}, 1695 | "outputs": [], 1696 | "source": [ 1697 | "# your code goes here\n" 1698 | ] 1699 | }, 1700 | { 1701 | "cell_type": "code", 1702 | "execution_count": null, 1703 | "metadata": { 1704 | "cell_type": "solution" 1705 | }, 1706 | "outputs": [], 1707 | "source": [ 1708 | "X = np.array([1, 2, 0, 4, 5, 6, 0, 0, 9, 10])\n", 1709 | "\n", 1710 | "#np.max(X)\n", 1711 | "X.max()" 1712 | ] 1713 | }, 1714 | { 1715 | "cell_type": "markdown", 1716 | "metadata": {}, 1717 | "source": [ 1718 | "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)" 1719 | ] 1720 | } 1721 | ], 1722 | "metadata": { 1723 | "kernelspec": { 1724 | "display_name": "Python 3", 1725 | "language": "python", 1726 | "name": "python3" 1727 | }, 1728 | "language_info": { 1729 | "codemirror_mode": { 1730 | "name": "ipython", 1731 | "version": 3 1732 | }, 1733 | "file_extension": ".py", 1734 | "mimetype": "text/x-python", 1735 | "name": "python", 1736 | "nbconvert_exporter": "python", 1737 | "pygments_lexer": "ipython3", 1738 | "version": "3.7.4" 1739 | } 1740 | }, 1741 | "nbformat": 4, 1742 | "nbformat_minor": 2 1743 | } 1744 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Untitled-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 14, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": null, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "![numpy](numpy.png)" 24 | ] 25 | } 26 | ], 27 | "metadata": { 28 | "kernelspec": { 29 | "display_name": "Python 3", 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.8.1" 44 | } 45 | }, 46 | "nbformat": 4, 47 | "nbformat_minor": 4 48 | } 49 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/untitled-checkpoint.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rmotr-curriculum/freecodecamp-intro-to-numpy/6f9b96e4c8b8333ed8a1b95f38e9b81e55d45fa4/.ipynb_checkpoints/untitled-checkpoint.txt -------------------------------------------------------------------------------- /1. Introduction to Data Science.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "![rmotr](https://user-images.githubusercontent.com/7065401/52071918-bda15380-2562-11e9-828c-7f95297e4a82.png)\n", 8 | "
\n", 9 | "\n", 10 | "\n", 12 | " \n", 13 | "# Intro to Data Science\n", 14 | "\n", 15 | "What is _Data Science_? The only thing we know for sure is that, _Data Science_ is a broad term. It depends on the organization/company, the people involved, etc. This is an good representation:\n", 16 | "\n", 17 | "\n", 18 | "\n", 19 | "> [Source](http://drewconway.com/zia/2013/3/26/the-data-science-venn-diagram)\n" 20 | ] 21 | }, 22 | { 23 | "cell_type": "markdown", 24 | "metadata": {}, 25 | "source": [ 26 | "\n", 27 | "Although, this one might be a little bit more accurate:\n", 28 | "\n", 29 | "\n", 30 | "\n", 31 | "As a \"Data Scientist\" you'll be combining your domain knowledge \"science\", with large amounts of data, using programming and computers. Your tasks as Data Scientists will involve, highly technical tasks like Machine Learning to boring and repetitive operative tasks like scraping a website from data and importing it in a Database.\n" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 39 | "\n", 40 | "### What does a Data Scientist do?\n", 41 | "\n", 42 | "It's probably easier to define the tasks that are most common to Data Scientists that to give a general definition that applies to everybody.\n", 43 | "\n", 44 | "\n", 45 | "\n", 46 | "> [Source from recommended article](https://cacm.acm.org/blogs/blog-cacm/169199-data-science-workflow-overview-and-challenges/fulltext)\n" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "\n", 54 | "**Getting the data**\n", 55 | "\n", 56 | "Depending on your company/organization, getting the data can be as simple as a SQL query or as difficult as scraping entire websites. The problem with this task is that it's not standardized.\n", 57 | "\n", 58 | "**Parsing and Cleaning the Data**\n", 59 | "\n", 60 | "Depending on your sources, you'll need to do a little bit of preparation. Excluding outliers, filling null values, translating values, etc.\n", 61 | "\n", 62 | "**Merging, combining data**\n", 63 | "\n", 64 | "If the data comes from different sources, merging it can be tedious. Specially if it's hard to define that piece of information that relates different data sources.\n", 65 | "\n", 66 | "**Doing the analysis**\n", 67 | "\n", 68 | "This involves your own domain expertise + the tools available for the job. For example, you need to know the principles of statistics and you can also use [`statsmodels`](https://www.statsmodels.org) to simplify your job. The analysis part is usually iterative and involves other subtasks as visualizations, validation testing, etc.\n", 69 | "\n", 70 | "**Building models**\n", 71 | "\n", 72 | "The whole point of the analysis part is finding patterns in particular cases to build general models. Your models can be predictions, clusterings, or just automated reports. In a general sense, it's the result of all the previous phases.\n", 73 | "\n", 74 | "**Deploying it**\n", 75 | "\n", 76 | "Perfect analyses and models are useless if they're not repeatable and scalable. This phase depends on your own models, but it'll usually imply a cloud provider. From simple reports (emailed every day at 3AM from an AWS Lambda) to a Machine Learning Model (built on top of Tensor Flow and deployed on Google's cloud infrastructure)." 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "metadata": {}, 82 | "source": [ 83 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 84 | "\n", 85 | "### The only thing that's certain\n", 86 | "\n", 87 | "We're sorry we can't give you better answers to the question \"What is Data Science?\", but this is a new discipline and we're putting together definitions, scopes and responsibilities of different actors. But there's one thing that's certain: **You need to know how to code...**\n", 88 | "\n", 89 | "\n", 90 | "\n", 91 | "> [Source](https://twitter.com/josh_wills/status/198093512149958656?lang=en)\n" 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "metadata": {}, 97 | "source": [ 98 | "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)" 99 | ] 100 | } 101 | ], 102 | "metadata": { 103 | "kernelspec": { 104 | "display_name": "Python 3", 105 | "language": "python", 106 | "name": "python3" 107 | }, 108 | "language_info": { 109 | "codemirror_mode": { 110 | "name": "ipython", 111 | "version": 3 112 | }, 113 | "file_extension": ".py", 114 | "mimetype": "text/x-python", 115 | "name": "python", 116 | "nbconvert_exporter": "python", 117 | "pygments_lexer": "ipython3", 118 | "version": "3.7.4" 119 | } 120 | }, 121 | "nbformat": 4, 122 | "nbformat_minor": 2 123 | } 124 | -------------------------------------------------------------------------------- /2. NumPy.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "![rmotr](https://user-images.githubusercontent.com/7065401/52071918-bda15380-2562-11e9-828c-7f95297e4a82.png)\n", 8 | "
\n", 9 | "\n", 10 | "\n", 12 | "\n", 13 | "# Numpy: Numeric computing library\n", 14 | "\n", 15 | "NumPy (Numerical Python) is one of the core packages for numerical computing in Python. Pandas, Matplotlib, Statmodels and many other Scientific libraries rely on NumPy.\n", 16 | "\n", 17 | "NumPy major contributions are:\n", 18 | "\n", 19 | "* Efficient numeric computation with C primitives\n", 20 | "* Efficient collections with vectorized operations\n", 21 | "* An integrated and natural Linear Algebra API\n", 22 | "* A C API for connecting NumPy with libraries written in C, C++, or FORTRAN.\n", 23 | "\n", 24 | "Let's develop on efficiency. In Python, **everything is an object**, which means that even simple ints are also objects, with all the required machinery to make object work. We call them \"Boxed Ints\". In contrast, NumPy uses primitive numeric types (floats, ints) which makes storing and computation efficient." 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "metadata": {}, 30 | "source": [ 31 | "\n" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", 39 | "\n", 40 | "## Hands on! " 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 1, 46 | "metadata": {}, 47 | "outputs": [], 48 | "source": [ 49 | "import sys\n", 50 | "import numpy as np" 51 | ] 52 | }, 53 | { 54 | "cell_type": "markdown", 55 | "metadata": {}, 56 | "source": [ 57 | "## Basic Numpy Arrays" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 2, 63 | "metadata": {}, 64 | "outputs": [ 65 | { 66 | "data": { 67 | "text/plain": [ 68 | "array([1, 2, 3, 4])" 69 | ] 70 | }, 71 | "execution_count": 2, 72 | "metadata": {}, 73 | "output_type": "execute_result" 74 | } 75 | ], 76 | "source": [ 77 | "np.array([1, 2, 3, 4])" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 3, 83 | "metadata": {}, 84 | "outputs": [], 85 | "source": [ 86 | "a = np.array([1, 2, 3, 4])" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": 4, 92 | "metadata": {}, 93 | "outputs": [], 94 | "source": [ 95 | "b = np.array([0, .5, 1, 1.5, 2])" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": 5, 101 | "metadata": {}, 102 | "outputs": [ 103 | { 104 | "data": { 105 | "text/plain": [ 106 | "(1, 2)" 107 | ] 108 | }, 109 | "execution_count": 5, 110 | "metadata": {}, 111 | "output_type": "execute_result" 112 | } 113 | ], 114 | "source": [ 115 | "a[0], a[1]" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": 6, 121 | "metadata": {}, 122 | "outputs": [ 123 | { 124 | "data": { 125 | "text/plain": [ 126 | "array([1, 2, 3, 4])" 127 | ] 128 | }, 129 | "execution_count": 6, 130 | "metadata": {}, 131 | "output_type": "execute_result" 132 | } 133 | ], 134 | "source": [ 135 | "a[0:]" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": 7, 141 | "metadata": {}, 142 | "outputs": [ 143 | { 144 | "data": { 145 | "text/plain": [ 146 | "array([2, 3])" 147 | ] 148 | }, 149 | "execution_count": 7, 150 | "metadata": {}, 151 | "output_type": "execute_result" 152 | } 153 | ], 154 | "source": [ 155 | "a[1:3]" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": 8, 161 | "metadata": { 162 | "scrolled": true 163 | }, 164 | "outputs": [ 165 | { 166 | "data": { 167 | "text/plain": [ 168 | "array([2, 3])" 169 | ] 170 | }, 171 | "execution_count": 8, 172 | "metadata": {}, 173 | "output_type": "execute_result" 174 | } 175 | ], 176 | "source": [ 177 | "a[1:-1]" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": 9, 183 | "metadata": {}, 184 | "outputs": [ 185 | { 186 | "data": { 187 | "text/plain": [ 188 | "array([1, 3])" 189 | ] 190 | }, 191 | "execution_count": 9, 192 | "metadata": {}, 193 | "output_type": "execute_result" 194 | } 195 | ], 196 | "source": [ 197 | "a[::2]" 198 | ] 199 | }, 200 | { 201 | "cell_type": "code", 202 | "execution_count": 10, 203 | "metadata": {}, 204 | "outputs": [ 205 | { 206 | "data": { 207 | "text/plain": [ 208 | "array([0. , 0.5, 1. , 1.5, 2. ])" 209 | ] 210 | }, 211 | "execution_count": 10, 212 | "metadata": {}, 213 | "output_type": "execute_result" 214 | } 215 | ], 216 | "source": [ 217 | "b" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": 11, 223 | "metadata": { 224 | "scrolled": true 225 | }, 226 | "outputs": [ 227 | { 228 | "data": { 229 | "text/plain": [ 230 | "(0.0, 1.0, 2.0)" 231 | ] 232 | }, 233 | "execution_count": 11, 234 | "metadata": {}, 235 | "output_type": "execute_result" 236 | } 237 | ], 238 | "source": [ 239 | "b[0], b[2], b[-1]" 240 | ] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "execution_count": 12, 245 | "metadata": {}, 246 | "outputs": [ 247 | { 248 | "data": { 249 | "text/plain": [ 250 | "array([0., 1., 2.])" 251 | ] 252 | }, 253 | "execution_count": 12, 254 | "metadata": {}, 255 | "output_type": "execute_result" 256 | } 257 | ], 258 | "source": [ 259 | "b[[0, 2, -1]]" 260 | ] 261 | }, 262 | { 263 | "cell_type": "markdown", 264 | "metadata": {}, 265 | "source": [ 266 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 267 | "\n", 268 | "## Array Types" 269 | ] 270 | }, 271 | { 272 | "cell_type": "code", 273 | "execution_count": 13, 274 | "metadata": {}, 275 | "outputs": [ 276 | { 277 | "data": { 278 | "text/plain": [ 279 | "array([1, 2, 3, 4])" 280 | ] 281 | }, 282 | "execution_count": 13, 283 | "metadata": {}, 284 | "output_type": "execute_result" 285 | } 286 | ], 287 | "source": [ 288 | "a" 289 | ] 290 | }, 291 | { 292 | "cell_type": "code", 293 | "execution_count": 14, 294 | "metadata": {}, 295 | "outputs": [ 296 | { 297 | "data": { 298 | "text/plain": [ 299 | "dtype('int64')" 300 | ] 301 | }, 302 | "execution_count": 14, 303 | "metadata": {}, 304 | "output_type": "execute_result" 305 | } 306 | ], 307 | "source": [ 308 | "a.dtype" 309 | ] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "execution_count": 15, 314 | "metadata": {}, 315 | "outputs": [ 316 | { 317 | "data": { 318 | "text/plain": [ 319 | "array([0. , 0.5, 1. , 1.5, 2. ])" 320 | ] 321 | }, 322 | "execution_count": 15, 323 | "metadata": {}, 324 | "output_type": "execute_result" 325 | } 326 | ], 327 | "source": [ 328 | "b" 329 | ] 330 | }, 331 | { 332 | "cell_type": "code", 333 | "execution_count": 16, 334 | "metadata": {}, 335 | "outputs": [ 336 | { 337 | "data": { 338 | "text/plain": [ 339 | "dtype('float64')" 340 | ] 341 | }, 342 | "execution_count": 16, 343 | "metadata": {}, 344 | "output_type": "execute_result" 345 | } 346 | ], 347 | "source": [ 348 | "b.dtype" 349 | ] 350 | }, 351 | { 352 | "cell_type": "code", 353 | "execution_count": 17, 354 | "metadata": { 355 | "scrolled": true 356 | }, 357 | "outputs": [ 358 | { 359 | "data": { 360 | "text/plain": [ 361 | "array([1., 2., 3., 4.])" 362 | ] 363 | }, 364 | "execution_count": 17, 365 | "metadata": {}, 366 | "output_type": "execute_result" 367 | } 368 | ], 369 | "source": [ 370 | "np.array([1, 2, 3, 4], dtype=np.float)" 371 | ] 372 | }, 373 | { 374 | "cell_type": "code", 375 | "execution_count": 18, 376 | "metadata": { 377 | "scrolled": true 378 | }, 379 | "outputs": [ 380 | { 381 | "data": { 382 | "text/plain": [ 383 | "array([1, 2, 3, 4], dtype=int8)" 384 | ] 385 | }, 386 | "execution_count": 18, 387 | "metadata": {}, 388 | "output_type": "execute_result" 389 | } 390 | ], 391 | "source": [ 392 | "np.array([1, 2, 3, 4], dtype=np.int8)" 393 | ] 394 | }, 395 | { 396 | "cell_type": "code", 397 | "execution_count": 19, 398 | "metadata": {}, 399 | "outputs": [], 400 | "source": [ 401 | "c = np.array(['a', 'b', 'c'])" 402 | ] 403 | }, 404 | { 405 | "cell_type": "code", 406 | "execution_count": 20, 407 | "metadata": {}, 408 | "outputs": [ 409 | { 410 | "data": { 411 | "text/plain": [ 412 | "dtype('= 2" 1686 | ] 1687 | }, 1688 | { 1689 | "cell_type": "code", 1690 | "execution_count": 90, 1691 | "metadata": {}, 1692 | "outputs": [ 1693 | { 1694 | "data": { 1695 | "text/plain": [ 1696 | "array([2, 3])" 1697 | ] 1698 | }, 1699 | "execution_count": 90, 1700 | "metadata": {}, 1701 | "output_type": "execute_result" 1702 | } 1703 | ], 1704 | "source": [ 1705 | "a[a >= 2]" 1706 | ] 1707 | }, 1708 | { 1709 | "cell_type": "code", 1710 | "execution_count": 91, 1711 | "metadata": {}, 1712 | "outputs": [ 1713 | { 1714 | "data": { 1715 | "text/plain": [ 1716 | "1.5" 1717 | ] 1718 | }, 1719 | "execution_count": 91, 1720 | "metadata": {}, 1721 | "output_type": "execute_result" 1722 | } 1723 | ], 1724 | "source": [ 1725 | "a.mean()" 1726 | ] 1727 | }, 1728 | { 1729 | "cell_type": "code", 1730 | "execution_count": 92, 1731 | "metadata": { 1732 | "scrolled": true 1733 | }, 1734 | "outputs": [ 1735 | { 1736 | "data": { 1737 | "text/plain": [ 1738 | "array([2, 3])" 1739 | ] 1740 | }, 1741 | "execution_count": 92, 1742 | "metadata": {}, 1743 | "output_type": "execute_result" 1744 | } 1745 | ], 1746 | "source": [ 1747 | "a[a > a.mean()]" 1748 | ] 1749 | }, 1750 | { 1751 | "cell_type": "code", 1752 | "execution_count": 93, 1753 | "metadata": {}, 1754 | "outputs": [ 1755 | { 1756 | "data": { 1757 | "text/plain": [ 1758 | "array([0, 1])" 1759 | ] 1760 | }, 1761 | "execution_count": 93, 1762 | "metadata": {}, 1763 | "output_type": "execute_result" 1764 | } 1765 | ], 1766 | "source": [ 1767 | "a[~(a > a.mean())]" 1768 | ] 1769 | }, 1770 | { 1771 | "cell_type": "code", 1772 | "execution_count": 94, 1773 | "metadata": {}, 1774 | "outputs": [ 1775 | { 1776 | "data": { 1777 | "text/plain": [ 1778 | "array([0, 1])" 1779 | ] 1780 | }, 1781 | "execution_count": 94, 1782 | "metadata": {}, 1783 | "output_type": "execute_result" 1784 | } 1785 | ], 1786 | "source": [ 1787 | "a[(a == 0) | (a == 1)]" 1788 | ] 1789 | }, 1790 | { 1791 | "cell_type": "code", 1792 | "execution_count": 95, 1793 | "metadata": {}, 1794 | "outputs": [ 1795 | { 1796 | "data": { 1797 | "text/plain": [ 1798 | "array([0, 2])" 1799 | ] 1800 | }, 1801 | "execution_count": 95, 1802 | "metadata": {}, 1803 | "output_type": "execute_result" 1804 | } 1805 | ], 1806 | "source": [ 1807 | "a[(a <= 2) & (a % 2 == 0)]" 1808 | ] 1809 | }, 1810 | { 1811 | "cell_type": "code", 1812 | "execution_count": 96, 1813 | "metadata": {}, 1814 | "outputs": [], 1815 | "source": [ 1816 | "A = np.random.randint(100, size=(3, 3))" 1817 | ] 1818 | }, 1819 | { 1820 | "cell_type": "code", 1821 | "execution_count": 97, 1822 | "metadata": {}, 1823 | "outputs": [ 1824 | { 1825 | "data": { 1826 | "text/plain": [ 1827 | "array([[71, 6, 42],\n", 1828 | " [40, 94, 24],\n", 1829 | " [ 2, 85, 36]])" 1830 | ] 1831 | }, 1832 | "execution_count": 97, 1833 | "metadata": {}, 1834 | "output_type": "execute_result" 1835 | } 1836 | ], 1837 | "source": [ 1838 | "A" 1839 | ] 1840 | }, 1841 | { 1842 | "cell_type": "code", 1843 | "execution_count": 98, 1844 | "metadata": {}, 1845 | "outputs": [ 1846 | { 1847 | "data": { 1848 | "text/plain": [ 1849 | "array([71, 42, 94, 2, 36])" 1850 | ] 1851 | }, 1852 | "execution_count": 98, 1853 | "metadata": {}, 1854 | "output_type": "execute_result" 1855 | } 1856 | ], 1857 | "source": [ 1858 | "A[np.array([\n", 1859 | " [True, False, True],\n", 1860 | " [False, True, False],\n", 1861 | " [True, False, True]\n", 1862 | "])]" 1863 | ] 1864 | }, 1865 | { 1866 | "cell_type": "code", 1867 | "execution_count": 99, 1868 | "metadata": {}, 1869 | "outputs": [ 1870 | { 1871 | "data": { 1872 | "text/plain": [ 1873 | "array([[ True, False, True],\n", 1874 | " [ True, True, False],\n", 1875 | " [False, True, True]])" 1876 | ] 1877 | }, 1878 | "execution_count": 99, 1879 | "metadata": {}, 1880 | "output_type": "execute_result" 1881 | } 1882 | ], 1883 | "source": [ 1884 | "A > 30" 1885 | ] 1886 | }, 1887 | { 1888 | "cell_type": "code", 1889 | "execution_count": 100, 1890 | "metadata": {}, 1891 | "outputs": [ 1892 | { 1893 | "data": { 1894 | "text/plain": [ 1895 | "array([71, 42, 40, 94, 85, 36])" 1896 | ] 1897 | }, 1898 | "execution_count": 100, 1899 | "metadata": {}, 1900 | "output_type": "execute_result" 1901 | } 1902 | ], 1903 | "source": [ 1904 | "A[A > 30]" 1905 | ] 1906 | }, 1907 | { 1908 | "cell_type": "markdown", 1909 | "metadata": {}, 1910 | "source": [ 1911 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1912 | "\n", 1913 | "## Linear Algebra" 1914 | ] 1915 | }, 1916 | { 1917 | "cell_type": "code", 1918 | "execution_count": 101, 1919 | "metadata": {}, 1920 | "outputs": [], 1921 | "source": [ 1922 | "A = np.array([\n", 1923 | " [1, 2, 3],\n", 1924 | " [4, 5, 6],\n", 1925 | " [7, 8, 9]\n", 1926 | "])" 1927 | ] 1928 | }, 1929 | { 1930 | "cell_type": "code", 1931 | "execution_count": 102, 1932 | "metadata": {}, 1933 | "outputs": [], 1934 | "source": [ 1935 | "B = np.array([\n", 1936 | " [6, 5],\n", 1937 | " [4, 3],\n", 1938 | " [2, 1]\n", 1939 | "])" 1940 | ] 1941 | }, 1942 | { 1943 | "cell_type": "code", 1944 | "execution_count": 103, 1945 | "metadata": {}, 1946 | "outputs": [ 1947 | { 1948 | "data": { 1949 | "text/plain": [ 1950 | "array([[20, 14],\n", 1951 | " [56, 41],\n", 1952 | " [92, 68]])" 1953 | ] 1954 | }, 1955 | "execution_count": 103, 1956 | "metadata": {}, 1957 | "output_type": "execute_result" 1958 | } 1959 | ], 1960 | "source": [ 1961 | "A.dot(B)" 1962 | ] 1963 | }, 1964 | { 1965 | "cell_type": "code", 1966 | "execution_count": 104, 1967 | "metadata": {}, 1968 | "outputs": [ 1969 | { 1970 | "data": { 1971 | "text/plain": [ 1972 | "array([[20, 14],\n", 1973 | " [56, 41],\n", 1974 | " [92, 68]])" 1975 | ] 1976 | }, 1977 | "execution_count": 104, 1978 | "metadata": {}, 1979 | "output_type": "execute_result" 1980 | } 1981 | ], 1982 | "source": [ 1983 | "A @ B" 1984 | ] 1985 | }, 1986 | { 1987 | "cell_type": "code", 1988 | "execution_count": 105, 1989 | "metadata": {}, 1990 | "outputs": [ 1991 | { 1992 | "data": { 1993 | "text/plain": [ 1994 | "array([[6, 4, 2],\n", 1995 | " [5, 3, 1]])" 1996 | ] 1997 | }, 1998 | "execution_count": 105, 1999 | "metadata": {}, 2000 | "output_type": "execute_result" 2001 | } 2002 | ], 2003 | "source": [ 2004 | "B.T" 2005 | ] 2006 | }, 2007 | { 2008 | "cell_type": "code", 2009 | "execution_count": 106, 2010 | "metadata": {}, 2011 | "outputs": [ 2012 | { 2013 | "data": { 2014 | "text/plain": [ 2015 | "array([[1, 2, 3],\n", 2016 | " [4, 5, 6],\n", 2017 | " [7, 8, 9]])" 2018 | ] 2019 | }, 2020 | "execution_count": 106, 2021 | "metadata": {}, 2022 | "output_type": "execute_result" 2023 | } 2024 | ], 2025 | "source": [ 2026 | "A" 2027 | ] 2028 | }, 2029 | { 2030 | "cell_type": "code", 2031 | "execution_count": 107, 2032 | "metadata": { 2033 | "scrolled": true 2034 | }, 2035 | "outputs": [ 2036 | { 2037 | "data": { 2038 | "text/plain": [ 2039 | "array([[36, 48, 60],\n", 2040 | " [24, 33, 42]])" 2041 | ] 2042 | }, 2043 | "execution_count": 107, 2044 | "metadata": {}, 2045 | "output_type": "execute_result" 2046 | } 2047 | ], 2048 | "source": [ 2049 | "B.T @ A" 2050 | ] 2051 | }, 2052 | { 2053 | "cell_type": "markdown", 2054 | "metadata": {}, 2055 | "source": [ 2056 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 2057 | "\n", 2058 | "## Size of objects in Memory" 2059 | ] 2060 | }, 2061 | { 2062 | "cell_type": "markdown", 2063 | "metadata": {}, 2064 | "source": [ 2065 | "### Int, floats" 2066 | ] 2067 | }, 2068 | { 2069 | "cell_type": "code", 2070 | "execution_count": 108, 2071 | "metadata": {}, 2072 | "outputs": [ 2073 | { 2074 | "data": { 2075 | "text/plain": [ 2076 | "28" 2077 | ] 2078 | }, 2079 | "execution_count": 108, 2080 | "metadata": {}, 2081 | "output_type": "execute_result" 2082 | } 2083 | ], 2084 | "source": [ 2085 | "# An integer in Python is > 24bytes\n", 2086 | "sys.getsizeof(1)" 2087 | ] 2088 | }, 2089 | { 2090 | "cell_type": "code", 2091 | "execution_count": 109, 2092 | "metadata": { 2093 | "scrolled": true 2094 | }, 2095 | "outputs": [ 2096 | { 2097 | "data": { 2098 | "text/plain": [ 2099 | "72" 2100 | ] 2101 | }, 2102 | "execution_count": 109, 2103 | "metadata": {}, 2104 | "output_type": "execute_result" 2105 | } 2106 | ], 2107 | "source": [ 2108 | "# Longs are even larger\n", 2109 | "sys.getsizeof(10**100)" 2110 | ] 2111 | }, 2112 | { 2113 | "cell_type": "code", 2114 | "execution_count": 110, 2115 | "metadata": { 2116 | "scrolled": true 2117 | }, 2118 | "outputs": [ 2119 | { 2120 | "data": { 2121 | "text/plain": [ 2122 | "8" 2123 | ] 2124 | }, 2125 | "execution_count": 110, 2126 | "metadata": {}, 2127 | "output_type": "execute_result" 2128 | } 2129 | ], 2130 | "source": [ 2131 | "# Numpy size is much smaller\n", 2132 | "np.dtype(int).itemsize" 2133 | ] 2134 | }, 2135 | { 2136 | "cell_type": "code", 2137 | "execution_count": 112, 2138 | "metadata": { 2139 | "scrolled": true 2140 | }, 2141 | "outputs": [ 2142 | { 2143 | "data": { 2144 | "text/plain": [ 2145 | "1" 2146 | ] 2147 | }, 2148 | "execution_count": 112, 2149 | "metadata": {}, 2150 | "output_type": "execute_result" 2151 | } 2152 | ], 2153 | "source": [ 2154 | "# Numpy size is much smaller\n", 2155 | "np.dtype(np.int8).itemsize" 2156 | ] 2157 | }, 2158 | { 2159 | "cell_type": "code", 2160 | "execution_count": 111, 2161 | "metadata": { 2162 | "scrolled": true 2163 | }, 2164 | "outputs": [ 2165 | { 2166 | "data": { 2167 | "text/plain": [ 2168 | "8" 2169 | ] 2170 | }, 2171 | "execution_count": 111, 2172 | "metadata": {}, 2173 | "output_type": "execute_result" 2174 | } 2175 | ], 2176 | "source": [ 2177 | "np.dtype(float).itemsize" 2178 | ] 2179 | }, 2180 | { 2181 | "cell_type": "markdown", 2182 | "metadata": {}, 2183 | "source": [ 2184 | "### Lists are even larger" 2185 | ] 2186 | }, 2187 | { 2188 | "cell_type": "code", 2189 | "execution_count": null, 2190 | "metadata": {}, 2191 | "outputs": [], 2192 | "source": [ 2193 | "# A one-element list\n", 2194 | "sys.getsizeof([1])" 2195 | ] 2196 | }, 2197 | { 2198 | "cell_type": "code", 2199 | "execution_count": null, 2200 | "metadata": { 2201 | "scrolled": true 2202 | }, 2203 | "outputs": [], 2204 | "source": [ 2205 | "# An array of one element in numpy\n", 2206 | "np.array([1]).nbytes" 2207 | ] 2208 | }, 2209 | { 2210 | "cell_type": "markdown", 2211 | "metadata": {}, 2212 | "source": [ 2213 | "### And performance is also important" 2214 | ] 2215 | }, 2216 | { 2217 | "cell_type": "code", 2218 | "execution_count": 117, 2219 | "metadata": {}, 2220 | "outputs": [], 2221 | "source": [ 2222 | "l = list(range(100000))" 2223 | ] 2224 | }, 2225 | { 2226 | "cell_type": "code", 2227 | "execution_count": 118, 2228 | "metadata": {}, 2229 | "outputs": [], 2230 | "source": [ 2231 | "a = np.arange(100000)" 2232 | ] 2233 | }, 2234 | { 2235 | "cell_type": "code", 2236 | "execution_count": 119, 2237 | "metadata": { 2238 | "scrolled": true 2239 | }, 2240 | "outputs": [ 2241 | { 2242 | "name": "stdout", 2243 | "output_type": "stream", 2244 | "text": [ 2245 | "CPU times: user 1.06 ms, sys: 279 µs, total: 1.34 ms\n", 2246 | "Wall time: 701 µs\n" 2247 | ] 2248 | }, 2249 | { 2250 | "data": { 2251 | "text/plain": [ 2252 | "333328333350000" 2253 | ] 2254 | }, 2255 | "execution_count": 119, 2256 | "metadata": {}, 2257 | "output_type": "execute_result" 2258 | } 2259 | ], 2260 | "source": [ 2261 | "%time np.sum(a ** 2)" 2262 | ] 2263 | }, 2264 | { 2265 | "cell_type": "code", 2266 | "execution_count": 120, 2267 | "metadata": { 2268 | "scrolled": true 2269 | }, 2270 | "outputs": [ 2271 | { 2272 | "name": "stdout", 2273 | "output_type": "stream", 2274 | "text": [ 2275 | "CPU times: user 36.1 ms, sys: 0 ns, total: 36.1 ms\n", 2276 | "Wall time: 35.5 ms\n" 2277 | ] 2278 | }, 2279 | { 2280 | "data": { 2281 | "text/plain": [ 2282 | "333328333350000" 2283 | ] 2284 | }, 2285 | "execution_count": 120, 2286 | "metadata": {}, 2287 | "output_type": "execute_result" 2288 | } 2289 | ], 2290 | "source": [ 2291 | "%time sum([x ** 2 for x in l])" 2292 | ] 2293 | }, 2294 | { 2295 | "cell_type": "markdown", 2296 | "metadata": {}, 2297 | "source": [ 2298 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 2299 | "\n", 2300 | "## Useful Numpy functions" 2301 | ] 2302 | }, 2303 | { 2304 | "cell_type": "markdown", 2305 | "metadata": {}, 2306 | "source": [ 2307 | "### `random` " 2308 | ] 2309 | }, 2310 | { 2311 | "cell_type": "code", 2312 | "execution_count": null, 2313 | "metadata": {}, 2314 | "outputs": [], 2315 | "source": [ 2316 | "np.random.random(size=2)" 2317 | ] 2318 | }, 2319 | { 2320 | "cell_type": "code", 2321 | "execution_count": null, 2322 | "metadata": {}, 2323 | "outputs": [], 2324 | "source": [ 2325 | "np.random.normal(size=2)" 2326 | ] 2327 | }, 2328 | { 2329 | "cell_type": "code", 2330 | "execution_count": null, 2331 | "metadata": {}, 2332 | "outputs": [], 2333 | "source": [ 2334 | "np.random.rand(2, 4)" 2335 | ] 2336 | }, 2337 | { 2338 | "cell_type": "markdown", 2339 | "metadata": {}, 2340 | "source": [ 2341 | "---\n", 2342 | "### `arange`" 2343 | ] 2344 | }, 2345 | { 2346 | "cell_type": "code", 2347 | "execution_count": null, 2348 | "metadata": {}, 2349 | "outputs": [], 2350 | "source": [ 2351 | "np.arange(10)" 2352 | ] 2353 | }, 2354 | { 2355 | "cell_type": "code", 2356 | "execution_count": null, 2357 | "metadata": {}, 2358 | "outputs": [], 2359 | "source": [ 2360 | "np.arange(5, 10)" 2361 | ] 2362 | }, 2363 | { 2364 | "cell_type": "code", 2365 | "execution_count": null, 2366 | "metadata": {}, 2367 | "outputs": [], 2368 | "source": [ 2369 | "np.arange(0, 1, .1)" 2370 | ] 2371 | }, 2372 | { 2373 | "cell_type": "markdown", 2374 | "metadata": {}, 2375 | "source": [ 2376 | "---\n", 2377 | "### `reshape`" 2378 | ] 2379 | }, 2380 | { 2381 | "cell_type": "code", 2382 | "execution_count": null, 2383 | "metadata": {}, 2384 | "outputs": [], 2385 | "source": [ 2386 | "np.arange(10).reshape(2, 5)" 2387 | ] 2388 | }, 2389 | { 2390 | "cell_type": "code", 2391 | "execution_count": null, 2392 | "metadata": {}, 2393 | "outputs": [], 2394 | "source": [ 2395 | "np.arange(10).reshape(5, 2)" 2396 | ] 2397 | }, 2398 | { 2399 | "cell_type": "markdown", 2400 | "metadata": {}, 2401 | "source": [ 2402 | "---\n", 2403 | "### `linspace`" 2404 | ] 2405 | }, 2406 | { 2407 | "cell_type": "code", 2408 | "execution_count": null, 2409 | "metadata": {}, 2410 | "outputs": [], 2411 | "source": [ 2412 | "np.linspace(0, 1, 5)" 2413 | ] 2414 | }, 2415 | { 2416 | "cell_type": "code", 2417 | "execution_count": null, 2418 | "metadata": {}, 2419 | "outputs": [], 2420 | "source": [ 2421 | "np.linspace(0, 1, 20)" 2422 | ] 2423 | }, 2424 | { 2425 | "cell_type": "code", 2426 | "execution_count": null, 2427 | "metadata": { 2428 | "scrolled": true 2429 | }, 2430 | "outputs": [], 2431 | "source": [ 2432 | "np.linspace(0, 1, 20, False)" 2433 | ] 2434 | }, 2435 | { 2436 | "cell_type": "markdown", 2437 | "metadata": {}, 2438 | "source": [ 2439 | "---\n", 2440 | "### `zeros`, `ones`, `empty`" 2441 | ] 2442 | }, 2443 | { 2444 | "cell_type": "code", 2445 | "execution_count": null, 2446 | "metadata": {}, 2447 | "outputs": [], 2448 | "source": [ 2449 | "np.zeros(5)" 2450 | ] 2451 | }, 2452 | { 2453 | "cell_type": "code", 2454 | "execution_count": null, 2455 | "metadata": { 2456 | "scrolled": true 2457 | }, 2458 | "outputs": [], 2459 | "source": [ 2460 | "np.zeros((3, 3))" 2461 | ] 2462 | }, 2463 | { 2464 | "cell_type": "code", 2465 | "execution_count": null, 2466 | "metadata": { 2467 | "scrolled": true 2468 | }, 2469 | "outputs": [], 2470 | "source": [ 2471 | "np.zeros((3, 3), dtype=np.int)" 2472 | ] 2473 | }, 2474 | { 2475 | "cell_type": "code", 2476 | "execution_count": null, 2477 | "metadata": {}, 2478 | "outputs": [], 2479 | "source": [ 2480 | "np.ones(5)" 2481 | ] 2482 | }, 2483 | { 2484 | "cell_type": "code", 2485 | "execution_count": null, 2486 | "metadata": {}, 2487 | "outputs": [], 2488 | "source": [ 2489 | "np.ones((3, 3))" 2490 | ] 2491 | }, 2492 | { 2493 | "cell_type": "code", 2494 | "execution_count": null, 2495 | "metadata": {}, 2496 | "outputs": [], 2497 | "source": [ 2498 | "np.empty(5)" 2499 | ] 2500 | }, 2501 | { 2502 | "cell_type": "code", 2503 | "execution_count": null, 2504 | "metadata": { 2505 | "scrolled": true 2506 | }, 2507 | "outputs": [], 2508 | "source": [ 2509 | "np.empty((2, 2))" 2510 | ] 2511 | }, 2512 | { 2513 | "cell_type": "markdown", 2514 | "metadata": {}, 2515 | "source": [ 2516 | "---\n", 2517 | "### `identity` and `eye`" 2518 | ] 2519 | }, 2520 | { 2521 | "cell_type": "code", 2522 | "execution_count": null, 2523 | "metadata": {}, 2524 | "outputs": [], 2525 | "source": [ 2526 | "np.identity(3)" 2527 | ] 2528 | }, 2529 | { 2530 | "cell_type": "code", 2531 | "execution_count": null, 2532 | "metadata": {}, 2533 | "outputs": [], 2534 | "source": [ 2535 | "np.eye(3, 3)" 2536 | ] 2537 | }, 2538 | { 2539 | "cell_type": "code", 2540 | "execution_count": null, 2541 | "metadata": {}, 2542 | "outputs": [], 2543 | "source": [ 2544 | "np.eye(8, 4)" 2545 | ] 2546 | }, 2547 | { 2548 | "cell_type": "code", 2549 | "execution_count": null, 2550 | "metadata": { 2551 | "scrolled": true 2552 | }, 2553 | "outputs": [], 2554 | "source": [ 2555 | "np.eye(8, 4, k=1)" 2556 | ] 2557 | }, 2558 | { 2559 | "cell_type": "code", 2560 | "execution_count": null, 2561 | "metadata": {}, 2562 | "outputs": [], 2563 | "source": [ 2564 | "np.eye(8, 4, k=-3)" 2565 | ] 2566 | }, 2567 | { 2568 | "cell_type": "code", 2569 | "execution_count": null, 2570 | "metadata": { 2571 | "scrolled": true 2572 | }, 2573 | "outputs": [], 2574 | "source": [ 2575 | "\"Hello World\"[6]" 2576 | ] 2577 | }, 2578 | { 2579 | "cell_type": "markdown", 2580 | "metadata": {}, 2581 | "source": [ 2582 | "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)" 2583 | ] 2584 | } 2585 | ], 2586 | "metadata": { 2587 | "kernelspec": { 2588 | "display_name": "Python 3", 2589 | "language": "python", 2590 | "name": "python3" 2591 | }, 2592 | "language_info": { 2593 | "codemirror_mode": { 2594 | "name": "ipython", 2595 | "version": 3 2596 | }, 2597 | "file_extension": ".py", 2598 | "mimetype": "text/x-python", 2599 | "name": "python", 2600 | "nbconvert_exporter": "python", 2601 | "pygments_lexer": "ipython3", 2602 | "version": "3.8.1" 2603 | } 2604 | }, 2605 | "nbformat": 4, 2606 | "nbformat_minor": 4 2607 | } 2608 | -------------------------------------------------------------------------------- /3. NumPy exercises.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "![rmotr](https://user-images.githubusercontent.com/7065401/52071918-bda15380-2562-11e9-828c-7f95297e4a82.png)\n", 8 | "
\n", 9 | "\n", 10 | "# NumPy exercises\n" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": null, 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "# Import the numpy package under the name np\n", 20 | "import numpy as np\n", 21 | "\n", 22 | "# Print the numpy version and the configuration\n", 23 | "print(np.__version__)" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", 31 | "\n", 32 | "## Array creation" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "metadata": {}, 38 | "source": [ 39 | "### Create a numpy array of size 10, filled with zeros." 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": null, 45 | "metadata": {}, 46 | "outputs": [], 47 | "source": [ 48 | "# your code goes here\n" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": null, 54 | "metadata": { 55 | "cell_type": "solution" 56 | }, 57 | "outputs": [], 58 | "source": [ 59 | "#np.array([0] * 10)\n", 60 | "np.zeros(10)" 61 | ] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "metadata": {}, 66 | "source": [ 67 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 68 | "\n", 69 | "### Create a numpy array with values ranging from 10 to 49" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": null, 75 | "metadata": {}, 76 | "outputs": [], 77 | "source": [ 78 | "# your code goes here\n" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": null, 84 | "metadata": { 85 | "cell_type": "solution" 86 | }, 87 | "outputs": [], 88 | "source": [ 89 | "np.arange(10,50)" 90 | ] 91 | }, 92 | { 93 | "cell_type": "markdown", 94 | "metadata": {}, 95 | "source": [ 96 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 97 | "\n", 98 | "### Create a numpy matrix of 2*2 integers, filled with ones." 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": null, 104 | "metadata": {}, 105 | "outputs": [], 106 | "source": [ 107 | "# your code goes here\n" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": null, 113 | "metadata": { 114 | "cell_type": "solution" 115 | }, 116 | "outputs": [], 117 | "source": [ 118 | "np.ones([2,2], dtype=np.int)" 119 | ] 120 | }, 121 | { 122 | "cell_type": "markdown", 123 | "metadata": {}, 124 | "source": [ 125 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 126 | "\n", 127 | "### Create a numpy matrix of 3*2 float numbers, filled with ones." 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": null, 133 | "metadata": {}, 134 | "outputs": [], 135 | "source": [ 136 | "# your code goes here\n" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": null, 142 | "metadata": { 143 | "cell_type": "solution" 144 | }, 145 | "outputs": [], 146 | "source": [ 147 | "np.ones([3,2], dtype=np.float)" 148 | ] 149 | }, 150 | { 151 | "cell_type": "markdown", 152 | "metadata": {}, 153 | "source": [ 154 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 155 | "\n", 156 | "### Given the X numpy array, create a new numpy array with the same shape and type as X, filled with ones." 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": null, 162 | "metadata": {}, 163 | "outputs": [], 164 | "source": [ 165 | "# your code goes here\n" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": null, 171 | "metadata": { 172 | "cell_type": "solution" 173 | }, 174 | "outputs": [], 175 | "source": [ 176 | "X = np.arange(4, dtype=np.int)\n", 177 | "\n", 178 | "np.ones_like(X)" 179 | ] 180 | }, 181 | { 182 | "cell_type": "markdown", 183 | "metadata": {}, 184 | "source": [ 185 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 186 | "\n", 187 | "### Given the X numpy matrix, create a new numpy matrix with the same shape and type as X, filled with zeros." 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": null, 193 | "metadata": {}, 194 | "outputs": [], 195 | "source": [ 196 | "# your code goes here\n" 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": null, 202 | "metadata": { 203 | "cell_type": "solution" 204 | }, 205 | "outputs": [], 206 | "source": [ 207 | "X = np.array([[1,2,3], [4,5,6]], dtype=np.int)\n", 208 | "\n", 209 | "np.zeros_like(X)" 210 | ] 211 | }, 212 | { 213 | "cell_type": "markdown", 214 | "metadata": {}, 215 | "source": [ 216 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 217 | "\n", 218 | "### Create a numpy matrix of 4*4 integers, filled with fives." 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": null, 224 | "metadata": {}, 225 | "outputs": [], 226 | "source": [ 227 | "# your code goes here\n" 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": null, 233 | "metadata": { 234 | "cell_type": "solution" 235 | }, 236 | "outputs": [], 237 | "source": [ 238 | "np.ones([4,4], dtype=np.int) * 5" 239 | ] 240 | }, 241 | { 242 | "cell_type": "markdown", 243 | "metadata": {}, 244 | "source": [ 245 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 246 | "\n", 247 | "### Given the X numpy matrix, create a new numpy matrix with the same shape and type as X, filled with sevens." 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": null, 253 | "metadata": {}, 254 | "outputs": [], 255 | "source": [ 256 | "# your code goes here\n" 257 | ] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "execution_count": null, 262 | "metadata": { 263 | "cell_type": "solution" 264 | }, 265 | "outputs": [], 266 | "source": [ 267 | "X = np.array([[2,3], [6,2]], dtype=np.int)\n", 268 | "\n", 269 | "np.ones_like(X) * 7" 270 | ] 271 | }, 272 | { 273 | "cell_type": "markdown", 274 | "metadata": {}, 275 | "source": [ 276 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 277 | "\n", 278 | "### Create a 3*3 identity numpy matrix with ones on the diagonal and zeros elsewhere." 279 | ] 280 | }, 281 | { 282 | "cell_type": "code", 283 | "execution_count": null, 284 | "metadata": {}, 285 | "outputs": [], 286 | "source": [ 287 | "# your code goes here" 288 | ] 289 | }, 290 | { 291 | "cell_type": "code", 292 | "execution_count": null, 293 | "metadata": { 294 | "cell_type": "solution" 295 | }, 296 | "outputs": [], 297 | "source": [ 298 | "#np.eye(3)\n", 299 | "np.identity(3)" 300 | ] 301 | }, 302 | { 303 | "cell_type": "markdown", 304 | "metadata": {}, 305 | "source": [ 306 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 307 | "\n", 308 | "### Create a numpy array, filled with 3 random integer values between 1 and 10." 309 | ] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "execution_count": null, 314 | "metadata": {}, 315 | "outputs": [], 316 | "source": [ 317 | "# your code goes here\n" 318 | ] 319 | }, 320 | { 321 | "cell_type": "code", 322 | "execution_count": null, 323 | "metadata": { 324 | "cell_type": "solution" 325 | }, 326 | "outputs": [], 327 | "source": [ 328 | "np.random.randint(10, size=3)" 329 | ] 330 | }, 331 | { 332 | "cell_type": "markdown", 333 | "metadata": {}, 334 | "source": [ 335 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 336 | "\n", 337 | "### Create a 3\\*3\\*3 numpy matrix, filled with random float values." 338 | ] 339 | }, 340 | { 341 | "cell_type": "code", 342 | "execution_count": null, 343 | "metadata": {}, 344 | "outputs": [], 345 | "source": [ 346 | "# your code goes here\n" 347 | ] 348 | }, 349 | { 350 | "cell_type": "code", 351 | "execution_count": null, 352 | "metadata": { 353 | "cell_type": "solution" 354 | }, 355 | "outputs": [], 356 | "source": [ 357 | "#np.random.random((3,3,3)) \n", 358 | "np.random.randn(3,3,3) # 0 to 1 floats" 359 | ] 360 | }, 361 | { 362 | "cell_type": "markdown", 363 | "metadata": {}, 364 | "source": [ 365 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 366 | "\n", 367 | "### Given the X python list convert it to an Y numpy array" 368 | ] 369 | }, 370 | { 371 | "cell_type": "code", 372 | "execution_count": null, 373 | "metadata": {}, 374 | "outputs": [], 375 | "source": [ 376 | "# your code goes here\n" 377 | ] 378 | }, 379 | { 380 | "cell_type": "code", 381 | "execution_count": null, 382 | "metadata": { 383 | "cell_type": "solution" 384 | }, 385 | "outputs": [], 386 | "source": [ 387 | "X = [1, 2, 3]\n", 388 | "print(X, type(X))\n", 389 | "\n", 390 | "Y = np.array(X)\n", 391 | "print(Y, type(Y)) # different type" 392 | ] 393 | }, 394 | { 395 | "cell_type": "markdown", 396 | "metadata": {}, 397 | "source": [ 398 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 399 | "\n", 400 | "### Given the X numpy array, make a copy and store it on Y." 401 | ] 402 | }, 403 | { 404 | "cell_type": "code", 405 | "execution_count": null, 406 | "metadata": {}, 407 | "outputs": [], 408 | "source": [ 409 | "# your code goes here\n" 410 | ] 411 | }, 412 | { 413 | "cell_type": "code", 414 | "execution_count": null, 415 | "metadata": { 416 | "cell_type": "solution" 417 | }, 418 | "outputs": [], 419 | "source": [ 420 | "X = np.array([5,2,3], dtype=np.int)\n", 421 | "print(X, id(X))\n", 422 | "\n", 423 | "Y = np.copy(X)\n", 424 | "print(Y, id(Y)) # different id" 425 | ] 426 | }, 427 | { 428 | "cell_type": "markdown", 429 | "metadata": {}, 430 | "source": [ 431 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 432 | "\n", 433 | "### Create a numpy array with numbers from 1 to 10" 434 | ] 435 | }, 436 | { 437 | "cell_type": "code", 438 | "execution_count": null, 439 | "metadata": {}, 440 | "outputs": [], 441 | "source": [ 442 | "# your code goes here\n" 443 | ] 444 | }, 445 | { 446 | "cell_type": "code", 447 | "execution_count": null, 448 | "metadata": { 449 | "cell_type": "solution" 450 | }, 451 | "outputs": [], 452 | "source": [ 453 | "np.arange(1, 11)" 454 | ] 455 | }, 456 | { 457 | "cell_type": "markdown", 458 | "metadata": {}, 459 | "source": [ 460 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 461 | "\n", 462 | "### Create a numpy array with the odd numbers between 1 to 10" 463 | ] 464 | }, 465 | { 466 | "cell_type": "code", 467 | "execution_count": null, 468 | "metadata": {}, 469 | "outputs": [], 470 | "source": [ 471 | "# your code goes here\n" 472 | ] 473 | }, 474 | { 475 | "cell_type": "code", 476 | "execution_count": null, 477 | "metadata": { 478 | "cell_type": "solution" 479 | }, 480 | "outputs": [], 481 | "source": [ 482 | "np.arange(1, 11, 2)" 483 | ] 484 | }, 485 | { 486 | "cell_type": "markdown", 487 | "metadata": {}, 488 | "source": [ 489 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 490 | "\n", 491 | "### Create a numpy array with numbers from 1 to 10, in descending order." 492 | ] 493 | }, 494 | { 495 | "cell_type": "code", 496 | "execution_count": null, 497 | "metadata": {}, 498 | "outputs": [], 499 | "source": [ 500 | "# your code goes here\n" 501 | ] 502 | }, 503 | { 504 | "cell_type": "code", 505 | "execution_count": null, 506 | "metadata": { 507 | "cell_type": "solution" 508 | }, 509 | "outputs": [], 510 | "source": [ 511 | "np.arange(1, 11)[::-1]" 512 | ] 513 | }, 514 | { 515 | "cell_type": "markdown", 516 | "metadata": {}, 517 | "source": [ 518 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 519 | "\n", 520 | "### Create a 3*3 numpy matrix, filled with values ranging from 0 to 8" 521 | ] 522 | }, 523 | { 524 | "cell_type": "code", 525 | "execution_count": null, 526 | "metadata": {}, 527 | "outputs": [], 528 | "source": [ 529 | "# your code goes here\n" 530 | ] 531 | }, 532 | { 533 | "cell_type": "code", 534 | "execution_count": null, 535 | "metadata": { 536 | "cell_type": "solution" 537 | }, 538 | "outputs": [], 539 | "source": [ 540 | "np.arange(9).reshape(3,3)" 541 | ] 542 | }, 543 | { 544 | "cell_type": "markdown", 545 | "metadata": {}, 546 | "source": [ 547 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 548 | "\n", 549 | "### Show the memory size of the given Z numpy matrix" 550 | ] 551 | }, 552 | { 553 | "cell_type": "code", 554 | "execution_count": null, 555 | "metadata": {}, 556 | "outputs": [], 557 | "source": [ 558 | "# your code goes here\n" 559 | ] 560 | }, 561 | { 562 | "cell_type": "code", 563 | "execution_count": null, 564 | "metadata": { 565 | "cell_type": "solution", 566 | "scrolled": true 567 | }, 568 | "outputs": [], 569 | "source": [ 570 | "Z = np.zeros((10,10))\n", 571 | "\n", 572 | "print(\"%d bytes\" % (Z.size * Z.itemsize))" 573 | ] 574 | }, 575 | { 576 | "cell_type": "markdown", 577 | "metadata": {}, 578 | "source": [ 579 | "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", 580 | "\n", 581 | "## Array indexation\n" 582 | ] 583 | }, 584 | { 585 | "cell_type": "markdown", 586 | "metadata": {}, 587 | "source": [ 588 | "### Given the X numpy array, show it's first element" 589 | ] 590 | }, 591 | { 592 | "cell_type": "code", 593 | "execution_count": null, 594 | "metadata": {}, 595 | "outputs": [], 596 | "source": [ 597 | "# your code goes here\n" 598 | ] 599 | }, 600 | { 601 | "cell_type": "code", 602 | "execution_count": null, 603 | "metadata": { 604 | "cell_type": "solution" 605 | }, 606 | "outputs": [], 607 | "source": [ 608 | "X = np.array(['A','B','C','D','E'])\n", 609 | "\n", 610 | "X[0]" 611 | ] 612 | }, 613 | { 614 | "cell_type": "markdown", 615 | "metadata": {}, 616 | "source": [ 617 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 618 | "\n", 619 | "### Given the X numpy array, show it's last element" 620 | ] 621 | }, 622 | { 623 | "cell_type": "code", 624 | "execution_count": null, 625 | "metadata": {}, 626 | "outputs": [], 627 | "source": [ 628 | "# your code goes here\n" 629 | ] 630 | }, 631 | { 632 | "cell_type": "code", 633 | "execution_count": null, 634 | "metadata": { 635 | "cell_type": "solution" 636 | }, 637 | "outputs": [], 638 | "source": [ 639 | "X = np.array(['A','B','C','D','E'])\n", 640 | "\n", 641 | "#X[len(X)-1]\n", 642 | "X[-1]" 643 | ] 644 | }, 645 | { 646 | "cell_type": "markdown", 647 | "metadata": {}, 648 | "source": [ 649 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 650 | "\n", 651 | "### Given the X numpy array, show it's first three elements" 652 | ] 653 | }, 654 | { 655 | "cell_type": "code", 656 | "execution_count": null, 657 | "metadata": {}, 658 | "outputs": [], 659 | "source": [ 660 | "# your code goes here\n" 661 | ] 662 | }, 663 | { 664 | "cell_type": "code", 665 | "execution_count": null, 666 | "metadata": { 667 | "cell_type": "solution" 668 | }, 669 | "outputs": [], 670 | "source": [ 671 | "X = np.array(['A','B','C','D','E'])\n", 672 | "\n", 673 | "X[0:3] # remember! elements start at zero index" 674 | ] 675 | }, 676 | { 677 | "cell_type": "markdown", 678 | "metadata": {}, 679 | "source": [ 680 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 681 | "\n", 682 | "### Given the X numpy array, show all middle elements" 683 | ] 684 | }, 685 | { 686 | "cell_type": "code", 687 | "execution_count": null, 688 | "metadata": {}, 689 | "outputs": [], 690 | "source": [ 691 | "# your code goes here\n" 692 | ] 693 | }, 694 | { 695 | "cell_type": "code", 696 | "execution_count": null, 697 | "metadata": { 698 | "cell_type": "solution" 699 | }, 700 | "outputs": [], 701 | "source": [ 702 | "X = np.array(['A','B','C','D','E'])\n", 703 | "\n", 704 | "X[1:-1]" 705 | ] 706 | }, 707 | { 708 | "cell_type": "markdown", 709 | "metadata": {}, 710 | "source": [ 711 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 712 | "\n", 713 | "### Given the X numpy array, show the elements in reverse position" 714 | ] 715 | }, 716 | { 717 | "cell_type": "code", 718 | "execution_count": null, 719 | "metadata": {}, 720 | "outputs": [], 721 | "source": [ 722 | "# your code goes here\n" 723 | ] 724 | }, 725 | { 726 | "cell_type": "code", 727 | "execution_count": null, 728 | "metadata": { 729 | "cell_type": "solution" 730 | }, 731 | "outputs": [], 732 | "source": [ 733 | "X = np.array(['A','B','C','D','E'])\n", 734 | "\n", 735 | "X[::-1]" 736 | ] 737 | }, 738 | { 739 | "cell_type": "markdown", 740 | "metadata": {}, 741 | "source": [ 742 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 743 | "\n", 744 | "### Given the X numpy array, show the elements in an odd position" 745 | ] 746 | }, 747 | { 748 | "cell_type": "code", 749 | "execution_count": null, 750 | "metadata": {}, 751 | "outputs": [], 752 | "source": [ 753 | "# your code goes here\n" 754 | ] 755 | }, 756 | { 757 | "cell_type": "code", 758 | "execution_count": null, 759 | "metadata": { 760 | "cell_type": "solution" 761 | }, 762 | "outputs": [], 763 | "source": [ 764 | "X = np.array(['A','B','C','D','E'])\n", 765 | "\n", 766 | "#X[[0, 2, -1]]\n", 767 | "X[::2]" 768 | ] 769 | }, 770 | { 771 | "cell_type": "markdown", 772 | "metadata": {}, 773 | "source": [ 774 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 775 | "\n", 776 | "### Given the X numpy matrix, show the first row elements" 777 | ] 778 | }, 779 | { 780 | "cell_type": "code", 781 | "execution_count": null, 782 | "metadata": {}, 783 | "outputs": [], 784 | "source": [ 785 | "# your code goes here\n" 786 | ] 787 | }, 788 | { 789 | "cell_type": "code", 790 | "execution_count": null, 791 | "metadata": { 792 | "cell_type": "solution" 793 | }, 794 | "outputs": [], 795 | "source": [ 796 | "X = np.array([\n", 797 | " [1, 2, 3, 4],\n", 798 | " [5, 6, 7, 8],\n", 799 | " [9, 10, 11, 12],\n", 800 | " [13, 14, 15, 16]\n", 801 | "])\n", 802 | "\n", 803 | "X[0]" 804 | ] 805 | }, 806 | { 807 | "cell_type": "markdown", 808 | "metadata": {}, 809 | "source": [ 810 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 811 | "\n", 812 | "### Given the X numpy matrix, show the last row elements" 813 | ] 814 | }, 815 | { 816 | "cell_type": "code", 817 | "execution_count": null, 818 | "metadata": {}, 819 | "outputs": [], 820 | "source": [ 821 | "# your code goes here\n" 822 | ] 823 | }, 824 | { 825 | "cell_type": "code", 826 | "execution_count": null, 827 | "metadata": { 828 | "cell_type": "solution" 829 | }, 830 | "outputs": [], 831 | "source": [ 832 | "X = np.array([\n", 833 | " [1, 2, 3, 4],\n", 834 | " [5, 6, 7, 8],\n", 835 | " [9, 10, 11, 12],\n", 836 | " [13, 14, 15, 16]\n", 837 | "])\n", 838 | "\n", 839 | "X[-1]" 840 | ] 841 | }, 842 | { 843 | "cell_type": "markdown", 844 | "metadata": {}, 845 | "source": [ 846 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 847 | "\n", 848 | "### Given the X numpy matrix, show the first element on first row" 849 | ] 850 | }, 851 | { 852 | "cell_type": "code", 853 | "execution_count": null, 854 | "metadata": {}, 855 | "outputs": [], 856 | "source": [ 857 | "# your code goes here\n" 858 | ] 859 | }, 860 | { 861 | "cell_type": "code", 862 | "execution_count": null, 863 | "metadata": { 864 | "cell_type": "solution" 865 | }, 866 | "outputs": [], 867 | "source": [ 868 | "X = np.array([\n", 869 | " [1, 2, 3, 4],\n", 870 | " [5, 6, 7, 8],\n", 871 | " [9, 10, 11, 12],\n", 872 | " [13, 14, 15, 16]\n", 873 | "])\n", 874 | "\n", 875 | "#X[0][0]\n", 876 | "X[0, 0]" 877 | ] 878 | }, 879 | { 880 | "cell_type": "markdown", 881 | "metadata": {}, 882 | "source": [ 883 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 884 | "\n", 885 | "### Given the X numpy matrix, show the last element on last row" 886 | ] 887 | }, 888 | { 889 | "cell_type": "code", 890 | "execution_count": null, 891 | "metadata": {}, 892 | "outputs": [], 893 | "source": [ 894 | "# your code goes here\n" 895 | ] 896 | }, 897 | { 898 | "cell_type": "code", 899 | "execution_count": null, 900 | "metadata": { 901 | "cell_type": "solution" 902 | }, 903 | "outputs": [], 904 | "source": [ 905 | "X = np.array([\n", 906 | " [1, 2, 3, 4],\n", 907 | " [5, 6, 7, 8],\n", 908 | " [9, 10, 11, 12],\n", 909 | " [13, 14, 15, 16]\n", 910 | "])\n", 911 | "\n", 912 | "#X[-1][-1]\n", 913 | "X[-1, -1]" 914 | ] 915 | }, 916 | { 917 | "cell_type": "markdown", 918 | "metadata": {}, 919 | "source": [ 920 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 921 | "\n", 922 | "### Given the X numpy matrix, show the middle row elements" 923 | ] 924 | }, 925 | { 926 | "cell_type": "code", 927 | "execution_count": null, 928 | "metadata": {}, 929 | "outputs": [], 930 | "source": [ 931 | "# your code goes here\n" 932 | ] 933 | }, 934 | { 935 | "cell_type": "code", 936 | "execution_count": null, 937 | "metadata": { 938 | "cell_type": "solution" 939 | }, 940 | "outputs": [], 941 | "source": [ 942 | "X = np.array([\n", 943 | " [1, 2, 3, 4],\n", 944 | " [5, 6, 7, 8],\n", 945 | " [9, 10, 11, 12],\n", 946 | " [13, 14, 15, 16]\n", 947 | "])\n", 948 | "\n", 949 | "#X[1:-1][1:-1] wrong!\n", 950 | "X[1:-1, 1:-1]" 951 | ] 952 | }, 953 | { 954 | "cell_type": "markdown", 955 | "metadata": {}, 956 | "source": [ 957 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 958 | "\n", 959 | "### Given the X numpy matrix, show the first two elements on the first two rows" 960 | ] 961 | }, 962 | { 963 | "cell_type": "code", 964 | "execution_count": null, 965 | "metadata": {}, 966 | "outputs": [], 967 | "source": [ 968 | "# your code goes here\n" 969 | ] 970 | }, 971 | { 972 | "cell_type": "code", 973 | "execution_count": null, 974 | "metadata": { 975 | "cell_type": "solution" 976 | }, 977 | "outputs": [], 978 | "source": [ 979 | "X = np.array([\n", 980 | " [1, 2, 3, 4],\n", 981 | " [5, 6, 7, 8],\n", 982 | " [9, 10, 11, 12],\n", 983 | " [13, 14, 15, 16]\n", 984 | "])\n", 985 | "\n", 986 | "#X[:2][:2] wrong!\n", 987 | "#X[0:2, 0:2]\n", 988 | "X[:2, :2]" 989 | ] 990 | }, 991 | { 992 | "cell_type": "markdown", 993 | "metadata": {}, 994 | "source": [ 995 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 996 | "\n", 997 | "### Given the X numpy matrix, show the last two elements on the last two rows" 998 | ] 999 | }, 1000 | { 1001 | "cell_type": "code", 1002 | "execution_count": null, 1003 | "metadata": {}, 1004 | "outputs": [], 1005 | "source": [ 1006 | "# your code goes here\n" 1007 | ] 1008 | }, 1009 | { 1010 | "cell_type": "code", 1011 | "execution_count": null, 1012 | "metadata": { 1013 | "cell_type": "solution" 1014 | }, 1015 | "outputs": [], 1016 | "source": [ 1017 | "X = np.array([\n", 1018 | " [1, 2, 3, 4],\n", 1019 | " [5, 6, 7, 8],\n", 1020 | " [9, 10, 11, 12],\n", 1021 | " [13, 14, 15, 16]\n", 1022 | "])\n", 1023 | "\n", 1024 | "X[2:, 2:]" 1025 | ] 1026 | }, 1027 | { 1028 | "cell_type": "markdown", 1029 | "metadata": {}, 1030 | "source": [ 1031 | "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", 1032 | "\n", 1033 | "## Array manipulation\n" 1034 | ] 1035 | }, 1036 | { 1037 | "cell_type": "markdown", 1038 | "metadata": {}, 1039 | "source": [ 1040 | "### Convert the given integer numpy array to float" 1041 | ] 1042 | }, 1043 | { 1044 | "cell_type": "code", 1045 | "execution_count": null, 1046 | "metadata": {}, 1047 | "outputs": [], 1048 | "source": [ 1049 | "# your code goes here\n" 1050 | ] 1051 | }, 1052 | { 1053 | "cell_type": "code", 1054 | "execution_count": null, 1055 | "metadata": { 1056 | "cell_type": "solution" 1057 | }, 1058 | "outputs": [], 1059 | "source": [ 1060 | "X = [-5, -3, 0, 10, 40]\n", 1061 | "\n", 1062 | "np.array(X, np.float)" 1063 | ] 1064 | }, 1065 | { 1066 | "cell_type": "markdown", 1067 | "metadata": {}, 1068 | "source": [ 1069 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1070 | "\n", 1071 | "### Reverse the given numpy array (first element becomes last)" 1072 | ] 1073 | }, 1074 | { 1075 | "cell_type": "code", 1076 | "execution_count": null, 1077 | "metadata": {}, 1078 | "outputs": [], 1079 | "source": [ 1080 | "# your code goes here\n" 1081 | ] 1082 | }, 1083 | { 1084 | "cell_type": "code", 1085 | "execution_count": null, 1086 | "metadata": { 1087 | "cell_type": "solution" 1088 | }, 1089 | "outputs": [], 1090 | "source": [ 1091 | "X = [-5, -3, 0, 10, 40]\n", 1092 | "\n", 1093 | "X[::-1]" 1094 | ] 1095 | }, 1096 | { 1097 | "cell_type": "markdown", 1098 | "metadata": {}, 1099 | "source": [ 1100 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1101 | "\n", 1102 | "### Order (sort) the given numpy array" 1103 | ] 1104 | }, 1105 | { 1106 | "cell_type": "code", 1107 | "execution_count": null, 1108 | "metadata": {}, 1109 | "outputs": [], 1110 | "source": [ 1111 | "# your code goes here\n" 1112 | ] 1113 | }, 1114 | { 1115 | "cell_type": "code", 1116 | "execution_count": null, 1117 | "metadata": { 1118 | "cell_type": "solution" 1119 | }, 1120 | "outputs": [], 1121 | "source": [ 1122 | "X = [0, 10, -5, 40, -3]\n", 1123 | "\n", 1124 | "X.sort()\n", 1125 | "X" 1126 | ] 1127 | }, 1128 | { 1129 | "cell_type": "markdown", 1130 | "metadata": {}, 1131 | "source": [ 1132 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1133 | "\n", 1134 | "### Given the X numpy array, set the fifth element equal to 1" 1135 | ] 1136 | }, 1137 | { 1138 | "cell_type": "code", 1139 | "execution_count": null, 1140 | "metadata": {}, 1141 | "outputs": [], 1142 | "source": [ 1143 | "# your code goes here\n" 1144 | ] 1145 | }, 1146 | { 1147 | "cell_type": "code", 1148 | "execution_count": null, 1149 | "metadata": { 1150 | "cell_type": "solution" 1151 | }, 1152 | "outputs": [], 1153 | "source": [ 1154 | "X = np.zeros(10)\n", 1155 | "\n", 1156 | "X[4] = 1\n", 1157 | "X" 1158 | ] 1159 | }, 1160 | { 1161 | "cell_type": "markdown", 1162 | "metadata": {}, 1163 | "source": [ 1164 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1165 | "\n", 1166 | "### Given the X numpy array, change the 50 with a 40" 1167 | ] 1168 | }, 1169 | { 1170 | "cell_type": "code", 1171 | "execution_count": null, 1172 | "metadata": {}, 1173 | "outputs": [], 1174 | "source": [ 1175 | "# your code goes here\n" 1176 | ] 1177 | }, 1178 | { 1179 | "cell_type": "code", 1180 | "execution_count": null, 1181 | "metadata": { 1182 | "cell_type": "solution" 1183 | }, 1184 | "outputs": [], 1185 | "source": [ 1186 | "X = np.array([10, 20, 30, 50])\n", 1187 | "\n", 1188 | "X[3] = 40\n", 1189 | "X" 1190 | ] 1191 | }, 1192 | { 1193 | "cell_type": "markdown", 1194 | "metadata": {}, 1195 | "source": [ 1196 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1197 | "\n", 1198 | "### Given the X numpy matrix, change the last row with all 1" 1199 | ] 1200 | }, 1201 | { 1202 | "cell_type": "code", 1203 | "execution_count": null, 1204 | "metadata": {}, 1205 | "outputs": [], 1206 | "source": [ 1207 | "# your code goes here\n" 1208 | ] 1209 | }, 1210 | { 1211 | "cell_type": "code", 1212 | "execution_count": null, 1213 | "metadata": { 1214 | "cell_type": "solution" 1215 | }, 1216 | "outputs": [], 1217 | "source": [ 1218 | "X = np.array([\n", 1219 | " [1, 2, 3, 4],\n", 1220 | " [5, 6, 7, 8],\n", 1221 | " [9, 10, 11, 12],\n", 1222 | " [13, 14, 15, 16]\n", 1223 | "])\n", 1224 | "\n", 1225 | "X[-1] = np.array([1, 1, 1, 1])\n", 1226 | "X" 1227 | ] 1228 | }, 1229 | { 1230 | "cell_type": "markdown", 1231 | "metadata": {}, 1232 | "source": [ 1233 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1234 | "\n", 1235 | "### Given the X numpy matrix, change the last item on the last row with a 0" 1236 | ] 1237 | }, 1238 | { 1239 | "cell_type": "code", 1240 | "execution_count": null, 1241 | "metadata": {}, 1242 | "outputs": [], 1243 | "source": [ 1244 | "# your code goes here\n" 1245 | ] 1246 | }, 1247 | { 1248 | "cell_type": "code", 1249 | "execution_count": null, 1250 | "metadata": { 1251 | "cell_type": "solution" 1252 | }, 1253 | "outputs": [], 1254 | "source": [ 1255 | "X = np.array([\n", 1256 | " [1, 2, 3, 4],\n", 1257 | " [5, 6, 7, 8],\n", 1258 | " [9, 10, 11, 12],\n", 1259 | " [13, 14, 15, 16]\n", 1260 | "])\n", 1261 | "\n", 1262 | "X[-1, -1] = 0\n", 1263 | "X" 1264 | ] 1265 | }, 1266 | { 1267 | "cell_type": "markdown", 1268 | "metadata": {}, 1269 | "source": [ 1270 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1271 | "\n", 1272 | "### Given the X numpy matrix, add 5 to every element" 1273 | ] 1274 | }, 1275 | { 1276 | "cell_type": "code", 1277 | "execution_count": null, 1278 | "metadata": {}, 1279 | "outputs": [], 1280 | "source": [ 1281 | "# your code goes here\n" 1282 | ] 1283 | }, 1284 | { 1285 | "cell_type": "code", 1286 | "execution_count": null, 1287 | "metadata": { 1288 | "cell_type": "solution" 1289 | }, 1290 | "outputs": [], 1291 | "source": [ 1292 | "X = np.array([\n", 1293 | " [1, 2, 3, 4],\n", 1294 | " [5, 6, 7, 8],\n", 1295 | " [9, 10, 11, 12],\n", 1296 | " [13, 14, 15, 16]\n", 1297 | "])\n", 1298 | "\n", 1299 | "X + 5" 1300 | ] 1301 | }, 1302 | { 1303 | "cell_type": "markdown", 1304 | "metadata": {}, 1305 | "source": [ 1306 | "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", 1307 | "\n", 1308 | "## Boolean arrays _(also called masks)_\n" 1309 | ] 1310 | }, 1311 | { 1312 | "cell_type": "markdown", 1313 | "metadata": {}, 1314 | "source": [ 1315 | "### Given the X numpy array, make a mask showing negative elements" 1316 | ] 1317 | }, 1318 | { 1319 | "cell_type": "code", 1320 | "execution_count": null, 1321 | "metadata": {}, 1322 | "outputs": [], 1323 | "source": [ 1324 | "# your code goes here\n" 1325 | ] 1326 | }, 1327 | { 1328 | "cell_type": "code", 1329 | "execution_count": null, 1330 | "metadata": { 1331 | "cell_type": "solution" 1332 | }, 1333 | "outputs": [], 1334 | "source": [ 1335 | "X = np.array([-1,2,0,-4,5,6,0,0,-9,10])\n", 1336 | "\n", 1337 | "mask = X <= 0\n", 1338 | "mask" 1339 | ] 1340 | }, 1341 | { 1342 | "cell_type": "markdown", 1343 | "metadata": {}, 1344 | "source": [ 1345 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1346 | "\n", 1347 | "### Given the X numpy array, get the negative elements" 1348 | ] 1349 | }, 1350 | { 1351 | "cell_type": "code", 1352 | "execution_count": null, 1353 | "metadata": {}, 1354 | "outputs": [], 1355 | "source": [ 1356 | "# your code goes here\n" 1357 | ] 1358 | }, 1359 | { 1360 | "cell_type": "code", 1361 | "execution_count": null, 1362 | "metadata": { 1363 | "cell_type": "solution" 1364 | }, 1365 | "outputs": [], 1366 | "source": [ 1367 | "X = np.array([-1, 2, 0, -4, 5, 6, 0, 0, -9, 10])\n", 1368 | "\n", 1369 | "mask = X <= 0\n", 1370 | "X[mask]" 1371 | ] 1372 | }, 1373 | { 1374 | "cell_type": "markdown", 1375 | "metadata": {}, 1376 | "source": [ 1377 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1378 | "\n", 1379 | "### Given the X numpy array, get numbers higher than 5" 1380 | ] 1381 | }, 1382 | { 1383 | "cell_type": "code", 1384 | "execution_count": null, 1385 | "metadata": {}, 1386 | "outputs": [], 1387 | "source": [ 1388 | "# your code goes here\n" 1389 | ] 1390 | }, 1391 | { 1392 | "cell_type": "code", 1393 | "execution_count": null, 1394 | "metadata": { 1395 | "cell_type": "solution" 1396 | }, 1397 | "outputs": [], 1398 | "source": [ 1399 | "X = np.array([-1, 2, 0, -4, 5, 6, 0, 0, -9, 10])\n", 1400 | "\n", 1401 | "mask = X > 5\n", 1402 | "X[mask]" 1403 | ] 1404 | }, 1405 | { 1406 | "cell_type": "markdown", 1407 | "metadata": {}, 1408 | "source": [ 1409 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1410 | "\n", 1411 | "### Given the X numpy array, get numbers higher than the elements mean" 1412 | ] 1413 | }, 1414 | { 1415 | "cell_type": "code", 1416 | "execution_count": null, 1417 | "metadata": {}, 1418 | "outputs": [], 1419 | "source": [ 1420 | "# your code goes here\n" 1421 | ] 1422 | }, 1423 | { 1424 | "cell_type": "code", 1425 | "execution_count": null, 1426 | "metadata": { 1427 | "cell_type": "solution" 1428 | }, 1429 | "outputs": [], 1430 | "source": [ 1431 | "X = np.array([-1, 2, 0, -4, 5, 6, 0, 0, -9, 10])\n", 1432 | "\n", 1433 | "mask = X > X.mean()\n", 1434 | "X[mask]" 1435 | ] 1436 | }, 1437 | { 1438 | "cell_type": "markdown", 1439 | "metadata": {}, 1440 | "source": [ 1441 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1442 | "\n", 1443 | "### Given the X numpy array, get numbers equal to 2 or 10" 1444 | ] 1445 | }, 1446 | { 1447 | "cell_type": "code", 1448 | "execution_count": null, 1449 | "metadata": {}, 1450 | "outputs": [], 1451 | "source": [ 1452 | "# your code goes here\n" 1453 | ] 1454 | }, 1455 | { 1456 | "cell_type": "code", 1457 | "execution_count": null, 1458 | "metadata": { 1459 | "cell_type": "solution", 1460 | "scrolled": true 1461 | }, 1462 | "outputs": [], 1463 | "source": [ 1464 | "X = np.array([-1, 2, 0, -4, 5, 6, 0, 0, -9, 10])\n", 1465 | "\n", 1466 | "mask = (X == 2) | (X == 10)\n", 1467 | "X[mask]" 1468 | ] 1469 | }, 1470 | { 1471 | "cell_type": "markdown", 1472 | "metadata": {}, 1473 | "source": [ 1474 | "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", 1475 | "\n", 1476 | "## Logic functions\n" 1477 | ] 1478 | }, 1479 | { 1480 | "cell_type": "markdown", 1481 | "metadata": {}, 1482 | "source": [ 1483 | "### Given the X numpy array, return True if none of its elements is zero" 1484 | ] 1485 | }, 1486 | { 1487 | "cell_type": "code", 1488 | "execution_count": null, 1489 | "metadata": {}, 1490 | "outputs": [], 1491 | "source": [ 1492 | "# your code goes here\n" 1493 | ] 1494 | }, 1495 | { 1496 | "cell_type": "code", 1497 | "execution_count": null, 1498 | "metadata": { 1499 | "cell_type": "solution" 1500 | }, 1501 | "outputs": [], 1502 | "source": [ 1503 | "X = np.array([-1, 2, 0, -4, 5, 6, 0, 0, -9, 10])\n", 1504 | "\n", 1505 | "X.all()" 1506 | ] 1507 | }, 1508 | { 1509 | "cell_type": "markdown", 1510 | "metadata": {}, 1511 | "source": [ 1512 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1513 | "\n", 1514 | "### Given the X numpy array, return True if any of its elements is zero" 1515 | ] 1516 | }, 1517 | { 1518 | "cell_type": "code", 1519 | "execution_count": null, 1520 | "metadata": {}, 1521 | "outputs": [], 1522 | "source": [ 1523 | "# your code goes here\n" 1524 | ] 1525 | }, 1526 | { 1527 | "cell_type": "code", 1528 | "execution_count": null, 1529 | "metadata": { 1530 | "cell_type": "solution" 1531 | }, 1532 | "outputs": [], 1533 | "source": [ 1534 | "X = np.array([-1, 2, 0, -4, 5, 6, 0, 0, -9, 10])\n", 1535 | "\n", 1536 | "X.any()" 1537 | ] 1538 | }, 1539 | { 1540 | "cell_type": "markdown", 1541 | "metadata": {}, 1542 | "source": [ 1543 | "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", 1544 | "\n", 1545 | "## Summary statistics" 1546 | ] 1547 | }, 1548 | { 1549 | "cell_type": "markdown", 1550 | "metadata": {}, 1551 | "source": [ 1552 | "### Given the X numpy array, show the sum of its elements" 1553 | ] 1554 | }, 1555 | { 1556 | "cell_type": "code", 1557 | "execution_count": null, 1558 | "metadata": {}, 1559 | "outputs": [], 1560 | "source": [ 1561 | "# your code goes here\n" 1562 | ] 1563 | }, 1564 | { 1565 | "cell_type": "code", 1566 | "execution_count": null, 1567 | "metadata": { 1568 | "cell_type": "solution" 1569 | }, 1570 | "outputs": [], 1571 | "source": [ 1572 | "X = np.array([3, 5, 6, 7, 2, 3, 4, 9, 4])\n", 1573 | "\n", 1574 | "#np.sum(X)\n", 1575 | "X.sum()" 1576 | ] 1577 | }, 1578 | { 1579 | "cell_type": "markdown", 1580 | "metadata": {}, 1581 | "source": [ 1582 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1583 | "\n", 1584 | "### Given the X numpy array, show the mean value of its elements" 1585 | ] 1586 | }, 1587 | { 1588 | "cell_type": "code", 1589 | "execution_count": null, 1590 | "metadata": {}, 1591 | "outputs": [], 1592 | "source": [ 1593 | "# your code goes here\n" 1594 | ] 1595 | }, 1596 | { 1597 | "cell_type": "code", 1598 | "execution_count": null, 1599 | "metadata": { 1600 | "cell_type": "solution" 1601 | }, 1602 | "outputs": [], 1603 | "source": [ 1604 | "X = np.array([1, 2, 0, 4, 5, 6, 0, 0, 9, 10])\n", 1605 | "\n", 1606 | "#np.mean(X)\n", 1607 | "X.mean()" 1608 | ] 1609 | }, 1610 | { 1611 | "cell_type": "markdown", 1612 | "metadata": {}, 1613 | "source": [ 1614 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1615 | "\n", 1616 | "### Given the X numpy matrix, show the sum of its columns" 1617 | ] 1618 | }, 1619 | { 1620 | "cell_type": "code", 1621 | "execution_count": null, 1622 | "metadata": {}, 1623 | "outputs": [], 1624 | "source": [ 1625 | "# your code goes here\n" 1626 | ] 1627 | }, 1628 | { 1629 | "cell_type": "code", 1630 | "execution_count": null, 1631 | "metadata": { 1632 | "cell_type": "solution" 1633 | }, 1634 | "outputs": [], 1635 | "source": [ 1636 | "X = np.array([\n", 1637 | " [1, 2, 3, 4],\n", 1638 | " [5, 6, 7, 8],\n", 1639 | " [9, 10, 11, 12],\n", 1640 | " [13, 14, 15, 16]\n", 1641 | "])\n", 1642 | "\n", 1643 | "X.sum(axis=0) # remember: axis=0 columns; axis=1 rows" 1644 | ] 1645 | }, 1646 | { 1647 | "cell_type": "markdown", 1648 | "metadata": {}, 1649 | "source": [ 1650 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1651 | "\n", 1652 | "### Given the X numpy matrix, show the mean value of its rows" 1653 | ] 1654 | }, 1655 | { 1656 | "cell_type": "code", 1657 | "execution_count": null, 1658 | "metadata": {}, 1659 | "outputs": [], 1660 | "source": [ 1661 | "# your code goes here\n" 1662 | ] 1663 | }, 1664 | { 1665 | "cell_type": "code", 1666 | "execution_count": null, 1667 | "metadata": { 1668 | "cell_type": "solution" 1669 | }, 1670 | "outputs": [], 1671 | "source": [ 1672 | "X = np.array([\n", 1673 | " [1, 2, 3, 4],\n", 1674 | " [5, 6, 7, 8],\n", 1675 | " [9, 10, 11, 12],\n", 1676 | " [13, 14, 15, 16]\n", 1677 | "])\n", 1678 | "\n", 1679 | "X.mean(axis=1) # remember: axis=0 columns; axis=1 rows" 1680 | ] 1681 | }, 1682 | { 1683 | "cell_type": "markdown", 1684 | "metadata": {}, 1685 | "source": [ 1686 | "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", 1687 | "\n", 1688 | "### Given the X numpy array, show the max value of its elements" 1689 | ] 1690 | }, 1691 | { 1692 | "cell_type": "code", 1693 | "execution_count": null, 1694 | "metadata": {}, 1695 | "outputs": [], 1696 | "source": [ 1697 | "# your code goes here\n" 1698 | ] 1699 | }, 1700 | { 1701 | "cell_type": "code", 1702 | "execution_count": null, 1703 | "metadata": { 1704 | "cell_type": "solution" 1705 | }, 1706 | "outputs": [], 1707 | "source": [ 1708 | "X = np.array([1, 2, 0, 4, 5, 6, 0, 0, 9, 10])\n", 1709 | "\n", 1710 | "#np.max(X)\n", 1711 | "X.max()" 1712 | ] 1713 | }, 1714 | { 1715 | "cell_type": "markdown", 1716 | "metadata": {}, 1717 | "source": [ 1718 | "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)" 1719 | ] 1720 | } 1721 | ], 1722 | "metadata": { 1723 | "kernelspec": { 1724 | "display_name": "Python 3", 1725 | "language": "python", 1726 | "name": "python3" 1727 | }, 1728 | "language_info": { 1729 | "codemirror_mode": { 1730 | "name": "ipython", 1731 | "version": 3 1732 | }, 1733 | "file_extension": ".py", 1734 | "mimetype": "text/x-python", 1735 | "name": "python", 1736 | "nbconvert_exporter": "python", 1737 | "pygments_lexer": "ipython3", 1738 | "version": "3.8.1" 1739 | } 1740 | }, 1741 | "nbformat": 4, 1742 | "nbformat_minor": 4 1743 | } 1744 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | #### We're in the process of adapting these notebooks into interactive projects in [DataWars](https://www.datawars.io/?utm_source=fccrepo&utm_medium=numpy). Sign up now, it's [completely free](https://www.datawars.io/?utm_source=fccrepo&utm_medium=numpy). 3 | 4 | Stay tuned! Have any questions? [Join our Discord](https://discord.gg/DSTe8tY38T) 5 | 6 | --- 7 | 8 | Created by Santiago Basulto. Connect with me on [X](https://x.com/santiagobasulto) or [LinkedIn](https://www.linkedin.com/in/santiagobasulto/) 9 | -------------------------------------------------------------------------------- /Untitled.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 14, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 15, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "n = 3" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 16, 24 | "metadata": {}, 25 | "outputs": [ 26 | { 27 | "data": { 28 | "text/plain": [ 29 | "8" 30 | ] 31 | }, 32 | "execution_count": 16, 33 | "metadata": {}, 34 | "output_type": "execute_result" 35 | } 36 | ], 37 | "source": [ 38 | "2 ** 3" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": null, 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [ 47 | "120" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 20, 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [ 56 | "x = 5" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 21, 62 | "metadata": {}, 63 | "outputs": [ 64 | { 65 | "data": { 66 | "text/plain": [ 67 | "numpy.int8" 68 | ] 69 | }, 70 | "execution_count": 21, 71 | "metadata": {}, 72 | "output_type": "execute_result" 73 | } 74 | ], 75 | "source": [ 76 | "np.int8" 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "metadata": {}, 82 | "source": [ 83 | "![numpy](numpy.png)" 84 | ] 85 | } 86 | ], 87 | "metadata": { 88 | "kernelspec": { 89 | "display_name": "Python 3", 90 | "language": "python", 91 | "name": "python3" 92 | }, 93 | "language_info": { 94 | "codemirror_mode": { 95 | "name": "ipython", 96 | "version": 3 97 | }, 98 | "file_extension": ".py", 99 | "mimetype": "text/x-python", 100 | "name": "python", 101 | "nbconvert_exporter": "python", 102 | "pygments_lexer": "ipython3", 103 | "version": "3.8.1" 104 | } 105 | }, 106 | "nbformat": 4, 107 | "nbformat_minor": 4 108 | } 109 | -------------------------------------------------------------------------------- /numpy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rmotr-curriculum/freecodecamp-intro-to-numpy/6f9b96e4c8b8333ed8a1b95f38e9b81e55d45fa4/numpy.png -------------------------------------------------------------------------------- /untitled.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rmotr-curriculum/freecodecamp-intro-to-numpy/6f9b96e4c8b8333ed8a1b95f38e9b81e55d45fa4/untitled.txt --------------------------------------------------------------------------------