{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Load in NumPy (remember to pip install numpy first)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The Basics" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3]\n" ] } ], "source": [ "a = np.array([1,2,3], dtype='int32')\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[9. 8. 7.]\n", " [6. 5. 4.]]\n" ] } ], "source": [ "b = np.array([[9.0,8.0,7.0],[6.0,5.0,4.0]])\n", "print(b)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get Dimension\n", "a.ndim" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2, 3)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get Shape\n", "b.shape" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dtype('int32')" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get Type\n", "a.dtype" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get Size\n", "a.itemsize" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "12" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get total size\n", "a.nbytes" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get number of elements\n", "a.size" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Accessing/Changing specific elements, rows, columns, etc" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1 2 3 4 5 6 7]\n", " [ 8 9 10 11 12 13 14]]\n" ] } ], "source": [ "a = np.array([[1,2,3,4,5,6,7],[8,9,10,11,12,13,14]])\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "13" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get a specific element [r, c]\n", "a[1, 5]" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4, 5, 6, 7])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get a specific row \n", "a[0, :]" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 3, 10])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get a specific column\n", "a[:, 2]" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2, 4, 6])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Getting a little more fancy [startindex:endindex:stepsize]\n", "a[0, 1:-1:2]" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1 2 1 4 5 6 7]\n", " [ 8 9 2 11 12 20 14]]\n" ] } ], "source": [ "a[1,5] = 20\n", "\n", "a[:,2] = [1,2]\n", "print(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*3-d example" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[[1 2]\n", " [3 4]]\n", "\n", " [[5 6]\n", " [7 8]]]\n" ] } ], "source": [ "b = np.array([[[1,2],[3,4]],[[5,6],[7,8]]])\n", "print(b)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get specific element (work outside in)\n", "b[0,1,1]" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "# replace \n", "b[:,1,:] = [[9,9],[8,8]]" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[[1, 2],\n", " [9, 9]],\n", "\n", " [[5, 6],\n", " [8, 8]]])" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Initializing Different Types of Arrays" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0., 0., 0.],\n", " [0., 0., 0.]])" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# All 0s matrix\n", "np.zeros((2,3))" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[[1, 1],\n", " [1, 1]],\n", "\n", " [[1, 1],\n", " [1, 1]],\n", "\n", " [[1, 1],\n", " [1, 1]],\n", "\n", " [[1, 1],\n", " [1, 1]]])" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# All 1s matrix\n", "np.ones((4,2,2), dtype='int32')" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[99, 99],\n", " [99, 99]])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Any other number\n", "np.full((2,2), 99)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[4, 4, 4, 4, 4, 4, 4],\n", " [4, 4, 4, 4, 4, 4, 4]])" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Any other number (full_like)\n", "np.full_like(a, 4)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0.24480678, 0.71347348],\n", " [0.56163517, 0.80732991],\n", " [0.72750015, 0.65200353],\n", " [0.13660036, 0.92045687]])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Random decimal numbers\n", "np.random.rand(4,2)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-4, 2, 6],\n", " [ 6, -4, -3],\n", " [ 3, 2, 4]])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Random Integer values\n", "np.random.randint(-4,8, size=(3,3))" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1., 0., 0., 0., 0.],\n", " [0., 1., 0., 0., 0.],\n", " [0., 0., 1., 0., 0.],\n", " [0., 0., 0., 1., 0.],\n", " [0., 0., 0., 0., 1.]])" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# The identity matrix\n", "np.identity(5)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2 3]\n", " [1 2 3]\n", " [1 2 3]]\n" ] } ], "source": [ "# Repeat an array\n", "arr = np.array([[1,2,3]])\n", "r1 = np.repeat(arr,3, axis=0)\n", "print(r1)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1. 1. 1. 1. 1.]\n", " [1. 1. 1. 1. 1.]\n", " [1. 1. 1. 1. 1.]\n", " [1. 1. 1. 1. 1.]\n", " [1. 1. 1. 1. 1.]]\n", "[[0. 0. 0.]\n", " [0. 9. 0.]\n", " [0. 0. 0.]]\n", "[[1. 1. 1. 1. 1.]\n", " [1. 0. 0. 0. 1.]\n", " [1. 0. 9. 0. 1.]\n", " [1. 0. 0. 0. 1.]\n", " [1. 1. 1. 1. 1.]]\n" ] } ], "source": [ "output = np.ones((5,5))\n", "print(output)\n", "\n", "z = np.zeros((3,3))\n", "z[1,1] = 9\n", "print(z)\n", "\n", "output[1:-1,1:-1] = z\n", "print(output)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Be careful when copying arrays!!!" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3]\n" ] } ], "source": [ "a = np.array([1,2,3])\n", "b = a.copy()\n", "b[0] = 100\n", "\n", "print(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Mathematics" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3 4]\n" ] } ], "source": [ "a = np.array([1,2,3,4])\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([3, 4, 5, 6])" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a + 2" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([-1, 0, 1, 2])" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a - 2" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2, 4, 6, 8])" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a * 2" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0.5, 1. , 1.5, 2. ])" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a / 2" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2, 2, 4, 4])" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = np.array([1,0,1,0])\n", "a + b" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1, 4, 9, 16])" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a ** 2" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0.54030231, -0.41614684, -0.9899925 , -0.65364362])" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Take the sin\n", "np.cos(a)\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Linear Algebra" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1. 1. 1.]\n", " [1. 1. 1.]]\n", "[[2 2]\n", " [2 2]\n", " [2 2]]\n" ] }, { "data": { "text/plain": [ "array([[6., 6.],\n", " [6., 6.]])" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.ones((2,3))\n", "print(a)\n", "\n", "b = np.full((3,2), 2)\n", "print(b)\n", "\n", "np.matmul(a,b)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Find the determinant\n", "c = np.identity(3)\n", "np.linalg.det(c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Statistics" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3],\n", " [4, 5, 6]])" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stats = np.array([[1,2,3],[4,5,6]])\n", "stats" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.min(stats)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([3, 6])" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.max(stats, axis=1)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([5, 7, 9])" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sum(stats, axis=0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Reorganizing Arrays" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2 3 4]\n", " [5 6 7 8]]\n", "[[1]\n", " [2]\n", " [3]\n", " [4]\n", " [5]\n", " [6]\n", " [7]\n", " [8]]\n" ] } ], "source": [ "before = np.array([[1,2,3,4],[5,6,7,8]])\n", "print(before)\n", "\n", "after = before.reshape((8,1))\n", "print(after)" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3, 4],\n", " [5, 6, 7, 8],\n", " [1, 2, 3, 4],\n", " [5, 6, 7, 8]])" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Vertically stacking vectors\n", "v1 = np.array([1,2,3,4])\n", "v2 = np.array([5,6,7,8])\n", "\n", "np.vstack([v1,v2,v1,v2])" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1., 1., 1., 1., 0., 0.],\n", " [1., 1., 1., 1., 0., 0.]])" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Horizontal stack\n", "h1 = np.ones((2,4))\n", "h2 = np.zeros((2,2))\n", "\n", "np.hstack((h1,h2))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.10.9" } }, "nbformat": 4, "nbformat_minor": 2 }