|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# M1L2 NumPy Data Challenge: Basketball Stats Analysis\n", |
| 8 | + "\n", |
| 9 | + "## Scenario\n", |
| 10 | + "\n", |
| 11 | + "Imagine you're analyzing basketball player statistics. Each player has several stats, such as points scored, rebounds, and assists. You'll use NumPy to store and manipulate this data.\n", |
| 12 | + "\n", |
| 13 | + "\n", |
| 14 | + "## Learning Objectives\n", |
| 15 | + "\n", |
| 16 | + "1. Create and manipulate NumPy arrays.\n", |
| 17 | + "2. Work with multidimensional arrays (players as rows, stats as columns).\n", |
| 18 | + "3. Perform mathematical operations on arrays.\n", |
| 19 | + "4. Transpose arrays and understand their significance.\n", |
| 20 | + "5. Learn about correlation and calculate it using NumPy." |
| 21 | + ] |
| 22 | + }, |
| 23 | + { |
| 24 | + "cell_type": "markdown", |
| 25 | + "metadata": {}, |
| 26 | + "source": [ |
| 27 | + "### Step 1: Import NumPy" |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "code", |
| 32 | + "execution_count": null, |
| 33 | + "metadata": {}, |
| 34 | + "outputs": [], |
| 35 | + "source": [ |
| 36 | + "# Import NumPy \n", |
| 37 | + "None" |
| 38 | + ] |
| 39 | + }, |
| 40 | + { |
| 41 | + "cell_type": "markdown", |
| 42 | + "metadata": {}, |
| 43 | + "source": [ |
| 44 | + "### Step 2: Create a 1D Array \n", |
| 45 | + "\n", |
| 46 | + "Create a 1D array to store the points scored by 5 players in a game.\n", |
| 47 | + "Make up any 5 numbers you want (or you can research your favorite basketball team and get these 5 numbers)" |
| 48 | + ] |
| 49 | + }, |
| 50 | + { |
| 51 | + "cell_type": "code", |
| 52 | + "execution_count": null, |
| 53 | + "metadata": {}, |
| 54 | + "outputs": [], |
| 55 | + "source": [ |
| 56 | + "points = None\n", |
| 57 | + "print(points)" |
| 58 | + ] |
| 59 | + }, |
| 60 | + { |
| 61 | + "cell_type": "markdown", |
| 62 | + "metadata": {}, |
| 63 | + "source": [ |
| 64 | + "### Step 3: Create a 2D Array\n", |
| 65 | + "\n", |
| 66 | + "Now, create a 2D array where:\n", |
| 67 | + "- Each row represents a player (5 rows total).\n", |
| 68 | + "- Each column represents a stat (e.g., points, rebounds, assists)." |
| 69 | + ] |
| 70 | + }, |
| 71 | + { |
| 72 | + "cell_type": "code", |
| 73 | + "execution_count": null, |
| 74 | + "metadata": {}, |
| 75 | + "outputs": [], |
| 76 | + "source": [ |
| 77 | + "# Create a 2D array for player stats\n", |
| 78 | + "# Example: [[points, rebounds, assists], ...]\n", |
| 79 | + "\n", |
| 80 | + "player_stats = None" |
| 81 | + ] |
| 82 | + }, |
| 83 | + { |
| 84 | + "cell_type": "markdown", |
| 85 | + "metadata": {}, |
| 86 | + "source": [ |
| 87 | + "### Step 4: Perform Mathematical Operations\n", |
| 88 | + "\n", |
| 89 | + "Calculate the total stats for each player (sum of points, rebounds, and assists).\n", |
| 90 | + "\n", |
| 91 | + "**This is done for you however determine what axis=1 does. Remove it and run the cell then add it back in. This will be important for future code**" |
| 92 | + ] |
| 93 | + }, |
| 94 | + { |
| 95 | + "cell_type": "code", |
| 96 | + "execution_count": null, |
| 97 | + "metadata": {}, |
| 98 | + "outputs": [], |
| 99 | + "source": [ |
| 100 | + "# Calculate total stats for each player\n", |
| 101 | + "total_stats = np.sum(player_stats, axis=1)\n", |
| 102 | + "print(total_stats)" |
| 103 | + ] |
| 104 | + }, |
| 105 | + { |
| 106 | + "cell_type": "markdown", |
| 107 | + "metadata": {}, |
| 108 | + "source": [ |
| 109 | + "### Step 5: Transpose the Array\n", |
| 110 | + "\n", |
| 111 | + "Transpose the `player_stats` array so that rows become columns and vice versa. Use the NumPy documentation online to learn about the `transpose()` function." |
| 112 | + ] |
| 113 | + }, |
| 114 | + { |
| 115 | + "cell_type": "code", |
| 116 | + "execution_count": null, |
| 117 | + "metadata": {}, |
| 118 | + "outputs": [], |
| 119 | + "source": [ |
| 120 | + "# Transpose the array\n", |
| 121 | + "transposed_stats = None\n", |
| 122 | + "print(transposed_stats)" |
| 123 | + ] |
| 124 | + }, |
| 125 | + { |
| 126 | + "cell_type": "markdown", |
| 127 | + "metadata": {}, |
| 128 | + "source": [ |
| 129 | + "### Step 6: Correlation \n", |
| 130 | + "\n", |
| 131 | + "Correlation measures the relationship between two variables. For example, you can calculate the correlation between points scored and assists.\n", |
| 132 | + "\n", |
| 133 | + "Use the `np.corrcoef()` function to calculate the correlation between two columns in the `player_stats` array." |
| 134 | + ] |
| 135 | + }, |
| 136 | + { |
| 137 | + "cell_type": "code", |
| 138 | + "execution_count": null, |
| 139 | + "metadata": {}, |
| 140 | + "outputs": [], |
| 141 | + "source": [ |
| 142 | + "# Calculate correlation between points and assists -- what does the output mean?\n", |
| 143 | + "\n", |
| 144 | + "correlation = None(player_stats[:, 0], player_stats[:, 2])\n", |
| 145 | + "print(correlation)" |
| 146 | + ] |
| 147 | + }, |
| 148 | + { |
| 149 | + "cell_type": "markdown", |
| 150 | + "metadata": {}, |
| 151 | + "source": [ |
| 152 | + "## Above and Beyond (Optional Challenge)" |
| 153 | + ] |
| 154 | + }, |
| 155 | + { |
| 156 | + "cell_type": "markdown", |
| 157 | + "metadata": {}, |
| 158 | + "source": [ |
| 159 | + "### AAB Question 1: Find the Player with the Best Stat\n", |
| 160 | + "\n", |
| 161 | + "Task:\n", |
| 162 | + "Using the player_stats array (where rows represent players and columns represent stats), find the player who has the highest total stats (sum of all stats for each player).\n", |
| 163 | + "\n", |
| 164 | + "1) Calculate the total stats for each player (you may have already done this in a previous step).\n", |
| 165 | + "2) Use NumPy to find the index of the player with the highest total stats.\n", |
| 166 | + "3) Print the index of the player and their total stats.\n", |
| 167 | + "\n", |
| 168 | + "\n", |
| 169 | + "Hint: Use `np.sum()` to calculate totals and `np.argmax()` to find the index of the maximum value.\n" |
| 170 | + ] |
| 171 | + }, |
| 172 | + { |
| 173 | + "cell_type": "code", |
| 174 | + "execution_count": null, |
| 175 | + "metadata": {}, |
| 176 | + "outputs": [], |
| 177 | + "source": [ |
| 178 | + "# Player stats array\n", |
| 179 | + "player_stats = np.array([\n", |
| 180 | + " [25, 10, 5],\n", |
| 181 | + " [18, 7, 8],\n", |
| 182 | + " [30, 12, 4],\n", |
| 183 | + " [22, 9, 6],\n", |
| 184 | + " [15, 5, 7]\n", |
| 185 | + "])\n", |
| 186 | + "\n", |
| 187 | + "# Calculate total stats for each player\n", |
| 188 | + "total_stats = None\n", |
| 189 | + "\n", |
| 190 | + "# Find the index of the player with the highest total stats\n", |
| 191 | + "best_player_index = None\n", |
| 192 | + "\n", |
| 193 | + "# Print the result\n", |
| 194 | + "print(f\"Player with the highest total stats: Player {best_player_index + 1}\")\n", |
| 195 | + "print(f\"Total stats: {total_stats[best_player_index]}\")" |
| 196 | + ] |
| 197 | + } |
| 198 | + ], |
| 199 | + "metadata": { |
| 200 | + "kernelspec": { |
| 201 | + "display_name": "Python (learn-env)", |
| 202 | + "language": "python", |
| 203 | + "name": "learn-env" |
| 204 | + }, |
| 205 | + "language_info": { |
| 206 | + "codemirror_mode": { |
| 207 | + "name": "ipython", |
| 208 | + "version": 3 |
| 209 | + }, |
| 210 | + "file_extension": ".py", |
| 211 | + "mimetype": "text/x-python", |
| 212 | + "name": "python", |
| 213 | + "nbconvert_exporter": "python", |
| 214 | + "pygments_lexer": "ipython3", |
| 215 | + "version": "3.12.4" |
| 216 | + } |
| 217 | + }, |
| 218 | + "nbformat": 4, |
| 219 | + "nbformat_minor": 2 |
| 220 | +} |
0 commit comments