{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# NumPy Fundamentals" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Indexing" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3],\n", " [4, 5, 6]])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_a = np.array([[1,2,3],[4,5,6]])\n", "array_a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Specific Values" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([4, 5, 6])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_a[1]\n", "\n", "## By adding numbers between square brackets, we can reference specific values of the array. \n", "## Python uses 0-indexing, so the first position has an index 0, the second position has index 1, and so on." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_a[1][0]\n", "\n", "## We can index every dimension of the array separately. " ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_a[1,0]\n", "\n", "## [1,0] is equivalent to [1][0] " ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 4])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_a[:,0]\n", "\n", "## The \":\" is equivalent to \"from start to end\" in this context. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Negative Indices" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_b = np.array([1,2,3])\n", "array_b[-1]\n", "\n", "## Negative indices mean traversing from the back. \n", "## No such thing as -0 , so the first negative index is -1." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3],\n", " [4, 5, 6]])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_a" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([4, 5, 6])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_a[-1]" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# array_a[-3] \n", "# Goes out of bounds, since -3 implies there are 3 rows. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Assigning Values" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3],\n", " [4, 5, 6]])" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_a = np.array([[1,2,3],[4,5,6]])\n", "array_a" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 9],\n", " [4, 5, 6]])" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_a[0,2] = 9\n", "array_a \n", "\n", "## Assign a value to an individual element." ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[9, 9, 9],\n", " [4, 5, 6]])" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_a[0] = 9\n", "array_a\n", "\n", "## Assign a value to an entire row." ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[9, 9, 9],\n", " [9, 5, 6]])" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_a[:,0] = 9\n", "array_a\n", "\n", "## Assign a value to an entire column." ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[8, 7, 8],\n", " [9, 5, 6]])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list_a = [8,7,8]\n", "\n", "array_a[0] = list_a\n", "array_a\n", "\n", "## Assign different values to an entire row via a list. " ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "numpy.ndarray" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(array_a[0])" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[9, 9, 9],\n", " [9, 9, 9]])" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_a[:] = 9\n", "array_a\n", "\n", "## Assign the same value to all the individual elements in the array." ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_a = 9\n", "array_a\n", "\n", "## Type assignment in Python is dynamic. Hence, a variable's type can change based on what values we assign to it. \n", "## Here, array_a changes from an ndarray to an integer." ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(array_a)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3],\n", " [4, 5, 6]])" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_a = np.array([[1,2,3],[4,5,6]])\n", "array_a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Elementwise Properties" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([7, 8, 9])" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_a = np.array([7,8,9])\n", "array_a" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3],\n", " [4, 5, 6]])" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_b = np.array([[1,2,3],[4,5,6]])\n", "array_b" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 2, 4, 6],\n", " [ 8, 10, 12]])" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_b * 2\n", "\n", "## Multiplying each element of array_b by 2" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 2]" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list_a = [1,2,3]\n", "list_a + [2]\n", "\n", "## Since lists don't work elementwise, we're concatenating [2] to list_a." ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 9, 10, 11])" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_a + 2\n", "\n", "## Elementwise addition adds 2 to each element of array_a." ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([28, 40, 54])" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_a * array_b[1]\n", "\n", "## Elementwise multiplication. \n", "## We multiply each individual element of array_a by its corresponding element in the second row of array_b." ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "array([[-6, -6, -6],\n", " [-3, -3, -3]])" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_b - array_a\n", "\n", "## The order of the elements matters for elementwise subtraction, division, as well as other operations. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Types of Data Supported by NumPy" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3],\n", " [4, 5, 6]])" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_a = np.array([[1,2,3],[4,5,6]])\n", "array_a" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1., 2., 3.],\n", " [4., 5., 6.]], dtype=float16)" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_a = np.array([[1,2,3],[4,5,6]], dtype = np.float16)\n", "array_a\n", "\n", "# Defining all the values as floats (decimals)." ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1.+0.j, 2.+0.j, 3.+0.j],\n", " [4.+0.j, 5.+0.j, 6.+0.j]], dtype=complex64)" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_a = np.array([[1,2,3],[4,5,6]], dtype = np.complex64)\n", "array_a\n", "\n", "# Defining all the values as complex numbers." ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ True, True, False],\n", " [ True, True, True]])" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_a = np.array([[1,2,0],[4,5,6]], dtype = np.bool)\n", "array_a\n", "\n", "# Defining all the values as Booleans." ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([['10', '2', '3'],\n", " ['4', '5', '6']], dtype=' A link to the documentation explaining the unicode abbreviation." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Characteristics of NumPy Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Universal Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "https://numpy.org/devdocs/reference/ufuncs.html <- A link to the documentation page on Universal Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Broadcasting" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3])" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_a = np.array([1,2,3])\n", "array_a" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1],\n", " [2]])" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_b = np.array([[1],[2]])\n", "array_b" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3],\n", " [4, 5, 6]])" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "matrix_C = np.array([[1,2,3],[4,5,6]])\n", "matrix_C" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[2, 3, 4],\n", " [6, 7, 8]])" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.add(array_b, matrix_C)\n", "\n", "## Adding up values, even though the arrays don't have matching shapes. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Type Casting" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[2., 3., 4.],\n", " [6., 7., 8.]])" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.add(array_b, matrix_C, dtype = np.float64)\n", "\n", "## We can define the datatyep" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Running over an Axis" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2., 5.])" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.mean(matrix_C, axis = 1)\n", "\n", "## Axis = 0 runs the function over every column. \n", "## Axis = 1 runs the function over every row. " ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3],\n", " [4, 5, 6]])" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "matrix_C " ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6" } }, "nbformat": 4, "nbformat_minor": 2 }