├── NumPy Tut.ipynb ├── README.md └── icecreamsales.csv /NumPy Tut.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### NumPy Tutorial" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "NumPy is an amazing scientific computing library that is used by numerous other Python Data Science libraries. It contains many mathematical, array and string functions that are extremely useful. Along with all the basic math functions you'll also find them for Linear Algebra, Statistics, Simulation, etc.\n", 15 | "\n", 16 | "I recommend that you use the Jupyter setup I use with Anaconda so that you'll have access to all of NumPys dependencies. \n", 17 | "\n", 18 | "NumPy utilizes vector (1D Arrays) and matrice arrays (2D Arrays). " 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": {}, 24 | "source": [ 25 | "### NumPy Arrays : Creating Arrays" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 3, 31 | "metadata": {}, 32 | "outputs": [], 33 | "source": [ 34 | "import numpy as np\n", 35 | "# Provides beautiful plots of numerous type that are either\n", 36 | "# static, animated and/or interactive\n", 37 | "import matplotlib.pylab as plt\n", 38 | "from numpy import random\n", 39 | "\n", 40 | "# A Python list\n", 41 | "list_1 = [1, 2, 3, 4, 5]\n", 42 | "\n", 43 | "# Create NumPy 1 dimensional (a axis) array list object of type byte (-128 to 127)\n", 44 | "# A N-dimensional array is a usyally fixed size multidimensional\n", 45 | "# array that contains items of the same type. \n", 46 | "np_arr_1 = np.array(list_1, dtype=np.int8)\n", 47 | "np_arr_1\n", 48 | "# Create multidimenional list\n", 49 | "m_list_1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n", 50 | "\n", 51 | "# Create NumPy multidimensional (2 axis) array without defining type\n", 52 | "np_m_arr_1 = np.array(m_list_1)\n", 53 | "\n", 54 | "# You can also create arrays by defining the start value,\n", 55 | "# stop value (up to but not include stop) and step amount\n", 56 | "np.arange(1, 10)\n", 57 | "\n", 58 | "# With floats define start, end and number of values\n", 59 | "np.linspace(0, 5, 7)\n", 60 | "\n", 61 | "# You can create a 3 item array of zeroes\n", 62 | "np.zeros(4)\n", 63 | "\n", 64 | "# You can create multidimensional arrays of zeroes by passing\n", 65 | "# a tuple with the 1st value being rows and the 2nd columns\n", 66 | "np.zeros((2, 3))\n", 67 | "\n", 68 | "# Create array of 1s\n", 69 | "np.ones((2, 3))\n", 70 | "\n", 71 | "# Get number of items in the array\n", 72 | "np_m_arr_1.size\n", 73 | "\n", 74 | "# Create array with defined values\n", 75 | "np_arr_2 = np.array([1, 2, 3, 4, 5, 6])\n", 76 | "\n", 77 | "# Get type for array\n", 78 | "np_arr_2.dtype\n", 79 | "\n", 80 | "# Data Types\n", 81 | "# Boolean : np.bool_\n", 82 | "# Char : np.byte\n", 83 | "# Short : np.short\n", 84 | "# Integer : np.short\n", 85 | "# Long : np.int_\n", 86 | "# Float : np.single & np.float32\n", 87 | "# Double : np.double & np.float64\n", 88 | "# np.int8 : -128 to 127\n", 89 | "# np.int16 : -32768 to 32767\n", 90 | "# np.int32 : -2147483648 to 2147483647\n", 91 | "# np.int64 : -9223372036854775808 to 9223372036854775807\n", 92 | "\n", 93 | "# Create random 5 value 1D array from 10 to 50\n", 94 | "np.random.randint(10, 50, 5)\n", 95 | "# Create random matrix 2x3 with values between 10 and 50\n", 96 | "np.random.randint(10, 50, size=(2, 3))\n", 97 | "\n", 98 | "# Get help with a function\n", 99 | "np.random.randint?" 100 | ] 101 | }, 102 | { 103 | "cell_type": "markdown", 104 | "metadata": {}, 105 | "source": [ 106 | "### Slicing and Indexes" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 222, 112 | "metadata": {}, 113 | "outputs": [ 114 | { 115 | "name": "stdout", 116 | "output_type": "stream", 117 | "text": [ 118 | "[[10 2 3]\n", 119 | " [10 5 6]\n", 120 | " [10 8 9]]\n" 121 | ] 122 | }, 123 | { 124 | "data": { 125 | "text/plain": [ 126 | "array([ 2, 3, 5, 6, 8, 9, 10])" 127 | ] 128 | }, 129 | "execution_count": 222, 130 | "metadata": {}, 131 | "output_type": "execute_result" 132 | } 133 | ], 134 | "source": [ 135 | "# Change value at index\n", 136 | "# np_m_arr_1[0,0] = 2\n", 137 | "# np_m_arr_1.itemset((0,1), 1)\n", 138 | "np_m_arr_1\n", 139 | "# Get size of array\n", 140 | "np_m_arr_1.shape\n", 141 | "# Get value by index\n", 142 | "np_m_arr_1[0,1]\n", 143 | "np_m_arr_1.item(0,1)\n", 144 | "\n", 145 | "# Get specific indices\n", 146 | "np.take(np_m_arr_1, [0, 3, 6])\n", 147 | "# Replace provided index values with new values\n", 148 | "np.put(np_m_arr_1, [0, 3, 6], [10, 10, 10])\n", 149 | "print(np_m_arr_1)\n", 150 | "\n", 151 | "# Start at 1st through 5th with 2 step\n", 152 | "np_arr_1[:5:2]\n", 153 | "# Get 2nd value from each row\n", 154 | "np_m_arr_1[:,1]\n", 155 | "# Flip Array\n", 156 | "np_arr_1[::-1]\n", 157 | "\n", 158 | "# Get evens\n", 159 | "evens = np_m_arr_1[np_m_arr_1%2==0]\n", 160 | "evens\n", 161 | "\n", 162 | "# Get values > 5\n", 163 | "np_m_arr_1[np_m_arr_1 > 5]\n", 164 | "# 5 < value < 9\n", 165 | "np_m_arr_1[(np_m_arr_1 > 5) & (np_m_arr_1 < 9)]\n", 166 | "# 5 < value or value = 10\n", 167 | "np_m_arr_1[(np_m_arr_1 > 5) | (np_m_arr_1 == 10)]\n", 168 | "\n", 169 | "# Find uniques\n", 170 | "np.unique(np_m_arr_1)\n" 171 | ] 172 | }, 173 | { 174 | "cell_type": "markdown", 175 | "metadata": {}, 176 | "source": [ 177 | "### Reshaping Arrays" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": 165, 183 | "metadata": {}, 184 | "outputs": [ 185 | { 186 | "name": "stdout", 187 | "output_type": "stream", 188 | "text": [ 189 | "[[ 2 3 5 10 10]\n", 190 | " [ 0 6 8 9 10]]\n" 191 | ] 192 | }, 193 | { 194 | "data": { 195 | "text/plain": [ 196 | "array([[ 0, 3, 5, 9, 10],\n", 197 | " [ 2, 6, 8, 10, 10]])" 198 | ] 199 | }, 200 | "execution_count": 165, 201 | "metadata": {}, 202 | "output_type": "execute_result" 203 | } 204 | ], 205 | "source": [ 206 | "# Reshape array to 1 by 9\n", 207 | "np_m_arr_1.reshape((1, 9))\n", 208 | "np_m_arr_1\n", 209 | "# Reshape array to 2 by 5 (Items are either lost or 0s added)\n", 210 | "np_m_arr_1.resize((2,5))\n", 211 | "np_m_arr_1\n", 212 | "# Transpose axes\n", 213 | "np_m_arr_1.transpose()\n", 214 | "# Swap axes \n", 215 | "np_m_arr_1.swapaxes(0,1)\n", 216 | "# Flatten in order\n", 217 | "np_m_arr_1.flatten()\n", 218 | "# Flatten in column order\n", 219 | "np_m_arr_1.flatten('F')\n", 220 | "# Sort rows\n", 221 | "np_m_arr_1.sort(axis=1)\n", 222 | "print(np_m_arr_1)\n", 223 | "# Sort columns\n", 224 | "np_m_arr_1.sort(axis=0)\n", 225 | "np_m_arr_1" 226 | ] 227 | }, 228 | { 229 | "cell_type": "markdown", 230 | "metadata": {}, 231 | "source": [ 232 | "### Stacking & Splitting" 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": 195, 238 | "metadata": {}, 239 | "outputs": [ 240 | { 241 | "name": "stdout", 242 | "output_type": "stream", 243 | "text": [ 244 | "ss_arr_1\n", 245 | " [[5 9]\n", 246 | " [3 8]]\n", 247 | "ss_arr_2\n", 248 | " [[7 1]\n", 249 | " [5 6]]\n", 250 | "ss_arr_3\n", 251 | " [[5 9]]\n", 252 | "ss_arr_4\n", 253 | " [[7 1]]\n", 254 | "ss_arr_5\n", 255 | " [[6 7 1 4 1 1 2 2 1 5]\n", 256 | " [8 3 9 7 6 0 1 9 9 2]]\n" 257 | ] 258 | }, 259 | { 260 | "data": { 261 | "text/plain": [ 262 | "[array([[6, 7],\n", 263 | " [8, 3]]),\n", 264 | " array([[1, 4],\n", 265 | " [9, 7]]),\n", 266 | " array([[1, 1, 2, 2, 1, 5],\n", 267 | " [6, 0, 1, 9, 9, 2]])]" 268 | ] 269 | }, 270 | "execution_count": 195, 271 | "metadata": {}, 272 | "output_type": "execute_result" 273 | } 274 | ], 275 | "source": [ 276 | "# Generate random arrays\n", 277 | "ss_arr_1 = np.random.randint(10, size=(2, 2))\n", 278 | "print(\"ss_arr_1\\n\", ss_arr_1)\n", 279 | "ss_arr_2 = np.random.randint(10, size=(2, 2))\n", 280 | "print(\"ss_arr_2\\n\", ss_arr_2)\n", 281 | "\n", 282 | "# Stack arr_2 under arr_1\n", 283 | "np.vstack((ss_arr_1, ss_arr_2))\n", 284 | "# Stack horizontally\n", 285 | "np.hstack((ss_arr_1, ss_arr_2))\n", 286 | "\n", 287 | "# Delete 2nd row on each array\n", 288 | "ss_arr_3 = np.delete(ss_arr_1, 1, 0)\n", 289 | "ss_arr_4 = np.delete(ss_arr_2, 1, 0)\n", 290 | "print(\"ss_arr_3\\n\", ss_arr_3)\n", 291 | "print(\"ss_arr_4\\n\", ss_arr_4)\n", 292 | "# Combine arrays\n", 293 | "np.column_stack((ss_arr_3, ss_arr_4))\n", 294 | "# Stack in a 2D array\n", 295 | "np.row_stack((ss_arr_3, ss_arr_4))\n", 296 | "\n", 297 | "# Generate 2x10 array\n", 298 | "ss_arr_5 = np.random.randint(10, size=(2, 10))\n", 299 | "print(\"ss_arr_5\\n\", ss_arr_5)\n", 300 | "# Split into 5 arrays taking from both arrays in multidimensional array\n", 301 | "np.hsplit(ss_arr_5, 5)\n", 302 | "# Split after 2nd & 4th column\n", 303 | "np.hsplit(ss_arr_5, (2, 4))\n" 304 | ] 305 | }, 306 | { 307 | "cell_type": "markdown", 308 | "metadata": {}, 309 | "source": [ 310 | "### Copying" 311 | ] 312 | }, 313 | { 314 | "cell_type": "code", 315 | "execution_count": 209, 316 | "metadata": {}, 317 | "outputs": [ 318 | { 319 | "name": "stdout", 320 | "output_type": "stream", 321 | "text": [ 322 | "cp_arr_1\n", 323 | " [[2 4]\n", 324 | " [6 4]]\n", 325 | "cp_arr_2\n", 326 | " [[2 4]\n", 327 | " [6 4]]\n", 328 | "cp_arr_3\n", 329 | " [[2 4]\n", 330 | " [6 4]]\n", 331 | "cp_arr_3\n", 332 | " [2 6 4 4]\n", 333 | "cp_arr_1\n", 334 | " [[2 4]\n", 335 | " [6 4]]\n" 336 | ] 337 | } 338 | ], 339 | "source": [ 340 | "cp_arr_1 = np.random.randint(10, size=(2, 2))\n", 341 | "# Both variables point at the same array\n", 342 | "cp_arr_2 = cp_arr_1\n", 343 | "# Change value\n", 344 | "cp_arr_1[0,0] = 2\n", 345 | "print(\"cp_arr_1\\n\", cp_arr_1)\n", 346 | "print(\"cp_arr_2\\n\", cp_arr_2)\n", 347 | "# Create a view of data where changes don't effect original\n", 348 | "cp_arr_3 = cp_arr_1.view()\n", 349 | "print(\"cp_arr_3\\n\", cp_arr_3)\n", 350 | "cp_arr_3 = cp_arr_3.flatten('F')\n", 351 | "print(\"cp_arr_3\\n\", cp_arr_3)\n", 352 | "print(\"cp_arr_1\\n\", cp_arr_1)\n", 353 | "# Copy and create new array\n", 354 | "cp_arr_4 = cp_arr_1.copy()" 355 | ] 356 | }, 357 | { 358 | "cell_type": "markdown", 359 | "metadata": {}, 360 | "source": [ 361 | "### Basic Math" 362 | ] 363 | }, 364 | { 365 | "cell_type": "code", 366 | "execution_count": 4, 367 | "metadata": {}, 368 | "outputs": [ 369 | { 370 | "name": "stdout", 371 | "output_type": "stream", 372 | "text": [ 373 | "[[67 7 80]\n", 374 | " [67 56 17]]\n", 375 | "arr_3 [1 2 3 4]\n", 376 | "arr_4 [2 4 6 8]\n", 377 | "arr_5\n", 378 | " [[1 2]\n", 379 | " [3 4]]\n", 380 | "arr_6\n", 381 | " [[2 4]\n", 382 | " [6 9]]\n", 383 | "arr_7\n", 384 | " [[22 65 89]\n", 385 | " [62 17 79]\n", 386 | " [74 92 46]\n", 387 | " [94 69 48]\n", 388 | " [48 81 43]]\n" 389 | ] 390 | }, 391 | { 392 | "data": { 393 | "text/plain": [ 394 | "array([94, 92, 89])" 395 | ] 396 | }, 397 | "execution_count": 4, 398 | "metadata": {}, 399 | "output_type": "execute_result" 400 | } 401 | ], 402 | "source": [ 403 | "arr_3 = np.array([1, 2, 3, 4])\n", 404 | "arr_4 = np.array([2, 4, 6, 8])\n", 405 | "# Add values\n", 406 | "arr_3 + arr_4\n", 407 | "# Subtract\n", 408 | "arr_3 - arr_4\n", 409 | "# Multiply\n", 410 | "arr_3 * arr_4\n", 411 | "# Divide\n", 412 | "arr_3 / arr_4\n", 413 | "# Random 4 digit 1D array between 0 to 100\n", 414 | "arr_5 = random.randint(100, size=(4))\n", 415 | "arr_5\n", 416 | "# Random 2 by 3 digit 2D array between 0 to 100\n", 417 | "arr_6 = random.randint(100, size=(2, 3))\n", 418 | "arr_6\n", 419 | "# 4 random floats\n", 420 | "random.rand(4)\n", 421 | "# Get random value from an array\n", 422 | "random.choice(arr_3)\n", 423 | "\n", 424 | "# Sum of values in array\n", 425 | "arr_3.sum()\n", 426 | "# Sum columns\n", 427 | "print(arr_6)\n", 428 | "arr_6.sum(axis=0)\n", 429 | "# Cumulative sum of rows\n", 430 | "arr_6.cumsum(axis=1)\n", 431 | "\n", 432 | "# Min of each row\n", 433 | "arr_6.min(axis=1)\n", 434 | "# Max of each column\n", 435 | "arr_6.max(axis=0)\n", 436 | "\n", 437 | "print(\"arr_3\", arr_3)\n", 438 | "print(\"arr_4\", arr_4)\n", 439 | "# Add individual numbers to array\n", 440 | "np.add(arr_3, 5)\n", 441 | "# Add arrays\n", 442 | "np.add(arr_3, arr_4)\n", 443 | "# Subtract\n", 444 | "np.subtract(arr_3, arr_4)\n", 445 | "# Multiply\n", 446 | "np.multiply(arr_3, arr_4)\n", 447 | "# Divide\n", 448 | "np.divide(arr_3, arr_4)\n", 449 | "\n", 450 | "arr_5 = np.array([[1, 2], [3, 4]])\n", 451 | "arr_6 = np.array([[2, 4], [6, 9]])\n", 452 | "print(\"arr_5\\n\", arr_5)\n", 453 | "print(\"arr_6\\n\", arr_6)\n", 454 | "# Divides elements in 1st array by 2nd array and returns remainder\n", 455 | "np.remainder(arr_6, arr_5)\n", 456 | "\n", 457 | "# Return values in 1st array to powers defined in 2nd array\n", 458 | "np.power(arr_6, arr_5)\n", 459 | "# Square root\n", 460 | "np.sqrt(arr_3)\n", 461 | "# Cube root\n", 462 | "np.cbrt(arr_3)\n", 463 | "# Absolute value of every element\n", 464 | "np.absolute([-1, -2])\n", 465 | "# Exponential of all elements in array\n", 466 | "np.exp(arr_3)\n", 467 | "# log functions\n", 468 | "np.log(arr_3)\n", 469 | "np.log2(arr_3)\n", 470 | "np.log10(arr_3)\n", 471 | "# Greatest common divisor\n", 472 | "np.gcd.reduce([9, 12, 15])\n", 473 | "# Lowest common multiple\n", 474 | "np.lcm.reduce([9, 12, 15])\n", 475 | "\n", 476 | "# Round down\n", 477 | "np.floor([1.2, 2.5])\n", 478 | "# Round up\n", 479 | "np.ceil([1.2, 2.5])\n", 480 | "\n", 481 | "# Can receive 6 values and square them\n", 482 | "sq_arr = np.arange(6)**2\n", 483 | "sq_arr[arr_3]\n", 484 | "\n", 485 | "arr_7 = random.randint(100, size=(5, 3))\n", 486 | "print(\"arr_7\\n\", arr_7)\n", 487 | "# Get index for max value per column\n", 488 | "mc_index = arr_7.argmax(axis=0)\n", 489 | "mc_index\n", 490 | "# Get numbers corresponding to indexes\n", 491 | "max_nums = arr_7[mc_index]\n", 492 | "arr_7[mc_index, range(arr_7.shape[1])]" 493 | ] 494 | }, 495 | { 496 | "cell_type": "markdown", 497 | "metadata": {}, 498 | "source": [ 499 | "### Reading from Files" 500 | ] 501 | }, 502 | { 503 | "cell_type": "code", 504 | "execution_count": 6, 505 | "metadata": {}, 506 | "outputs": [ 507 | { 508 | "data": { 509 | "text/plain": [ 510 | "[array([], dtype=float64),\n", 511 | " array([ 37., 292.]),\n", 512 | " array([ 40., 228.]),\n", 513 | " array([ 49., 324.]),\n", 514 | " array([ 61., 376.]),\n", 515 | " array([ 72., 440.]),\n", 516 | " array([ 79., 496.]),\n", 517 | " array([ 83., 536.]),\n", 518 | " array([ 81., 556.]),\n", 519 | " array([ 75., 496.]),\n", 520 | " array([ 64., 412.]),\n", 521 | " array([ 53., 324.]),\n", 522 | " array([ 40., 320.])]" 523 | ] 524 | }, 525 | "execution_count": 6, 526 | "metadata": {}, 527 | "output_type": "execute_result" 528 | } 529 | ], 530 | "source": [ 531 | "# Pandas is used to manipulate tabular data and more\n", 532 | "import pandas as pd\n", 533 | "# Import using NumPy\n", 534 | "from numpy import genfromtxt\n", 535 | "\n", 536 | "# Read table of data from CSV file and convert to Numpy array\n", 537 | "ic_sales = pd.read_csv('icecreamsales.csv').to_numpy()\n", 538 | "ic_sales\n", 539 | "\n", 540 | "# Read data using NumPy\n", 541 | "ic_sales_2 = genfromtxt('icecreamsales.csv', delimiter=',')\n", 542 | "# Remove NANs\n", 543 | "ic_sales_2 = [row[~np.isnan(row)] for row in ic_sales_2]\n", 544 | "ic_sales_2" 545 | ] 546 | }, 547 | { 548 | "cell_type": "markdown", 549 | "metadata": {}, 550 | "source": [ 551 | "### Statistics Functions" 552 | ] 553 | }, 554 | { 555 | "cell_type": "code", 556 | "execution_count": 7, 557 | "metadata": {}, 558 | "outputs": [ 559 | { 560 | "name": "stdout", 561 | "output_type": "stream", 562 | "text": [ 563 | "ic_sales\n", 564 | " [[ 37 292]\n", 565 | " [ 40 228]\n", 566 | " [ 49 324]\n", 567 | " [ 61 376]\n", 568 | " [ 72 440]\n", 569 | " [ 79 496]\n", 570 | " [ 83 536]\n", 571 | " [ 81 556]\n", 572 | " [ 75 496]\n", 573 | " [ 64 412]\n", 574 | " [ 53 324]\n", 575 | " [ 40 320]]\n" 576 | ] 577 | }, 578 | { 579 | "data": { 580 | "text/plain": [ 581 | "array([256.01208459, 273.8864465 , 327.50953224, 399.00697989,\n", 582 | " 464.54630691, 506.25315137, 530.08563392, 518.16939265,\n", 583 | " 482.42066882, 416.88134181, 351.34201479, 273.8864465 ])" 584 | ] 585 | }, 586 | "execution_count": 7, 587 | "metadata": {}, 588 | "output_type": "execute_result" 589 | } 590 | ], 591 | "source": [ 592 | "# Array 1 - 5\n", 593 | "sarr_1 = np.arange(1, 6)\n", 594 | "np.mean(sarr_1)\n", 595 | "np.median(sarr_1)\n", 596 | "np.average(sarr_1)\n", 597 | "np.std([4, 6, 3, 5, 2]) # Standard Deviation\n", 598 | "np.var([4, 6, 3, 5, 2]) # Variance\n", 599 | "# Also nanmedian, nanmean, nanstd, nanvar\n", 600 | "\n", 601 | "print(\"ic_sales\\n\", ic_sales)\n", 602 | "# Get the 50th percentile of the data\n", 603 | "np.percentile(ic_sales, 50, axis=0)\n", 604 | "# Get 1st column\n", 605 | "ic_sales[:,0]\n", 606 | "\n", 607 | "# Correlation coefficient : Measure of correlation between data\n", 608 | "# Closer to 1 the more the data is correlated\n", 609 | "np.corrcoef(ic_sales[:,0], ic_sales[:,1])\n", 610 | "\n", 611 | "# Calculating Regression line\n", 612 | "# Σ(x-x̅)*(y-ȳ) / Σ(x-x̅)2\n", 613 | "temp_mean = np.mean(ic_sales[:,0])\n", 614 | "sales_mean = np.mean(ic_sales[:,1])\n", 615 | "numerator = np.sum(((ic_sales[:,0] - temp_mean)*(ic_sales[:,1] - sales_mean)))\n", 616 | "denominator = np.sum(np.square(ic_sales[:,0] - temp_mean))\n", 617 | "slope = numerator/denominator\n", 618 | "# Calculate y intercept\n", 619 | "y_i = sales_mean - slope * temp_mean\n", 620 | "y_i\n", 621 | "reg_arr = ic_sales[:,0] * slope + y_i\n", 622 | "reg_arr" 623 | ] 624 | }, 625 | { 626 | "cell_type": "markdown", 627 | "metadata": {}, 628 | "source": [ 629 | "### Trig Functions" 630 | ] 631 | }, 632 | { 633 | "cell_type": "code", 634 | "execution_count": 8, 635 | "metadata": {}, 636 | "outputs": [ 637 | { 638 | "data": { 639 | "text/plain": [ 640 | "14.142135623730951" 641 | ] 642 | }, 643 | "execution_count": 8, 644 | "metadata": {}, 645 | "output_type": "execute_result" 646 | } 647 | ], 648 | "source": [ 649 | "# Generate array of 200 values between -pi & pi\n", 650 | "t_arr = np.linspace(-np.pi, np.pi, 200)\n", 651 | "\n", 652 | "# Plot with x axis & y axis data \n", 653 | "# plt.plot(t_arr, np.sin(t_arr)) # SIN\n", 654 | "# plt.plot(t_arr, np.cos(t_arr)) # COS\n", 655 | "# plt.plot(t_arr, np.tan(t_arr)) # TAN\n", 656 | "# Display plot\n", 657 | "# plt.show()\n", 658 | "\n", 659 | "# Provides inverse of If y = cos(x), x = arccos(y)\n", 660 | "np.arcsin(1)\n", 661 | "np.arccos(1)\n", 662 | "np.arctan(0)\n", 663 | "\n", 664 | "# Also arctan2, sinh, cosh, tanh, arcsinh, arccosh, arctanh\n", 665 | "\n", 666 | "# Radians to degrees\n", 667 | "np.rad2deg(np.pi)\n", 668 | "# Degrees to radians\n", 669 | "np.deg2rad(180)\n", 670 | "\n", 671 | "# Hypotenuse c = √w² + h²\n", 672 | "np.hypot(10,10)" 673 | ] 674 | }, 675 | { 676 | "cell_type": "markdown", 677 | "metadata": {}, 678 | "source": [ 679 | "### Matrix Functions" 680 | ] 681 | }, 682 | { 683 | "cell_type": "code", 684 | "execution_count": 10, 685 | "metadata": {}, 686 | "outputs": [ 687 | { 688 | "name": "stdout", 689 | "output_type": "stream", 690 | "text": [ 691 | "arr_5\n", 692 | " [[1 2]\n", 693 | " [3 4]]\n", 694 | "arr_6\n", 695 | " [[2 4]\n", 696 | " [6 9]]\n", 697 | "arr_3\n", 698 | " [1 2 3 4]\n", 699 | "arr_4\n", 700 | " [2 4 6 8]\n" 701 | ] 702 | }, 703 | { 704 | "data": { 705 | "text/plain": [ 706 | "array([[1, 0],\n", 707 | " [0, 1]])" 708 | ] 709 | }, 710 | "execution_count": 10, 711 | "metadata": {}, 712 | "output_type": "execute_result" 713 | } 714 | ], 715 | "source": [ 716 | "from numpy import linalg as LA\n", 717 | "\n", 718 | "print(\"arr_5\\n\", arr_5)\n", 719 | "print(\"arr_6\\n\", arr_6)\n", 720 | "arr_8 = np.array([[5, 6], [7, 8]])\n", 721 | "\n", 722 | "# Matrix multiplication with Dot Product\n", 723 | "# (1 * 2) + (2 * 6) = 14 [0,0]\n", 724 | "# (1 * 4) + (2 * 9) = 22 [0,1]\n", 725 | "# (3 * 2) + (4 * 6) = 30 [1,0]\n", 726 | "# (3 * 4) + (4 * 9) = 12 + 36 = 48 [1,1]\n", 727 | "np.dot(arr_5, arr_6)\n", 728 | "# Compute dot product of 2 or more arrays\n", 729 | "LA.multi_dot([arr_5, arr_6, arr_8])\n", 730 | "\n", 731 | "# Inner product \n", 732 | "# (1 * 2) + (2 * 4) = 10 [0,0]\n", 733 | "# (1 * 6) + (2 * 9) = 24 [0,1]\n", 734 | "# (3 * 2) + (4 * 4) = 22 [1,0]\n", 735 | "# (3 * 6) + (4 * 9) = 54 [1,1]\n", 736 | "np.inner(arr_5, arr_6)\n", 737 | "np.dot(arr_5, arr_6)\n", 738 | "\n", 739 | "# Tensor Dot Product\n", 740 | "# (1 * 1) + (2 * 2) + (3 * 3) + (4 * 4) = 30\n", 741 | "# (5 * 1) + (6 * 2) + (7 * 3) + (8 * 4) = \n", 742 | "arr_9 = np.array([[[1, 2],\n", 743 | " [3, 4]],\n", 744 | " [[5, 6],\n", 745 | " [7, 8]]])\n", 746 | "arr_10 = np.array([[1, 2],\n", 747 | " [3, 4]], dtype=object)\n", 748 | "np.tensordot(arr_9, arr_10)\n", 749 | "\n", 750 | "# Einstein Summation : Provides many ways to perform\n", 751 | "# operations on multiple arrays\n", 752 | "arr_11 = np.array([0, 1])\n", 753 | "arr_12 = np.array([[0, 1, 2, 3], [4, 5, 6, 7]])\n", 754 | "# Left Side of -> : 1 axis for arr_11 and 2 axis for arr_12\n", 755 | "# Right of -> : Array we want (1D Array)\n", 756 | "# ij : Means multiply arr_11 single item by each column of arr_12 and sum\n", 757 | "# [0, 4 + 5 + 6 + 7]\n", 758 | "np.einsum('i,ij->i', arr_11, arr_12)\n", 759 | "# Sum values in arr_11\n", 760 | "np.einsum('i->', arr_11)\n", 761 | "# Dot Product\n", 762 | "print(\"arr_3\\n\", arr_3)\n", 763 | "print(\"arr_4\\n\", arr_4)\n", 764 | "np.einsum('i,i->', arr_3, arr_4)\n", 765 | "# Matrix multiplication\n", 766 | "np.einsum('ij,jk', arr_5, arr_6)\n", 767 | "# Get diagonal\n", 768 | "np.einsum('ii', arr_5)\n", 769 | "# Transpose\n", 770 | "np.einsum('ji', arr_5)\n", 771 | "\n", 772 | "# Raise matrix to the power of n\n", 773 | "# Given [[a, b], [c, d]]\n", 774 | "# [[a² + bc, ab +db], [ac + dc, d² + bc]\n", 775 | "LA.matrix_power(arr_5, 2)\n", 776 | "\n", 777 | "# Kronecker product of 2 arrays\n", 778 | "# Given [[a, b], [c, d]], [[e, f], [g, h]]\n", 779 | "# [[a*e, a*f, b*e, b*f], [a*g, a*h, b*g, b*h], ...]\n", 780 | "np.kron(arr_5, arr_6)\n", 781 | "\n", 782 | "# Compute eigenvalues\n", 783 | "LA.eig(arr_5) # Returns eigenvectors\n", 784 | "LA.eigvals(arr_5)\n", 785 | "\n", 786 | "# Get Vector Norm sqrt(sum(x**2))\n", 787 | "LA.norm(arr_5)\n", 788 | "\n", 789 | "# Get Multiplicative Inverse of a matrix\n", 790 | "LA.inv(arr_5)\n", 791 | "\n", 792 | "# Get Condition number of matrix\n", 793 | "LA.cond(arr_5)\n", 794 | "\n", 795 | "# Determinates are used to compute volume, area, to solve systems\n", 796 | "# of equations and more. It is a way you can multiply values in a\n", 797 | "# matrix to get 1 number.\n", 798 | "# For a matrix to have an inverse its determinate must not equal 0\n", 799 | "# det([[a, b], [c, d]]) = a*d - b*c\n", 800 | "arr_12 = np.array([[1, 2], [3, 4]])\n", 801 | "# 1*4 - 2*3 = -2\n", 802 | "LA.det(arr_12)\n", 803 | "\n", 804 | "# Determinate of 3x3 Matrix\n", 805 | "# det([[a, b, c], [d, e, f], [g, h, i]]) = a*e*i - b*d*i + c*d*h\n", 806 | "# - a*f*h + b*f*g - c*e*g\n", 807 | "\n", 808 | "# When we multiply a matrix times its inverse we get the identity\n", 809 | "# matrix [[1,0],[0,1]] for a 2x2 matrix\n", 810 | "# Calculate the inverse 1/(a*d - b*c) * [[d, -b], [-c, a]]\n", 811 | "# 1/(4 - 6) = -.5 -> [[-.5*4, -.5*-2], [-.5*-3, -.5*a]]\n", 812 | "arr_12_i = LA.inv(arr_12)\n", 813 | "arr_12_i\n", 814 | "\n", 815 | "np.dot(arr_12, arr_12_i)\n", 816 | "\n", 817 | "# Solving Systems of Linear Equations\n", 818 | "# If you have 3x + 5 = 9x -> 5 = 6x -> x = 5/6\n", 819 | "# If you have x + 4y = 10 & 6x + 18y = 42\n", 820 | "# Isolate x -> x = 10 - 4y\n", 821 | "# 6(10 - 4y) + 18y = 42 -> 60 - 24y + 18y = 42 - > -6y = -18 -> y = 3\n", 822 | "# x + 4*3 = 10 -> x = -2\n", 823 | "arr_13 = np.array([[1, 4], [6, 18]])\n", 824 | "arr_14 = np.array([10, 42])\n", 825 | "# Solve will solve this for you as well\n", 826 | "LA.solve(arr_13, arr_14)\n", 827 | "\n", 828 | "# Return a identity matrix with defined number of rows and columns\n", 829 | "np.eye(2, 2, dtype=int)" 830 | ] 831 | }, 832 | { 833 | "cell_type": "markdown", 834 | "metadata": {}, 835 | "source": [ 836 | "### Saving & Loading NumPy Objects" 837 | ] 838 | }, 839 | { 840 | "cell_type": "code", 841 | "execution_count": 12, 842 | "metadata": {}, 843 | "outputs": [ 844 | { 845 | "data": { 846 | "text/plain": [ 847 | "array([[1., 2.],\n", 848 | " [3., 4.]])" 849 | ] 850 | }, 851 | "execution_count": 12, 852 | "metadata": {}, 853 | "output_type": "execute_result" 854 | } 855 | ], 856 | "source": [ 857 | "arr_15 = np.array([[1, 2], [3, 4]])\n", 858 | "# Save as randarray.npy\n", 859 | "np.save('randarray', arr_15)\n", 860 | "# Load saved array \n", 861 | "arr_16 = np.load('randarray.npy')\n", 862 | "arr_16\n", 863 | "\n", 864 | "# Save as a CSV \n", 865 | "np.savetxt('randcsv.csv', arr_15)\n", 866 | "# Load CSV\n", 867 | "arr_17 = np.loadtxt('randcsv.csv')\n", 868 | "arr_17" 869 | ] 870 | }, 871 | { 872 | "cell_type": "markdown", 873 | "metadata": {}, 874 | "source": [ 875 | "### Financial Functions" 876 | ] 877 | }, 878 | { 879 | "cell_type": "code", 880 | "execution_count": 25, 881 | "metadata": {}, 882 | "outputs": [ 883 | { 884 | "name": "stdout", 885 | "output_type": "stream", 886 | "text": [ 887 | "1 -239.58 -23.12 2760.42\n", 888 | "2 -241.42 -21.28 2519.0\n", 889 | "3 -243.29 -19.42 2275.71\n", 890 | "4 -245.16 -17.54 2030.55\n", 891 | "5 -247.05 -15.65 1783.5\n", 892 | "6 -248.95 -13.75 1534.55\n", 893 | "7 -250.87 -11.83 1283.67\n", 894 | "8 -252.81 -9.89 1030.87\n", 895 | "9 -254.76 -7.95 776.11\n", 896 | "10 -256.72 -5.98 519.39\n", 897 | "11 -258.7 -4.0 260.69\n", 898 | "12 -260.69 -2.01 0.0\n" 899 | ] 900 | }, 901 | { 902 | "data": { 903 | "text/plain": [ 904 | "2898.6" 905 | ] 906 | }, 907 | "execution_count": 25, 908 | "metadata": {}, 909 | "output_type": "execute_result" 910 | } 911 | ], 912 | "source": [ 913 | "# Install in Conda terminal with\n", 914 | "# conda install pip\n", 915 | "# pip install numpy-financial\n", 916 | "import numpy_financial as npf\n", 917 | "\n", 918 | "# Compute future value of $400 investment every month\n", 919 | "# with an annual rate of 8% after 10 years\n", 920 | "npf.fv(.08/12, 10*12, -400, -400)\n", 921 | "\n", 922 | "# Calculate interest portion of payment on a loan of $3,000\n", 923 | "# at 9.25% per year compounded monthly\n", 924 | "# Period of loan (year)\n", 925 | "period = np.arange(1*12) + 1\n", 926 | "principle = 3000.00\n", 927 | "# Interest Payment\n", 928 | "ipmt = npf.ipmt(0.0925/12, period, 1*12, principle)\n", 929 | "# Principle Payment\n", 930 | "ppmt = npf.ppmt(0.0925/12, period, 1*12, principle)\n", 931 | "for payment in period:\n", 932 | " index = payment - 1\n", 933 | " principle = principle + ppmt[index]\n", 934 | " print(f\"{payment} {np.round(ppmt[index], 2)} {np.round(ipmt[index],2)} {np.round(principle, 2)}\")\n", 935 | "\n", 936 | "# Compute number of payments to pay off $3,000 if you paid\n", 937 | "# $150 per month with an interest rate of 9.25%\n", 938 | "np.round(npf.nper(0.0925/12, -150, 3000.00), 2)\n", 939 | "\n", 940 | "# Calculate net present value of cash flows of $4,000, $5,000\n", 941 | "# $6,000, $7,000 after $15,000 investment with .08 rate per period\n", 942 | "npf.npv(0.08, [-15000, 4000, 5000, 6000, 7000]).round(2)" 943 | ] 944 | }, 945 | { 946 | "cell_type": "markdown", 947 | "metadata": {}, 948 | "source": [ 949 | "### Comparison Functions" 950 | ] 951 | }, 952 | { 953 | "cell_type": "code", 954 | "execution_count": 157, 955 | "metadata": {}, 956 | "outputs": [ 957 | { 958 | "data": { 959 | "text/plain": [ 960 | "array([False, False])" 961 | ] 962 | }, 963 | "execution_count": 157, 964 | "metadata": {}, 965 | "output_type": "execute_result" 966 | } 967 | ], 968 | "source": [ 969 | "carr_1 = np.array([2, 3])\n", 970 | "carr_2 = np.array([3, 2])\n", 971 | "# Returns boolean based on whether arr_1 value Comparison arr_2 value\n", 972 | "np.greater(carr_1, carr_2)\n", 973 | "np.greater_equal(carr_1, carr_2)\n", 974 | "np.less(carr_1, carr_2)\n", 975 | "np.less_equal(carr_1, carr_2)\n", 976 | "np.not_equal(carr_1, carr_2)\n", 977 | "np.equal(carr_1, carr_2)" 978 | ] 979 | } 980 | ], 981 | "metadata": { 982 | "kernelspec": { 983 | "display_name": "Python 3", 984 | "language": "python", 985 | "name": "python3" 986 | }, 987 | "language_info": { 988 | "codemirror_mode": { 989 | "name": "ipython", 990 | "version": 3 991 | }, 992 | "file_extension": ".py", 993 | "mimetype": "text/x-python", 994 | "name": "python", 995 | "nbconvert_exporter": "python", 996 | "pygments_lexer": "ipython3", 997 | "version": "3.7.6" 998 | } 999 | }, 1000 | "nbformat": 4, 1001 | "nbformat_minor": 4 1002 | } 1003 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NumPy-Tutorial 2 | This is a cheat sheet I created for my NumPy learn in one tutorial. 3 | -------------------------------------------------------------------------------- /icecreamsales.csv: -------------------------------------------------------------------------------- 1 | Temperature,Sales 2 | 37,292 3 | 40,228 4 | 49,324 5 | 61,376 6 | 72,440 7 | 79,496 8 | 83,536 9 | 81,556 10 | 75,496 11 | 64,412 12 | 53,324 13 | 40,320 --------------------------------------------------------------------------------