{ "cells": [ { "cell_type": "markdown", "id": "25261fc5", "metadata": {}, "source": [ "# Introduction" ] }, { "cell_type": "code", "execution_count": 2, "id": "49df5fea", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.21.1\n" ] } ], "source": [ "import numpy as np\n", "\n", "print(np.__version__)" ] }, { "cell_type": "code", "execution_count": 43, "id": "76eb87c4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 3 6 9]\n" ] } ], "source": [ "a = np.array([1, 3, 6, 9], dtype='int16')\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 3, "id": "7bcab5dc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 3. 9. 5. ]\n", " [ 1. 79. 4.9]]\n" ] } ], "source": [ "b = np.array([[3.0,9.0,5.0], [1.,79.0,4.9]])\n", "print(b)" ] }, { "cell_type": "code", "execution_count": 6, "id": "1cb46e2a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Get Dimension\n", "a.ndim" ] }, { "cell_type": "code", "execution_count": 4, "id": "2a646dba", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.ndim" ] }, { "cell_type": "code", "execution_count": 7, "id": "97bd073b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2, 3)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Get Shape\n", "b.shape" ] }, { "cell_type": "code", "execution_count": 15, "id": "e342c5ae", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(4,)" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.shape" ] }, { "cell_type": "code", "execution_count": 13, "id": "9cf9c1a5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dtype('int16')" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Get Type\n", "a.dtype" ] }, { "cell_type": "code", "execution_count": 17, "id": "f296b6af", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dtype('float64')" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.dtype" ] }, { "cell_type": "code", "execution_count": 45, "id": "86cb9e1c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Get Size\n", "a.size" ] }, { "cell_type": "code", "execution_count": 46, "id": "d37d10d3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.size" ] }, { "cell_type": "code", "execution_count": 20, "id": "6d5fafa0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Get Total Size\n", "a.nbytes" ] }, { "cell_type": "code", "execution_count": 22, "id": "6def5537", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "48" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.nbytes" ] }, { "cell_type": "markdown", "id": "b616f79f", "metadata": {}, "source": [ "# Accessing/Changing specific elements, rows, columns, etc." ] }, { "cell_type": "markdown", "id": "8cfcea2e", "metadata": {}, "source": [ "## 2-D Array" ] }, { "cell_type": "code", "execution_count": 70, "id": "db06cf3a", "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": 24, "id": "f4a2adda", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "13" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Get a specific element [r, c]\n", "a[1,5] \n", "#or a[1,-2]" ] }, { "cell_type": "code", "execution_count": 28, "id": "073ff9c3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4, 5, 6, 7])" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Get a specific row\n", "a[0, :]" ] }, { "cell_type": "code", "execution_count": 30, "id": "47153377", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 5, 12])" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Get a specific column\n", "a[:, 4]" ] }, { "cell_type": "code", "execution_count": 34, "id": "c217443f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2, 4, 6])" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Doing more specific elements [startindex:endindex:stepsize]\n", "a[0,1:6:2]" ] }, { "cell_type": "code", "execution_count": 38, "id": "2a52a2ac", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1 2 5 4 5 6 7]\n", " [ 8 9 5 11 12 20 14]]\n" ] } ], "source": [ "#Re-assigning values in the array\n", "a[1,5] = 20\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 39, "id": "ecce34d1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1 2 5 4 5 6 7]\n", " [ 8 9 5 11 12 20 14]]\n" ] } ], "source": [ "a[:,2] = 5\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 40, "id": "17e582df", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1 2 3 4 5 6 7]\n", " [ 8 9 10 11 12 20 14]]\n" ] } ], "source": [ "a[:,2] = [3, 10]\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 9, "id": "4567354e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2, 4, 6])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Get elements divisible by 2\n", "a[a%2==0]" ] }, { "cell_type": "code", "execution_count": 11, "id": "7307803e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[False False False False True True]\n" ] } ], "source": [ "#Get element 5 and up\n", "print((a>5) | (a==5))" ] }, { "cell_type": "markdown", "id": "d7a618f5", "metadata": {}, "source": [ "## 3-D Array" ] }, { "cell_type": "code", "execution_count": 50, "id": "a8f37cf3", "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": 52, "id": "26d8678c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([3, 4])" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Get specific element (work outside in)\n", "b[0,1,:]" ] }, { "cell_type": "code", "execution_count": 53, "id": "362dda18", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[3, 4],\n", " [7, 8]])" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b[:,1,:]" ] }, { "cell_type": "code", "execution_count": 55, "id": "b65bc25a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([4, 8])" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b[:,1,1]" ] }, { "cell_type": "code", "execution_count": 56, "id": "50439662", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2, 4])" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b[0,:,1]" ] }, { "cell_type": "code", "execution_count": 12, "id": "bd3ecef2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 4, 1],\n", " [2, 7, 2],\n", " [3, 9, 3]])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c = np.array([[1,4,1], [2,7,2], [3,9,3]])\n", "c" ] }, { "cell_type": "code", "execution_count": 13, "id": "3aa0bac1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[3, 9, 3],\n", " [2, 7, 2],\n", " [1, 4, 1]])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Reversing the order of row by subsetting \n", "c[:: -1, ]" ] }, { "cell_type": "code", "execution_count": 16, "id": "82256a09", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[3, 9, 3],\n", " [2, 7, 2],\n", " [1, 4, 1]])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#reversing the order of rows & columns by subsetting\n", "c[:: - 1, :: -1]" ] }, { "cell_type": "code", "execution_count": 57, "id": "6ba67ebe", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[[ 1 2]\n", " [78 34]]\n", "\n", " [[ 5 6]\n", " [22 56]]]\n" ] } ], "source": [ "#Replacing elements in the array\n", "b[:,1,:] = [[78,34], [22,56]]\n", "print(b)" ] }, { "cell_type": "markdown", "id": "64b7a4b0", "metadata": {}, "source": [ "### How to add a new axis to an array" ] }, { "cell_type": "code", "execution_count": 3, "id": "8b7d603c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(6,)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array([1, 2, 3, 4, 5, 6])\n", "a.shape" ] }, { "cell_type": "code", "execution_count": 6, "id": "f31c535c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 6)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a2 = a[np.newaxis, :] #row_vector\n", "a2.shape" ] }, { "cell_type": "code", "execution_count": 8, "id": "5a7970d9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(6, 1)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a3 = a[:, np.newaxis] #col_vector\n", "a3.shape" ] }, { "cell_type": "markdown", "id": "52528c51", "metadata": {}, "source": [ "## Initialize Different Types of Arrays" ] }, { "cell_type": "code", "execution_count": 58, "id": "a5201173", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0., 0., 0., 0., 0., 0., 0.])" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#All 0s matrix\n", "np.zeros(7)" ] }, { "cell_type": "code", "execution_count": 61, "id": "405d9588", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0., 0.],\n", " [0., 0.]])" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.zeros((2,2))" ] }, { "cell_type": "code", "execution_count": 64, "id": "ac242246", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[[0., 0., 0.],\n", " [0., 0., 0.],\n", " [0., 0., 0.],\n", " [0., 0., 0.],\n", " [0., 0., 0.],\n", " [0., 0., 0.]],\n", "\n", " [[0., 0., 0.],\n", " [0., 0., 0.],\n", " [0., 0., 0.],\n", " [0., 0., 0.],\n", " [0., 0., 0.],\n", " [0., 0., 0.]],\n", "\n", " [[0., 0., 0.],\n", " [0., 0., 0.],\n", " [0., 0., 0.],\n", " [0., 0., 0.],\n", " [0., 0., 0.],\n", " [0., 0., 0.]]])" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.zeros((3,6,3))" ] }, { "cell_type": "code", "execution_count": 68, "id": "480ddf96", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[[1, 1, 1],\n", " [1, 1, 1],\n", " [1, 1, 1],\n", " [1, 1, 1],\n", " [1, 1, 1]],\n", "\n", " [[1, 1, 1],\n", " [1, 1, 1],\n", " [1, 1, 1],\n", " [1, 1, 1],\n", " [1, 1, 1]],\n", "\n", " [[1, 1, 1],\n", " [1, 1, 1],\n", " [1, 1, 1],\n", " [1, 1, 1],\n", " [1, 1, 1]],\n", "\n", " [[1, 1, 1],\n", " [1, 1, 1],\n", " [1, 1, 1],\n", " [1, 1, 1],\n", " [1, 1, 1]]], dtype=int16)" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#All 1s matrix\n", "np.ones((4, 5, 3), dtype = 'int16')" ] }, { "cell_type": "code", "execution_count": 69, "id": "f5890179", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[77, 77, 77, 77, 77],\n", " [77, 77, 77, 77, 77],\n", " [77, 77, 77, 77, 77]])" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Any other number\n", "np.full((3,5), 77)" ] }, { "cell_type": "code", "execution_count": 73, "id": "323777dc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[4, 4, 4, 4, 4, 4, 4],\n", " [4, 4, 4, 4, 4, 4, 4]])" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Any other number (full_like)\n", "np.full_like(a, 4)" ] }, { "cell_type": "markdown", "id": "36d076b6", "metadata": {}, "source": [ "### Generating Random Integers" ] }, { "cell_type": "code", "execution_count": 73, "id": "2cb40565", "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "array([[[0.17264182, 0.62904951],\n", " [0.57135584, 0.94190107],\n", " [0.08369627, 0.59921877]],\n", "\n", " [[0.92672639, 0.47279508],\n", " [0.3824544 , 0.47744123],\n", " [0.14927045, 0.61844486]],\n", "\n", " [[0.57606845, 0.69176239],\n", " [0.66544527, 0.24365116],\n", " [0.27317977, 0.86767478]],\n", "\n", " [[0.17626309, 0.21091273],\n", " [0.36600274, 0.95390974],\n", " [0.68874675, 0.80438325]]])" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Random float number (uniformally distributed around 0 and 1)\n", "np.random.rand(4, 3, 2)" ] }, { "cell_type": "code", "execution_count": 74, "id": "fd9a755d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[[-1.75793781e-01, -9.32617860e-01],\n", " [ 2.94349299e+00, 4.57776720e-01],\n", " [ 1.77346713e-01, -1.19284227e-01]],\n", "\n", " [[ 2.04774781e-03, -3.80101667e-01],\n", " [-9.62869237e-01, -1.79875640e-01],\n", " [-1.59578007e+00, -1.86062939e+00]],\n", "\n", " [[ 1.79637479e+00, 1.36169348e+00],\n", " [ 2.41698665e-01, -1.84841139e+00],\n", " [-6.19981314e-01, -6.64723441e-01]],\n", "\n", " [[-5.94095569e-01, 1.74623586e+00],\n", " [ 1.53226211e+00, -5.99715275e-02],\n", " [ 9.36623463e-01, -2.58938942e-01]]])" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Random float number (uniformally distributed around -1 and 1)\n", "np.random.randn(4, 3, 2)" ] }, { "cell_type": "code", "execution_count": 86, "id": "fe60be83", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0.23252098, 0.46116439, 0.27726669, 0.5987351 , 0.17023959,\n", " 0.22901113, 0.5696084 ],\n", " [0.99991291, 0.16844318, 0.73811295, 0.00275439, 0.15681662,\n", " 0.65347567, 0.24129701]])" ] }, "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.random_sample(a.shape)" ] }, { "cell_type": "code", "execution_count": 84, "id": "074edf14", "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "array([[ 1, -4, -4],\n", " [ 2, 7, 3],\n", " [-4, 1, 5]])" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Random integer values\n", "np.random.randint(-4, 9, size=(3,3))" ] }, { "cell_type": "code", "execution_count": 82, "id": "62044f37", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 7, 8],\n", " [ 4, 5, 7],\n", " [ 1, -4, -4]])" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Saving the random value generated to the memory for further use\n", "np.random.seed(1)\n", "np.random.randint(-4, 9, size=(3,3))" ] }, { "cell_type": "code", "execution_count": 35, "id": "f11c1cef", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[4, 3, 2, 1],\n", " [1, 0, 0, 0]], dtype=int64)" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#generating a matrix with random integers between 0 & 4\n", "rng = np.random.default_rng(0)\n", "rng.integers(5, size=(2, 4))" ] }, { "cell_type": "code", "execution_count": 62, "id": "9e5ba9e0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4, 5, 6, 7])" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#geerating an array between specific sequence\n", "np.arange(1,8, dtype = 'int')" ] }, { "cell_type": "code", "execution_count": 61, "id": "3afc96de", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33,\n", " 35, 37, 39, 41, 43, 45, 47, 49])" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#generating an array with odd numbers b/w 1 and 50\n", "np.arange(1,50,2)" ] }, { "cell_type": "code", "execution_count": 66, "id": "fcade17a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1. , 2.68965517, 4.37931034, 6.06896552, 7.75862069,\n", " 9.44827586, 11.13793103, 12.82758621, 14.51724138, 16.20689655,\n", " 17.89655172, 19.5862069 , 21.27586207, 22.96551724, 24.65517241,\n", " 26.34482759, 28.03448276, 29.72413793, 31.4137931 , 33.10344828,\n", " 34.79310345, 36.48275862, 38.17241379, 39.86206897, 41.55172414,\n", " 43.24137931, 44.93103448, 46.62068966, 48.31034483, 50. ])" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#generating an array with as many elemnts we want\n", "np.linspace(1,50,30)" ] }, { "cell_type": "code", "execution_count": 65, "id": "fa4cdc61", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1.00000000e+01, 2.78255940e+06, 7.74263683e+11, 2.15443469e+17,\n", " 5.99484250e+22, 1.66810054e+28, 4.64158883e+33, 1.29154967e+39,\n", " 3.59381366e+44, 1.00000000e+50])" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#generating an array with equqlly space log scale elements\n", "np.logspace(1,50,10)" ] }, { "cell_type": "code", "execution_count": 88, "id": "8d8fd3ac", "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": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#the identity matrix\n", "np.identity(5)" ] }, { "cell_type": "code", "execution_count": 68, "id": "e2d07b8b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2 3]\n", " [1 2 3]\n", " [1 2 3]]\n" ] } ], "source": [ "#repeating 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": 69, "id": "f47dd067", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]])" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#repeating an array with tile()\n", "np.tile(arr, 4)" ] }, { "cell_type": "code", "execution_count": 38, "id": "ce7ec097", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[11 12 13 14 15 16 17 18 19 20]\n" ] } ], "source": [ "#get unique items and counts\n", "a = np.array([11, 11, 12, 13, 14, 15, 16, 17, 12, 13, 11, 14, 18, 19, 20])\n", "uni = np.unique(a)\n", "print(uni)" ] }, { "cell_type": "code", "execution_count": 40, "id": "5806a954", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 0 2 3 4 5 6 7 12 13 14]\n" ] } ], "source": [ "#Get the indices of unique values\n", "uni, ind = np.unique(a ,return_index=True)\n", "print(ind)" ] }, { "cell_type": "code", "execution_count": 41, "id": "684d2eab", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[3 2 2 2 1 1 1 1 1 1]\n" ] } ], "source": [ "#Get the count of unique values\n", "uni, count = np.unique(a, return_counts=True)\n", "print(count)" ] }, { "cell_type": "code", "execution_count": 45, "id": "5d4ad8be", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1 2 3 4]\n", " [ 5 6 7 8]\n", " [ 9 10 11 12]]\n" ] } ], "source": [ "a_2d = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [1, 2, 3, 4]])\n", "uni = np.unique(a_2d, axis=0) #row\n", "print(uni)" ] }, { "cell_type": "code", "execution_count": 46, "id": "2547067c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1 2 3 4]\n", " [ 5 6 7 8]\n", " [ 9 10 11 12]\n", " [ 1 2 3 4]]\n" ] } ], "source": [ "uni = np.unique(a_2d, axis=1) #col\n", "print(uni)" ] }, { "cell_type": "markdown", "id": "9827da81", "metadata": {}, "source": [ "# Problem 1 : Creating a Custom Matrix" ] }, { "cell_type": "code", "execution_count": 100, "id": "4a0b5883", "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": [ "#Creating a custom matrix\n", "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:4,1:4] = z\n", "print(output)" ] }, { "cell_type": "markdown", "id": "5705cac8", "metadata": {}, "source": [ "- **Be careful while copying arrays.**" ] }, { "cell_type": "code", "execution_count": 111, "id": "ed46c498", "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3]\n", "[100 2 3]\n" ] } ], "source": [ "a = np.array([1,2,3])\n", "b = a.copy() #do NOT forget to add copy(), otherwise changes you run in b will also happen in a.\n", "b[0] = 100\n", "print(a)\n", "print(b)" ] }, { "cell_type": "markdown", "id": "24d5b0a4", "metadata": {}, "source": [ "# Basic Mathematics in NumPy" ] }, { "cell_type": "code", "execution_count": 121, "id": "99b6a1ca", "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": 122, "id": "c0b93fd8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([3, 4, 5, 6])" ] }, "execution_count": 122, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a + 2" ] }, { "cell_type": "code", "execution_count": 115, "id": "680b2fab", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([-1, 0, 1, 2])" ] }, "execution_count": 115, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a - 2" ] }, { "cell_type": "code", "execution_count": 116, "id": "752a1198", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2, 4, 6, 8])" ] }, "execution_count": 116, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a * 2" ] }, { "cell_type": "code", "execution_count": 117, "id": "1582759f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0.5, 1. , 1.5, 2. ])" ] }, "execution_count": 117, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a / 2" ] }, { "cell_type": "code", "execution_count": 123, "id": "248a258f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2, 2, 4, 4])" ] }, "execution_count": 123, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = np.array([1,0,1,0])\n", "a + b" ] }, { "cell_type": "code", "execution_count": 124, "id": "03f21069", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1, 4, 9, 16], dtype=int32)" ] }, "execution_count": 124, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a ** 2" ] }, { "cell_type": "code", "execution_count": 131, "id": "e5b29572", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1.55740772, -2.18503986, -0.14254654, 1.15782128])" ] }, "execution_count": 131, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Take sin of an array\n", "np.sin(a)\n", "\n", "#cos and tan values of a \n", "#np.cos(a)\n", "#np.tan(a)" ] }, { "cell_type": "code", "execution_count": 54, "id": "137bbca6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0.45053314, 0.17296777, 0.34376245, 0.5510652 ],\n", " [0.54627315, 0.05093587, 0.40067661, 0.55645993],\n", " [0.12697628, 0.82485143, 0.26590556, 0.56917101]])" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stats = np. array([[0.45053314, 0.17296777, 0.34376245, 0.5510652],\n", " [0.54627315, 0.05093587, 0.40067661, 0.55645993],\n", " [0.12697628, 0.82485143, 0.26590556, 0.56917101]])\n", "stats" ] }, { "cell_type": "code", "execution_count": 22, "id": "68ef3be4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.05093587" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.min(stats)" ] }, { "cell_type": "code", "execution_count": 27, "id": "47d6ae35", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0.12697628, 0.05093587, 0.26590556, 0.5510652 ])" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.min(stats, axis = 0) # givevs the minimum value within each column" ] }, { "cell_type": "code", "execution_count": 28, "id": "ba3208c1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0.17296777, 0.05093587, 0.12697628])" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.min(stats, axis = 1) # givevs the minimum value within each row" ] }, { "cell_type": "code", "execution_count": 24, "id": "6e78873c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.82485143" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.max(stats)" ] }, { "cell_type": "code", "execution_count": 25, "id": "1924c0cf", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4.8595784" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sum(stats)" ] }, { "cell_type": "code", "execution_count": 50, "id": "a072bcb2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.4049648666666667" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stats.mean()" ] }, { "cell_type": "code", "execution_count": 51, "id": "ec456fca", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.21392120766089617" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stats.std()" ] }, { "cell_type": "code", "execution_count": 52, "id": "005c0a43", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0.45053314, 0.17296777, 0.34376245, 0.5510652 ],\n", " [0.54627315, 0.05093587, 0.40067661, 0.55645993],\n", " [0.12697628, 0.82485143, 0.26590556, 0.56917101]])" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stats.squeeze()" ] }, { "cell_type": "code", "execution_count": 55, "id": "3c945086", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0.45053314, 0.62350091, 0.96726336, 1.51832856, 2.06460171,\n", " 2.11553758, 2.51621419, 3.07267412, 3.1996504 , 4.02450183,\n", " 4.29040739, 4.8595784 ])" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stats.cumsum()" ] }, { "cell_type": "code", "execution_count": 30, "id": "efe0561f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[2, 3],\n", " [4, 5],\n", " [6, 7]])" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Doing Arithematic Operations on matices of diif. sizes\n", "d = np.array([[1, 2], [3, 4], [5, 6]])\n", "one_row = np.array([[1,1]])\n", "\n", "d + one_row #NumPy uses its broadcast rules for this operation." ] }, { "cell_type": "markdown", "id": "ca456cd6", "metadata": {}, "source": [ "## Reeorginizing Arrays" ] }, { "cell_type": "code", "execution_count": 18, "id": "1d763da7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2 3 4]\n", " [5 6 7 8]]\n" ] } ], "source": [ "before = np.array([[1,2,3,4], [5,6,7,8]])\n", "print(before)" ] }, { "cell_type": "code", "execution_count": 49, "id": "8b0d0dd8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1]\n", " [2]\n", " [3]\n", " [4]\n", " [5]\n", " [6]\n", " [7]\n", " [8]]\n" ] } ], "source": [ "after = before.reshape((8,1))\n", "print(after)" ] }, { "cell_type": "code", "execution_count": 50, "id": "a63d4b42", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2]\n", " [3 4]\n", " [5 6]\n", " [7 8]]\n" ] } ], "source": [ "after = before.reshape((4,2))\n", "print(after)" ] }, { "cell_type": "code", "execution_count": 51, "id": "377474b1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[[1 2]\n", " [3 4]]\n", "\n", " [[5 6]\n", " [7 8]]]\n" ] } ], "source": [ "after = before.reshape((2,2,2))\n", "print(after)" ] }, { "cell_type": "code", "execution_count": 19, "id": "83d393b0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 5],\n", " [2, 6],\n", " [3, 7],\n", " [4, 8]])" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Use transpose() to reverse the axes of an array\n", "#before was 2x4 matrix\n", "#before.transpose() is 4x2 matrix\n", "before.transpose()" ] }, { "cell_type": "code", "execution_count": 59, "id": "2b6600d2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Reversed array: [8 7 6 5 4 3 2 1]\n" ] } ], "source": [ "#flipping 1-D arrays\n", "arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])\n", "rev= np.flip(arr)\n", "print('Reversed array: ', rev)" ] }, { "cell_type": "code", "execution_count": 60, "id": "7f91fd2e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[12 11 10 9]\n", " [ 8 7 6 5]\n", " [ 4 3 2 1]]\n" ] } ], "source": [ "#flipping 2-D arrays\n", "arr_2d = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])\n", "rev_2d = np.flip(arr_2d)\n", "print(rev_2d)" ] }, { "cell_type": "code", "execution_count": 62, "id": "9559e7ad", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 9 10 11 12]\n", " [ 5 6 7 8]\n", " [ 1 2 3 4]]\n" ] } ], "source": [ "#reversing rows\n", "rev_row = np.flip(arr_2d, axis = 0)\n", "print(rev_row)" ] }, { "cell_type": "code", "execution_count": 64, "id": "44e0cfa5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 4 3 2 1]\n", " [ 8 7 6 5]\n", " [12 11 10 9]]\n" ] } ], "source": [ "#reversing columns\n", "rev_col = np.flip(arr_2d, axis=1)\n", "print(rev_col)" ] }, { "cell_type": "code", "execution_count": 65, "id": "2a11d73d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1 2 3 4]\n", " [ 8 7 6 5]\n", " [ 9 10 11 12]]\n" ] } ], "source": [ "#flipping only second row\n", "arr_2d[1] = np.flip(arr_2d[1])\n", "print(arr_2d)" ] }, { "cell_type": "code", "execution_count": 66, "id": "93ae771a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1 10 3 4]\n", " [ 8 7 6 5]\n", " [ 9 2 11 12]]\n" ] } ], "source": [ "#fliping second column\n", "arr_2d[:,1] = np.flip(arr_2d[:,1])\n", "print(arr_2d)" ] }, { "cell_type": "markdown", "id": "b0616e14", "metadata": {}, "source": [ "## Miscellaneous" ] }, { "cell_type": "markdown", "id": "924ff6b1", "metadata": {}, "source": [ "### Splitting Arrays" ] }, { "cell_type": "code", "execution_count": 14, "id": "8b5ff8a8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[array([[ 1, 2, 3, 4],\n", " [13, 14, 15, 16]]),\n", " array([[ 5, 6, 7, 8],\n", " [17, 18, 19, 20]]),\n", " array([[ 9, 10, 11, 12],\n", " [21, 22, 23, 24]])]" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Splitting long arrays into small arrays\n", "x = np.arange(1,25).reshape(2,12)\n", "np.hsplit(x,3)" ] }, { "cell_type": "code", "execution_count": 15, "id": "9306bd00", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[array([[ 1, 2, 3],\n", " [13, 14, 15]]),\n", " array([[ 4, 5, 6],\n", " [16, 17, 18]]),\n", " array([[ 7, 8, 9],\n", " [19, 20, 21]]),\n", " array([[10, 11, 12],\n", " [22, 23, 24]])]" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.hsplit(x,4)" ] }, { "cell_type": "code", "execution_count": 17, "id": "ac430aa4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[array([[ 1, 2, 3],\n", " [13, 14, 15]]),\n", " array([[ 4],\n", " [16]]),\n", " array([[ 5, 6, 7, 8, 9, 10, 11, 12],\n", " [17, 18, 19, 20, 21, 22, 23, 24]])]" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.hsplit(x, (3,4))" ] }, { "cell_type": "markdown", "id": "8fd2aa68", "metadata": {}, "source": [ "### Load data from file" ] }, { "cell_type": "code", "execution_count": 87, "id": "dac30f31", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1., 13., 21., 11., 196., 75., 4., 3., 34., 6., 7.,\n", " 8., 0., 1., 2., 3., 4., 5.],\n", " [ 3., 42., 12., 33., 766., 75., 4., 55., 6., 4., 3.,\n", " 4., 5., 6., 7., 0., 11., 12.],\n", " [ 1., 22., 33., 11., 999., 11., 2., 1., 78., 0., 1.,\n", " 2., 9., 8., 7., 1., 76., 88.]])" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = np.genfromtxt('data.txt', delimiter = ',')\n", "data" ] }, { "cell_type": "markdown", "id": "bb74c511", "metadata": {}, "source": [ "### Changing data type of a file" ] }, { "cell_type": "code", "execution_count": 88, "id": "19b11d97", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 13, 21, 11, 196, 75, 4, 3, 34, 6, 7, 8, 0,\n", " 1, 2, 3, 4, 5],\n", " [ 3, 42, 12, 33, 766, 75, 4, 55, 6, 4, 3, 4, 5,\n", " 6, 7, 0, 11, 12],\n", " [ 1, 22, 33, 11, 999, 11, 2, 1, 78, 0, 1, 2, 9,\n", " 8, 7, 1, 76, 88]])" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = data.astype('int32')\n", "data" ] }, { "cell_type": "markdown", "id": "411da1ab", "metadata": {}, "source": [ "### Loading data from a URL" ] }, { "cell_type": "code", "execution_count": 97, "id": "afa21bf4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 18., 8., 307., ..., 70., 1., nan],\n", " [ 15., 8., 350., ..., 70., 1., nan],\n", " [ 18., 8., 318., ..., 70., 1., nan],\n", " ...,\n", " [ 32., 4., 135., ..., 82., 1., nan],\n", " [ 28., 4., 120., ..., 82., 1., nan],\n", " [ 31., 4., 119., ..., 82., 1., nan]])" ] }, "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dta= np.genfromtxt('https://raw.githubusercontent.com/selva86/datasets/master/Auto.csv', delimiter =',', skip_header = 1)\n", "dta" ] }, { "cell_type": "markdown", "id": "68457b9e", "metadata": {}, "source": [ "### Filling NaN values with other values" ] }, { "cell_type": "code", "execution_count": 98, "id": "da32f891", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1.800e+01, 8.000e+00, 3.070e+02, ..., 7.000e+01, 1.000e+00,\n", " 9.999e+03],\n", " [1.500e+01, 8.000e+00, 3.500e+02, ..., 7.000e+01, 1.000e+00,\n", " 9.999e+03],\n", " [1.800e+01, 8.000e+00, 3.180e+02, ..., 7.000e+01, 1.000e+00,\n", " 9.999e+03],\n", " ...,\n", " [3.200e+01, 4.000e+00, 1.350e+02, ..., 8.200e+01, 1.000e+00,\n", " 9.999e+03],\n", " [2.800e+01, 4.000e+00, 1.200e+02, ..., 8.200e+01, 1.000e+00,\n", " 9.999e+03],\n", " [3.100e+01, 4.000e+00, 1.190e+02, ..., 8.200e+01, 1.000e+00,\n", " 9.999e+03]])" ] }, "execution_count": 98, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dta= np.genfromtxt('https://raw.githubusercontent.com/selva86/datasets/master/Auto.csv', delimiter =',', skip_header = 1, \n", " filling_values= 9999, dtype = 'float')\n", "dta" ] }, { "cell_type": "markdown", "id": "a989b25f", "metadata": {}, "source": [ "### Supressing Scientific Notation in the Dataset" ] }, { "cell_type": "code", "execution_count": 101, "id": "8cacac69", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 18., 8., 307., ..., 70., 1., 9999.],\n", " [ 15., 8., 350., ..., 70., 1., 9999.],\n", " [ 18., 8., 318., ..., 70., 1., 9999.],\n", " ...,\n", " [ 32., 4., 135., ..., 82., 1., 9999.],\n", " [ 28., 4., 120., ..., 82., 1., 9999.],\n", " [ 31., 4., 119., ..., 82., 1., 9999.]])" ] }, "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.set_printoptions(suppress=True)\n", "dta" ] }, { "cell_type": "markdown", "id": "80f88afa", "metadata": {}, "source": [ "### Saving Files into Local" ] }, { "cell_type": "code", "execution_count": 102, "id": "93a68e6c", "metadata": {}, "outputs": [], "source": [ "np.savetxt('auto.csv', dta, delimiter = ',')" ] }, { "cell_type": "markdown", "id": "bc36aa39", "metadata": {}, "source": [ "### Saving Files as NumPy File" ] }, { "cell_type": "code", "execution_count": 103, "id": "74dd0467", "metadata": {}, "outputs": [], "source": [ "#this will open in console only\n", "np.save('auto.npy', dta)" ] }, { "cell_type": "markdown", "id": "0102b813", "metadata": {}, "source": [ "### Loading Files through NumPy" ] }, { "cell_type": "code", "execution_count": 108, "id": "77d6d71b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 18., 8., 307., ..., 70., 1., 9999.],\n", " [ 15., 8., 350., ..., 70., 1., 9999.],\n", " [ 18., 8., 318., ..., 70., 1., 9999.],\n", " ...,\n", " [ 32., 4., 135., ..., 82., 1., 9999.],\n", " [ 28., 4., 120., ..., 82., 1., 9999.],\n", " [ 31., 4., 119., ..., 82., 1., 9999.]])" ] }, "execution_count": 108, "metadata": {}, "output_type": "execute_result" } ], "source": [ "k = np.load('auto.npy')\n", "k" ] }, { "cell_type": "markdown", "id": "1b869e2c", "metadata": {}, "source": [ "### Concat (Row & Col wise)" ] }, { "cell_type": "code", "execution_count": 111, "id": "31801de1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0., 0., 0., 0.],\n", " [0., 0., 0., 0.],\n", " [0., 0., 0., 0.],\n", " [0., 0., 0., 0.]])" ] }, "execution_count": 111, "metadata": {}, "output_type": "execute_result" } ], "source": [ "arr1 = np.zeros([4,4])\n", "arr1" ] }, { "cell_type": "code", "execution_count": 112, "id": "d7867b6d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1., 1., 1., 1.],\n", " [1., 1., 1., 1.],\n", " [1., 1., 1., 1.],\n", " [1., 1., 1., 1.]])" ] }, "execution_count": 112, "metadata": {}, "output_type": "execute_result" } ], "source": [ "arr2 = np.ones([4,4])\n", "arr2" ] }, { "cell_type": "markdown", "id": "a9c47ee8", "metadata": {}, "source": [ "#### Method 1 : np.concatenate()" ] }, { "cell_type": "code", "execution_count": 115, "id": "5a37ac8c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0., 0., 0., 0., 1., 1., 1., 1.],\n", " [0., 0., 0., 0., 1., 1., 1., 1.],\n", " [0., 0., 0., 0., 1., 1., 1., 1.],\n", " [0., 0., 0., 0., 1., 1., 1., 1.]])" ] }, "execution_count": 115, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#cancate along rows\n", "np.concatenate([arr1, arr2], axis = 1)" ] }, { "cell_type": "code", "execution_count": 116, "id": "64f05a44", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0., 0., 0., 0.],\n", " [0., 0., 0., 0.],\n", " [0., 0., 0., 0.],\n", " [0., 0., 0., 0.],\n", " [1., 1., 1., 1.],\n", " [1., 1., 1., 1.],\n", " [1., 1., 1., 1.],\n", " [1., 1., 1., 1.]])" ] }, "execution_count": 116, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#cancate along cols\n", "np.concatenate([arr1, arr2], axis = 0)" ] }, { "cell_type": "markdown", "id": "92ea3692", "metadata": {}, "source": [ "#### Method 2 : vstack() & hstack()" ] }, { "cell_type": "code", "execution_count": 152, "id": "fdd9b60e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 2, 3, 4, 5],\n", " [ 6, 7, 8, 9, 10]])" ] }, "execution_count": 152, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Vertically stacking vectors\n", "v1 = np.array([1,2,3,4,5])\n", "v2 = np.array([6,7,8,9,10])\n", "\n", "np.vstack([v1,v2])" ] }, { "cell_type": "code", "execution_count": 153, "id": "b09446c2", "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 2, 3, 4, 5],\n", " [ 6, 7, 8, 9, 10],\n", " [ 6, 7, 8, 9, 10],\n", " [ 1, 2, 3, 4, 5]])" ] }, "execution_count": 153, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.vstack([v1,v2, v2, v1])" ] }, { "cell_type": "code", "execution_count": 118, "id": "aa2a5164", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1., 1., 1., 1., 0., 0.],\n", " [1., 1., 1., 1., 0., 0.]])" ] }, "execution_count": 118, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Horizantally stacking vectors\n", "h1 = np.ones((2,4))\n", "h2 = np.zeros((2,2))\n", "\n", "np.hstack([h1,h2])" ] }, { "cell_type": "code", "execution_count": 119, "id": "e871e8db", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1., 1., 1., 1., 0., 0., 0., 0., 1., 1., 1., 1., 0., 0., 1., 1.,\n", " 1., 1.],\n", " [1., 1., 1., 1., 0., 0., 0., 0., 1., 1., 1., 1., 0., 0., 1., 1.,\n", " 1., 1.]])" ] }, "execution_count": 119, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.hstack([h1,h2,h2,h1,h2,h1])" ] }, { "cell_type": "markdown", "id": "43161fc2", "metadata": {}, "source": [ "### Sorting Arrays" ] }, { "cell_type": "code", "execution_count": 121, "id": "f667208f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[4, 7, 9, 1, 3],\n", " [8, 8, 8, 4, 1],\n", " [9, 8, 8, 2, 2],\n", " [4, 1, 9, 7, 5],\n", " [6, 7, 3, 6, 8],\n", " [9, 5, 5, 8, 8],\n", " [5, 1, 3, 1, 8],\n", " [2, 8, 9, 5, 1],\n", " [2, 9, 3, 4, 2],\n", " [3, 8, 3, 7, 1]])" ] }, "execution_count": 121, "metadata": {}, "output_type": "execute_result" } ], "source": [ "kk = np.random.randint(1,10,size = [10,5])\n", "kk" ] }, { "cell_type": "code", "execution_count": 126, "id": "19a7415e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 3, 4, 7, 9],\n", " [1, 4, 8, 8, 8],\n", " [2, 2, 8, 8, 9],\n", " [1, 4, 5, 7, 9],\n", " [3, 6, 6, 7, 8],\n", " [5, 5, 8, 8, 9],\n", " [1, 1, 3, 5, 8],\n", " [1, 2, 5, 8, 9],\n", " [2, 2, 3, 4, 9],\n", " [1, 3, 3, 7, 8]])" ] }, "execution_count": 126, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#sorting along rows of the array\n", "np.sort(kk)" ] }, { "cell_type": "code", "execution_count": 129, "id": "600dcc7f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[2, 8, 9, 5, 1],\n", " [2, 9, 3, 4, 2],\n", " [3, 8, 3, 7, 1],\n", " [4, 7, 9, 1, 3],\n", " [4, 1, 9, 7, 5],\n", " [5, 1, 3, 1, 8],\n", " [6, 7, 3, 6, 8],\n", " [8, 8, 8, 4, 1],\n", " [9, 8, 8, 2, 2],\n", " [9, 5, 5, 8, 8]])" ] }, "execution_count": 129, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#sorting a single a col\n", "sorted_col = kk[:, 0].argsort()\n", "kk[sorted_col]" ] }, { "cell_type": "markdown", "id": "aebbc090", "metadata": {}, "source": [ "### Working with Dates" ] }, { "cell_type": "code", "execution_count": 133, "id": "cbbb7a8e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "numpy.datetime64('2021-08-03T23:10:00')" ] }, "execution_count": 133, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = np.datetime64('2021-08-03 23:10:00')\n", "d" ] }, { "cell_type": "code", "execution_count": 137, "id": "94e048cd", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "numpy.datetime64('2021-08-05T02:56:40')" ] }, "execution_count": 137, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#adding seconds to the date-time\n", "d + 100000" ] }, { "cell_type": "code", "execution_count": 141, "id": "3389d84a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "numpy.datetime64('2021-08-04T23:10:00')" ] }, "execution_count": 141, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#adding one day to the date-time\n", "oneday = np.timedelta64(1, \"D\")\n", "oneday\n", "d + oneday" ] }, { "cell_type": "code", "execution_count": 143, "id": "5b332bd4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "numpy.datetime64('2021-08-03T23:11:00')" ] }, "execution_count": 143, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#adding one minute to the date-time\n", "onemin = np.timedelta64(1, 'm')\n", "onemin\n", "d + onemin" ] }, { "cell_type": "code", "execution_count": 146, "id": "70d49b14", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['2019-09-03', '2019-09-06', '2019-09-09', '2019-09-12',\n", " '2019-09-15', '2019-09-18', '2019-09-21', '2019-09-24',\n", " '2019-09-27', '2019-09-30'], dtype='datetime64[D]')" ] }, "execution_count": 146, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#creating a sequence of date-time array\n", "dates = np.arange(np.datetime64('2019-09-03'), np.datetime64('2019-10-03'), 3)\n", "dates" ] }, { "cell_type": "code", "execution_count": 147, "id": "071dc84a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['2019-09-03', '2019-09-04', '2019-09-05', '2019-09-06',\n", " '2019-09-07', '2019-09-08', '2019-09-09', '2019-09-10',\n", " '2019-09-11', '2019-09-12', '2019-09-13', '2019-09-14',\n", " '2019-09-15', '2019-09-16', '2019-09-17', '2019-09-18',\n", " '2019-09-19', '2019-09-20', '2019-09-21', '2019-09-22',\n", " '2019-09-23', '2019-09-24', '2019-09-25', '2019-09-26',\n", " '2019-09-27', '2019-09-28', '2019-09-29', '2019-09-30',\n", " '2019-10-01', '2019-10-02'], dtype='datetime64[D]')" ] }, "execution_count": 147, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dates = np.arange(np.datetime64('2019-09-03'), np.datetime64('2019-10-03'))\n", "dates" ] }, { "cell_type": "markdown", "id": "b7144fab", "metadata": {}, "source": [ "### Advanced Functions" ] }, { "cell_type": "markdown", "id": "caec0fec", "metadata": {}, "source": [ "#### vectorize() : heklps us to apply created functiion to an array" ] }, { "cell_type": "code", "execution_count": 154, "id": "27ccd3b9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "121" ] }, "execution_count": 154, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def foo(x):\n", " if x%2 == 1:\n", " return x**2\n", " else:\n", " return x/2\n", "\n", "foo(10)\n", "foo(11)" ] }, { "cell_type": "code", "execution_count": 157, "id": "efb44b9c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 2., 49., 81., 1., 9.],\n", " [ 4., 4., 4., 2., 1.],\n", " [81., 4., 4., 1., 1.],\n", " [ 2., 1., 81., 49., 25.],\n", " [ 3., 49., 9., 3., 4.],\n", " [81., 25., 25., 4., 4.],\n", " [25., 1., 9., 1., 4.],\n", " [ 1., 4., 81., 25., 1.],\n", " [ 1., 81., 9., 2., 1.],\n", " [ 9., 4., 9., 49., 1.]])" ] }, "execution_count": 157, "metadata": {}, "output_type": "execute_result" } ], "source": [ "foo_v = np.vectorize(foo, otypes = [float])\n", "foo_v(kk)" ] }, { "cell_type": "markdown", "id": "c69fe5ad", "metadata": {}, "source": [ "### Boolean Masking & Advanced Indexing" ] }, { "cell_type": "code", "execution_count": 160, "id": "5c7e3082", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[False, False, False, False, True, True, False, False, False,\n", " False, False, False, False, False, False, False, False, False],\n", " [False, False, False, False, True, True, False, True, False,\n", " False, False, False, False, False, False, False, False, False],\n", " [False, False, False, False, True, False, False, False, True,\n", " False, False, False, False, False, False, False, True, True]])" ] }, "execution_count": 160, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data > 50" ] }, { "cell_type": "code", "execution_count": 165, "id": "cc726da7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([196, 75, 766, 75, 55, 999, 78, 76, 88])" ] }, "execution_count": 165, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data[data > 50]" ] }, { "cell_type": "code", "execution_count": 169, "id": "90403a1b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([False, False, False, False, True, True, False, True, True,\n", " False, False, False, False, False, False, False, True, True])" ] }, "execution_count": 169, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.any(data > 50, axis = 0)" ] }, { "cell_type": "code", "execution_count": 170, "id": "3d8f9273", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([False, False, False, False, True, False, False, False, False,\n", " False, False, False, False, False, False, False, False, False])" ] }, "execution_count": 170, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.all(data > 50, axis = 0)" ] }, { "cell_type": "code", "execution_count": 89, "id": "65eed7ed", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([75, 75, 55, 78, 76, 88])" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data[((data > 50) & (data < 100))]" ] }, { "cell_type": "code", "execution_count": 91, "id": "ff96cbb0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1, 13, 21, 11, 4, 3, 34, 6, 7, 8, 0, 1, 2, 3, 4, 5, 3,\n", " 42, 12, 33, 4, 6, 4, 3, 4, 5, 6, 7, 0, 11, 12, 1, 22, 33,\n", " 11, 11, 2, 1, 0, 1, 2, 9, 8, 7, 1])" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data[(~(data > 50) & (data < 100))]" ] }, { "cell_type": "markdown", "id": "61e0d37c", "metadata": {}, "source": [ "### Checking if Data has NAN or INF values" ] }, { "cell_type": "code", "execution_count": 41, "id": "b7ea55d0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1., 2., inf, 3., 4., 5., 6., 7., nan, 8., 9., 10.])" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = np.arange(1, 11, dtype=float)\n", "x = np.insert(x, 2, np.inf, axis=0)\n", "x = np.insert(x, 8, np.nan, axis=0)\n", "x" ] }, { "cell_type": "code", "execution_count": 43, "id": "7ca0f4a5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([False, False, False, False, False, False, False, False, True,\n", " False, False, False])" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.isnan(x)" ] }, { "cell_type": "code", "execution_count": 44, "id": "2798438c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([False, False, True, False, False, False, False, False, False,\n", " False, False, False])" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.isinf(x)" ] }, { "cell_type": "markdown", "id": "d3a84ae9", "metadata": {}, "source": [ "### Replacing NAn & INF with other values" ] }, { "cell_type": "code", "execution_count": 45, "id": "b5ddb034", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([False, False, True, False, False, False, False, False, True,\n", " False, False, False])" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "missing = np.isnan(x) | np.isinf(x)\n", "missing" ] }, { "cell_type": "code", "execution_count": 46, "id": "10145b90", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([inf, nan])" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x[missing]" ] }, { "cell_type": "code", "execution_count": 48, "id": "2bce9c29", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1., 2., 0., 3., 4., 5., 6., 7., 0., 8., 9., 10.])" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x[missing] = 0\n", "x" ] }, { "cell_type": "markdown", "id": "0a1a6b84", "metadata": {}, "source": [ "# Problem 2 : Indexing" ] }, { "cell_type": "code", "execution_count": 178, "id": "aed08887", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 2, 3, 4, 5],\n", " [ 6, 7, 8, 9, 10],\n", " [11, 12, 13, 14, 15],\n", " [16, 17, 18, 19, 20],\n", " [21, 22, 23, 24, 25],\n", " [26, 27, 28, 29, 30]])" ] }, "execution_count": 178, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob = np.array([[1,2,3,4,5], [6,7,8,9,10], [11,12,13,14,15], [16,17,18,19,20], [21,22,23,24,25], [26,27,28,29,30]])\n", "prob" ] }, { "cell_type": "code", "execution_count": 179, "id": "629be5aa", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[11, 12],\n", " [16, 17]])" ] }, "execution_count": 179, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob[2:4, 0:2]" ] }, { "cell_type": "code", "execution_count": 185, "id": "e1dd9c97", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 2, 8, 14, 20])" ] }, "execution_count": 185, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob[[0,1,2,3], [1,2,3,4]]" ] }, { "cell_type": "code", "execution_count": 186, "id": "00dfcda7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 4, 5],\n", " [24, 25],\n", " [29, 30]])" ] }, "execution_count": 186, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob[[0,4,5], 3:]" ] } ], "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.2" } }, "nbformat": 4, "nbformat_minor": 5 }