{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# NUMPY" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Arrays- Structured list of Numbers\n", "\n", "Vectors(1-D array), matrix(2-D,3-D array), Images(array of pixels value), Tensors(Combination of matrices value), convnets" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1, 2, 3, 4], [5, 6, 7, 8]]\n", "\n", "[[1 2 3 4]\n", " [5 6 7 8]]\n", "\n" ] } ], "source": [ "import numpy as np\n", "d=[[1,2,3,4],[5,6,7,8]]\n", "print(d)\n", "print(type(d))\n", "#convert d into an array with rows and columns\n", "arr = np.array(d)\n", "print(arr)\n", "print(type(arr))" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Dimension: (2, 4)\n", "Rows: 2\n", "Columns: 4\n", "Length: 2\n" ] } ], "source": [ "#This tells us the size of array in the form of - (rows,columns)\n", "dimension=arr.shape\n", "# No. of rows\n", "rows=arr.shape[0]\n", "#No. of columns\n", "columns=arr.shape[1]\n", "#No of channels\n", "#channels= arr.shape[2] -- applicable only when 3d matrix is there. for e.g - colourful image(RGB is a 3 dimensional matrix) \n", "#Length of array\n", "tot=len(arr)\n", "print(\"Dimension: \",dimension)\n", "print(\"Rows: \",rows)\n", "print(\"Columns: \",columns)\n", "#print(\"Channels: \",channels)\n", "print(\"Length: \",tot)\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "int32\n" ] } ], "source": [ "#datatype\n", "print(arr.dtype)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0. 0. 0. 0. 0. 0.]\n", " [0. 0. 0. 0. 0. 0.]\n", " [0. 0. 0. 0. 0. 0.]]\n" ] } ], "source": [ "#creating an array with all elements zero\n", "arr2 = np.zeros((3,6)) # with 3 rows and 6 Columns\n", "print(arr2)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0 1 2 3 4 5 6 7 8 9]\n" ] } ], "source": [ "#creating an array with elements ranging from 0 to n-1\n", "arr3 = np.arange(10)\n", "print(arr3)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1., 1., 1., 1., 1.],\n", " [1., 1., 1., 1., 1.]])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "##creating an array with all elements one\n", "z=np.ones((2,5))\n", "z" ] }, { "cell_type": "code", "execution_count": 7, "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": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "##creating an identity matrix\n", "z=np.eye(5,5)\n", "z" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[89, 89, 89],\n", " [89, 89, 89],\n", " [89, 89, 89]])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# creating a array with all values 'v'\n", "v = 89\n", "z=np.full((3,3),v) # creates a 3x3 matrix with all values as v \n", "z" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 1 1]\n", " [1 1 1]\n", " [1 1 1]]\n", "[[0 0 0]\n", " [0 0 0]\n", " [0 0 0]]\n" ] } ], "source": [ "# creating an array with same dimension as z but with all values 1\n", "z1=np.ones_like(z) # Thus, as dimension of z is 3x3, z1 will be a 3x3 matrix with all entries 1\n", "print(z1)\n", "# Similarly zeros_like(z) will create a matrix with all values 0\n", "z2=np.zeros_like(z)\n", "print(z2)\n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.74643431 0.71002042 0.67719732]\n", " [0.22304139 0.68259609 0.27216034]\n", " [0.90730328 0.35941384 0.3744247 ]\n", " [0.17858991 0.71641657 0.6704685 ]\n", " [0.34989429 0.86210942 0.93489585]]\n" ] } ], "source": [ "# creating array with random values\n", "z=np.random.rand(5,3)\n", "print(z)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Array - Slicing" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[910, 6, 7]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#printing the array elements from index 5 to 7\n", "arr=[1,2,3,4,6,910,6,7,90,7]\n", "arr[5:8]" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 2, 3, 4],\n", " [ 5, 6, 3, 7],\n", " [ 7, 8, 10, 6]])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#2-d array\n", "arr=np.array([[1,2,3,4],[5,6,3,7],[7,8,10,6]])\n", "arr" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[5, 6],\n", " [7, 8]])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 2-d array slicing:\n", "b=arr[1:3,:2] # Elements of row from: index 1 to 2 and elements of columns from: Starting(0) to 1\n", "b" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 6],\n", " [7, 8]])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Making changes in b\n", "b[0][0]=0\n", "b" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 2, 3, 4],\n", " [ 0, 6, 3, 7],\n", " [ 7, 8, 10, 6]])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Change in b will lead to change in arr\n", "arr" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Thus, arr[1][0] changed from 5 t0 0.\n", "\n", "So, if any changes are made in the array formed by slicing of another array, the original array is also changed." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0, 1, 2, 3, 4, 12, 12, 12, 8, 9])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#changing the elements from index a to b to some other value\n", "arr3 = np.arange(10)\n", "arr3[5:8]=12\n", "arr3" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0, 1, 2, 3, 4, 12, 12, 12, 8, 9])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ss = np.array([[arr3[1],arr3[2],arr3[3]]])\n", "ss[0]=3\n", "arr3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This doesnt make any change in arr3 i.e arr3[1] which is ss[0] is still the same. \n", "\n", "Thus the original array is altered only if the new array is formed by slicing, not otherwise." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Reshape, transpose & Sign" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0 1 2 3 4]\n", " [5 6 7 8 9]]\n" ] } ], "source": [ "#reshaping the array-deciding the number of rows and columns of an array\n", "re_arr= np.arange(10).reshape(2,5)\n", "print(re_arr)\n" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[5, 6, 7, 8, 9]])" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#printing the array from second row(index =1)\n", "re_arr[1:]" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 5],\n", " [1, 6],\n", " [2, 7],\n", " [3, 8],\n", " [4, 9]])" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Transposing the array --> Interchange rows and columns\n", "re_arr.T" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1, -1, 1],\n", " [-1, 1, 0]])" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Tells the sign of the array elements\n", "arr = np.array([[1,-3,4],[-6,7,0]])\n", "np.sign(arr) # 1 for positive, -1 for negative and 0 for 0\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Universal functions" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1. 1.73205081 2. ]\n", " [2.44948974 2.64575131 2.82842712]]\n" ] } ], "source": [ "arr = np.array([[1,3,4],[6,7,8]])\n", "#1) Evaluate square root of all the elements\n", "print(np.sqrt(arr))\n", "arr2 =np.sqrt(arr)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1. 1. 2.]\n", " [2. 2. 2.]]\n", "\n", "\n", "[[1. 2. 2.]\n", " [3. 3. 3.]]\n" ] } ], "source": [ "#2) floor - returns the greatest integer smaller than the number\n", " # ceil - returns the smallest integer greater than the number\n", "# e.g - 3 <3.4 < 4 --> 3 is floor(3.4) and 4 is ceil(3.4)\n", "print(np.floor(arr2))\n", "print('\\n')\n", "print(np.ceil(arr2))" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1. 2. 2.]\n", " [2. 3. 3.]]\n" ] } ], "source": [ "#3)rounding off the elements\n", "print(np.round(arr2))" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0. 0.54930614 0.69314718]\n", " [0.89587973 0.97295507 1.03972077]]\n" ] } ], "source": [ "#4) log of all elements\n", "print(np.log(arr2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Binary Functions" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[7 9 9]\n", "[ 1 -3 -5]\n", "[12 18 14]\n", "[1.33333333 0.5 0.28571429]\n", "[1 3 2]\n" ] } ], "source": [ "a = np.array([4,3,2])\n", "b= np.array([3,6,7])\n", "print(np.add(a,b))\n", "print(np.subtract(a,b))\n", "print(np.multiply(a,b))\n", "print(np.divide(a,b))\n", "print(np.mod(a,b)) #reminder" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Multiply: [[12 9 6]\n", " [24 18 12]\n", " [28 21 14]]\n", "Dot: [44]\n" ] } ], "source": [ "# Dot product and multiply\n", "# Multiply : multiplies each element of array a with other array b\n", "a = np.array([4,3,2])\n", "b= np.array([[3],[6],[7]])\n", "print(\"Multiply: \" ,np.multiply(a,b))\n", "# Dot product performs row to column matriux multiplication\n", "print(\"Dot: \",a.dot(b))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Logical Functions" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[4 3 2]\n", "[[3]\n", " [6]\n", " [7]]\n", "[[ True False False]\n", " [False False False]\n", " [False False False]]\n", "[[False False True]\n", " [ True True True]\n", " [ True True True]]\n", "[[ True True False]\n", " [False False False]\n", " [False False False]]\n", "[[False True False]\n", " [False False False]\n", " [False False False]]\n" ] } ], "source": [ "print(a)\n", "print(b)\n", "print(np.greater(a,b))\n", "print(np.less(a,b))\n", "print(np.greater_equal(a,b))\n", "print(np.equal(a,b))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Statistics Functions" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.2222222222222223\n", "1.617802197617893\n", "2.617283950617284\n" ] } ], "source": [ "arr = np.array([[1,2,3],[3,2,5],[5,2,6]])\n", "print(np.mean(arr)) # finds average of all elements\n", "print(np.std(arr)) # finds standard deviation of all elements\n", "print(np.var(arr)) #finds variance of all elements\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Append " ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 2, 3],\n", " [ 3, 2, 5],\n", " [ 5, 26, 32],\n", " [ 1, 8, 9]])" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#axis =0 : appends the arrays row wise i.e rows of second array will be appended below first array\n", "a= np.array([[1,2,3],[3,2,5]])\n", "b= np.array([[5,26,32],[1,8,9]])\n", "np.append(a,b,axis=0)\n" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 2, 3, 5, 26, 32],\n", " [ 3, 2, 5, 1, 8, 9]])" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#axis =1 : appends the arrays column wise i.e rows of second array will be appended to the right of first array\n", "a= np.array([[1,2,3],[3,2,5]])\n", "b= np.array([[5,26,32],[1,8,9]])\n", "np.append(a,b,axis=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Broadcasting" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Passing arguments of 1 variable to other." ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3],\n", " [4, 5, 6],\n", " [7, 8, 9]])" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x=np.array([[1,2,3],[4,5,6],[7,8,9]])\n", "x" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 0, 1]])" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v=np.array([[1,0,1]])\n", "v" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We want to add elements of vector v to every row of vector x: and form y" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 2, 2, 4],\n", " [ 5, 5, 7],\n", " [ 8, 8, 10]])" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y=np.empty_like(x)\n", "for i in range(len(x)):\n", " y[i,:] = x[i,:]+v[:]\n", "y " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Combining Arrays" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1)Vertical Stack -\n", "\n", "Combines array vertically i.e places the second array below first array.\n", "\n", "The two arrays should have same no of columns." ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 2, 3],\n", " [ 9, 22, 36]])" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a1=np.array([[1,2,3]])\n", "a2=np.array([[9,22,36]])\n", "np.vstack((a1,a2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2) Horizontal stack-\n", "\n", "Combines array horizontally i.e places the second array to the right of first array." ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 2, 3, 9, 22, 36]])" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a1=np.array([[1,2,3]])\n", "a2=np.array([[9,22,36]])\n", "np.hstack((a1,a2))" ] } ], "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.9.12" } }, "nbformat": 4, "nbformat_minor": 4 }