{"id":831,"date":"2024-01-15T21:06:09","date_gmt":"2024-01-15T13:06:09","guid":{"rendered":"https:\/\/datascihubs.com\/?p=831"},"modified":"2024-01-27T21:18:12","modified_gmt":"2024-01-27T13:18:12","slug":"a-quick-guide-to-numpy-functions","status":"publish","type":"post","link":"https:\/\/datascihubs.com\/index.php\/2024\/01\/15\/a-quick-guide-to-numpy-functions\/","title":{"rendered":"A Quick Guide to NumPy Functions"},"content":{"rendered":"\n<p class=\"has-text-align-justify\">NumPy, short for Numerical Python, is a library that is like a superhero cape for Python when dealing with numbers. It is a powerful Python library designed to help you work with numbers effortlessly. Whether you&#8217;re dealing with structured or unstructured data (eg. image data), NumPy has your back. Most importantly, it is easy to use and plays well with other data science tools. So whether you&#8217;re a beginner or a seasoned data pro, NumPy is your must-go-to ally in the world of data science. In this article, we will try the best to give you a quick guide to NumPy functions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Table of Contents<\/h3>\n\n\n\n<div class=\"wp-block-aioseo-table-of-contents\"><ul><li><a href=\"#aioseo-the-core-of-numpy\">Introduction<\/a><\/li><li><a href=\"#aioseo-numpy-basic\">NumPy Fundamentals<\/a><ul><li><a href=\"#aioseo-1-create-basic-array\">1. Create NumPy Array<\/a><ul><li><a href=\"#aioseo-convert-existing-data-into-numpy-array\">Convert existing data into NumPy Array<\/a><\/li><li><a href=\"#aioseo-1-1d-array-creation-functions\">1D array creation functions<\/a><\/li><li><a href=\"#aioseo-2-2d-array-creation-functions\">2D array creation functions<\/a><\/li><li><a href=\"#aioseo-3-general-ndarray-creation-functions\">General ndarray creation functions<\/a><\/li><\/ul><\/li><li><a href=\"#aioseo-indexing-with-numpy\">2. Indexing in NumPy<\/a><ul><li><a href=\"#aioseo-1-basic-indexing-numpy-arrays-can-be-indexed-using-square-brackets-and-integer-indices-similar-to-python-lists-the-indexing-starts-from-0\">Basic Indexing<\/a><\/li><li><a href=\"#aioseo-multidimensional-indexing\">Slicing with Steps<\/a><\/li><li><a href=\"#aioseo-multidimensional-indexing\">Multidimensional Indexing<\/a><\/li><li><a href=\"#aioseo-multidimensional-indexing\">Boolean Indexing<\/a><\/li><li><a href=\"#aioseo-multidimensional-indexing\">Fancy Indexing<\/a><\/li><li><a href=\"#aioseo-multidimensional-indexing\">Integer Array Indexing<\/a><\/li><\/ul><\/li><\/ul><\/li><li><a href=\"#aioseo-conclusion\">Conclusion<\/a><\/li><\/ul><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"aioseo-the-core-of-numpy\">Introduction<\/h2>\n\n\n\n<p class=\"has-text-align-justify\">In general, the core of NumPy is all about arrays, which act as a container for numbers. Unlike regular Python lists or tuples, NumPy provides `numpy.ndarray` data structure which allows you to efficiently store and manipulate large arrays of data. The word `ndarray` is a short form for N-dimensional array, meaning these arrays can have one or more dimensions, making them suitable for various types of data. <\/p>\n\n\n\n<p class=\"has-text-align-justify\">Imagine that we have a dataset either in 2D, 3D or any dimension, and we want to manipulate the data to meet our needs. Instead of doing our own calculations, NumPy provides a broad range of mathematical operations, ranging from basic arithmetic (addition, subtraction, multiplication or division) to complex data analysis such as Statistics or Linear Algebra. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"aioseo-numpy-basic\">NumPy Fundamentals<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"aioseo-1-create-basic-array\">1. Create NumPy Array<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"aioseo-convert-existing-data-into-numpy-array\">Convert existing data into NumPy Array<\/h5>\n\n\n\n<p class=\"has-text-align-justify\">Let&#8217;s get started with the simplest and most used method, which is converting existing data into one. If we have a Python list, we can convert it to NumPy by simply using the &#8216;np.array()&#8217; function. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># covnert Python list to NumPy array\nimport numpy as np\n\npython_list = &#91;1, 2, 3, 4, 5]\nnumpy_array = np.array(&#91;python_list])<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\">In the example above, I&#8217;ve only shown how to convert a 1D python list to NumPy, but you could use the same method for any dimensional data. <\/p>\n\n\n\n<p class=\"has-text-align-justify\">What if we don&#8217;t have a Python List or we want to create a large matrix from scratch? NumPy also provides some built-in functions for array generation. These functions include <\/p>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"aioseo-1-1d-array-creation-functions\">1D array creation functions<\/h5>\n\n\n\n<ul class=\"wp-block-list\">\n<li>np.arrange()<\/li>\n\n\n\n<li>np.linspace()<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code># create an array with evenly spaced values within a given interval\n&gt;&gt;&gt; np.arange(0, 5, 0.5)\narray(&#91;0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5])\n\n\n# create an array with evenly spaced numbers over a specified interval\n&gt;&gt;&gt; np.linspace(0, 5, 5)\narray(&#91;0.  , 1.25, 2.5 , 3.75, 5.  ])<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"aioseo-2-2d-array-creation-functions\">2D array creation functions<\/h5>\n\n\n\n<ul class=\"wp-block-list\">\n<li>np.eye()<\/li>\n\n\n\n<li>np.diag()<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code># create an identity matrix \n&gt;&gt;&gt; np.eye(3)\narray(&#91;&#91;1., 0., 0.],\n       &#91;0., 1., 0.],\n       &#91;0., 0., 1.]])\n\n\n# create 2D array with given diagonal elements\n&gt;&gt;&gt; np.diag(&#91;1, 2, 3])\narray(&#91;&#91;1, 0, 0],\n       &#91;0, 2, 0],\n       &#91;0, 0, 3]])<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"aioseo-3-general-ndarray-creation-functions\">General ndarray creation functions<\/h5>\n\n\n\n<ul class=\"wp-block-list\">\n<li>np.zeros()<\/li>\n\n\n\n<li>np.ones()<\/li>\n\n\n\n<li>np.random()<\/li>\n\n\n\n<li>np.indices()<\/li>\n<\/ul>\n\n\n\n<p class=\"has-text-align-justify\">These functions accept a desired array shape and data type as parameters. For example, np.zeros(2) returns a 1D array with 2 zeros. The default data type is <strong>float64<\/strong>, but you could also change it by changing the <strong>dtype<\/strong> argument. We could also replace it with (a, b) which returns an a-by-b array. You also try np.zeros((1, 2, 3)) and see what it returns (supposed to be a 3D array).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># create an array of given shape and type, filled with zeros\n&gt;&gt;&gt; np.zeros((2, 2))\narray(&#91;&#91;0., 0.],\n       &#91;0., 0.]])\n\n# create an array of given shape and type, filled with ones\n&gt;&gt;&gt; np.ones((2, 3))\narray(&#91;&#91;1., 1.],\n       &#91;1., 1.],\n       &#91;1., 1.]])\n\n# create an array of given shape and type, without initializing entries\n&gt;&gt;&gt; np.empty(&#91;2, 2])\narray(&#91;&#91; -9.74499359e+001,   6.69583040e-309],\n       &#91;  2.13182611e-314,   3.06959433e-309]])  <\/code><\/pre>\n\n\n\n<p>The arrays created above are all 1D, but you could also create different dimensional data by providing the data shape. For example, creating a 2D zero array with <strong><em>&#8220;np.zeros((2, 2))&#8221;<\/em><\/strong>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"aioseo-indexing-with-numpy\">2. Indexing in NumPy<\/h4>\n\n\n\n<p class=\"has-text-align-justify\">When playing with data, we often only want to access specific elements or subsets of data. NumPy offers various indexing techniques to work with arrays efficiently. Here are some common indexing methods:<\/p>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"aioseo-1-basic-indexing-numpy-arrays-can-be-indexed-using-square-brackets-and-integer-indices-similar-to-python-lists-the-indexing-starts-from-0\"><strong>Basic Indexing<\/strong><\/h5>\n\n\n\n<p class=\"has-text-align-justify\">NumPy arrays can be indexed using square brackets and integer indices, similar to Python lists. The indexing starts from 0.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\n\narr = np.array(&#91;0, 1, 2, 3, 4])\n\n# Accessing a single element\nelement = arr&#91;2]  # Returns 2\n\n# Slicing to get a subset of elements\nsubset = arr&#91;1:4]  # Returns &#91;1, 2, 3]<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"aioseo-multidimensional-indexing\">Slicing with Steps<\/h5>\n\n\n\n<p class=\"has-text-align-justify\">You can use slicing with step values to access elements at regular intervals.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\n\narr = np.array(&#91;0, 1, 2, 3, 4, 5, 6, 7, 8, 9])\n\n# Slicing with a step of 2\nsubset = arr&#91;1:8:2]  # Returns &#91;1, 3, 5, 7]<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"aioseo-multidimensional-indexing\">Multidimensional Indexing<\/h5>\n\n\n\n<p class=\"has-text-align-justify\">NumPy supports indexing in multiple dimensions for multidimensional arrays (e.g., matrices). You can use comma-separated indices or separate brackets for each dimension.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\n\nmatrix = np.array(&#91;&#91;1, 2, 3], &#91;4, 5, 6], &#91;7, 8, 9]])\n\n# Accessing a single element\nelement = matrix&#91;1, 2]  # Returns 6\n\n# Slicing rows and columns\nrow = matrix&#91;0]  # Returns &#91;1, 2, 3]\ncol = matrix&#91;:, 1]  # Returns &#91;2, 5, 8]<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"aioseo-multidimensional-indexing\">Boolean Indexing<\/h5>\n\n\n\n<p class=\"has-text-align-justify\">You can use boolean arrays to filter elements based on a condition. This is particularly useful for selecting elements that satisfy specific criteria.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\n\narr = np.array(&#91;1, 2, 3, 4, 5])\n\n# Boolean indexing to select elements greater than 3\nfiltered = arr&#91;arr &gt; 3]  # Returns &#91;4, 5]<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"aioseo-multidimensional-indexing\">Fancy Indexing<\/h5>\n\n\n\n<p class=\"has-text-align-justify\">NumPy allows you to use arrays of integers or other sequences as indices to access or modify multiple elements simultaneously.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\n\narr = np.array(&#91;10, 20, 30, 40, 50])\n\n# Using an array of indices\nindices = np.array(&#91;0, 2, 4])\nselected = arr&#91;indices]  # Returns &#91;10, 30, 50]<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"aioseo-multidimensional-indexing\">Integer Array Indexing<\/h5>\n\n\n\n<p class=\"has-text-align-justify\">NumPy also provides advanced indexing techniques like integer array indexing, which allows you to create arbitrary arrays using the data from another array.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\n\narr = np.array(&#91;&#91;1, 2], &#91;3, 4], &#91;5, 6]])\n\n# Integer array indexing to select specific elements\nselected = arr&#91;&#91;0, 1], &#91;1, 0]]  # Returns &#91;2, 3]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"aioseo-conclusion\">Conclusion<\/h2>\n\n\n\n<p class=\"has-text-align-justify\">NumPy is a powerful numerical computing library in Python that provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these elements. Understanding how numpy works would also help us to understand other array-like data structures, eg. Tensor. <\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>NumPy, short for Numerical Python, is a library that is like a superhero cape for Python when dealing with numbers. It is a powerful Python library designed to help you work with numbers effortlessly. Whether you&#8217;re dealing with structured or unstructured data (eg. image data), NumPy has your back. Most importantly, it is easy to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[28,57,86,47],"class_list":["post-831","post","type-post","status-publish","format-standard","hentry","category-information","tag-data-analysis","tag-data-preprocessing","tag-numpy","tag-python"],"blocksy_meta":[],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/datascihubs.com\/index.php\/wp-json\/wp\/v2\/posts\/831","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datascihubs.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datascihubs.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datascihubs.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datascihubs.com\/index.php\/wp-json\/wp\/v2\/comments?post=831"}],"version-history":[{"count":17,"href":"https:\/\/datascihubs.com\/index.php\/wp-json\/wp\/v2\/posts\/831\/revisions"}],"predecessor-version":[{"id":909,"href":"https:\/\/datascihubs.com\/index.php\/wp-json\/wp\/v2\/posts\/831\/revisions\/909"}],"wp:attachment":[{"href":"https:\/\/datascihubs.com\/index.php\/wp-json\/wp\/v2\/media?parent=831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datascihubs.com\/index.php\/wp-json\/wp\/v2\/categories?post=831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datascihubs.com\/index.php\/wp-json\/wp\/v2\/tags?post=831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}