{"id":8964,"date":"2021-03-16T18:08:50","date_gmt":"2021-03-16T12:38:50","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=8964"},"modified":"2022-04-28T16:00:00","modified_gmt":"2022-04-28T10:30:00","slug":"python-numpy-array-tutorial","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/python-numpy-array-tutorial\/","title":{"rendered":"Python Numpy Array Tutorial: A Beginners Guide"},"content":{"rendered":"\n<p>In this tutorial, we will discuss Python Numpy Arrays. While datasets come in different formats such as texts, images, audios, etc, they can all be seen as arrays of numbers. For example, a document can be seen as an array of vectors based on the frequency of the words or similarity of one to another. Images can be seen as a 2-dimensional array of the intensity of the pixels. Audios can be seen as a 1-dimensional array of the sound pitch as time progresses. Generally speaking, all forms of data can be converted into Numpy array, and thus it is vital to understand how to work with them.&nbsp;<\/p>\n\n\n\n<p>The NumPy library is a robust library that allows for data manipulation in arrays. Arrays in NumPy are called ndarrays. If you are wondering why arrays and not list? Well, <a href=\"https:\/\/www.h2kinfosys.com\/blog\/python-numpy-tutorial\/\" class=\"rank-math-link\">Python Numpy<\/a> arrays are way faster than lists in mathematical computations and they are also easier to manage. This tutorial will focus on how to create NumPy arrays and check for some of their key properties.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to create a NumPy array.&nbsp;<\/h2>\n\n\n\n<p>A NumPy array can be created by calling the array() method. The argument received is typically a list. When the array() is called, the list passed is converted to a Numpy array, whose data type is called ndarray. See the example below.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import numpy as np\n&nbsp;\nnumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]\nprint(type(numbers))\n&nbsp;\nnumbers = np.array(numbers)\nprint(type(numbers))\n<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\n&lt;class 'list'&gt;\n&lt;class 'numpy.ndarray'&gt;\n\nAs seen, the numbers were first of type list but calling the array() method on them converts them to a NumPy arr\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Performing Basic Mathematical Operations on a NumPy Array<\/h2>\n\n\n\n<p>NumPy arrays are capable of performing basic mathematical operations such as addition, subtraction, multiplication, and division. Given that the arrays are of the same length, the operations will be performed based on the elements of the arrays. For instance, the computation of [1, 2] + [2, 1] returns [3, 3]. See the coding example below for the four basic operations.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import numpy as np\n&nbsp;\nnumber1 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])\nnumber2 = np.array([2, 4, 5, 2, 6, 2, 5, 2, 8])\n&nbsp;\nprint(number1 + number2)\nprint()\nprint(number1 - number2)\nprint()\nprint(number1 * number2)\nprint()\nprint(number1 \/ number2)<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\n&#91; 3  6  8  6 11  8 12 10 17]\n\n&#91;-1 -2 -2  2 -1  4  2  6  1]\n\n&#91; 2  8 15  8 30 12 35 16 72]\n\n&#91;0.5        0.5        0.6        2.         0.83333333 3.\n 1.4        4.         1.125     ]\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Finding the shape of an array&nbsp;<\/h2>\n\n\n\n<p>One important property of an array is its shape. The shape of an array is simply the number of elements horizontally by the number of elements vertically. To check the shape of an array, the shape attribute is used.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import numpy as np\n&nbsp;\nnumber = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])\nprint(number.shape)<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\n(9,)\n<\/code><\/pre>\n\n\n\n<p>The result shows that the array is a 1-dimensional array with 9 elements on the x-axis and 1 element on the y axis. Going forward in this series, we will discuss how to create a 2-dimensional array and check its shape.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Finding the DataTypes of the Elements of an Array<\/h2>\n\n\n\n<p>Another important property of an array is the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Array_data_type\" class=\"rank-math-link\" rel=\"nofollow noopener\" target=\"_blank\">datatype of the array<\/a>. You can find the datatype which the elements of the array are of, using the dtype attribute. An array that contains only integers has a dtype, int. Meanwhile, you can specify the dtype of an array using the dtype parameter when the array was created.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import numpy as np\n&nbsp;\nnumber1 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])\nnumber2 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9], dtype='float64')\n&nbsp;\n#check the datatype of the arrays\nprint(number 1.dtype)\nprint(number2.dtype)\n<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\nint32\nfloat64\n<\/code><\/pre>\n\n\n\n<p>As seen, while the dtype of the first array is an int, the dtype of the second is a float. Printing the array to the console makes it clearer.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">print(number2)\n<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\n&#91;1. 2. 3. 4. 5. 6. 7. 8. 9.]\n<\/code><\/pre>\n\n\n\n<p>From the result, observe that the integers were converted to floating-point numbers. That\u2019s it for this tutorial. If you have any questions, feel free to leave them in the comment section and I\u2019d do my best to answer them.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will discuss Python Numpy Arrays. While datasets come in different formats such as texts, images, audios, etc, they can all be seen as arrays of numbers. For example, a document can be seen as an array of vectors based on the frequency of the words or similarity of one to another. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":8967,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[498],"tags":[],"class_list":["post-8964","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-artificial-intelligence-tutorials"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/8964","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=8964"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/8964\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/8967"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=8964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=8964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=8964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}