{"id":23244,"date":"2023-11-08T12:09:30","date_gmt":"2023-11-08T06:39:30","guid":{"rendered":"https:\/\/www.mygreatlearning.com\/blog\/python-numpy-tutorial\/"},"modified":"2025-01-06T19:05:54","modified_gmt":"2025-01-06T13:35:54","slug":"python-numpy-tutorial","status":"publish","type":"post","link":"https:\/\/www.mygreatlearning.com\/blog\/python-numpy-tutorial\/","title":{"rendered":"Python NumPy Tutorial"},"content":{"rendered":"\n<p>So you've learned the <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/master-python-programming\" target=\"_blank\" rel=\"noreferrer noopener\">basics of Python<\/a> and you're looking for a more powerful way to analyse data? NumPy is what you need.NumPy is a module for Python that allows you to work with multidimensional arrays and matrices. It's perfect for scientific or mathematical calculations because it's fast and efficient. In addition, NumPy includes support for signal processing and linear algebra operations. So if you need to do any mathematical operations on your data, NumPy is probably the library for you.&nbsp;<\/p>\n\n\n\n<p>In this tutorial, we'll show you how to use NumPy to its full potential. You'll learn more about arrays as well as operate on them using mathematical functions.&nbsp;<\/p>\n\n\n\n    <div class=\"courses-cta-container\">\n        <div class=\"courses-cta-card\">\n            <div class=\"courses-cta-header\">\n                <div class=\"courses-learn-icon\"><\/div>\n                <span class=\"courses-learn-text\">Academy Pro<\/span>\n            <\/div>\n            <p class=\"courses-cta-title\">\n                <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/master-python-programming\" class=\"courses-cta-title-link\">Python Programming Course<\/a>\n            <\/p>\n            <p class=\"courses-cta-description\">In this course, you will learn the fundamentals of Python: from basic syntax to mastering data structures, loops, and functions. You will also explore OOP concepts and objects to build robust programs.<\/p>\n            <div class=\"courses-cta-stats\">\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-user-icon\"><\/div>\n                    <span>11.5 Hrs<\/span>\n                <\/div>\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-star-icon\"><\/div>\n                    <span>51 Coding Exercises<\/span>\n                <\/div>\n            <\/div>\n            <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/master-python-programming\" class=\"courses-cta-button\">\n                Start Free Trial\n                <div class=\"courses-arrow-icon\"><\/div>\n            <\/a>\n        <\/div>\n    <\/div>\n\n\n\n<p>NumPy, which stands for Numerical Python, is a library consisting of multidimensional array objects and a collection of routines for processing those arrays. Using NumPy, mathematical and logical operations on <a href=\"https:\/\/www.mygreatlearning.com\/blog\/what-is-an-array-learn-more-in-one-read\/\" target=\"_blank\" rel=\"noreferrer noopener\">arrays <\/a>can be performed. In this Python Numpy Tutorial, we will be learning about NumPy in Python, What is NumPy in Python, Data Types in NumPy, and more.  <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-numpy-in-python\"><strong>What is NumPy in Python?<\/strong><\/h2>\n\n\n\n<p>NumPy in Python is a library that is used to work with arrays and was created in 2005 by Travis Oliphant. NumPy library in Python has functions for working in domain of Fourier transform, linear algebra, and matrices. Python NumPy is an open-source project that can be used freely. NumPy stands for Numerical Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-install-numpy-python\"><strong>How to install NumPy Python<\/strong>?<\/h2>\n\n\n\n<p>Installing the NumPy library is a straightforward process. You can use pip to install the library.Go to the command line and type the following:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-cyan-bluish-gray-background-color has-background\">pip install numpy \nIf you are using Anaconda distribution, then you can use conda to install NumPy. conda install numpy \nOnce the installation is complete, you can verify it by importing the NumPy library in the python interpreter. One can use the numpy library by importing it as shown below. \nimport numpy \nIf the import is successful, then you will see the following output. \n&gt;&gt;&gt; import numpy \n&gt;&gt;&gt; numpy.__version__ \n'1.17.2' \n<\/pre>\n\n\n\n<p>NumPy is a library for the Python programming language, and it's specifically designed to help you work with data.&nbsp;<\/p>\n\n\n\n<p>With NumPy, you can easily create arrays, which is a data structure that allows you to store multiple values in a single variable.<\/p>\n\n\n\n<p>In particular, NumPy arrays provide an efficient way of storing and manipulating data.NumPy also includes a number of functions that make it easy to perform mathematical operations on arrays. This can be really useful for scientific or engineering applications. And if you're working with data from a Python script, using NumPy can make your life a lot easier.&nbsp;<\/p>\n\n\n\n<p>Let us take a look at how to create NumPy arrays, copy and view arrays, reshape arrays, and iterate over arrays.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"numpy-creating-arrays\"><strong>NumPy Creating Arrays<\/strong><\/h2>\n\n\n\n<p>Arrays are different from Python lists in several ways. First, NumPy arrays are multi-dimensional, while Python lists are one-dimensional. Second, NumPy arrays are homogeneous, while Python lists are heterogeneous. This means that all the elements of a NumPy array must be of the same type. Third, NumPy arrays are more efficient than Python lists.NumPy arrays can be created in several ways. One way is to create an array from a Python list. Once you have created a NumPy array, you can manipulate it in various ways. For example, you can change the shape of an array, or you can index into an array to access its elements. You can also perform mathematical operations on NumPy arrays, such as addition, multiplication, and division.&nbsp;<\/p>\n\n\n\n<p>One has to import the library in the program to use it. The module NumPy has an array function in it which creates an array.&nbsp;<\/p>\n\n\n\n<p><strong>Creating an Array:&nbsp;<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>import numpy as np \narr = np.array(&#091;1, 2, 3, 4, 5]) \nprint(arr) \n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>Output: \n<\/strong>&#091;1 2 3 4 5] \n<\/code><\/pre>\n\n\n\n<p>We can also pass a tuple in the array function to create an array. 2<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np \narr = np.array((1, 2, 3, 4, 5)) \nprint(arr) \n<\/code><\/pre>\n\n\n\n<p>The output would be similar to the above case.<\/p>\n\n\n\n<p><strong>Dimensions- Arrays:&nbsp;<\/strong><\/p>\n\n\n\n<p><strong>0-D Arrays:&nbsp;<\/strong><\/p>\n\n\n\n<p>The following code will create a zero-dimensional array with a value 36.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>import numpy as np \narr = np.array(36) \nprint(arr) \n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code><strong>Output: \n<\/strong>36 \n<\/code><\/pre>\n\n\n\n<p><strong>1-Dimensional Array:&nbsp;<\/strong><\/p>\n\n\n\n<p>The array that has Zero Dimensional arrays as its elements is a uni-dimensional or 1-D array.&nbsp;<\/p>\n\n\n\n<p>The code below creates a 1-D array,&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>import numpy as np \narr = np.array(&#091;1, 2, 3, 4, 5]) \nprint(arr) \n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code><strong>Output: \n<\/strong>&#091;1 2 3 4 5] \n<\/code><\/pre>\n\n\n\n<p><strong>Two Dimensional Arrays:<\/strong><strong>&nbsp;<\/strong><\/p>\n\n\n\n<p>2-D Arrays are the ones that have 1-D arrays as its element. The following code will create a 2-D array with 1,2,3 and 4,5,6 as its values.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>import numpy as np \n3\narr1 = np.array(&#091;&#091;1, 2, 3], &#091;4, 5, 6]]) \nprint(arr1) \n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>Output: \n&#091;&#091;1 2 3] \n&#091;4 5 6]] <\/code><\/pre>\n\n\n\n<p><strong>Three Dimensional Arrays:&nbsp;<\/strong><\/p>\n\n\n\n<p>Let us see an example of creating a 3-D array with two 2-D arrays:<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>import numpy as np \narr1 = np.array(&#091;&#091;&#091;1, 2, 3], &#091;4, 5, 6]], &#091;&#091;1, 2, 3], &#091;4, 5, 6]]]) print(arr1) \n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>Output: \n&#091;&#091;&#091;1 2 3] \n&#091;4 5 6]] \n&#091;&#091;1 2 3] \n&#091;4 5 6]]] <\/code><\/pre>\n\n\n\n<p>To identify the dimensions of the array, we can use ndim as shown below:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>import numpy as np \na = np.array(36) \nd = np.array(&#091;&#091;&#091;1, 2, 3], &#091;4, 5, 6]], &#091;&#091;1, 2, 3], &#091;4, 5, 6]]]) \nprint(a.ndim) \nprint(d.ndim) \n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code><strong>Output: \n<\/strong>0 \n3 \n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"operations-using-numpy\"><strong>Operations using NumPy<\/strong><\/h2>\n\n\n\n<p>Using NumPy, a developer can perform the following operations \u2212<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Mathematical and logical operations on arrays.<\/li>\n\n\n\n<li>Fourier transforms and routines for shape manipulation.<\/li>\n\n\n\n<li>Operations related to linear algebra. NumPy has in-built functions for linear algebra and random number generation.<\/li>\n<\/ul>\n\n\n\n<p>NumPy \u2013 A Replacement for MatLab<\/p>\n\n\n\n<p>NumPy is often used along with packages like&nbsp;<a href=\"https:\/\/www.mygreatlearning.com\/blog\/scipy-tutorial\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>SciPy<\/strong>&nbsp;<\/a>(Scientific Python) and&nbsp;<a href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/python-matplotlib\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Matplotlib<\/strong>&nbsp;<\/a>(plotting library). This combination is widely used as a replacement for MatLab, a popular platform for technical computing. However, Python alternative to MatLab is now seen as a more modern and complete programming language.<\/p>\n\n\n\n<p>It is open-source, which is an added advantage of NumPy.<\/p>\n\n\n\n<p>The most important object defined in NumPy is an N-dimensional array type called&nbsp;<strong>ndarray<\/strong>. It describes the collection of items of the same type. Items in the collection can be accessed using a zero-based index.<\/p>\n\n\n\n<p>Every item in a ndarray takes the same size as the block in the memory. Each element in ndarray is an object of the data-type object (called&nbsp;<strong>dtype<\/strong>).<\/p>\n\n\n\n<p>Any item extracted from ndarray object (by slicing) is represented by a Python object of one of array scalar types. The following diagram shows a relationship between ndarray, data-type object (dtype) and array scalar type \u2212<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>An instance of ndarray class can be constructed by different array creation routines described later in the tutorial. The basic ndarray is created using an array function in NumPy as follows-<\/p>\n\n\n\n<p><strong>numpy.array&nbsp;<\/strong><\/p>\n\n\n\n<p>It creates a ndarray from any object exposing an array interface, or from any method that returns an array.<\/p>\n\n\n\n<p><strong>numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)<\/strong><\/p>\n\n\n\n<p>The&nbsp;<strong>ndarray<\/strong>&nbsp;object consists of a contiguous one-dimensional segment of computer memory, combined with an indexing scheme that maps each item to a location in the memory block. The memory block holds the elements in row-major order (C style) or a column-major order (FORTRAN or MatLab style).<\/p>\n\n\n\n<p>The above constructor takes the following parameters \u2212<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Sr.No.<\/td><td>Parameter &amp; Description<\/td><\/tr><tr><td>1&nbsp;<\/td><td><strong>object<\/strong> Any object exposing the array interface method returns an array or any (nested) sequence.<br><\/td><\/tr><tr><td>2<br>3<br><\/td><td><strong>dtype<\/strong> The desired data type of array, optional<strong>copy<\/strong>Optional. By default (true), the object is copied<br><\/td><\/tr><tr><td>4<\/td><td><strong>order<\/strong>C (row-major) or F (column-major) or A (any) (default)<br><\/td><\/tr><tr><td>5<\/td><td><strong>subok<\/strong> By default, returned array forced to be a base class array. If true, sub-classes passed through<br><\/td><\/tr><tr><td>6<\/td><td><strong>ndmin<\/strong> Specifies minimum dimensions of the resultant array<br><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Take a look at the following examples to understand better.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-1\"><strong>Example 1<\/strong><\/h3>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/HGPnM5\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np&nbsp;\na = np.array(&#091;1,2,3])&nbsp;\nprint a<\/code><\/pre>\n\n\n\n<p>The output is as follows \u2013<\/p>\n\n\n\n<p>[1, 2, 3]<br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-2\"><strong>Example 2<\/strong><\/h3>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/pQqmFF\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># more than one dimensions&nbsp;\nimport numpy as np&nbsp;\na = np.array(&#091;&#091;1, 2], &#091;3, 4]])&nbsp;\nprint a<\/code><\/pre>\n\n\n\n<p>The output is as follows \u2212<\/p>\n\n\n\n<p>[[1, 2]&nbsp;<\/p>\n\n\n\n<p>[3, 4]]<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-3\"><strong>Example 3<\/strong><\/h3>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/1206Tu\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># minimum dimensions&nbsp;\nimport numpy as np&nbsp;\na = np.array(&#091;1, 2, 3,4,5], ndmin = 2)&nbsp;\nprint a<\/code><\/pre>\n\n\n\n<p>The output is as follows \u2212<\/p>\n\n\n\n<p>[[1, 2, 3, 4, 5]]<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-4\"><strong>Example 4<\/strong><\/h3>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/XnFxTM\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># dtype parameter&nbsp;\nimport numpy as np&nbsp;\na = np.array(&#091;1, 2, 3], dtype = complex)&nbsp;\nprint a<\/code><\/pre>\n\n\n\n<p>The output is as follows \u2212<\/p>\n\n\n\n<p>[ 1.+0.j,&nbsp; 2.+0.j,&nbsp; 3.+0.j]<\/p>\n\n\n\n<p>The&nbsp;<strong>ndarray<\/strong>&nbsp;object consists of a contiguous one-dimensional segment of computer memory, combined with an indexing scheme that maps each item to a location in the memory block. The memory block holds the elements in row-major order (C style) or a column-major order (FORTRAN or MatLab style).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"numpy-data-types\"><strong>NumPy - Data Types<\/strong><\/h2>\n\n\n\n<p>Here is a list of the different Data Types in NumPy:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>bool_<\/li>\n\n\n\n<li>int_<\/li>\n\n\n\n<li>intc<\/li>\n\n\n\n<li>intp<\/li>\n\n\n\n<li>int8<\/li>\n\n\n\n<li>int16<\/li>\n\n\n\n<li>float_<\/li>\n\n\n\n<li>float64<\/li>\n\n\n\n<li>complex_<\/li>\n\n\n\n<li>complex64<\/li>\n\n\n\n<li>complex128<\/li>\n<\/ol>\n\n\n\n<p><strong>bool_<\/strong><\/p>\n\n\n\n<p>Boolean (True or False) stored as a byte<\/p>\n\n\n\n<p><strong>int_<\/strong><\/p>\n\n\n\n<p>Default integer type (same as C long; normally either int64 or int32)<\/p>\n\n\n\n<p><strong>intc<\/strong><\/p>\n\n\n\n<p>Identical to C int (normally int32 or int64)<\/p>\n\n\n\n<p><strong>intp<\/strong><\/p>\n\n\n\n<p>An integer used for indexing (same as C ssize_t; normally either int32 or int64)<\/p>\n\n\n\n<p><strong>int8<\/strong><\/p>\n\n\n\n<p>Byte (-128 to 127)<\/p>\n\n\n\n<p><strong>int16<\/strong><\/p>\n\n\n\n<p>Integer (-32768 to 32767)<\/p>\n\n\n\n<p><strong>float_<\/strong><\/p>\n\n\n\n<p>Shorthand for float64<\/p>\n\n\n\n<p><strong>float64<\/strong><\/p>\n\n\n\n<p>Double precision float: sign bit, 11 bits exponent, 52 bits mantissa<\/p>\n\n\n\n<p><strong>complex_<\/strong><\/p>\n\n\n\n<p>Shorthand for complex128<\/p>\n\n\n\n<p><strong>complex64<\/strong><\/p>\n\n\n\n<p>Complex number, represented by two 32-bit floats (real and imaginary components)<\/p>\n\n\n\n<p><strong>complex128<\/strong><\/p>\n\n\n\n<p>Complex number, represented by two 64-bit floats (real and imaginary components)<\/p>\n\n\n\n<p>NumPy numerical types are instances of dtype (data-type) objects, each having unique characteristics. The dtypes are available as np.bool_, np.float32, etc.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"data-type-objects-dtype\"><strong>Data Type Objects (dtype)<\/strong><\/h2>\n\n\n\n<p>A data type object describes the interpretation of a fixed block of memory corresponding to an array, depending on the following aspects \u2212<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Type of data (integer, float or Python object)<\/li>\n\n\n\n<li>Size of data<\/li>\n\n\n\n<li>Byte order (little-endian or big-endian)<\/li>\n\n\n\n<li>In case of structured type, the names of fields, data type of each field and part of the memory block taken by each field.<\/li>\n\n\n\n<li>If the data type is a subarray, its shape and data type<\/li>\n<\/ul>\n\n\n\n<p>The byte order is decided by prefixing '&lt;' or '&gt;' to the data type. '&lt;' means that encoding is little-endian (least significant is stored in smallest address). '&gt;' means that encoding is big-endian (a most significant byte is stored in smallest address).<\/p>\n\n\n\n<p>A dtype object is constructed using the following syntax \u2212<\/p>\n\n\n\n<p>numpy.dtype(object, align, copy)<\/p>\n\n\n\n<p>The parameters are \u2212<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Object<\/strong>&nbsp;\u2212 To be converted to data type object<\/li>\n\n\n\n<li><strong>Align<\/strong>&nbsp;\u2212 If true, adds padding to the field to make it similar to C-struct<\/li>\n\n\n\n<li><strong>Copy<\/strong>&nbsp;\u2212 Makes a new copy of dtype object. If false, the result is a reference to builtin data type object<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-1\"><strong>Example 1<\/strong><\/h3>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/Vp3O4u\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># using array-scalar type&nbsp;\nimport numpy as np&nbsp;\ndt = np.dtype(np.int32)&nbsp;\nprint dt<\/code><\/pre>\n\n\n\n<p>The output is as follows \u2212<\/p>\n\n\n\n<p>int32<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-2\"><strong>Example 2<\/strong><\/h3>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/1zalFW\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#int8, int16, int32, int64 can be replaced by equivalent string 'i1', 'i2','i4', etc.&nbsp;\nimport numpy as np&nbsp;\ndt = np.dtype('i4')\nprint dt&nbsp;<\/code><\/pre>\n\n\n\n<p>The output is as follows \u2212<\/p>\n\n\n\n<p>int32<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-3\"><strong>Example 3<\/strong><\/h3>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/MJf44e\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># using endian notation&nbsp;\nimport numpy as np&nbsp;\ndt = np.dtype('&gt;i4')&nbsp;\nprint dt<\/code><\/pre>\n\n\n\n<p>The output is as follows \u2212<\/p>\n\n\n\n<p>&gt;i4<\/p>\n\n\n\n<p>The following examples show the use of a structured data type. Here, the field name and the corresponding scalar data type is to be declared.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-4\"><strong>Example 4<\/strong><\/h3>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/Dr11A1\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># first create structured data type&nbsp;\nimport numpy as np&nbsp;\ndt = np.dtype(&#091;('age',np.int8)])&nbsp;\nprint dt&nbsp;<\/code><\/pre>\n\n\n\n<p>The output is as follows \u2013 [('age', 'i1')]&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-5\"><strong>Example 5<\/strong><\/h3>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/70YKZc\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># now apply it to ndarray object&nbsp;\nimport numpy as np&nbsp;\ndt = np.dtype(&#091;('age',np.int8)])&nbsp;\na = np.array(&#091;(10,),(20,),(30,)], dtype = dt)&nbsp;\nprint a<\/code><\/pre>\n\n\n\n<p>The output is as follows \u2013&nbsp;<\/p>\n\n\n\n<p>[(10,) (20,) (30,)]<\/p>\n\n\n\n<p>Each built-in data type has a character code that uniquely identifies it.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>'b'<\/strong>&nbsp;\u2212 boolean<\/li>\n\n\n\n<li><strong>'i'<\/strong>&nbsp;\u2212 (signed) integer<\/li>\n\n\n\n<li><strong>'u'<\/strong>&nbsp;\u2212 unsigned integer<\/li>\n\n\n\n<li><strong>'f'<\/strong>&nbsp;\u2212 floating-point<\/li>\n\n\n\n<li><strong>'c'<\/strong>&nbsp;\u2212 complex-floating point<\/li>\n\n\n\n<li><strong>'m'<\/strong>&nbsp;\u2212 timedelta<\/li>\n\n\n\n<li><strong>'M'<\/strong>&nbsp;\u2212 datetime<\/li>\n\n\n\n<li><strong>'O'<\/strong>&nbsp;\u2212 (Python) objects<\/li>\n\n\n\n<li><strong>'S', 'a'<\/strong>&nbsp;\u2212 (byte-)string<\/li>\n\n\n\n<li><strong>'U'<\/strong>&nbsp;\u2212 Unicode<\/li>\n\n\n\n<li><strong>'V'<\/strong>&nbsp;\u2212 raw data (void)<\/li>\n<\/ul>\n\n\n\n<p>We will also discuss the various array attributes of NumPy.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"ndarray-shape\"><strong>ndarray.shape<\/strong><\/h2>\n\n\n\n<p>This array attribute returns a tuple consisting of array dimensions. It can also be used to resize the array.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-1\"><strong>Example 1<\/strong><\/h3>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/SPvAYQ\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np&nbsp;\na = np.array(&#091;&#091;1,2,3],&#091;4,5,6]])&nbsp;\nprint a.shape<\/code><\/pre>\n\n\n\n<p>The output is as follows \u2212 (2, 3)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-2\"><strong>Example 2<\/strong><\/h3>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/SVO3RX\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># this resizes the ndarray&nbsp;\nimport numpy as np&nbsp;\na = np.array(&#091;&#091;1,2,3],&#091;4,5,6]])&nbsp;\na.shape = (3,2)&nbsp;\nprint a&nbsp;<\/code><\/pre>\n\n\n\n<p>The output is as follows -[[1, 2][3, 4] [5, 6]]<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"ndarray-ndim\"><strong>ndarray.ndim<\/strong><\/h2>\n\n\n\n<p>This array attribute returns the number of array dimensions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-1\"><strong>Example 1<\/strong><\/h3>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/za4WJQ\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># an array of evenly spaced numbers&nbsp;\nimport numpy as np&nbsp;\na = np.arange(24)&nbsp;\nprint a<\/code><\/pre>\n\n\n\n<p>The output is as follows \u2212<\/p>\n\n\n\n<p>[0 1&nbsp; 2&nbsp; 3&nbsp; 4&nbsp; 5&nbsp; 6&nbsp; 7&nbsp; 8&nbsp; 9&nbsp; 10&nbsp; 11&nbsp; 12&nbsp; 13&nbsp; 14&nbsp; 15&nbsp; 16 17 18 19 20 21 22 23]&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-2\"><strong>Example 2<\/strong><\/h3>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/sFgnoR\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># this is one dimensional array&nbsp;\nimport numpy as np&nbsp;\na = np.arange(24)&nbsp;\na.ndim&nbsp;&nbsp;\n# now reshape it&nbsp;\nb = a.reshape(2,4,3)&nbsp;\nprint b&nbsp;\n# b is having three dimensions<\/code><\/pre>\n\n\n\n<p>The output is as follows \u2212<\/p>\n\n\n\n<p>[[[ 0,&nbsp; 1,&nbsp; 2]&nbsp;<\/p>\n\n\n\n<p>[ 3,&nbsp; 4,&nbsp; 5]&nbsp;<\/p>\n\n\n\n<p>[ 6,&nbsp; 7,&nbsp; 8]&nbsp;<\/p>\n\n\n\n<p>[ 9, 10, 11]]&nbsp;&nbsp;<\/p>\n\n\n\n<p>[[12, 13, 14]&nbsp;<\/p>\n\n\n\n<p>[15, 16, 17]<\/p>\n\n\n\n<p>[18, 19, 20]&nbsp;<\/p>\n\n\n\n<p>[21, 22, 23]]]&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"numpy-itemsize\"><strong>numpy.itemsize<\/strong><\/h2>\n\n\n\n<p>This array attribute returns the length of each element of array in bytes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-1\"><strong>Example 1<\/strong><\/h3>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/pmlHmj\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># dtype of array is int8 (1 byte)&nbsp;\nimport numpy as np&nbsp;\nx = np.array(&#091;1,2,3,4,5], dtype = np.int8)\nprint x.itemsize<\/code><\/pre>\n\n\n\n<p>The output is as follows \u2212<\/p>\n\n\n\n<p>1<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-2\"><strong>Example 2<\/strong><\/h3>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/DyQL6L\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># dtype of array is now float32 (4 bytes)&nbsp;\nimport numpy as np&nbsp;\nx = np.array(&#091;1,2,3,4,5], dtype = np.float32)&nbsp;\nprint x.itemsize<\/code><\/pre>\n\n\n\n<p>The output is as follows \u2212<\/p>\n\n\n\n<p>4<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"numpy-flags\"><strong>numpy.flags<\/strong><\/h2>\n\n\n\n<p>The ndarray object has the following attributes. Its current values are returned by this function.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Sr.No.<\/strong><\/td><td><strong>Attribute &amp; Description<\/strong><\/td><\/tr><tr><td>1<\/td><td><strong>C_CONTIGUOUS (C)<\/strong>The data is in a single, C-style contiguous segment<\/td><\/tr><tr><td>2<\/td><td><strong>F_CONTIGUOUS (F)<\/strong>The data is in a single, Fortran-style contiguous segment<\/td><\/tr><tr><td>3<\/td><td><strong>OWNDATA (O)<\/strong>The array owns the memory it uses or borrows it from another object<\/td><\/tr><tr><td>4<\/td><td><strong>WRITEABLE (W)<\/strong>The data area can be written to. Setting this to False locks the data, making it read-only<\/td><\/tr><tr><td>5<\/td><td><strong>ALIGNED (A)<\/strong>The data and all elements are aligned appropriately for the hardware<\/td><\/tr><tr><td>6<\/td><td><strong>UPDATEIFCOPY (U)<\/strong>This array is a copy of some other array. When this array is deallocated, the base array will be updated with the contents of this array<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example\"><strong>Example<\/strong><\/h3>\n\n\n\n<p>The following example shows the current values of flags.<\/p>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/rZNm5x\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np&nbsp;\nx = np.array(&#091;1,2,3,4,5])&nbsp;\nprint x.flags<\/code><\/pre>\n\n\n\n<p>The output is as follows \u2212<\/p>\n\n\n\n<p>C_CONTIGUOUS : True&nbsp;<\/p>\n\n\n\n<p>F_CONTIGUOUS : True&nbsp;<\/p>\n\n\n\n<p>OWNDATA : True&nbsp;<\/p>\n\n\n\n<p>WRITEABLE : True&nbsp;<\/p>\n\n\n\n<p>ALIGNED : True&nbsp;<\/p>\n\n\n\n<p>UPDATEIFCOPY : False<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"numpy-array-creation-routines\"><strong>NumPy - Array Creation Routines<\/strong><\/h2>\n\n\n\n<p>A new&nbsp;<strong>ndarray<\/strong>&nbsp;object can be constructed by any of the following array creation routines or using a low-level ndarray constructor.<\/p>\n\n\n\n<p>numpy.empty<\/p>\n\n\n\n<p>It creates an uninitialized array of specified shape and dtype. It uses the following constructor \u2212<\/p>\n\n\n\n<p>numpy.empty(shape, dtype = float, order = 'C')<\/p>\n\n\n\n<p>The constructor takes the following parameters.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Sr.No.<\/strong><\/td><td><strong>Parameter &amp; Description<\/strong><\/td><\/tr><tr><td>1<\/td><td><strong>Shape<\/strong>Shape of an empty array in int or tuple of int<\/td><\/tr><tr><td>2<\/td><td><strong>Dtype<\/strong>Desired output data type. Optional<\/td><\/tr><tr><td>3<\/td><td><strong>Order<\/strong>'C' for C-style row-major array, 'F' for FORTRAN style column-<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example\"><strong>Example<\/strong><\/h3>\n\n\n\n<p>The following code shows an example of an empty array.<\/p>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/0oGlTq\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np&nbsp;\nx = np.empty(&#091;3,2], dtype = int)&nbsp;\nprint x<\/code><\/pre>\n\n\n\n<p>The output is as follows \u2212[[22649312&nbsp; &nbsp; 1701344351]&nbsp;&nbsp;<\/p>\n\n\n\n<p>[1818321759&nbsp; 1885959276] [16779776&nbsp; &nbsp; 156368896]]<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"numpy-zeros\"><strong>numpy.zeros<\/strong><\/h2>\n\n\n\n<p>Returns a new array of specified size, filled with zeros.<\/p>\n\n\n\n<p>numpy.zeros(shape, dtype = float, order = 'C')<\/p>\n\n\n\n<p>The constructor takes the following parameters.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Sr.No.<\/strong><\/td><td><strong>Parameter &amp; Description<\/strong><\/td><\/tr><tr><td>1<\/td><td><strong>Shape<\/strong>Shape of an empty array in int or sequence of int<\/td><\/tr><tr><td>2<\/td><td><strong>Dtype<\/strong>Desired output data type. Optional<\/td><\/tr><tr><td>3<\/td><td><strong>Order<\/strong>'C' for C-style row-major array, 'F' for FORTRAN style column-major array<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-1\"><strong>Example 1<\/strong><\/h3>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/j0LifQ\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># array of five ones. Default dtype is float&nbsp;\nimport numpy as np&nbsp;\nx = np.ones(5)&nbsp;\nprint x<\/code><\/pre>\n\n\n\n<p>The output is as follows \u2212<\/p>\n\n\n\n<p>[ 1.&nbsp; 1.&nbsp; 1.&nbsp; 1.&nbsp; 1.]<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"numpy-indexing-slicing\"><strong>NumPy - Indexing &amp; Slicing<\/strong><\/h2>\n\n\n\n<p>Contents of ndarray object can be accessed and modified by indexing or slicing, just like Python's in-built container objects.<\/p>\n\n\n\n<p>As mentioned earlier, items in ndarray object follows zero-based index. Three types of indexing methods are available \u2212&nbsp;<strong>field access, basic slicing<\/strong>&nbsp;and&nbsp;<strong>advanced indexing<\/strong>.<\/p>\n\n\n\n<p>Basic slicing is an extension of Python's basic concept of slicing to n dimensions. A Python slice object is constructed by giving&nbsp;<strong>start, stop<\/strong>, and&nbsp;<strong>step<\/strong>&nbsp;parameters to the built-in&nbsp;<strong>slice<\/strong>&nbsp;function. This slice object is passed to the array to extract a part of array.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-1\"><strong>Example 1<\/strong><\/h3>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/umffOi\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np&nbsp;\na = np.arange(10)&nbsp;\ns = slice(2,7,2)&nbsp;\nprint a&#091;s]<\/code><\/pre>\n\n\n\n<p>Its output is as follows \u2212<\/p>\n\n\n\n<p>[2&nbsp; 4&nbsp; 6]<\/p>\n\n\n\n<p>n the above example, an&nbsp;<strong>ndarray<\/strong>&nbsp;object is prepared by&nbsp;<strong>arange()<\/strong>&nbsp;function. Then a slice object is defined with start, stop, and step values 2, 7, and 2 respectively. When this slice object is passed to the ndarray, a part of it starting with index 2 up to 7 with a step of 2 is sliced.<\/p>\n\n\n\n<p>The same result can also be obtained by giving the slicing parameters separated by a colon : (start:stop:step) directly to the&nbsp;<strong>ndarray<\/strong>&nbsp;object.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-2\"><strong>Example 2<\/strong><\/h3>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/umffOi\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np&nbsp;\na = np.arange(10)&nbsp;\nb = a&#091;2:7:2]&nbsp;\nprint b<\/code><\/pre>\n\n\n\n<p>Here, we will get the same output \u2212 [2&nbsp; 4&nbsp; 6]<\/p>\n\n\n\n<p>If only one parameter is put, a single item corresponding to the index will be returned. If a: is inserted in front of it, all items from that index onwards will be extracted. If two parameters (with: between them) is used, items between the two indexes (not including the stop index) with default step one are sliced.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-3\"><strong>Example 3<\/strong><\/h3>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/A2tgc7\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># slice single item&nbsp;\nimport numpy as np&nbsp;\na = np.arange(10)&nbsp;\nb = a&#091;5]&nbsp;\nprint b<\/code><\/pre>\n\n\n\n<p>Its output is as follows \u2212<\/p>\n\n\n\n<p>5<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-4\"><strong>Example 4<\/strong><\/h3>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/7XwmEy\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># slice items starting from index&nbsp;\nimport NumPy as np&nbsp;\na = np.arange(10)&nbsp;\nprint a&#091;2:]<\/code><\/pre>\n\n\n\n<p>Now, the output would be \u2212<\/p>\n\n\n\n<p>[2&nbsp; 3&nbsp; 4&nbsp; 5&nbsp; 6&nbsp; 7&nbsp; 8&nbsp; 9]<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-5\"><strong>Example 5<\/strong><\/h3>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/S42KnK\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># slice items between indexes&nbsp;\nimport numpy as np&nbsp;\na = np.arange(10)&nbsp;\nprint a&#091;2:5]<\/code><\/pre>\n\n\n\n<p>Here, the output would be \u2212<\/p>\n\n\n\n<p>[2&nbsp; 3&nbsp; 4]&nbsp;<\/p>\n\n\n\n<p>The above description applies to multi-dimensional&nbsp;<strong>ndarray<\/strong>&nbsp;too.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"numpy-advanced-indexing\"><strong>NumPy - Advanced Indexing<\/strong><\/h2>\n\n\n\n<p>It is possible to make a selection from ndarray that is a non-tuple sequence, ndarray object of integer or Boolean data type, or a tuple with at least one item being a sequence object. Advanced indexing always returns a copy of the data. As against this, the slicing only presents a view.<\/p>\n\n\n\n<p>There are two types of advanced indexing \u2212&nbsp;<strong>Integer<\/strong>&nbsp;and&nbsp;<strong>Boolean<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"integer-indexing\"><strong>Integer Indexing<\/strong><\/h2>\n\n\n\n<p>This mechanism helps in selecting any arbitrary item in an array based on its N-dimensional index. Each integer array represents the number of indexes into that dimension. When the index consists of as many integer arrays as the dimensions of the target ndarray, it becomes straightforward.<\/p>\n\n\n\n<p>In the following example, one element of the specified column from each row of ndarray object is selected. Hence, the row index contains all row numbers, and the column index specifies the element to be selected.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-1\"><strong>Example 1<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np&nbsp;\nx = np.array(&#091;&#091;1, 2], &#091;3, 4], &#091;5, 6]])&nbsp;\ny = x&#091;&#091;0,1,2], &#091;0,1,0]]&nbsp;\nprint y<\/code><\/pre>\n\n\n\n<p>Its output would be as follows \u2212<\/p>\n\n\n\n<p>[1&nbsp; 4&nbsp; 5]<\/p>\n\n\n\n<p>The selection includes elements at (0,0), (1,1) and (2,0) from the first array.<\/p>\n\n\n\n<p>In the following example, elements placed at corners of a 4X3 array are selected. The row indices of selection are [0, 0] and [3,3] whereas the column indices are [0,2] and [0,2].<\/p>\n\n\n\n<p>Advanced and basic indexing can be combined by using one slice (:) or ellipsis (\u2026) with an index array. The following example uses a slice for the advanced index for column. The result is the same when a slice is used for both. But advanced index results in copy and may have different memory layout.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"boolean-array-indexing\"><strong>Boolean Array Indexing<\/strong><\/h2>\n\n\n\n<p>This type of advanced indexing is used when the resultant object is meant to be the result of Boolean operations, such as comparison operators.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-1\"><strong>Example 1<\/strong><\/h3>\n\n\n\n<p>In this example, items greater than 5 are returned as a result of Boolean indexing.<\/p>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/zjO0hs\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np&nbsp;\nx = np.array(&#091;&#091; 0,&nbsp; 1,&nbsp; 2],&#091; 3,&nbsp; 4,&nbsp; 5],&#091; 6,&nbsp; 7,&nbsp; 8],&#091; 9, 10, 11]])&nbsp;\nprint 'Our array is:'&nbsp;\nprint x&nbsp;\nprint '\\n'&nbsp;&nbsp;\n# Now we will print the items greater than 5&nbsp;\nprint 'The items greater than 5 are:'&nbsp;\nprint x&#091;x &gt; 5]<\/code><\/pre>\n\n\n\n<p>The output of this program would be \u2212<\/p>\n\n\n\n<p>Our array is:&nbsp;<\/p>\n\n\n\n<p>[[ 0&nbsp; 1&nbsp; 2]&nbsp;<\/p>\n\n\n\n<p>&nbsp;[ 3&nbsp; 4&nbsp; 5]&nbsp;<\/p>\n\n\n\n<p>&nbsp;[ 6&nbsp; 7&nbsp; 8]&nbsp;<\/p>\n\n\n\n<p>&nbsp;[ 9 10 11]]&nbsp;<\/p>\n\n\n\n<p>The items greater than 5 are:<\/p>\n\n\n\n<p>[ 6&nbsp; 7&nbsp; 8&nbsp; 9 10 11]&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"numpy-broadcasting\"><strong>NumPy - Broadcasting<\/strong><\/h2>\n\n\n\n<p>The term&nbsp;<strong>broadcasting<\/strong>&nbsp;refers to the ability of NumPy to treat arrays of different shapes during arithmetic operations. Arithmetic operations on arrays are usually done on corresponding elements. If two arrays are of exactly the same shape, then these operations are smoothly performed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-1\"><strong>Example 1<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np&nbsp;\na = np.array(&#091;1,2,3,4])&nbsp;\nb = np.array(&#091;10,20,30,40])&nbsp;\nc = a * b&nbsp;\nprint c<\/code><\/pre>\n\n\n\n<p>Its output is as follows \u2212[10 &nbsp; 40 &nbsp; 90 &nbsp; 160]<\/p>\n\n\n\n<p>If the dimensions of the two arrays are dissimilar, element-to-element operations are not possible. However, operations on arrays of non-similar shapes is still possible in NumPy, because of the broadcasting capability. The smaller array is&nbsp;<strong>broadcast<\/strong>&nbsp;to the size of the larger array so that they have compatible shapes.<\/p>\n\n\n\n<p>Broadcasting is possible if the following rules are satisfied \u2212<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Array with smaller&nbsp;<strong>ndim<\/strong>&nbsp;than the other is prepended with '1' in its shape.<\/li>\n\n\n\n<li>Size in each dimension of the output shape is maximum of the input sizes in that dimension.<\/li>\n\n\n\n<li>An input can be used in calculation if its size in a particular dimension matches the output size or its value is exactly 1.<\/li>\n\n\n\n<li>If an input has a dimension size of 1, the first data entry in that dimension is used for all calculations along that dimension.<\/li>\n<\/ul>\n\n\n\n<p>A set of arrays is said to be&nbsp;<strong>broadcastable<\/strong>&nbsp;if the above rules produce a valid result and one of the following is true \u2212<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Arrays have exactly the same shape.<\/li>\n\n\n\n<li>Arrays have the same number of dimensions and the length of each dimension is either a common length or 1.<\/li>\n\n\n\n<li>Array having too few dimensions can have its shape prepended with a dimension of length 1, so that the above stated property is true.<\/li>\n\n\n\n<li><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"numpy-iterating-over-array\"><strong>NumPy - Iterating Over Array<\/strong><\/h2>\n\n\n\n<p>NumPy package contains an iterator object&nbsp;<strong>numpy.nditer<\/strong>. It is an efficient multidimensional iterator object using which it is possible to iterate over an array. Each element of an array is visited using Python\u2019s standard Iterator interface.<\/p>\n\n\n\n<p>Let us create a 3X4 array using arrange() function and iterate over it using&nbsp;<strong>nditer<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"numpy-array-manipulation\"><strong>NumPy - Array Manipulation<\/strong><\/h2>\n\n\n\n<p>Several routines are available in NumPy package for manipulation of elements in ndarray object. They can be classified into the following types \u2212<\/p>\n\n\n\n<p>Changing Shape<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Sr.No.<\/strong><\/td><td><strong>Shape &amp; Description<\/strong><\/td><\/tr><tr><td>1<\/td><td>reshape: Gives a new shape to an array without changing its data<\/td><\/tr><tr><td>2<\/td><td>flatA 1-D iterator over the array<\/td><\/tr><tr><td>3<\/td><td>flatten: Returns a copy of the array collapsed into one dimension<\/td><\/tr><tr><td>4<\/td><td>ravel: Returns a contiguous flattened array<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Transpose Operations<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Sr.No.<\/strong><\/td><td><strong>Operation &amp; Description<\/strong><\/td><\/tr><tr><td>1<\/td><td>transpose: Permutes the dimensions of an array<\/td><\/tr><tr><td>2<\/td><td>ndarray.T Same as self.transpose()<\/td><\/tr><tr><td>3<\/td><td>rollaxis: Rolls the specified axis backwards<\/td><\/tr><tr><td>4<\/td><td>swapaxes: Interchanges the two axes of an array<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Changing Dimensions<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Sr.No.<\/strong><\/td><td><strong>Dimension &amp; Description<\/strong><\/td><\/tr><tr><td>1<\/td><td>broadcast: Produces an object that mimics broadcasting<\/td><\/tr><tr><td>2<\/td><td>broadcast_to: Broadcasts an array to a new shape<\/td><\/tr><tr><td>3<\/td><td>expand_dims: Expands the shape of an array<\/td><\/tr><tr><td>4<\/td><td>squeeze: Removes single-dimensional entries from the shape of an array<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Joining Arrays<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Sr.No.<\/strong><\/td><td><strong>Array &amp; Description<\/strong><\/td><\/tr><tr><td>1<\/td><td>concatenate: Joins a sequence of arrays along an existing axis<\/td><\/tr><tr><td>2<\/td><td>stack: Joins a sequence of arrays along a new axis<\/td><\/tr><tr><td>3<\/td><td>hstack: Stacks arrays in sequence horizontally (column wise)<\/td><\/tr><tr><td>4<\/td><td>vstack: Stacks arrays in sequence vertically (row wise)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Splitting Arrays<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><br><strong>Sr.No.<\/strong><\/td><td><strong>Array &amp; Description<\/strong><\/td><\/tr><tr><td>1<\/td><td>split: Splits an array into multiple sub-arrays<\/td><\/tr><tr><td>2<\/td><td>hsplit: Splits an array into multiple sub-arrays horizontally (column-wise)<\/td><\/tr><tr><td>3<\/td><td>vsplit: Splits an array into multiple sub-arrays vertically (row-wise)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Adding \/ Removing Elements<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Sr.No.<\/strong><\/td><td><strong>Element &amp; Description<\/strong><\/td><\/tr><tr><td>1<\/td><td>resize: Returns a new array with the specified shape<\/td><\/tr><tr><td>2<\/td><td>append: Appends the values to the end of an array<\/td><\/tr><tr><td>3<\/td><td>insert: Inserts the values along the given axis before the given indices<\/td><\/tr><tr><td>4<\/td><td>delete: Returns a new array with sub-arrays along an axis deleted<\/td><\/tr><tr><td>5<\/td><td>unique: Finds the unique elements of an array<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>NumPy - Binary Operators<\/p>\n\n\n\n<p>Following are the functions for bitwise operations available in NumPy package.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Sr.No.<\/strong><\/td><td><strong>Operation &amp; Description<\/strong><\/td><\/tr><tr><td>1<\/td><td>bitwise_and: Computes bitwise AND operation of array elements<\/td><\/tr><tr><td>2<\/td><td>bitwise_or: Computes bitwise OR operation of array elements<\/td><\/tr><tr><td>3<\/td><td>invert: Computes bitwise NOT<\/td><\/tr><tr><td>4<\/td><td>right_shift: Shifts bits of binary representation to the right<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"numpy-mathematical-functions\"><strong>NumPy - Mathematical Functions<\/strong><\/h2>\n\n\n\n<p>Quite understandably, NumPy contains a large number of various mathematical operations. NumPy provides standard trigonometric functions, functions for arithmetic operations, handling complex numbers, etc.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"trigonometric-functions\"><strong>Trigonometric Functions<\/strong><\/h3>\n\n\n\n<p>NumPy has standard trigonometric functions which return trigonometric ratios for a given angle in radians.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/c1di4d\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np&nbsp;\na = np.array(&#091;0,30,45,60,90])&nbsp;\nprint 'Sine of different angles:'&nbsp;\n# Convert to radians by multiplying with pi\/180&nbsp;\nprint np.sin(a*np.pi\/180)&nbsp;\nprint '\\n'&nbsp;&nbsp;\nprint 'Cosine values for angles in array:'&nbsp;\nprint np.cos(a*np.pi\/180)&nbsp;\nprint '\\n'&nbsp;&nbsp;\nprint 'Tangent values for given angles:'&nbsp;\nprint np.tan(a*np.pi\/180)&nbsp;<\/code><\/pre>\n\n\n\n<p>Here is its output \u2212<\/p>\n\n\n\n<p>Sine of different angles:<\/p>\n\n\n\n<p>[ 0.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0.5 &nbsp; &nbsp; &nbsp; &nbsp; 0.70710678&nbsp; 0.8660254 &nbsp; 1.&nbsp; &nbsp; &nbsp; &nbsp; ]<\/p>\n\n\n\n<p>Cosine values for angles in array:<\/p>\n\n\n\n<p>[&nbsp; 1.00000000e+00 &nbsp; 8.66025404e-01 &nbsp; 7.07106781e-01 &nbsp; 5.00000000e-01<\/p>\n\n\n\n<p>6.12323400e-17]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n\n\n\n<p>Tangent values for given angles:<\/p>\n\n\n\n<p>[&nbsp; 0.00000000e+00 &nbsp; 5.77350269e-01 &nbsp; 1.00000000e+00 &nbsp; 1.73205081e+00<\/p>\n\n\n\n<p>1.63312394e+16]<\/p>\n\n\n\n<p><strong>arcsin, arcos,<\/strong>&nbsp;and&nbsp;<strong>arctan<\/strong>&nbsp;functions return the trigonometric inverse of sin, cos, and tan of the given angle. The result of these functions can be verified by&nbsp;<strong>numpy.degrees() function<\/strong>&nbsp;by converting radians to degrees.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"functions-for-rounding\"><strong>Functions for Rounding<\/strong><\/h3>\n\n\n\n<p>numpy.around()<\/p>\n\n\n\n<p>This is a function that returns the value rounded to the desired precision. The function takes the following parameters.<\/p>\n\n\n\n<p>numpy.around(a,decimals)<\/p>\n\n\n\n<p>Where,&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Sr.No.<\/strong><\/td><td><strong>Parameter &amp; Description<\/strong><\/td><\/tr><tr><td>1<\/td><td><strong>a<\/strong>Input data<\/td><\/tr><tr><td>2<\/td><td><strong>decimals<\/strong>The number of decimals to round to. Default is 0. If negative, the integer is rounded to position to the left of the decimal point<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"numpy-statistical-functions\"><strong>NumPy - Statistical Functions<\/strong><\/h3>\n\n\n\n<p>NumPy has quite a few useful statistical functions for finding minimum, maximum, percentile standard deviation and variance, etc. from the given elements in the array. The functions are explained as follows \u2212<\/p>\n\n\n\n<p>numpy.amin() and numpy.amax()numpy.amin() and numpy.amax()<\/p>\n\n\n\n<p>These functions return the minimum and the maximum from the elements in the given array along the specified axis.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example\"><strong>Example<\/strong><\/h3>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/nLXo8o\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np&nbsp;\na = np.array(&#091;&#091;3,7,5],&#091;8,4,3],&#091;2,4,9]])&nbsp;\nprint 'Our array is:'&nbsp;\nprint a&nbsp;&nbsp;\nprint '\\n'&nbsp;&nbsp;\nprint 'Applying amin() function:'&nbsp;\nprint np.amin(a,1)&nbsp;\nprint '\\n'&nbsp;&nbsp;\nprint 'Applying amin() function again:'&nbsp;\nprint np.amin(a,0)&nbsp;\nprint '\\n'&nbsp;&nbsp;\nprint 'Applying amax() function:'&nbsp;\nprint np.amax(a)&nbsp;\nprint '\\n\u2019\nprint 'Applying amax() function again:'&nbsp;\nprint np.amax(a, axis = 0)<\/code><\/pre>\n\n\n\n<p>It will produce the following output \u2212<\/p>\n\n\n\n<p>Our array is:<\/p>\n\n\n\n<p>[[3 7 5]<\/p>\n\n\n\n<p>[8 4 3]<\/p>\n\n\n\n<p>[2 4 9]]<\/p>\n\n\n\n<p>Applying amin() function:<\/p>\n\n\n\n<p>[3 3 2]<\/p>\n\n\n\n<p>Applying amin() function again:<\/p>\n\n\n\n<p>[2 4 3]<\/p>\n\n\n\n<p>Applying amax() function:<\/p>\n\n\n\n<p>9<\/p>\n\n\n\n<p>Applying amax() function again:<\/p>\n\n\n\n<p>[8 7 9]<\/p>\n\n\n\n<p>numpy.ptp()<\/p>\n\n\n\n<p>The&nbsp;<strong>numpy.ptp()<\/strong>&nbsp;function returns the range (maximum-minimum) of values along an axis.<\/p>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/fkSafj\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np&nbsp;\na = np.array(&#091;&#091;3,7,5],&#091;8,4,3],&#091;2,4,9]])&nbsp;\nprint 'Our array is:'&nbsp;\nprint a&nbsp;\nprint '\\n'&nbsp;&nbsp;\nprint 'Applying ptp() function:'&nbsp;\nprint np.ptp(a)&nbsp;\nprint '\\n'&nbsp;&nbsp;\nprint 'Applying ptp() function along axis 1:'&nbsp;\nprint np.ptp(a, axis = 1)&nbsp;\nprint '\\n'&nbsp;&nbsp;&nbsp;\nprint 'Applying ptp() function along axis 0:'\nprint np.ptp(a, axis = 0)&nbsp;\nnumpy.percentile()<\/code><\/pre>\n\n\n\n<p>Percentile (or a centile) is a measure used in statistics indicating the value below which a given percentage of observations in a group of observations fall. The function&nbsp;<strong>numpy.percentile()<\/strong>&nbsp;takes the following arguments.<\/p>\n\n\n\n<p>Where,<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Sr.No.<\/strong><\/td><td><strong>Argument &amp; Description<\/strong><\/td><\/tr><tr><td>1<\/td><td><strong>a<\/strong>: Input array<\/td><\/tr><tr><td>2<\/td><td><strong>q: <\/strong>The percentile to compute must be between 0-100<\/td><\/tr><tr><td>3<\/td><td><strong>axis<\/strong>: The axis along which the percentile is to be calculated<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>A variety of sorting related functions are available in NumPy. These sorting functions implement different sorting algorithms, each of them characterized by the speed of execution, worst-case performance, the workspace required and the stability of algorithms. Following table shows the comparison of three sorting algorithms.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>kind<\/strong><\/td><td><strong>speed<\/strong><\/td><td><strong>worst case<\/strong><\/td><td><strong>work space<\/strong><\/td><td><strong>stable<\/strong><\/td><\/tr><tr><td>\u2018quicksort\u2019<\/td><td>1<\/td><td>O(n^2)<\/td><td>0<\/td><td>no<\/td><\/tr><tr><td>\u2018mergesort\u2019<\/td><td>2<\/td><td>O(n*log(n))<\/td><td>~n\/2<\/td><td>yes<\/td><\/tr><tr><td>\u2018heapsort\u2019<\/td><td>3<\/td><td>O(n*log(n))<\/td><td>0<\/td><td>no<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>numpy.sort()<\/p>\n\n\n\n<p>The sort() function returns a sorted copy of the input array. It has the following parameters \u2212<\/p>\n\n\n\n<p>numpy.sort(a, axis, kind, order)<\/p>\n\n\n\n<p>Where,<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Sr.No.<\/strong><\/td><td><strong>Parameter &amp; Description<\/strong><\/td><\/tr><tr><td>1<\/td><td><strong>a<\/strong>Array to be sorted<\/td><\/tr><tr><td>2<\/td><td><strong>axis<\/strong>The axis along which the array is to be sorted. If none, the array is flattened, sorting on the last axis<\/td><\/tr><tr><td>3<\/td><td><strong>kind<\/strong>Default is quicksort<\/td><\/tr><tr><td>4<\/td><td><strong>order<\/strong>If the array contains fields, the order of fields to be sorted<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"numpy-byte-swapping\"><strong>NumPy - Byte Swapping<\/strong><\/h2>\n\n\n\n<p>We have seen that the data stored in the memory of a computer depends on which architecture the CPU uses. It may be little-endian (least significant is stored in the smallest address) or big-endian (most significant byte in the smallest address).<\/p>\n\n\n\n<p>numpy.ndarray.byteswap()<\/p>\n\n\n\n<p>The&nbsp;<strong>numpy.ndarray.byteswap()<\/strong>&nbsp;function toggles between the two representations: bigendian and little-endian.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"numpy-copies-views\"><strong>NumPy - Copies &amp; Views<\/strong><\/h2>\n\n\n\n<p>While executing the functions, some of them return a copy of the input array, while some return the view. When the contents are physically stored in another location, it is called&nbsp;<strong>Copy<\/strong>. If on the other hand, a different view of the same memory content is provided, we call it as&nbsp;<strong>View<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"no-copy\"><strong>No Copy<\/strong><\/h3>\n\n\n\n<p>Simple assignments do not make the copy of array object. Instead, it uses the same id() of the original array to access it. The&nbsp;<strong>id()<\/strong>&nbsp;returns a universal identifier of Python object, similar to the pointer in C.<\/p>\n\n\n\n<p>Furthermore, any changes in either gets reflected in the other. For example, the changing shape of one will change the shape of the other too.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"view-or-shallow-copy\"><strong>View or Shallow Copy<\/strong><\/h3>\n\n\n\n<p>NumPy has&nbsp;<strong>ndarray.view()<\/strong>&nbsp;method which is a new array object that looks at the same data of the original array. Unlike the earlier case, change in dimensions of the new array doesn\u2019t change dimensions of the original.<\/p>\n\n\n\n<p>NumPy - Matrix Library<\/p>\n\n\n\n<p>NumPy package contains a Matrix library&nbsp;<strong>numpy.matlib<\/strong>. This module has functions that return matrices instead of ndarray objects.<\/p>\n\n\n\n<p>matlib.empty()<\/p>\n\n\n\n<p>The&nbsp;<strong>matlib.empty()<\/strong>&nbsp;function returns a new matrix without initializing the entries. The function takes the following parameters.<\/p>\n\n\n\n<p>numpy.matlib.empty(shape, dtype, order)<\/p>\n\n\n\n<p>Where,<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Sr.No.<\/strong><\/td><td><strong>Parameter &amp; Description<\/strong><\/td><\/tr><tr><td>1<\/td><td><strong>shape<\/strong><strong>int<\/strong>&nbsp;or tuple of&nbsp;<strong>int<\/strong>&nbsp;defining the shape of the new matrix<\/td><\/tr><tr><td>2<\/td><td><strong>Dtype<\/strong>Optional. Data type of the output<\/td><\/tr><tr><td>3<\/td><td><strong>order<\/strong>C or F<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example\"><strong>Example<\/strong><\/h3>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/KSq8tv\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy.matlib&nbsp;\nimport numpy as np&nbsp;\nprint np.matlib.empty((2,2))&nbsp;\n# filled with random data<\/code><\/pre>\n\n\n\n<p>It will produce the following output \u2212<\/p>\n\n\n\n<p>[[ 2.12199579e-314, &nbsp; 4.24399158e-314]&nbsp;<\/p>\n\n\n\n<p>&nbsp;[ 4.24399158e-314, &nbsp; 2.12199579e-314]]&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"numpy-matlib-eye\"><strong>numpy.matlib.eye()<\/strong><\/h2>\n\n\n\n<p>This function returns a matrix with 1 along the diagonal elements and the zeros elsewhere. The function takes the following parameters.<\/p>\n\n\n\n<p>numpy.matlib.eye(n, M,k, dtype)<\/p>\n\n\n\n<p>Where,<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Sr.No.<\/strong><\/td><td><strong>Parameter &amp; Description<\/strong><\/td><\/tr><tr><td>1<\/td><td><strong>n<\/strong>The number of rows in the resulting matrix<\/td><\/tr><tr><td>2<\/td><td><strong>M<\/strong>The number of columns, defaults to n<\/td><\/tr><tr><td>3<\/td><td><strong>k<\/strong>Index of diagonal<\/td><\/tr><tr><td>4<\/td><td><strong>dtype<\/strong>Data type of the output<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example\"><strong>Example<\/strong><\/h3>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/7RQVas\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy.matlib&nbsp;\nimport numpy as np&nbsp;\nprint np.matlib.eye(n = 3, M = 4, k = 0, dtype = float)<\/code><\/pre>\n\n\n\n<p>It will produce the following output \u2212<\/p>\n\n\n\n<p>[[ 1.&nbsp; 0.&nbsp; 0.&nbsp; 0.]&nbsp;<\/p>\n\n\n\n<p>&nbsp;[ 0.&nbsp; 1.&nbsp; 0.&nbsp; 0.]&nbsp;<\/p>\n\n\n\n<p>&nbsp;[ 0.&nbsp; 0.&nbsp; 1.&nbsp; 0.]]&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"numpy-matplotlib\"><strong>NumPy - Matplotlib<\/strong><\/h2>\n\n\n\n<p>Matplotlib is a plotting library for Python. It is used along with NumPy to provide an environment that is an effective open-source alternative for MatLab. It can also be used with graphics toolkits like PyQt and wxPython.<\/p>\n\n\n\n<p>Matplotlib module was first written by John D. Hunter. Since 2012, Michael Droettboom is the principal developer. Currently, Matplotlib ver. 1.5.1 is the stable version available. The package is available in binary distribution as well as in the source code form on&nbsp;<a href=\"http:\/\/www.matplotlib.org\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">www.matplotlib.org<\/a>.<\/p>\n\n\n\n<p>Conventionally, the package is imported into the Python script by adding the following statement \u2212<\/p>\n\n\n\n<p>from matplotlib import pyplot as plt<\/p>\n\n\n\n<p>Here&nbsp;<strong>pyplot()<\/strong>&nbsp;is the most important function in matplotlib library, which is used to plot 2D data. The following script plots the equation&nbsp;<strong>y = 2x + 5<\/strong><\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np \nfrom matplotlib import pyplot as plt \nx = np.arange(1,11) \ny = 2 * x + 5 \nplt.title(\"Matplotlib demo\") \nplt.xlabel(\"x axis caption\") \nplt.ylabel(\"y axis caption\") \nplt.plot(x,y) \nplt.show()\n<\/code><\/pre>\n\n\n\n<p>An ndarray object x is created from&nbsp;<strong>np.arange() function<\/strong>&nbsp;as the values on the&nbsp;<strong>x axis<\/strong>. The corresponding values on the&nbsp;<strong>y axis<\/strong>&nbsp;are stored in another&nbsp;<strong>ndarray object y<\/strong>. These values are plotted using&nbsp;<strong>plot()<\/strong>&nbsp;function of pyplot submodule of matplotlib package.<\/p>\n\n\n\n<p>The graphical representation is displayed by&nbsp;<strong>show()<\/strong>&nbsp;function.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Instead of the linear graph, the values can be displayed discretely by adding a format string to the&nbsp;<strong>plot()<\/strong>&nbsp;function. Following formatting characters can be used.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"numpy-using-matplotlib\"><strong>NumPy -&nbsp;Using Matplotlib<\/strong><\/h2>\n\n\n\n<p>NumPy has a&nbsp;<strong>numpy.histogram()<\/strong>&nbsp;function that is a graphical representation of the frequency distribution of data. Rectangles of equal horizontal size corresponding to class interval called&nbsp;<strong>bin<\/strong>&nbsp;and&nbsp;<strong>variable height<\/strong>&nbsp;corresponding to frequency.<\/p>\n\n\n\n<p>numpy.histogram()<\/p>\n\n\n\n<p>The numpy.histogram() function takes the input array and bins as two parameters. The successive elements in bin array act as the boundary of each bin.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np&nbsp;\na = np.array(&#091;22,87,5,43,56,73,55,54,11,20,51,5,79,31,27])&nbsp;\nnp.histogram(a,bins = &#091;0,20,40,60,80,100])&nbsp;\nhist,bins = np.histogram(a,bins = &#091;0,20,40,60,80,100])&nbsp;\nprint hist&nbsp;\nprint bins&nbsp;<\/code><\/pre>\n\n\n\n<p>It will produce the following output \u2212<\/p>\n\n\n\n<p>[3 4 5 2 1]<\/p>\n\n\n\n<p>[0 20 40 60 80 100]<\/p>\n\n\n\n<p>plt()<\/p>\n\n\n\n<p>Matplotlib can convert this numeric representation of histogram into a graph. The&nbsp;<strong>plt() function<\/strong>&nbsp;of pyplot submodule takes the array containing the data and bin array as parameters and converts into a histogram.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from matplotlib import pyplot as plt&nbsp;\nimport numpy as np&nbsp;&nbsp;\na = np.array(&#091;22,87,5,43,56,73,55,54,11,20,51,5,79,31,27])&nbsp;\nplt.hist(a, bins = &#091;0,20,40,60,80,100])&nbsp;\nplt.title(\"histogram\")&nbsp;\nplt.show()<\/code><\/pre>\n\n\n\n<p>It should produce the following output \u2013<\/p>\n\n\n\n<p>I\/O with NumPy<\/p>\n\n\n\n<p>The ndarray objects can be saved to and loaded from the disk files. The IO functions available are \u2212<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>load()<\/strong>&nbsp;and&nbsp;<strong>save()<\/strong>&nbsp;functions handle \/numPy binary files (with&nbsp;<strong>npy<\/strong>&nbsp;extension)<\/li>\n\n\n\n<li><strong>loadtxt()<\/strong>&nbsp;and&nbsp;<strong>savetxt()<\/strong>&nbsp;functions handle normal text files<\/li>\n<\/ul>\n\n\n\n<p>NumPy introduces a simple file format for ndarray objects. This&nbsp;<strong>.npy<\/strong>&nbsp;file stores data, shape, dtype and other information required to reconstruct the ndarray in a disk file such that the array is correctly retrieved even if the file is on another machine with different architecture.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"numpy-save\"><strong>numpy.save()<\/strong><\/h2>\n\n\n\n<p>The&nbsp;<strong>numpy.save()<\/strong>&nbsp;file stores the input array in a disk file with&nbsp;<strong>npy<\/strong>&nbsp;extension.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np \na = np.array(&#091;1,2,3,4,5]) \nnp.save('outfile',a)\n<\/code><\/pre>\n\n\n\n<p>To reconstruct array from&nbsp;<strong>outfile.npy<\/strong>, use&nbsp;<strong>load()<\/strong>&nbsp;function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np&nbsp;\nb = np.load('outfile.npy')&nbsp;\nprint b&nbsp;<\/code><\/pre>\n\n\n\n<p>It will produce the following output \u2212<\/p>\n\n\n\n<p>array([1, 2, 3, 4, 5])<\/p>\n\n\n\n<p>The save() and load() functions accept an additional Boolean parameter&nbsp;<strong>allow_pickles<\/strong>. A pickle in Python is used to serialize and de-serialize objects before saving to or reading from a disk file.<\/p>\n\n\n\n<p>savetxt()<\/p>\n\n\n\n<p>The storage and retrieval of array data in simple text file format is done with&nbsp;<strong>savetxt()<\/strong>&nbsp;and&nbsp;<strong>loadtxt()<\/strong>&nbsp;functions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example\"><strong>Example<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np&nbsp;\na = np.array(&#091;1,2,3,4,5])&nbsp;\nnp.savetxt('out.txt',a)&nbsp;\nb = np.loadtxt('out.txt')&nbsp;\nprint b&nbsp;<\/code><\/pre>\n\n\n\n<p>It will produce the following output \u2212<\/p>\n\n\n\n<p>[ 1.&nbsp; 2.&nbsp; 3.&nbsp; 4.&nbsp; 5.]&nbsp;<\/p>\n\n\n\n<p>We\u2019d also recommend you to visit&nbsp;<a href=\"https:\/\/www.mygreatlearning.com\/academy\" target=\"_blank\" rel=\"noreferrer noopener\">Great Learning Academy<\/a>, where you will find a free <a href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/numpy-tutorial\" target=\"_blank\" rel=\"noreferrer noopener\">NumPy course<\/a> and 1000+ other courses. You will also receive a certificate after the completion of these courses. We hope that this Python NumPy Tutorial was beneficial and you are now better equipped. <\/p>\n\n\n\n<p><strong>NumPy Copy vs View&nbsp;<\/strong><\/p>\n\n\n\n<p>The difference between copy and view of an array in NumPy is that the view is merely a view of the original array whereas copy is a new array. The copy will not affect the original array and the chances are restricted to the new array created and many modifications made to the original array will not be reflected in the copy array too. But in view, the changes made to the view will be reflected in the original array and vice versa.&nbsp;<\/p>\n\n\n\n<p>Let us understand with code snippets:&nbsp;<\/p>\n\n\n\n<p><strong>Example of Copy:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>import numpy as np \narr1 = np.array(&#091;1, 2, 3, 4, 5]) \ny = arr1.copy() \narr1&#091;0] = 36 \nprint(arr1) \nprint(y) \n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>Output : \n&#091;42 2 3 4 5] \n&#091;1 2 3 4 5] \n<\/code><\/pre>\n\n\n\n<p><strong>Example of view:&nbsp;<\/strong><\/p>\n\n\n\n<p>Notice the output of the below code; the changes made to the original array are also reflected in the view.<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>import numpy as np \narr1 = np.array(&#091;1, 2, 3, 4, 5]) \ny= arr1.view() \narr1&#091;0] = 36 \nprint(arr1) \nprint(y) \n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>Output: \n&#091;36 2 3 4 5] \n&#091;36 2 3 4 5] \n<\/code><\/pre>\n\n\n\n<p><strong>NumPy Array Shape&nbsp;<\/strong><\/p>\n\n\n\n<p>The shape of an array is nothing but the number of elements in each dimension. To get the shape of an array, we can use a .shape attribute that returns a tuple indicating the number of elements.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np \narray1 = np.array(&#091;&#091;2, 3, 4,5], &#091; 6, 7, 8,9]]) \nprint(array1.shape) \n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output: (2,4) <\/code><\/pre>\n\n\n\n<p><strong>NumPy Array Reshape&nbsp;<\/strong><\/p>\n\n\n\n<p><strong>1-D to 2-D:&nbsp;<\/strong><\/p>\n\n\n\n<p>Array reshape is nothing but changing the shape of the array, through which one can add or remove a number of elements in each dimension. The following code will convert a 1-D array into 2-D array. The resulting will have 3 arrays having 4 elements&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>import numpy as np \narray_1 = np.array(&#091;1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) \nnewarr1 = array_1.reshape(3, 4) \nprint(newarr1) \n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>Output: \n&#091;&#091; 1 2 3 4] \n&#091; 5 6 7 8] \n&#091; 9 10 11 12]] \n<\/code><\/pre>\n\n\n\n<p><strong>1-D to 3-D:&nbsp;<\/strong><\/p>\n\n\n\n<p>The outer dimension will contain two arrays that have three arrays with two elements each.<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>import numpy as np \narray_1= np.array(&#091;1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) \nnewarr1 = array_1.reshape(2, 3, 2) \nprint(newarr1) \n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>Output: \n&#091;&#091;&#091; 1 2] \n&#091; 3 4] \n&#091; 5 6]] \n&#091;&#091; 7 8] \n&#091; 9 10] \n&#091;11 12]]] \n<\/code><\/pre>\n\n\n\n<p><strong>Flattening arrays:&nbsp;<\/strong><\/p>\n\n\n\n<p>Converting higher dimensions arrays into one-dimensional arrays is called flattening of arrays.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>import numpy as np \narr1= np.array(&#091;&#091;4,5,6], &#091;7, 8, 9]]) \nnewarr1 = arr1.reshape(-1) \nprint(newarr1) \n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>Output : \n&#091;1 2 3 4 5 6] \n<\/code><\/pre>\n\n\n\n<p><strong>NumPy Array Iterating&nbsp;<\/strong><\/p>\n\n\n\n<p>Iteration through the arrays is possible using for loop.&nbsp;<\/p>\n\n\n\n<p>Example 1:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>import numpy as np \narr1 = np.array(&#091;1, 2, 3]) \nfor i in arr1: \nprint(i) \n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>Output: 1 \n2 \n3 \n<\/code><\/pre>\n\n\n\n<p>Example 2:<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>import numpy as np \narr = np.array(&#091;&#091;4, 5, 6], &#091;1, 2, 3]]) \nfor x in arr: \nprint(x) \nOutput: &#091;4, 5, 6] \n&#091;1, 2, 3] \n<\/code><\/pre>\n\n\n\n<p>Example3:  <\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>import numpy as np \narray1 = np.array(&#091;&#091;1, 2, 3], &#091;4, 5, 6]]) \nfor x in array1: \nfor y in x: \nprint(y) \n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"numpy-array-join\"><strong>NumPy Array Join&nbsp;<\/strong><\/h2>\n\n\n\n<p>Joining is an operation of combining one or two arrays into a single array. In Numpy, the arrays are joined by axes. The concatenate() function is used for this operation, it takes a sequence of arrays that are to be joined, and if the axis is not specified, it will be taken as 0.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>import numpy as np \narr1 = np.array(&#091;1, 2, 3]) \narr2 = np.array(&#091;4, 5, 6]) \nfinalarr = np.concatenate((arr1, arr2)) \nprint(finalarr) \n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>Output: &#091;1 2 3 4 5 6] \n<\/code><\/pre>\n\n\n\n<p>The following code joins the specified arrays along the rows&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>import numpy as np \narr1 = np.array(&#091;&#091;1, 2], &#091;3, 4]]) \narr2 = np.array(&#091;&#091;5, 6], &#091;7, 8]]) \nfinalarr = np.concatenate((arr1, arr2), axis=1) \nprint(finalarr) \n<strong>Output: \n<\/strong>&#091;&#091;1 2 5 6] \n&#091;3 4 7 8]] \n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"numpy-array-split\"><strong>NumPy Array Split&nbsp;<\/strong><\/h2>\n\n\n\n<p>As we know, split does the opposite of join operation. Split breaks a single array as specified. The function array_split() is used for this operation and one has to pass the number of splits along with the array.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>import numpy as np \narr1 = np.array(&#091;7, 8, 3, 4, 1, 2]) \nfinalarr = np.array_split(arr1, 3) \nprint(finalarr) \nOutput: &#091;array(&#091;7, 8]), array(&#091;3, 4]), array(&#091;1, 2])] \n<\/code><\/pre>\n\n\n\n<p>Look at an exceptional case where the no of elements is less than required and observe the output&nbsp;<\/p>\n\n\n\n<p>Example :<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>import numpy as np \narray_1 = np.array(&#091;4, 5, 6,1,2,3]) \nfinalarr = np.array_split(array_1, 4) \nprint(finalarr) \nOutput : &#091;array(&#091;4, 5]), array(&#091;6, 1]), array(&#091;2]), array(&#091;3])] \n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"split-into-arrays\"><strong>Split into Arrays<\/strong><\/h2>\n\n\n\n<p>The array_split() will return an array containing an array as a split, we can access the elements just as we do in a normal array.<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>import numpy as np \narray1 = np.array(&#091;4, 5, 6,7,8,9]) \nfinalarr = np.array_split(array1, 3) \nprint(finalarr&#091;0]) \n\nprint(finalarr&#091;1]) \n<strong>Output : \n<\/strong>&#091;4 5] \n&#091;6 7] \n<\/code><\/pre>\n\n\n\n<p>Splitting of 2-D arrays is also similar, send the 2-d array in the array_split()&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>import numpy as np \narr1 = np.array(&#091;&#091;1, 2], &#091;3, 4], &#091;5, 6], &#091;7, 8], &#091;9, 10], &#091;11, 12]]) \nfinalarr = np.array_split(arr1, 3) \nprint(finalarr) \nOutput: \n&#091;array(&#091;&#091;1, 2], \n&#091;3, 4]]), array(&#091;&#091;5, 6], \n&#091;7, 8]]), array(&#091;&#091; 9, 10], \n&#091;11, 12]])] \n<\/code><\/pre>\n\n\n\n<p><strong>NumPy Array Search&nbsp;<\/strong><\/p>\n\n\n\n<p>The where() method is used to search an array. It returns the index of the value specified in the where method.&nbsp;<\/p>\n\n\n\n<p>The below code will return a tuple indicating that element 4 is at 3,5 and 6&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>import numpy as np \narr1 = np.array(&#091;1, 2, 3, 4, 5, 4, 4]) \ny = np.where(arr1 == 4) \nprint(y) \nOutput : (array(&#091;3, 5, 6]),) \n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"built-in-methods\"><strong>Built-In Methods<\/strong><\/h3>\n\n\n\n<p>Numpy allows us to use many built-in methods for generating arrays. Let\u2019s examine the most used from those, as well as their purpose:<\/p>\n\n\n\n<p>np.arange() - array of arranged values from low to high value<br>\nnp.zeros() - array of zeros with specified shape<br>\nnp.ones() - similarly to zeros, array of ones with specified shape<br>\nnp.linspace() - array of linearly spaced numbers, with specified size<br>\nnp.eye() - two dimensional array with ones on the diagonal, zeros elsewhere<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"arange\"><strong>Arange<\/strong><\/h3>\n\n\n\n<p>Numpy\u2019s arange function will return evenly spaced values within a given interval. Works similarly to Python\u2019s range() function. The only required parameter is \u2018stop\u2019, while all the other parameters are optional:<\/p>\n\n\n<figure class=\"wp-block-image size-large is-resized zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-4.png\"><img decoding=\"async\" width=\"476\" height=\"390\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-4.png\" alt=\"\" class=\"wp-image-11766\" style=\"width:482px;height:395px\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-4.png 476w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-4-300x246.png 300w\" sizes=\"(max-width: 476px) 100vw, 476px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"zeros-and-ones\"><strong>Zeros and ones<\/strong><\/h3>\n\n\n\n<p>Numpy provides functions that are able to create arrays of 1\u2019s and 0\u2019s. The required parameter for these functions is \u2018shape\u2019.<\/p>\n\n\n\n<p>Create array filled with zero values:<\/p>\n\n\n<figure class=\"wp-block-image size-large is-resized zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-5.png\"><img decoding=\"async\" width=\"483\" height=\"510\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-5.png\" alt=\"\" class=\"wp-image-11768\" style=\"width:514px;height:543px\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-5.png 483w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-5-284x300.png 284w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-5-398x420.png 398w\" sizes=\"(max-width: 483px) 100vw, 483px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"linspace\"><strong>Linspace<\/strong><\/h3>\n\n\n\n<p>Numpy\u2019s linspace function will return evenly spaced numbers over a specified interval. Required parameters for this functions are \u2018start\u2019 and \u2018stop\u2019. <\/p>\n\n\n<figure class=\"wp-block-image size-large is-resized zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-6.png\"><img decoding=\"async\" width=\"289\" height=\"52\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-6.png\" alt=\"\" class=\"wp-image-11769\" style=\"width:364px;height:65px\"><\/figure>\n\n\n\n<p>The parameter \u2018num\u2019 specifies the number of samples to generate, and the default value is 50. The value defined in the parameter \u2018num\u2019 must be non-negative. You are able to change the data type of your values using \u2018dtype\u2019 as parameter.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"eye\"><strong>Eye<\/strong><\/h3>\n\n\n\n<p>Numpy\u2019s eye function will return Identity Matrix. The identity matrix is a square matrix that has 1\u2019s along the main diagonal and 0\u2019s for all other entries. This matrix is often written simply as \u2018I\u2019, and is special in that it acts like 1 in matrix multiplication. Required parameter for this function is \u2018N\u2019, number of rows in the output.<\/p>\n\n\n<figure class=\"wp-block-image size-large is-resized zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-7.png\"><img decoding=\"async\" width=\"475\" height=\"363\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-7.png\" alt=\"\" class=\"wp-image-11770\" style=\"width:554px;height:423px\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-7.png 475w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-7-300x229.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-7-80x60.png 80w\" sizes=\"(max-width: 475px) 100vw, 475px\" \/><\/figure>\n\n\n\n<p>Specification of a data type of the matrix\u2019s values using \u2018dtype\u2019 is also possible.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"random\"><strong>Random<\/strong><\/h3>\n\n\n\n<p>Numpy allows you to use various functions to produce arrays with random values. To access these functions, first we have to access the \u2018random\u2019 function itself. This is done using \u2018np.random\u2019, after which we specify which function we need. Here is a list of the most used random functions and their purpose:<\/p>\n\n\n\n<p>np.random.rand() \u2013 produce random values in the given shape from 0 to 1<br>\nnp.random.randn() - produce random values with a \u2018standard normal\u2019 distribution, from -1 to 1<br>\nnp.random.randint() - produce random numbers from low to high, specified as parameter<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"rand\"><strong>Rand<\/strong><\/h3>\n\n\n\n<p>The rand function uses only one parameter which is the \u2018shape\u2019 of the output. You need to specify the output format you need, whether it is one or two dimensional array. If there is no argument passed to the function, it returns a single value. Otherwise, it produces number of numbers as specified. For example:<\/p>\n\n\n<figure class=\"wp-block-image size-large is-resized zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-8.png\"><img decoding=\"async\" width=\"496\" height=\"284\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-8.png\" alt=\"\" class=\"wp-image-11772\" style=\"width:601px;height:344px\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-8.png 496w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-8-300x172.png 300w\" sizes=\"(max-width: 496px) 100vw, 496px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"randn\"><strong>Randn<\/strong><\/h3>\n\n\n\n<p>The randn function is similar to the rand function, except it produces a number with standard normal distribution. What this means, is that it generates number with distribution of 1 and mean of 0, i.e. value from -1 to +1 by default:<\/p>\n\n\n<figure class=\"wp-block-image size-large is-resized zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-9.png\"><img decoding=\"async\" width=\"492\" height=\"394\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-9.png\" alt=\"\" class=\"wp-image-11773\" style=\"width:575px;height:461px\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-9.png 492w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-9-300x240.png 300w\" sizes=\"(max-width: 492px) 100vw, 492px\" \/><\/figure>\n\n\n\n<p>The distribution is now equal to 4, so the given floats vary between minus and plus 4. Other mathematical operations such as multiplication, division, subtraction are possible in order to modify the distribution, depending on the needs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"randint\"><strong>Randint<\/strong><\/h3>\n\n\n\n<p>Randint is used to generate whole random numbers, ranging between low(inclusive) and high(exclusive) value. Specifying a parameter like \u2018(1, 100)\u2019 will create random values from 1 to 99.<\/p>\n\n\n<figure class=\"wp-block-image size-large is-resized zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-10.png\"><img decoding=\"async\" width=\"479\" height=\"483\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-10.png\" alt=\"\" class=\"wp-image-11775\" style=\"width:538px;height:542px\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-10.png 479w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-10-298x300.png 298w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-10-150x150.png 150w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-10-417x420.png 417w\" sizes=\"(max-width: 479px) 100vw, 479px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"array-attributes-and-methods\"><strong>Array Attributes and Methods<\/strong><\/h3>\n\n\n\n<p>Now we will continue with more attributes and methods that can be used on arrays. In this lecture we will talk about:<\/p>\n\n\n\n<p>Reshape \u2013 changes the shape of an array into the desired shape<br>\nShape \u2013 returns the shape of the given array as parameter<br>\nDtype \u2013 returns the data type of the values in the array<\/p>\n\n\n\n<p>These methods will improve your \u2018trial-and-error\u2019, meaning, once you find yourself in a situation where you encounter an error, applying methods like this may help you locate the error faster, thus it will save you a lot of time in the future. Let\u2019s dive straight in.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"reshape\"><strong>Reshape<\/strong><\/h3>\n\n\n\n<p>This method allows you to transform one dimensional array to more dimensional, or the other way around. Reshape will not affect your data values. Let\u2019s check out this code:<\/p>\n\n\n<figure class=\"wp-block-image size-large is-resized zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-11.png\"><img decoding=\"async\" width=\"485\" height=\"315\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-11.png\" alt=\"\" class=\"wp-image-11776\" style=\"width:559px;height:363px\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-11.png 485w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-11-300x195.png 300w\" sizes=\"(max-width: 485px) 100vw, 485px\" \/><\/figure>\n\n\n\n<p>The array named \u2018arr\u2019 is now reshaped into a 5 by 5 matrix and by this, we specify the number of rows and the number of columns. Key thing to notice is that the array still has all 25 elements. Reshaping it into a 4 by 5 matrix(4 rows, 5 columns), would\u2019ve produced an error since the reshape size is not the same size as the array\u2019s. It would\u2019ve been possible if the array had only 20 elements. To reverse the process and return the array into it\u2019s original shape, we could do this:<\/p>\n\n\n<figure class=\"wp-block-image size-large is-resized zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-12.png\"><img decoding=\"async\" width=\"315\" height=\"98\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-12.png\" alt=\"\" class=\"wp-image-11777\" style=\"width:367px;height:114px\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-12.png 315w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-12-300x93.png 300w\" sizes=\"(max-width: 315px) 100vw, 315px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"shape\"><strong>Shape<\/strong><\/h3>\n\n\n\n<p>The \u2018shape\u2019 method will return you a tuple consisting of the array\u2019s dimensions. Let\u2019s check out the shape of the previously used array:<\/p>\n\n\n<figure class=\"wp-block-image size-large is-resized zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-13.png\"><img decoding=\"async\" width=\"465\" height=\"413\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-13.png\" alt=\"\" class=\"wp-image-11778\" style=\"width:561px;height:498px\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-13.png 465w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-13-300x266.png 300w\" sizes=\"(max-width: 465px) 100vw, 465px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"dtype\"><strong>Dtype<\/strong><\/h3>\n\n\n\n<p>This function allows you to check the data type of the array\u2019s values. <\/p>\n\n\n<figure class=\"wp-block-image size-large is-resized zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-14.png\"><img decoding=\"async\" width=\"168\" height=\"76\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-14.png\" alt=\"\" class=\"wp-image-11779\" style=\"width:206px;height:93px\"><\/figure>\n\n\n\n<p>There can be more the one data type present in an array, so make sure to check Numpy\u2019s documentation on \u2018dtypes\u2019 for more. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"numpy-indexing-and-selection\"><strong>Numpy Indexing and Selection<\/strong><\/h3>\n\n\n\n<p>In this lecture, we will discuss how to select element or groups of elements from an array and change them. Here is a list of what we will cover in this lecture:<\/p>\n\n\n\n<p><strong>Indexing <\/strong>\u2013 pick one or more elements from an array<br><strong>Broadcasting <\/strong>\u2013 changing values within an index range<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"bracket-indexing-and-selection\">Bracket Indexing and Selection<\/h3>\n\n\n\n<p><br>The simplest way to pick one or more elements of an array looks very similar to Python lists. We will be using the following array as an example<\/p>\n\n\n<figure class=\"wp-block-image size-large is-resized zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-15.png\"><img decoding=\"async\" width=\"491\" height=\"475\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-15.png\" alt=\"\" class=\"wp-image-11780\" style=\"width:589px;height:569px\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-15.png 491w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-15-300x290.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-15-434x420.png 434w\" sizes=\"(max-width: 491px) 100vw, 491px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"broadcasting\"><strong>Broadcasting<\/strong><\/h3>\n\n\n\n<p>Numpy arrays differ from a normal Python list because of their ability to broadcast. Below is an example of setting a value within index range (Broadcasting).<\/p>\n\n\n\n<p><\/p>\n\n\n<figure class=\"wp-block-image size-large is-resized zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-16.png\"><img decoding=\"async\" width=\"491\" height=\"457\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-16.png\" alt=\"\" class=\"wp-image-11781\" style=\"width:581px;height:541px\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-16.png 491w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-16-300x279.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-16-451x420.png 451w\" sizes=\"(max-width: 491px) 100vw, 491px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"indexing-2d-array-matrix\"><strong>Indexing 2D Array\/Matrix<\/strong><\/h3>\n\n\n\n<p>The main idea behind this lecture is to help you get comfortable with indexing in more than 1 dimensions. Below is a list of what we will cover.<\/p>\n\n\n\n<p>Indexing a 2D array \u2013 Indexing matrices differs from vectors<br> Fancy indexing \u2013 Selecting entire rows or columns out of order<br> Selection \u2013 Selection based off of comparison operators<\/p>\n\n\n<figure class=\"wp-block-image size-large is-resized zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-17.png\"><img decoding=\"async\" width=\"483\" height=\"369\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-17.png\" alt=\"\" class=\"wp-image-11782\" style=\"width:592px;height:452px\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-17.png 483w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-17-300x229.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-17-80x60.png 80w\" sizes=\"(max-width: 483px) 100vw, 483px\" \/><\/figure>\n\n\n\n<p>They both work. Throughout this lecture we will be using the second notation.<\/p>\n\n\n\n<p>Now, how do you index a column? I will show you an example of selecting the second column with all the values inside.<\/p>\n\n\n<figure class=\"wp-block-image size-large is-resized zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-18.png\"><img decoding=\"async\" width=\"488\" height=\"392\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-18.png\" alt=\"\" class=\"wp-image-11784\" style=\"width:618px;height:496px\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-18.png 488w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-18-300x241.png 300w\" sizes=\"(max-width: 488px) 100vw, 488px\" \/><\/figure>\n\n\n\n<p>You can see that both examples provide the same output. In these kinds of cases, using the first example is recommended for the sake of simplicity.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"fancy-indexing\"><strong>Fancy Indexing<\/strong><\/h3>\n\n\n\n<p>Fancy indexing allows you to select entire rows or columns out of order. To show this, let's quickly build out a numpy array of zeros.<\/p>\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-19.png\"><img decoding=\"async\" width=\"474\" height=\"449\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-19.png\" alt=\"\" class=\"wp-image-11786\" style=\"width:586px;height:555px\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-19.png 474w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-19-300x284.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-19-443x420.png 443w\" sizes=\"(max-width: 474px) 100vw, 474px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"selection\"><strong>Selection<\/strong><\/h3>\n\n\n\n<p> Let's briefly go over how to use brackets for selection based off of comparison operators. But first we need to create an array we will use as an example.<\/p>\n\n\n<figure class=\"wp-block-image size-large is-resized zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-20.png\"><img decoding=\"async\" width=\"494\" height=\"218\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-20.png\" alt=\"\" class=\"wp-image-11790\" style=\"width:594px;height:262px\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-20.png 494w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-20-300x132.png 300w\" sizes=\"(max-width: 494px) 100vw, 494px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"numpy-operations\"><strong>Numpy Operations<\/strong><\/h3>\n\n\n\n<p>We can perform different types of operations on NumPy arrays. What this means is we can sum, subtract, multiply or divide the values inside our array, even do things like taking the square root. Below is a list of what we will cover in this lecture.<\/p>\n\n\n\n<p>Arithmetic Operations \u2013 sum, subtract, multiply, divide on arrays<br>\nUniversal Array Functions \u2013 Mathematical operations provided by NumPy<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"arithmetic\"><strong>Arithmetic<\/strong><\/h2>\n\n\n\n<p>While performing arithmetic operations between two arrays it is important that they have the same dimensions. We will use the following array as an example<\/p>\n\n\n<figure class=\"wp-block-image size-large is-resized zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-21.png\"><img decoding=\"async\" width=\"497\" height=\"522\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-21.png\" alt=\"\" class=\"wp-image-11798\" style=\"width:609px;height:640px\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-21.png 497w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-21-286x300.png 286w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-21-400x420.png 400w\" sizes=\"(max-width: 497px) 100vw, 497px\" \/><\/figure>\n\n\n\n<p>If you run the last example you will again get a warning for dividing with zero. Note that this time instead of None, we get a value of infinity.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"universal-array-functions\"><strong>Universal Array Functions<\/strong><\/h3>\n\n\n\n<p>Numpy comes with many universal array functions, which are essentially just mathematical operations you can use to perform the operation across the array. Let's show some common ones.<\/p>\n\n\n<figure class=\"wp-block-image size-large is-resized zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-22.png\"><img decoding=\"async\" width=\"414\" height=\"426\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-22.png\" alt=\"\" class=\"wp-image-11799\" style=\"width:511px;height:526px\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-22.png 414w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-22-292x300.png 292w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-22-408x420.png 408w\" sizes=\"(max-width: 414px) 100vw, 414px\" \/><\/figure>\n\n\n\n<p>When dealing with messy data you will often need to stick multiple arrays together. In this lecture we will cover how this is done using numpy. Another list of what we will cover:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"append-concatenate-and-stack\">Append, Concatenate and Stack<\/h3>\n\n\n\n<p>Append \u2013 append one array to another<br>\nConcatenate \u2013 Concatenate two arrays<br>\nStack \u2013 Stack one array to another horizontally or vertically<\/p>\n\n\n\n<p>These functions don\u2019t work in-place, meaning you need to put your combined arrays to a new variable. Throughout this lecture we will be using the following arrays as an example:<\/p>\n\n\n<figure class=\"wp-block-image size-large is-resized zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-23.png\"><img decoding=\"async\" width=\"497\" height=\"330\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-23.png\" alt=\"\" class=\"wp-image-11800\" style=\"width:650px;height:431px\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-23.png 497w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-23-300x199.png 300w\" sizes=\"(max-width: 497px) 100vw, 497px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"append\"><strong>Append<\/strong><\/h3>\n\n\n\n<p>To append using numpy we use np.append() function which requires three parameters, \u2018arr\u2019, \u2018values\u2019 and \u2018axis\u2019 on which to append.<\/p>\n\n\n<figure class=\"wp-block-image size-large is-resized zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-24.png\"><img decoding=\"async\" width=\"507\" height=\"491\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-24.png\" alt=\"\" class=\"wp-image-11803\" style=\"width:621px;height:601px\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-24.png 507w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-24-300x291.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-24-434x420.png 434w\" sizes=\"(max-width: 507px) 100vw, 507px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"concatenate\"><strong>Concatenate<\/strong><\/h3>\n\n\n\n<p>Concatenate works similarly to append, but instead of \u2018arr\u2019 and \u2018values\u2019 as parameters it takes a tuple of two arrays. Let\u2019s show you few examples.<\/p>\n\n\n<figure class=\"wp-block-image size-large is-resized zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-25.png\"><img decoding=\"async\" width=\"296\" height=\"228\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-25.png\" alt=\"\" class=\"wp-image-11804\" style=\"width:394px;height:303px\"><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"stack\"><strong>Stack<\/strong><\/h3>\n\n\n\n<p>There are two ways of stacking arrays together, horizontally and vertically. Example for vertical stack:<\/p>\n\n\n<figure class=\"wp-block-image size-large is-resized zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-26.png\"><img decoding=\"async\" width=\"349\" height=\"208\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-26.png\" alt=\"\" class=\"wp-image-11805\" style=\"width:563px;height:336px\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-26.png 349w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/01\/image-26-300x179.png 300w\" sizes=\"(max-width: 349px) 100vw, 349px\" \/><\/figure>\n\n\n\n<p>Watch the following video to understand how NumPy Scikit works in Machine Learning.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"ML Packages Pandas NumPy Scikit-learn [Part 2] | Machine Learning With Python Tutorial for Beginners\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/MFZ-eq5bOmU?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"frequently-asked-questions-on-numpy-in-python\"><strong>Frequently Asked Questions on NumPy<\/strong> <strong>in Python<\/strong><\/h2>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"1-what-is-numpy-and-why-is-it-used-in-python\"><strong>1. What is NumPy and why is it used in Python?<\/strong><\/h4>\n\n\n\n<p>Numpy- Also known as numerical Python, is a library used for working with arrays. It is also a general-purpose array-processing package that provides comprehensive mathematical functions, linear algebra routines, Fourier transforms, and more.<\/p>\n\n\n\n<p>NumPy aims to provide less memory to store the data compared to python list and also helps in creating n-dimensional arrays. This is the reason why NumPy is used in Python.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"2-how-do-you-define-a-numpy-in-python\"><strong>2. How do you define a NumPy in Python?<\/strong><\/h4>\n\n\n\n<p>NumPy in python is defined as a fundamental package for scientific computing that helps in facilitating advanced mathematical and other types of operations on large numbers of data.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"3-where-is-numpy-used\"><strong>3. Where is NumPy used?<\/strong><\/h4>\n\n\n\n<p>NumPy is a python library mainly used for working with arrays and to perform a wide variety of mathematical operations on arrays.NumPy guarantees efficient calculations with arrays and matrices on high-level mathematical functions that operate on these arrays and matrices.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"4-should-i-use-numpy-or-pandas\"><strong>4. Should I use NumPy or pandas?<\/strong><\/h4>\n\n\n\n<p>Go through the below points and decide whether to use NumPy or Pandas, here we go:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>NumPy and Pandas are the most used libraries in Data Science, ML and AI.<\/li>\n\n\n\n<li>NumPy and Pandas are used to save n number of lines of Codes.<\/li>\n\n\n\n<li>NumPy and Pandas are open source libraries.<\/li>\n\n\n\n<li>NumPy is used for fast scientific computing and Pandas is used for data manipulation, analysis and cleaning.&nbsp;<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"5-what-is-the-difference-between-numpy-and-pandas\"><strong>5. What is the difference between NumPy and pandas?<\/strong><br><\/h4>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td><strong>NumPy<\/strong><\/td><td><strong>Pandas<\/strong><\/td><\/tr><tr><td>Numpy creates an n-dimensional array object.<\/td><td>Pandas create DataFrame and Series.<\/td><\/tr><tr><td>Numpy array contains data of same data types<\/td><td>Pandas is well suited for tabular data<\/td><\/tr><tr><td>Numpy requires less memory<\/td><td>Pandas required more memory compared to NumPy<\/td><\/tr><tr><td>NumPy supports multidimensional arrays.<\/td><td>Pandas support 2 dimensional arrays<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"6-what-is-a-numpy-array\"><strong>6. What is a NumPy array?<\/strong><\/h4>\n\n\n\n<p>Numpy array is formed by all the computations performed by the NumPy library. This is a powerful N-dimensional array object with a central data structure and is a collection of elements that have the same data types.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"7-what-is-numpy-written-in\"><strong>7. What is NumPy written in?<\/strong><\/h4>\n\n\n\n<p>NumPy is a Python library that is partially written in Python and most of the parts are written in C or C++. And it also supports extensions in other languages, commonly C++ and Fortran.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"8-is-numpy-easy-to-learn\"><strong>8. Is NumPy easy to learn?<\/strong><\/h4>\n\n\n\n<p>NumPy is an open-source Python library that is mainly used for data manipulation and processing in the form of arrays.NumPy is easy to learn as it works fast, works well with other libraries, has lots of built-in functions, and lets you do matrix operations.<\/p>\n\n\n\n<script type=\"application\/ld+json\">\n{\n  \"@context\": \"https:\/\/schema.org\",\n  \"@type\": \"FAQPage\",\n  \"mainEntity\": [{\n    \"@type\": \"Question\",\n    \"name\": \"What is NumPy and why is it used in Python?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"Numpy- Also known as numerical Python, is a library used for working with arrays. It is also a general-purpose array-processing package that provides comprehensive mathematical functions, linear algebra routines, Fourier transforms, and more.\n\nNumPy aims to provide less memory to store the data compared to python list and also helps in creating n-dimensional arrays. This is the reason why NumPy is used in Python.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"How do you define a NumPy in Python?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"NumPy in python is defined as a fundamental package for scientific computing that helps in facilitating advanced mathematical and other types of operations on large numbers of data.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"Where is NumPy used?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"NumPy is a python library mainly used for working with arrays and to perform a wide variety of mathematical operations on arrays.NumPy guarantees efficient calculations with arrays and matrices on high-level mathematical functions that operate on these arrays and matrices.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"Should I use NumPy or pandas?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"Go through the below points and decide whether to use NumPy or Pandas, here we go:\n\nNumPy and Pandas are the most used libraries in Data Science, ML and AI.\nNumPy and Pandas are used to save n number of lines of Codes.\nNumPy and Pandas are open source libraries.\nNumPy is used for fast scientific computing and Pandas is used for data manipulation, analysis and cleaning.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"What is a NumPy array?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"Numpy array is formed by all the computations performed by the NumPy library. This is a powerful N-dimensional array object with a central data structure and is a collection of elements that have the same data types.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"What is NumPy written in?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"NumPy is a Python library that is partially written in Python and most of the parts are written in C or C++. And it also supports extensions in other languages, commonly C++ and Fortran.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"Is NumPy easy to learn?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"NumPy is an open-source Python library that is mainly used for data manipulation and processing in the form of arrays.NumPy is easy to learn as it works fast, works well with other libraries, has lots of built-in functions, and lets you do matrix operations.\"\n    }\n  }]\n}\n<\/script>\n\n\n\n<p>NumPy is a fundamental <a href=\"https:\/\/www.mygreatlearning.com\/blog\/open-source-python-libraries\/\" target=\"_blank\" rel=\"noreferrer noopener\">Python library<\/a> that gives you access to powerful mathematical functions. If you're looking to dive deep into scientific computing and data analysis, then NumPy is definitely the way to go.&nbsp;<\/p>\n\n\n\n<p>On the other hand, pandas is a data analysis library that makes it easy to work with tabular data. If your focus is on business intelligence and data wrangling, then pandas are the library for you.&nbsp;<\/p>\n\n\n\n<p>In the end, it's up to you which one you want to learn first. Just be sure to focus on one at a time, and you'll be mastering NumPy in no time!&nbsp;<\/p>\n\n\n\n<p>Embarking on a journey towards a career in data science opens up a world of limitless possibilities. Whether you\u2019re an aspiring data scientist or someone intrigued by the power of data, understanding the key factors that contribute to success in this field is crucial. The below path will guide you to become a proficient data scientist.<\/p>\n\n\n\n<figure class=\"wp-block-table aligncenter\"><table class=\"has-cyan-bluish-gray-background-color has-background\"><tbody><tr><td><a href=\"https:\/\/www.mygreatlearning.com\/data-science\/courses\/certificates\" target=\"_blank\" rel=\"noreferrer noopener\">Data Science Course Certificates<\/a><\/td><\/tr><tr><td><a href=\"https:\/\/www.mygreatlearning.com\/data-science\/courses\/placements\" target=\"_blank\" rel=\"noreferrer noopener\">Data Science Course Placements<\/a><\/td><\/tr><tr><td><a href=\"https:\/\/www.mygreatlearning.com\/data-science\/courses\/syllabus\" target=\"_blank\" rel=\"noreferrer noopener\">Data Science Course Syllabus<\/a><\/td><\/tr><tr><td><a href=\"https:\/\/www.mygreatlearning.com\/data-science\/courses\/eligibility\" target=\"_blank\" rel=\"noreferrer noopener\">Data Science Course Eligibility<\/a><\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>So you've learned the basics of Python and you're looking for a more powerful way to analyse data? NumPy is what you need.NumPy is a module for Python that allows you to work with multidimensional arrays and matrices. It's perfect for scientific or mathematical calculations because it's fast and efficient. In addition, NumPy includes support [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":74778,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25860],"tags":[36804,36796],"content_type":[36248],"class_list":["post-23244","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software","tag-data-analytics","tag-python","content_type-career-guide"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Python NumPy Tutorial - Great Learning<\/title>\n<meta name=\"description\" content=\"Python Numpy Tutorial: In this tutorial, you&#039;ll learn everything you need to know to get up and running with NumPy,\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mygreatlearning.com\/blog\/python-numpy-tutorial\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python NumPy Tutorial\" \/>\n<meta property=\"og:description\" content=\"Python Numpy Tutorial: In this tutorial, you&#039;ll learn everything you need to know to get up and running with NumPy,\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mygreatlearning.com\/blog\/python-numpy-tutorial\/\" \/>\n<meta property=\"og:site_name\" content=\"Great Learning Blog: Free Resources what Matters to shape your Career!\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GreatLearningOfficial\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-08T06:39:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-06T13:35:54+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/07\/coding-924920_1280.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"853\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Great Learning Editorial Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/Great_Learning\" \/>\n<meta name=\"twitter:site\" content=\"@Great_Learning\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Great Learning Editorial Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/python-numpy-tutorial\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/python-numpy-tutorial\\\/\"},\"author\":{\"name\":\"Great Learning Editorial Team\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\"},\"headline\":\"Python NumPy Tutorial\",\"datePublished\":\"2023-11-08T06:39:30+00:00\",\"dateModified\":\"2025-01-06T13:35:54+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/python-numpy-tutorial\\\/\"},\"wordCount\":7534,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/python-numpy-tutorial\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/coding-924920_1280.jpg\",\"keywords\":[\"Data Analytics\",\"python\"],\"articleSection\":[\"IT\\\/Software Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/python-numpy-tutorial\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/python-numpy-tutorial\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/python-numpy-tutorial\\\/\",\"name\":\"Python NumPy Tutorial - Great Learning\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/python-numpy-tutorial\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/python-numpy-tutorial\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/coding-924920_1280.jpg\",\"datePublished\":\"2023-11-08T06:39:30+00:00\",\"dateModified\":\"2025-01-06T13:35:54+00:00\",\"description\":\"Python Numpy Tutorial: In this tutorial, you'll learn everything you need to know to get up and running with NumPy,\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/python-numpy-tutorial\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/python-numpy-tutorial\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/python-numpy-tutorial\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/coding-924920_1280.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/coding-924920_1280.jpg\",\"width\":1280,\"height\":853,\"caption\":\"Python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/python-numpy-tutorial\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"IT\\\/Software Development\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/software\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python NumPy Tutorial\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"name\":\"Great Learning Blog\",\"description\":\"Learn, Upskill &amp; Career Development Guide and Resources\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"alternateName\":\"Great Learning\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\",\"name\":\"Great Learning\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"width\":900,\"height\":900,\"caption\":\"Great Learning\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/GreatLearningOfficial\\\/\",\"https:\\\/\\\/x.com\\\/Great_Learning\",\"https:\\\/\\\/www.instagram.com\\\/greatlearningofficial\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/in.pinterest.com\\\/greatlearning12\\\/\",\"https:\\\/\\\/www.youtube.com\\\/user\\\/beaconelearning\\\/\"],\"description\":\"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.\",\"email\":\"info@mygreatlearning.com\",\"legalName\":\"Great Learning Education Services Pvt. Ltd\",\"foundingDate\":\"2013-11-29\",\"numberOfEmployees\":{\"@type\":\"QuantitativeValue\",\"minValue\":\"1001\",\"maxValue\":\"5000\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\",\"name\":\"Great Learning Editorial Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"caption\":\"Great Learning Editorial Team\"},\"description\":\"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.\",\"sameAs\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/\",\"https:\\\/\\\/in.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/Great_Learning\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCObs0kLIrDjX2LLSybqNaEA\"],\"award\":[\"Best EdTech Company of the Year 2024\",\"Education Economictimes Outstanding Education\\\/Edtech Solution Provider of the Year 2024\",\"Leading E-learning Platform 2024\"],\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/author\\\/greatlearning\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python NumPy Tutorial - Great Learning","description":"Python Numpy Tutorial: In this tutorial, you'll learn everything you need to know to get up and running with NumPy,","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mygreatlearning.com\/blog\/python-numpy-tutorial\/","og_locale":"en_US","og_type":"article","og_title":"Python NumPy Tutorial","og_description":"Python Numpy Tutorial: In this tutorial, you'll learn everything you need to know to get up and running with NumPy,","og_url":"https:\/\/www.mygreatlearning.com\/blog\/python-numpy-tutorial\/","og_site_name":"Great Learning Blog: Free Resources what Matters to shape your Career!","article_publisher":"https:\/\/www.facebook.com\/GreatLearningOfficial\/","article_published_time":"2023-11-08T06:39:30+00:00","article_modified_time":"2025-01-06T13:35:54+00:00","og_image":[{"width":1280,"height":853,"url":"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/07\/coding-924920_1280.jpg","type":"image\/jpeg"}],"author":"Great Learning Editorial Team","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/Great_Learning","twitter_site":"@Great_Learning","twitter_misc":{"Written by":"Great Learning Editorial Team","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.mygreatlearning.com\/blog\/python-numpy-tutorial\/#article","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/python-numpy-tutorial\/"},"author":{"name":"Great Learning Editorial Team","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad"},"headline":"Python NumPy Tutorial","datePublished":"2023-11-08T06:39:30+00:00","dateModified":"2025-01-06T13:35:54+00:00","mainEntityOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/python-numpy-tutorial\/"},"wordCount":7534,"commentCount":0,"publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/python-numpy-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/07\/coding-924920_1280.jpg","keywords":["Data Analytics","python"],"articleSection":["IT\/Software Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.mygreatlearning.com\/blog\/python-numpy-tutorial\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.mygreatlearning.com\/blog\/python-numpy-tutorial\/","url":"https:\/\/www.mygreatlearning.com\/blog\/python-numpy-tutorial\/","name":"Python NumPy Tutorial - Great Learning","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/python-numpy-tutorial\/#primaryimage"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/python-numpy-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/07\/coding-924920_1280.jpg","datePublished":"2023-11-08T06:39:30+00:00","dateModified":"2025-01-06T13:35:54+00:00","description":"Python Numpy Tutorial: In this tutorial, you'll learn everything you need to know to get up and running with NumPy,","breadcrumb":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/python-numpy-tutorial\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mygreatlearning.com\/blog\/python-numpy-tutorial\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/python-numpy-tutorial\/#primaryimage","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/07\/coding-924920_1280.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/07\/coding-924920_1280.jpg","width":1280,"height":853,"caption":"Python"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mygreatlearning.com\/blog\/python-numpy-tutorial\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/www.mygreatlearning.com\/blog\/"},{"@type":"ListItem","position":2,"name":"IT\/Software Development","item":"https:\/\/www.mygreatlearning.com\/blog\/software\/"},{"@type":"ListItem","position":3,"name":"Python NumPy Tutorial"}]},{"@type":"WebSite","@id":"https:\/\/www.mygreatlearning.com\/blog\/#website","url":"https:\/\/www.mygreatlearning.com\/blog\/","name":"Great Learning Blog","description":"Learn, Upskill &amp; Career Development Guide and Resources","publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"alternateName":"Great Learning","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mygreatlearning.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization","name":"Great Learning","url":"https:\/\/www.mygreatlearning.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","width":900,"height":900,"caption":"Great Learning"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/GreatLearningOfficial\/","https:\/\/x.com\/Great_Learning","https:\/\/www.instagram.com\/greatlearningofficial\/","https:\/\/www.linkedin.com\/school\/great-learning\/","https:\/\/in.pinterest.com\/greatlearning12\/","https:\/\/www.youtube.com\/user\/beaconelearning\/"],"description":"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.","email":"info@mygreatlearning.com","legalName":"Great Learning Education Services Pvt. Ltd","foundingDate":"2013-11-29","numberOfEmployees":{"@type":"QuantitativeValue","minValue":"1001","maxValue":"5000"}},{"@type":"Person","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad","name":"Great Learning Editorial Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","caption":"Great Learning Editorial Team"},"description":"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.","sameAs":["https:\/\/www.mygreatlearning.com\/","https:\/\/in.linkedin.com\/school\/great-learning\/","https:\/\/x.com\/https:\/\/twitter.com\/Great_Learning","https:\/\/www.youtube.com\/channel\/UCObs0kLIrDjX2LLSybqNaEA"],"award":["Best EdTech Company of the Year 2024","Education Economictimes Outstanding Education\/Edtech Solution Provider of the Year 2024","Leading E-learning Platform 2024"],"url":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"}]}},"uagb_featured_image_src":{"full":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/07\/coding-924920_1280.jpg",1280,853,false],"thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/07\/coding-924920_1280-150x150.jpg",150,150,true],"medium":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/07\/coding-924920_1280-300x200.jpg",300,200,true],"medium_large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/07\/coding-924920_1280-768x512.jpg",768,512,true],"large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/07\/coding-924920_1280-1024x682.jpg",1024,682,true],"1536x1536":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/07\/coding-924920_1280.jpg",1280,853,false],"2048x2048":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/07\/coding-924920_1280.jpg",1280,853,false],"web-stories-poster-portrait":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/07\/coding-924920_1280-640x853.jpg",640,853,true],"web-stories-publisher-logo":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/07\/coding-924920_1280-96x96.jpg",96,96,true],"web-stories-thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/07\/coding-924920_1280-150x100.jpg",150,100,true]},"uagb_author_info":{"display_name":"Great Learning Editorial Team","author_link":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"},"uagb_comment_info":0,"uagb_excerpt":"So you've learned the basics of Python and you're looking for a more powerful way to analyse data? NumPy is what you need.NumPy is a module for Python that allows you to work with multidimensional arrays and matrices. It's perfect for scientific or mathematical calculations because it's fast and efficient. In addition, NumPy includes support&hellip;","_links":{"self":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/23244","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/comments?post=23244"}],"version-history":[{"count":68,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/23244\/revisions"}],"predecessor-version":[{"id":116991,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/23244\/revisions\/116991"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media\/74778"}],"wp:attachment":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media?parent=23244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/categories?post=23244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/tags?post=23244"},{"taxonomy":"content_type","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/content_type?post=23244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}