{"id":711,"date":"2020-05-30T14:14:47","date_gmt":"2020-05-30T14:14:47","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=711"},"modified":"2020-05-30T14:14:47","modified_gmt":"2020-05-30T14:14:47","slug":"python-numpy-tutorial","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=711","title":{"rendered":"Python Numpy Tutorial For Beginners With Examples"},"content":{"rendered":"<p>This Python Numpy tutorial for beginners talks about Numpy basic concepts, practical examples, and real-world Numpy use cases related to machine learning and data science<\/p>\n<h2 id=\"what-is-numpy\">What is NumPy?<\/h2>\n<p><a href=\"https:\/\/numpy.org\/?ref=devopscube.com\" rel=\"noreferrer noopener\"><strong>NumPy<\/strong><\/a><strong> <\/strong>in python<strong> <\/strong>is a general-purpose array-processing package. It stands for <strong>Numerical Python<\/strong>. NumPy helps to create arrays (multidimensional arrays), with the help of bindings of C++. Therefore, it is quite fast. There are in-built functions of NumPy as well. It is the fundamental package for scientific computing with Python.<\/p>\n<p>The NumPy library also contains a multidimensional array and matrix data structures. It provides <strong>ndarray<\/strong>, a homogeneous n-dimensional array object, with methods to efficiently operate on it.<\/p>\n<p>It is one of the very important libraries used in the field of <a href=\"https:\/\/devopscube.com\/list-best-frameworks-data-scientists\/\" rel=\"noreferrer noopener\">Data Science<\/a> &amp; Machine Learning.<\/p>\n<p>Here is an interesting search trend for NumPy for the last five years.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/numpy-min-1.png\" class=\"kg-image\" alt=\"NumPy trends\" loading=\"lazy\" width=\"949\" height=\"636\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/numpy-min-1.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/numpy-min-1.png 949w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<h2 id=\"why-do-we-need-numpy\">Why do we need NumPy?<\/h2>\n<p>Does a question arise that <strong>why do we need a NumPy array when we have python lists?<\/strong><\/p>\n<p>The answer is we can perform operations on all the elements of a NumPy array at once, which are not possible with python lists.<\/p>\n<p>For example, we can&#8217;t multiply two lists directly we will have to do it element-wise. This is where the role of NumPy comes into play.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre><code>list1 = [2, 4, 6, 7, 8]\nlist2 = [3, 4, 6, 1, 5]\n\nprint(list1*list2)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre><code>TypeError: can't multiply sequence by non-int of type 'list'<\/code><\/pre>\n<p>Where the same thing can be done easily with NumPy arrays. Here is how NumPy works.<\/p>\n<p><strong>Numpy Array Example:<\/strong><\/p>\n<pre><code>import numpy as np\n\nlist1 = [2, 4, 6, 7, 8]\nlist2 = [3, 4, 6, 1, 5]\n\narr1 = np.array(list1)\narr2 = np.array(list2)\n\nprint(arr1*arr2)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre><code>[ 6 16 36 7 40]<\/code><\/pre>\n<h2 id=\"python-numpy-tutorial\">Python Numpy Tutorial<\/h2>\n<p>In this section, we will start with the installation of the key concepts involved in numPy. Also, we will go through some of the practical examples of NumPy library.<\/p>\n<h3 id=\"installing-numpy-in-python\">Installing NumPy In Python<\/h3>\n<p>Before you use NumPy you must install the NumPy library as a prerequisite. It can be installed with <strong><code>conda<\/code><\/strong>, with <strong><code>pip<\/code><\/strong>, or with a package manager.<\/p>\n<p>If you use <strong><code>conda<\/code><\/strong>, you can install it with:<\/p>\n<pre><code>conda install numpy<\/code><\/pre>\n<p>If you use <strong><code>pip<\/code><\/strong>, you can install it with:<\/p>\n<pre><code>pip install numpy<\/code><\/pre>\n<h3 id=\"arrays-in-numpy\">Arrays in NumPy<\/h3>\n<p>Array in NumPy is a table of elements, all of the same type, indexed by a tuple of positive integers. In NumPy, the number of dimensions of the array is called the rank of the array. A tuple of integers giving the size of the array along each dimension is known as the shape of the array. An array class in NumPy is called as <strong>ndarray<\/strong>.<\/p>\n<p><strong>Example<\/strong><\/p>\n<p>First of all import the numpy library<\/p>\n<pre><code>import numpy as np<\/code><\/pre>\n<p>Creating an array object using <strong>np.array()<\/strong><\/p>\n<pre><code>array = np.array([\n    [1, 2, 3, 5, 6],\n    [2, 1, 5, 6, 7]\n])<\/code><\/pre>\n<p>Printing the array dimension using <strong>array.ndim<\/strong><\/p>\n<pre><code>print(\"No. of dimensions of the array: \", array.ndim)<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>No. of dimensions of the array: 2<\/code><\/pre>\n<p>Printing the shape of the array using <strong>array.shape<\/strong><\/p>\n<pre><code>print(\"Shape of the array: \", array.shape)<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>Shape of the array: (2, 5)<\/code><\/pre>\n<p>Printing the size of the array using <strong>array.size<\/strong>. The size of the array means nothing but the total number of elements in the given array.<\/p>\n<pre><code>print(\"Size of the array: \", array.size)<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>Size of the array: 10<\/code><\/pre>\n<h3 id=\"creating-a-numpy-array\">Creating a NumPy Array<\/h3>\n<p>Arrays in NumPy can be created in multiple ways, with various number of Ranks, defining the size of the Array. Arrays can also be created with the use of various data types such as lists, tuples, etc.<\/p>\n<p><strong>Example<\/strong>:<\/p>\n<p>First of all import the numpy library<\/p>\n<pre><code>import numpy as np<\/code><\/pre>\n<p>Creating a rank 1 array by passing one python list<\/p>\n<pre><code>list = [1, 2, 3, 5, 6]\narray = np.array(list)\nprint(array)<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>[1 2 3 5 6]<\/code><\/pre>\n<p>Creating a rank 2 array by passing two python lists<\/p>\n<pre><code>list1 = [1, 2, 3, 5, 6]\nlist2 = [3, 1, 4, 5, 1]\narray = np.array(\n    [list1, list2]\n)\nprint(array)<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>[[1 2 3 5 6] \n[3 1 4 5 1]]<\/code><\/pre>\n<p>Creating array by passing a python tuple<\/p>\n<pre><code>tuple = (1, 2, 3, 5, 6)\narray = np.array(tuple)\nprint(array)<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>[1 2 3 5 6]<\/code><\/pre>\n<p>Creating a 3&#215;4 array with all zeros using <strong>np.zeros( )<\/strong><\/p>\n<pre><code>array = np.zeros((3, 4))\nprint(array)<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>[[0. 0. 0. 0.] \n[0. 0. 0. 0.] \n[0. 0. 0. 0.]]<\/code><\/pre>\n<p>Creating a constant value array of complex type using <strong>np.full( )<\/strong><\/p>\n<pre><code>array = np.full((3, 3), 6, dtype='complex')\nprint(array)<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>[[6.+0.j 6.+0.j 6.+0.j] \n[6.+0.j 6.+0.j 6.+0.j] \n[6.+0.j 6.+0.j 6.+0.j]]<\/code><\/pre>\n<h3 id=\"numpy-array-indexing\">NumPy Array Indexing<\/h3>\n<p>Indexing can be done in NumPy by using an array as an index. In case of slice, a view of the array is returned but in index array a copy of the original array is returned. Note that the first element is indexed by 0 second first by 1 and so on, whereas the last element is indexed by -1 second last by -2 and so on.<\/p>\n<p><strong>Example<\/strong>:<\/p>\n<p>Creating a rank 1 array by passing one python list<\/p>\n<pre><code>list = [1, 2, 3, 5, 6]\narray = np.array(list)\nprint(array)<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>[1 2 3 5 6]<\/code><\/pre>\n<p>Accessing elements and creating new array by passing indices<\/p>\n<pre><code>newArray = array[\n    np.array([2, 0, 1, 4])\n]\nprint(newArray)<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>[3 1 2 6]<\/code><\/pre>\n<ol>\n<li>Index values can be negative<\/li>\n<\/ol>\n<pre><code>newArray = array[\n    np.array([2, 0, -1, -4])\n]\nprint(newArray)<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>[3 1 6 2]<\/code><\/pre>\n<p>Basic slicing occurs when an object is a slice object that is of the form [start: stop: step]. <strong>Note that stop position is not included in slicing.<\/strong><\/p>\n<pre><code>newArray = array[1:-2:1]\nprint(newArray)<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>[2 3]<\/code><\/pre>\n<h3 id=\"basic-slicing-and-indexing-in-a-multidimensional-array\">Basic slicing and indexing in a multidimensional array<\/h3>\n<p>Slicing and indexing in a multidimensional array are a little bit tricky compared to slicing and indexing in a one-dimensional array.<\/p>\n<pre><code>import numpy as np\n\narray = np.array([\n    [2, 4, 5, 6],\n    [3, 1, 6, 9],\n    [4, 5, 1, 9],\n    [2, 9, 1, 7]\n])\nprint(array)\n\n# Slicing and indexing in 4x4 array\n# Print first two rows and first two columns\nprint(\"\\n\", array[0:2, 0:2])\n\n# Print all rows and last two columns\nprint(\"\\n\", array[:, 2:4])\n\n# Print all column but middle two rows\nprint(\"\\n\", array[1:3, :])<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>[[2 4 5 6]\n[3 1 6 9]\n[4 5 1 9]\n[2 9 1 7]]\n\n[[2 4]\n[3 1]]\n\n[[5 6]\n[6 9]\n[1 9]\n[1 7]]\n\n[[3 1 6 9]\n[4 5 1 9]]<\/code><\/pre>\n<h3 id=\"numpy-operations-on-array\">NumPy Operations on Array<\/h3>\n<p>In NumPy, arrays allow various operations that can be performed on a particular array or a combination of Arrays. These operations may include some basic Mathematical operations as well as Unary and Binary operations.<\/p>\n<p><strong>Example<\/strong>:<\/p>\n<p>Creating two different two dimensional arrays<\/p>\n<pre><code>array1 = np.array([\n    [2, 4, 5],\n    [3, 1, 6]\n])\n\narray2 = np.array([\n    [1, 5, 9],\n    [10, 32, 78]\n])<\/code><\/pre>\n<p>Adding 5 to every element in array1<\/p>\n<pre><code>newArray = array1 + 5\nprint(newArray)<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>[[ 7 9 10] \n[ 8 6 11]]<\/code><\/pre>\n<p>Adding 5 to every element in array1<\/p>\n<pre><code>newArray = array1 + 5\nprint(newArray)<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>[[ 7 9 10] \n[ 8 6 11]]<\/code><\/pre>\n<p>Multiplying 2 to every element in array2<\/p>\n<pre><code>newArray = array2 * 2\nprint(newArray)<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>[[ 2 10 18] \n[ 20 64 156]]<\/code><\/pre>\n<p>Sum of every element in array2<\/p>\n<pre><code>newArray = array2.sum()\nprint(newArray)<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>135<\/code><\/pre>\n<p>Adding two arrays<\/p>\n<pre><code>newArray = array1 + array2\nprint(newArray)<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>[[ 3 9 14] \n[13 33 84]]<\/code><\/pre>\n<h2 id=\"string-operations-using-numpy\">String Operations using NumPy<\/h2>\n<p>This module is used to perform vectorized string operations for arrays of dtype numpy.string_ or numpy.unicode_.<\/p>\n<p><strong>numpy.upper( )<\/strong><\/p>\n<p>Returns the uppercased string from the given string. It converts all lowercase characters to uppercase. If no lowercase characters exist, it returns the original string.<\/p>\n<p><strong>Example<\/strong>:<\/p>\n<pre><code>import numpy as np\n\nstring = \"devopscube\"\nprint(np.char.upper(string))<\/code><\/pre>\n<p><strong>Output<\/strong>:<\/p>\n<pre><code>DEVOPSCUBE<\/code><\/pre>\n<p>Similary one can use <strong>numpy.lower( )<\/strong> to convert all uppercase characters to lowercase.<\/p>\n<p><strong>numpy.split( )<\/strong><\/p>\n<p>Returns a list of strings after breaking the given string by the specified separator.<\/p>\n<p><strong>Example<\/strong>:<\/p>\n<pre><code>import numpy as np\n\nstring = \"devopscube.com\"\nprint(np.char.split(string, sep='.'))<\/code><\/pre>\n<p><strong>Output<\/strong>:<\/p>\n<pre><code>['devopscube', 'com']<\/code><\/pre>\n<p><strong>numpy.title( )<\/strong><\/p>\n<p>It is used to convert the first character in each word to Uppercase and remaining characters to Lowercase in the string and returns a new string.<\/p>\n<p><strong>Example<\/strong>:<\/p>\n<pre><code>import numpy as np\n\nstring = \"devopsCube is An amaZing pOrtal\"\nprint(np.char.title(string))<\/code><\/pre>\n<p><strong>Output<\/strong>:<\/p>\n<pre><code>Devopscube Is An Amazing Portal<\/code><\/pre>\n<p><strong>numpy.equal( )<\/strong><\/p>\n<p>This function checks for string1 == string2 elementwise and return a boolean value true or false.<\/p>\n<p><strong>Example<\/strong>:<\/p>\n<pre><code>import numpy as np\n\nstring1 = \"Devopscube\"\nstring2 = \"Devopscube.com\"\nprint(np.char.equal(string1, string2))<\/code><\/pre>\n<p><strong>Output<\/strong>:<\/p>\n<pre><code>False<\/code><\/pre>\n<p>To know more string functions in NumPy refer <a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/routines.char.html?ref=devopscube.com\" rel=\"noreferrer noopener\">String operations.<\/a><\/p>\n<h2 id=\"mathematical-functions-in-numpy\">Mathematical Functions in NumPy<\/h2>\n<p>NumPy contains a large number of various mathematical operations. NumPy provides standard trigonometric functions, functions for arithmetic operations, handling complex numbers, etc.<\/p>\n<p><strong>numpy.sin( )<\/strong><\/p>\n<p>This mathematical function helps the user to calculate trigonometric <strong>sine <\/strong>for given values.<\/p>\n<p><strong>Example<\/strong>:<\/p>\n<pre><code>import numpy as np\n\nx = 0\nprint(np.sin(x))\n\nx = np.pi \/ 2\nprint(np.sin(x))\n\nx = np.pi \/ 4\nprint(np.sin(x))<\/code><\/pre>\n<p><strong>Output<\/strong>:<\/p>\n<pre><code>0.0\n1.0\n0.7071067811865475<\/code><\/pre>\n<p>Similarly one can use <strong>numpy.cos( )<\/strong>, <strong>numpy.tan( )<\/strong>, <strong>numpy.arcsin( )<\/strong>, <strong>numpy.arccos( )<\/strong>, <strong>numpy.arctan( )<\/strong>  to calculate trignometric <strong>cosine (cos), tangent (tan), cosecant (csc), secant (sec), and cotangent (cot)<\/strong> respectively for given values.<\/p>\n<p><strong>numpy.round_( )<\/strong><\/p>\n<p>This mathematical function round an array to the given number of decimals.<\/p>\n<p><strong>Example<\/strong>:<\/p>\n<pre><code>import numpy as np\n\narr = [.33345, .1234, 1.456789]\nroundOffValues = np.round_(arr, decimals=3)\nprint(roundOffValues)<\/code><\/pre>\n<p><strong>Output<\/strong>:<\/p>\n<pre><code>[0.333 0.123 1.457]<\/code><\/pre>\n<p><strong>numpy.log( )<\/strong><\/p>\n<p>This mathematical function helps users to calculate <strong>Natural logarithm<\/strong> of all elements in the input array.<\/p>\n<p><strong>Example<\/strong>:<\/p>\n<pre><code>import numpy as np\n\narr = [1, 3, 50]\nlogValues = np.log(arr)\nprint(logValues)<\/code><\/pre>\n<p><strong>Output<\/strong>:<\/p>\n<pre><code>[0. 1.09861229 3.91202301]<\/code><\/pre>\n<p><strong>numpy.exp( )<\/strong><\/p>\n<p>This mathematical function helps users to calculate the exponential of all elements in the input array.<\/p>\n<p><strong>Example<\/strong>:<\/p>\n<pre><code>import numpy as np\n\narr = [1, 3, 50]\nexpValues = np.exp(arr)\nprint(expValues)<\/code><\/pre>\n<p><strong>Output<\/strong>:<\/p>\n<pre><code>[2.71828183e+00 2.00855369e+01 5.18470553e+21]<\/code><\/pre>\n<p>To know more mathematical functions in NumPy refer <a href=\"https:\/\/docs.scipy.org\/doc\/numpy-1.13.0\/reference\/routines.math.html?ref=devopscube.com\" rel=\"noreferrer noopener\">Mathematical functions.<\/a><\/p>\n<h2 id=\"numpy-use-cases-in-data-science-machine-learning\">NumPy Use Cases in Data Science &amp; Machine Learning<\/h2>\n<p>NumPy is a very popular Python library for large multi-dimensional array and matrix processing. With the help of a large collection of high-level mathematical functions it is very useful for fundamental scientific computations in Machine Learning.<\/p>\n<p>It is particularly useful for,<\/p>\n<ol>\n<li>Linear Algebra<\/li>\n<li>Fourier Transform<\/li>\n<li>Random Number Generations<\/li>\n<\/ol>\n<p>High-end libraries like TensorFlow uses NumPy internally for manipulation of Tensors.<\/p>\n<h3 id=\"numpy-linear-algebra-examples\">NumPy Linear Algebra Examples<\/h3>\n<p>Lots of ML concepts are tied up with linear algebra. It helps in<\/p>\n<ol>\n<li>To understand PCA(Principal Component Analysis),<\/li>\n<li>To build better ML algorithms from scratch,<\/li>\n<li>For processing Graphics in ML,<\/li>\n<li>It helps to understand Matrix factorization.<\/li>\n<\/ol>\n<p>In fact, it could be said that ML completely uses matrix operations.<\/p>\n<p>The Linear Algebra module of NumPy offers various methods to apply linear algebra on any NumPy array. One can find:<\/p>\n<ol>\n<li>Rank, determinant, transpose, trace, inverse, etc. of an array.<\/li>\n<li>Eigenvalues and eigenvectors of the given matrices<\/li>\n<li>The dot product of two scalar values, as well as vector values.<\/li>\n<li>Solve a linear matrix equation and much more!<\/li>\n<\/ol>\n<p>Lets looks at some NumPy sample exercises<\/p>\n<p><strong>1.<\/strong>  <strong>Find rank, determinant, transpose, trace, inverse, etc. of an array using Numpy<\/strong><\/p>\n<p><strong>Example<\/strong>:<\/p>\n<p>Creating a 3&#215;3 NumPy array<\/p>\n<pre><code>array = np.array([\n    [6, 1, 1],\n    [4, -2, 5],\n    [2, 8, 7]\n])<\/code><\/pre>\n<p>Calculating rank of array<\/p>\n<pre><code>rank = np.linalg.matrix_rank(array)\nprint(rank)<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>3<\/code><\/pre>\n<p>Calculating determinant of array<\/p>\n<pre><code>determinant = np.linalg.det(array)\nprint(determinant)<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>306.0<\/code><\/pre>\n<p>Calculating trace of array<\/p>\n<pre><code>trace = np.trace(array)\nprint(trace)<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>11<\/code><\/pre>\n<p>Calculating transpose of array<\/p>\n<pre><code>transpose = np.transpose(array)\nprint(transpose)<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>[[ 6 4 2]\n[ 1 -2 8]\n[ 1 5 7]]<\/code><\/pre>\n<p>Calculating inverse of array<\/p>\n<pre><code>inverse = np.linalg.inv(array)\nprint(inverse)<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>[[ 0.17647059 -0.00326797 -0.02287582]\n[ 0.05882353 -0.13071895 0.08496732]\n[-0.11764706 0.1503268 0.05228758]]<\/code><\/pre>\n<p><strong>2<\/strong>. <strong>Find eigenvalues and eigenvectors of the given matrices using NumPy<\/strong><\/p>\n<pre><code>import numpy as np\n\narray = np.array([\n    [1, 2, 3],\n    [4, 5, 6],\n    [7, 8, 9]\n])\n\neigenVal, eigenVec = np.linalg.eig(array)\nprint(eigenVal)\nprint(eigenVec)<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>[ 1.61168440e+01 -1.11684397e+00 -1.30367773e-15]\n[[-0.23197069 -0.78583024 0.40824829]\n[-0.52532209 -0.08675134 -0.81649658]\n[-0.8186735 0.61232756 0.40824829]]<\/code><\/pre>\n<p><strong>3. Find the dot product of two scalar values and vector values using NumPy<\/strong><\/p>\n<pre><code>import numpy as np\n\nscalarProduct = np.dot(6, 9)\nprint(\"Dot Product of scalar values  : \", scalarProduct)\n\nvector_a = 4 + 3j\nvector_b = 2 + 6j\n\nvectorProduct = np.dot(vector_a, vector_b)\nprint(\"Dot Product of vector values  : \", vectorProduct)<\/code><\/pre>\n<p>Output:<\/p>\n<p>Dot Product of scalar values : 54 Dot Product of vector values : (-10+30j)<\/p>\n<p><strong>4. Solve a linear matrix equation using Numpy<\/strong><\/p>\n<pre><code>import numpy as np\n\nA = np.array([\n    [1, 3],\n    [2, 4]\n])\n\nb = np.array([\n    [7],\n    [10]\n])\n\nx = np.linalg.solve(A, b)\nprint(x)<\/code><\/pre>\n<p>Output:<\/p>\n<p>[[1.]<br \/>[2.]]<\/p>\n<h3 id=\"numpy-fourier-transform-examples\">NumPy <strong>Fourier Transform<\/strong> Examples<\/h3>\n<p>In mathematics, a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Fourier_transform?ref=devopscube.com\" rel=\"noreferrer noopener\"><strong>Fourier transform<\/strong><\/a> (<strong>FT<\/strong>) is a mathematical transform that decomposes a function (often a function of time, or a signal) into its constituent frequencies. Some of the important applications of the FT include:<\/p>\n<ol>\n<li>Fast large-integer and polynomial multiplication,<\/li>\n<li>Efficient matrix-vector multiplication for Toeplitz, circulant and other structured matrices,<\/li>\n<li>Filtering algorithms,<\/li>\n<li>Fast algorithms for discrete cosine or sine transform (e.g. Fast Discrete Cosine Transform used for JPEG and MPEG\/MP3 encoding and decoding),<\/li>\n<li>Solving difference equations.<\/li>\n<\/ol>\n<p><strong>Example<\/strong>:<\/p>\n<p>Using <strong>np.fft( )<\/strong>, get the 1D  Fourier Transform<\/p>\n<pre><code>import numpy as np\n\nA = np.array([2, 4, 6, 8, 9])\nresult = np.fft.fft(A)\nprint(result)<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>[29. +0.j -5.30901699+5.93085309j -4.19098301+1.03681323j\n-4.19098301-1.03681323j -5.30901699-5.93085309j]<\/code><\/pre>\n<p>Using <strong>np.fft2( )<\/strong>, get the 2D  Fourier Transform<\/p>\n<pre><code>import numpy as np\n\nA = np.array([\n    [2, 4, 6, 8, 9],\n    [3, 1, 6, -2, -4]\n])\nresult = np.fft.fft2(A)\nprint(result)<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>[[ 33. +0.j -6.47213595 -3.52671151j\n2.47213595 +5.7063391j 2.47213595 -5.7063391j\n-6.47213595 +3.52671151j]\n[ 25. +0.j -4.14589803+15.38841769j\n-10.85410197 -3.63271264j -10.85410197 +3.63271264j\n-4.14589803-15.38841769j]]<\/code><\/pre>\n<p>Using <strong>np.fftn( )<\/strong>, get the N-D  Fourier Transform<\/p>\n<pre><code>import numpy as np\n\nA = np.array([\n    [2.3, 4.1, 6.5, 8, 9],\n    [3, -1.2, 6, -2, -4]\n])\nresult = np.fft.fftn(A)\nprint(result)<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>[[ 31.7 +0.j -7.22558014 -1.82338546j\n4.62558014 +7.41621639j 4.62558014 -7.41621639j\n-7.22558014 +1.82338546j]\n[ 28.1 +0.j -3.53966744+12.90709507j\n-12.26033256 -4.50909046j -12.26033256 +4.50909046j\n-3.53966744-12.90709507j]]<\/code><\/pre>\n<h3 id=\"numpy-random-number-generations\">NumPy <strong>Random Number Generations<\/strong><\/h3>\n<p>Using <strong>numpy.random.rand(d0, d1, &#8230;., dn ) <\/strong>creates an array of specified shape and fills it with random values, where d0, d1, &#8230;., dn are dimensions of the returned array. This function returns an array of defined shape and filled with random values.<\/p>\n<p><strong>Example<\/strong>:<\/p>\n<p>Randomly constructing 1D array<\/p>\n<pre><code>import numpy as np\n\nA = np.random.rand(7)\nprint(A)<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>[0.3126664 0.99492257 0.73164575 0.77857217 0.94840314 0.10833222\n0.14896065]<\/code><\/pre>\n<p>Randomly constructing 2D array<\/p>\n<pre><code>import numpy as np\n\nA = np.random.rand(3, 4)\nprint(A)<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>[[0.22751116 0.09730939 0.97083485 0.67629309]\n[0.94896123 0.96087311 0.8725199 0.48835455]\n[0.86496409 0.32296315 0.72891428 0.27729306]]<\/code><\/pre>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>In this python Numpy tutorial, I have covered the key concepts with some practical examples pertaining to Machine learning use cases.<\/p>\n<p>If you want to have a look at more Numpy practical examples, you can check out the <a href=\"https:\/\/devopscube.com\/numpy-practical-examples\/\" rel=\"noreferrer noopener\">practical NumPy examples.<\/a><\/p>\n<p>Now, I would like to hear from you. What Numpy use cases are you currently working on?<\/p>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/python-numpy-tutorial\/\" target=\"_blank\" rel=\"noopener noreferrer\">Python Numpy Tutorial For Beginners With Examples \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/python-numpy-tutorial\/<\/p>\n","protected":false},"author":1,"featured_media":712,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-711","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/711","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=711"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/711\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/712"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=711"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}