{ "cells": [ { "cell_type": "markdown", "id": "6073eb74", "metadata": {}, "source": [ "# In Class Activity - Python - More for-loops and numpy" ] }, { "cell_type": "markdown", "id": "bd755d44", "metadata": {}, "source": [ "### Problem 0: A more complex for-loop over a dictionary" ] }, { "cell_type": "markdown", "id": "67e11322", "metadata": {}, "source": [ "Here is a dictionary `purchases` that records a series of purchases, broken down into different categories. Each category is a list of the relevant purchases under that category." ] }, { "cell_type": "code", "execution_count": null, "id": "fb934ead", "metadata": {}, "outputs": [], "source": [ "purchases = {}\n", "purchases['entertainment'] = [100., 50.50, 22.12]\n", "purchases['food'] = [5.45, 1.20, 65.20, 5.99, 12.23]\n", "purchases['transportation'] = [24.20, 26.11, 5.03, 9.99]" ] }, { "cell_type": "markdown", "id": "ce17ffbc", "metadata": {}, "source": [ "Write a nested for-loop to compute the total cost of all the purchases. Alternatively, you can use the python `sum` function and a single for-loop." ] }, { "cell_type": "code", "execution_count": null, "id": "b37b0b1e", "metadata": {}, "outputs": [], "source": [ "# your answer goes here, after this comment" ] }, { "cell_type": "markdown", "id": "a0c2970a", "metadata": {}, "source": [ "### Problem 1: Using numpy" ] }, { "cell_type": "markdown", "id": "7b90d30d", "metadata": {}, "source": [ "The code below imports numpy and creates a random matrix `A` with 5 rows and 4 columns. The entries in `A` are uniform random samples from $0,\\dots,2$." ] }, { "cell_type": "code", "execution_count": null, "id": "6182e6a2", "metadata": { "scrolled": true }, "outputs": [], "source": [ "import numpy as np\n", "import numpy.random as npr\n", "\n", "A = npr.randint(0,3,size=(5,4))\n", "A" ] }, { "cell_type": "markdown", "id": "062cb464", "metadata": {}, "source": [ "Use numpy to compute the sum of each column (google the numpy function for `sum`)" ] }, { "cell_type": "code", "execution_count": null, "id": "1e9dc7f8", "metadata": {}, "outputs": [], "source": [ "# Your answer here" ] }, { "cell_type": "markdown", "id": "1b73c8c0", "metadata": {}, "source": [ "Use numpy to compute the sum of each row" ] }, { "cell_type": "code", "execution_count": null, "id": "24f922bd", "metadata": {}, "outputs": [], "source": [ "# Your answer here" ] }, { "cell_type": "markdown", "id": "4d0b174f", "metadata": {}, "source": [ "Use numpy to compute the overall sum across the entire matrix" ] }, { "cell_type": "code", "execution_count": null, "id": "0f222f73", "metadata": {}, "outputs": [], "source": [ "# Your answer here" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.9" } }, "nbformat": 4, "nbformat_minor": 5 }