{"id":83888,"date":"2021-12-27T10:30:07","date_gmt":"2021-12-27T15:30:07","guid":{"rendered":"https:\/\/blog.logrocket.com\/?p=83888"},"modified":"2024-06-04T17:13:16","modified_gmt":"2024-06-04T21:13:16","slug":"introduction-data-science-numpy","status":"publish","type":"post","link":"https:\/\/blog.logrocket.com\/introduction-data-science-numpy\/","title":{"rendered":"Introduction to data science with NumPy"},"content":{"rendered":"<!DOCTYPE html>\n<html><h2>Introduction<\/h2>\n<p>Data science is an evolutionary extension of statistics capable of dealing with the massive amounts of data that are regularly produced today. It adds methods from computer science to the repertoire of statistics.<\/p><img loading=\"lazy\" decoding=\"async\" width=\"668\" height=\"446\" src=\"https:\/\/blog.logrocket.com\/wp-content\/uploads\/2021\/12\/introduction-data-science-numpy.png\" class=\"attachment-full size-full wp-post-image\" alt=\"Introduction to data science with NumPy\" srcset=\"https:\/\/blog.logrocket.com\/wp-content\/uploads\/2021\/12\/introduction-data-science-numpy.png 668w, https:\/\/blog.logrocket.com\/wp-content\/uploads\/2021\/12\/introduction-data-science-numpy-300x200.png 300w\" sizes=\"auto, (max-width: 668px) 100vw, 668px\">\n<p>Data scientists who need to work with data for analysis, modeling, or forecasting should become familiar with NumPy\u2019s usage and its capabilities, as it will help them quickly prototype and test their ideas. This article aims to introduce you to some basic fundamental concepts of NumPy, such as:<\/p>\n<ul>\n<li><a href=\"#what-is-numpy-array\">What is a NumPy array?<\/a><\/li>\n<li><a href=\"#type-promotion-numpy-array\">Type promotion in NumPy<\/a><\/li>\n<li><a href=\"#working-numpy-arrays\">Working with NumPy arrays<\/a><\/li>\n<li><a href=\"#changing-type-elements-with-dtype-attribute\">Changing the type of the elements with the dtype attribute<\/a><\/li>\n<li><a href=\"#useful-functions-numpy\">Useful functions in NumPy<\/a><\/li>\n<li><a href=\"#finding-array-dimensions-shapes\">Finding array dimensions and shapes<\/a><\/li>\n<li><a href=\"#arithmetic-operations-numpy\">Arithmetic operations in NumPy<\/a><\/li>\n<li><a href=\"#statistical-functions-numpy\">Statistical functions in NumPy<\/a><\/li>\n<li><a href=\"#indexing-numpy-arrays\">Indexing NumPy arrays<\/a><\/li>\n<li><a href=\"#selecting-element-matrix\">Selecting an element from the matrix<\/a><\/li>\n<\/ul>\n<p>Let\u2019s get started.<\/p>\n<h2 id=\"what-is-numpy-array\">What is a NumPy array?<\/h2>\n<p>NumPy, short for Numerical Python, provides an efficient interface for storing and manipulating extensive data in the Python programming language. NumPy supplies functions you can call, which makes it especially useful for data manipulations. Later in this article, we will look into the methods and operations we can perform in NumPy.<\/p>\n<h3>How do NumPy arrays differ from Python lists?<\/h3>\n<p>In one way or another, a NumPy array is like Python\u2019s inbuilt <a href=\"https:\/\/blog.logrocket.com\/working-lists-python\/\" target=\"_blank\" rel=\"noopener\">list type<\/a>, but NumPy arrays offer much more efficient storage and data operations as the dataset grows larger. NumPy offers a special kind of array that makes use of multidimensional arrays, called <a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/arrays.ndarray.html#:~:text=An%20ndarray%20is%20a%20(usually,the%20sizes%20of%20each%20dimension.\" target=\"_blank\" rel=\"noopener\">ndarrays, or N-dimensional arrays<\/a>.<\/p>\n<figure id=\"attachment_83891\" aria-describedby=\"caption-attachment-83891\" style=\"width: 730px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-83891 size-full\" src=\"https:\/\/blog.logrocket.com\/wp-content\/uploads\/2021\/12\/list-python-diagram.png\" alt=\"Diagram of Python lists\" width=\"730\" height=\"427\" srcset=\"https:\/\/blog.logrocket.com\/wp-content\/uploads\/2021\/12\/list-python-diagram.png 730w, https:\/\/blog.logrocket.com\/wp-content\/uploads\/2021\/12\/list-python-diagram-300x175.png 300w\" sizes=\"auto, (max-width: 730px) 100vw, 730px\" \/><figcaption id=\"caption-attachment-83891\" class=\"wp-caption-text\">Source: <a href=\"https:\/\/pynative.com\/python-lists\/\" target=\"_blank\" rel=\"noopener\">PyNative<\/a><\/figcaption><\/figure>\n<p>An array is a container or wrapper that has a collection of elements of the same type, and can be one or more dimensions. A NumPy array is also homogenous \u2014 i.e., it contains data of all the same data type.<\/p>\n<h3>NumPy arrays by dimensions<\/h3>\n<p>As data scientists, the dimension of our array is essential to us, as it will enable us to know the structure of our dataset. NumPy has an inbuilt function for finding the dimension of the array.<\/p>\n<p>A dimension of an array is a direction in which elements are arranged. It is similar to the concept of axes and could be equated to visualizing data in x-, y-, or z-axes etc., depending on the number of rows and columns we have in a dataset.<\/p>\n<p>When we have one feature or column, the dimension is a one-dimensional array. It is 2D when we have two columns.<\/p>\n<figure id=\"attachment_83895\" aria-describedby=\"caption-attachment-83895\" style=\"width: 730px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-83895\" src=\"https:\/\/blog.logrocket.com\/wp-content\/uploads\/2021\/12\/array-dimension-examples.jpg\" alt=\"Examples of multidimensional arrays\" width=\"730\" height=\"407\" srcset=\"https:\/\/blog.logrocket.com\/wp-content\/uploads\/2021\/12\/array-dimension-examples.jpg 730w, https:\/\/blog.logrocket.com\/wp-content\/uploads\/2021\/12\/array-dimension-examples-300x167.jpg 300w\" sizes=\"auto, (max-width: 730px) 100vw, 730px\" \/><figcaption id=\"caption-attachment-83895\" class=\"wp-caption-text\">Source: <a href=\"https:\/\/www.educba.com\" target=\"_blank\" rel=\"noopener\">eduCBA<\/a><\/figcaption><\/figure>\n<h3>What are vectors and matrices?<\/h3>\n<p>A vector is an array of one dimension. We have a single vector when our dataset is meant to take a single column of input and is expected to make predictions from it.<\/p>\n<p>Data scientists constantly work with matrices and vectors; however, whenever we have many features in our dataset, and we end up using only one of the features for our model, the dimension of the feature has changed to one, which makes it a vector.<\/p>\n<p>Below is a sample dataset. Our inputs\/features are x1 and x2 while output\/target is y.<\/p>\n<p id=\"gdcalert3\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-83898\" src=\"https:\/\/blog.logrocket.com\/wp-content\/uploads\/2021\/12\/sample-data-set-vector-matrix.png\" alt=\"A sample data set for our vector and matrix examples\" width=\"730\" height=\"348\" srcset=\"https:\/\/blog.logrocket.com\/wp-content\/uploads\/2021\/12\/sample-data-set-vector-matrix.png 730w, https:\/\/blog.logrocket.com\/wp-content\/uploads\/2021\/12\/sample-data-set-vector-matrix-300x143.png 300w\" sizes=\"auto, (max-width: 730px) 100vw, 730px\" \/><\/p>\n<p>If we selected the x1 feature for our model, then we have a vector of a one-dimensional array. But, if we have x1 and x2 features, then we have a matrix, or a 2-dimensional array.<\/p>\n<pre class=\"language-python hljs\">python\nimport numpy as np\nx1 = np.array([1,2,3,5,7,1,5,7])\nx2 = np.array([5,3,2,1,1,6,3,1.2])\nx1\nprint(x2)\n<\/pre>\n<p>A matrix is an array of two dimensions and above. As data scientists, we may encounter a state where we have a dataset with single input and single output columns. Therefore, our array has more than one dimension, and then it is called a matrix of x and y-axis. In this case, we say our array is n-dimensional.<\/p>\n<p>This is a matrix of a 2D array, and here we have x- and y-axes.<\/p>\n<table class=\"center aligncenter\">\n<tbody>\n<tr>\n<td>1<\/td>\n<td>2<\/td>\n<td>3<\/td>\n<td>4<\/td>\n<td style=\"text-align: center;\">5<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>3<\/td>\n<td>4<\/td>\n<td>3<\/td>\n<td>4<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>This is a matrix of a 3D array with three axes: x, y, and z.<\/p>\n<table class=\"center aligncenter\">\n<tbody>\n<tr>\n<td>1<\/td>\n<td>2<\/td>\n<td>3<\/td>\n<td>4<\/td>\n<td>5<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>3<\/td>\n<td>4<\/td>\n<td>3<\/td>\n<td>4<\/td>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td>3<\/td>\n<td>5<\/td>\n<td>9<\/td>\n<td>6<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>All ndarray elements are homogeneous \u2014 meaning they are of the same data type, so they use the same amount of computer memory. This leads us to the concept of type promotion and data types in NumPy.<\/p>\n<h2 id=\"type-promotion-numpy\">Type promotion in NumPy<\/h2>\n<p>Type promotion is a situation where NumPy converts any element from one data type to another.<\/p>\n<p>In the diagram below, there is a mix of numbers in different data types, <code>float<\/code> and <code>int<\/code>. The result will give us the same number if they are in the Python list format.<\/p>\n<table class=\"center aligncenter\">\n<tbody>\n<tr>\n<td>1.2<\/td>\n<td>2<\/td>\n<td>3<\/td>\n<td>4<\/td>\n<td>5<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>If we had a Python list with <code>int<\/code> and <code>float<\/code> types, nothing would change here.<\/p>\n<table class=\"center aligncenter\">\n<tbody>\n<tr>\n<td>1.2<\/td>\n<td>2<\/td>\n<td>3<\/td>\n<td>4<\/td>\n<td>5<\/td>\n<\/tr>\n<tr>\n<td>1.2<\/td>\n<td>2<\/td>\n<td>3<\/td>\n<td>4<\/td>\n<td>5<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>But unlike a Python list, a NumPy array interacts better with elements of the same type. Let\u2019s see how this plays out in practice.<\/p>\n<p>NumPy promotes all the arrays to a floating-point number. This diagram is the result of converting the NumPy array to this <a href=\"https:\/\/numpy.org\/doc\/stable\/user\/basics.types.html\" target=\"_blank\" rel=\"noopener\">data type<\/a>.<\/p>\n<table class=\"center aligncenter\">\n<tbody>\n<tr>\n<td>1.2<\/td>\n<td>2<\/td>\n<td>3<\/td>\n<td>4<\/td>\n<td>5<\/td>\n<\/tr>\n<tr>\n<td>1.2<\/td>\n<td>2.0<\/td>\n<td>3.0<\/td>\n<td>4.0<\/td>\n<td>5.0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>In the code sample below, we created a Python list. Next, we shall make a NumPy array of this combination of two different types of elements \u2014 i.e., integers and floats.<\/p>\n<pre class=\"language-python hljs\">python\nimport numpy as np\npythonList = [1,2,3,3.3]\nnumpyArray = np.array(pythonList)\nprint(\"all elements promoted to\",numpyArray.dtype)\n\nResult;\nall elements promoted to float64\n<\/pre>\n<p>Using the <code>dtype<\/code> function in NumPy, the type of elements in the array are promoted to <code>float64<\/code>. It emphasizes that the NumPy array prioritizes floats above integers by converting the entire array of integers to floats.<\/p>\n<p>The code sample below combines a list of integers with a list of strings and then promotes them all to Unicode string. It implies that the string has a higher priority over the integers.<\/p>\n<pre class=\"language-python hljs\">python\nimport numpy as np\npythonList = [1,2,3,'t']\nprint(pythonList)\nnumpyArray = np.array(pythonList)\nprint(numpyArray.dtype)\n\nWe get this result:\n[1, 2, 3, 't']\n&lt;U21\n<\/pre>\n<p>Understanding the concept of type promotion will guide us through what to do when we have type errors while working with NumPy. In the code sample below, we have a type error:<\/p>\n<p>python<\/p>\n<pre class=\"language-python hljs\">import numpy as np\npythonList = [1,2,3,'t']\nprint(pythonList)\nnumpyArray = np.array(pythonList)\nprint(numpyArray + 2)\n\nUFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (dtype('&lt;U21'), dtype('&lt;U21')) -&gt; dtype('&lt;U21')\n<\/pre>\n<p>Which means that, when elements are promoted to a Unicode string, we cannot perform any mathematical operations on them.<\/p>\n<h2 id=\"working-numpy-arrays\">Working with NumPy arrays<\/h2>\n<p>Before we get started, make sure you have a version of Python that\u2019s at least \u2265 3.0, and have installed NumPy \u2265 v1.8.<\/p>\n<h3>Why do we import NumPy?<\/h3>\n<p>Working with NumPy entails importing the NumPy module before you start writing the code.<\/p>\n<p>When we import NumPy as <code>np<\/code>, we establish a link with NumPy. We are also shortening the word \u201cnumpy\u201d to \u201cnp\u201d to make our code easier to read and help avoid namespace issues.<\/p>\n<pre class=\"language-python hljs\">python\nimport numpy as np\n\nThe above is the same as the below:\n\npython\nimport numpy \nnp = numpy \ndel numpy\n<\/pre>\n<p>The standard NumPy import, under the alias <code>np<\/code>, can also be named anything you want it to be.<\/p>\n<h3>Creating a NumPy array from a Python list<\/h3>\n<p>The code snippet below depicts how to call NumPy\u2019s inbuilt method (array) on a Python list of integers to form a NumPy array object.<\/p>\n<pre class=\"language-python hljs\">python\nimport numpy as np\npyList = [1,2,3,4,5]\nnumpy_array = np.array(pyList)\nnumpy_array\n<\/pre>\n<h3>Or, just use the NumPy <code>array<\/code> function<\/h3>\n<p>We can import the <code>array()<\/code> function from the NumPy library to create our arrays.<\/p>\n<pre class=\"language-python hljs\">python\n\u200b\u200bfrom numpy import array\narr = array([[1],[2],[3]])\narr\n<\/pre>\n<h3>Using the <code>zeros<\/code> and <code>ones<\/code> function to create NumPy arrays<\/h3>\n<p>As data scientists, we sometimes create arrays filled solely with 0 or 1. For instance, binary data is labeled with 0 and 1, we may need dummy datasets of one label.<\/p>\n<p>In order to create these arrays, NumPy provides the functions <a href=\"https:\/\/docs.scipy.org\/doc\/numpy\/reference\/generated\/numpy.zeros.html\" target=\"_blank\" rel=\"noopener\"><code>np.zeros<\/code><\/a> and <a href=\"https:\/\/docs.scipy.org\/doc\/numpy\/reference\/generated\/numpy.ones.html\" target=\"_blank\" rel=\"noopener\"><code>np.ones<\/code><\/a>. They both take in the same arguments, which includes just one required argument \u2014 the array shape. The functions also allow for manual casting using the <code>dtype<\/code> keyword argument.<\/p>\n<p>The code below shows example usages of <code>np.zeros<\/code> and <code>np.ones<\/code>.<\/p>\n<pre class=\"language-python hljs\">python\nimport numpy as nd\nzeros = nd.zeros(6)\nzeros\n<\/pre>\n<p>Change the type here:<\/p>\n<pre class=\"prettyprint\">python\nimport numpy as np\nones_array = np.ones(6, dtype = int)\nones_array\n<\/pre>\n<p>We can alternative create a matrix of it:<\/p>\n<pre class=\"language-python hljs\">python\nimport numpy as np\narr = np.ones(6, dtype = int).reshape(3,2)\narr\n<\/pre>\n<p>In order to create an array filled with a specific number of ones, we\u2019ll use the <code>ones<\/code> function.<\/p>\n<pre class=\"language-python hljs\">python\nimport numpy as np\narr = np.ones(12, dtype = int)\narr\n\nMatrix form\npython\n\u200b\u200bimport numpy as np\narr = np.ones(12, dtype = int).reshape(3,4)\narr\n<\/pre>\n<p>We can as well perform a mathematical operation on the array:<\/p>\n<p>This will fill our array with <code>3<\/code>s instead of <code>1<\/code>s:<\/p>\n<pre class=\"language-python hljs\">python\nimport numpy as np\nones_array = np.ones(6, dtype = int) * 3\nones_array\n<\/pre>\n<h2 id=\"changing-type-elements-with-dtype-attribute\">Changing the type of the elements with the <code>dtype<\/code> attribute<\/h2>\n<p>While exploring a dataset, it is part of the standard to familiarize yourself with the type of elements you have in each column. This will give us an overview of the dataset. To learn more about the usage of this attribute, check the <a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/arrays.dtypes.html#arrays-dtypes\" target=\"_blank\" rel=\"noopener\">documentation<\/a>.<\/p>\n<p>The <code>dtype<\/code> attribute can show the type of elements in an array.<\/p>\n<pre class=\"language-python hljs\">python\nimport numpy as nd\nfind_type1 = nd.array([2,3,5,3,3,1,2,0,3.4,3.3])\nfind_type2 = nd.array([[2,3,5],[3,5,4],[1,2,3],[0,3,3]])\nprint(\"first variable is of type\", find_type1.dtype)\nprint(\"second variable is of type\", find_type2.dtype)\n<\/pre>\n<p>In order to have more control over the form of data we want to feed to our model, we can change the type of element in our dataset using the <code>dtype<\/code> property.<\/p>\n<p>However, while we can convert integers to floats, or vice versa, and integers or floats to complex numbers, and vice versa, we cannot convert any of the data types above to a string.<\/p>\n<p>Using the <code>dtype<\/code> function in NumPy enables us to convert the elements from floats to ints:<\/p>\n<pre class=\"language-python hljs\">python\nimport numpy as nd\nones = nd.ones(6,dtype = int)\nones\n\nResult;\narray([1, 1, 1, 1, 1, 1])\n\npython\nimport numpy as nd\narr = nd.array([[2,3,5],[3,5,4],[1,2,3],[0,3,3]],dtype = float)\nprint(\"the elements type  is\", arr.dtype)\n<\/pre>\n<h3>Differences between the <code>type<\/code> and <code>dtype<\/code> attributes<\/h3>\n<p>The <code>type<\/code> belongs to Python. It unravels the type of Python data type we are working with. Visit the documentation for <a href=\"https:\/\/docs.python.org\/3\/library\/datatypes.html\" target=\"_blank\" rel=\"noopener\">more on Python data types<\/a>.<\/p>\n<p>Using <code>type<\/code> in the code sample below shows us that we have a special Python object, which is <code>numpy.ndarray<\/code>. It is similar to how <code>type(\"string\")<\/code> works for Python strings; for example, the code sample below displays the type of the object.<\/p>\n<pre class=\"language-python hljs\">python\nimport numpy as np\narrs = np.array([[2,4,6],[3,2,4],[6,4,2]])\ntype(arrs)\n<\/pre>\n<p>The <code>dtype<\/code> property, on the other hand, is one of NumPy\u2019s inbuilt properties. As we explained earlier, NumPy has its own data types that are different from Python data types, so we can use the <code>dtype<\/code> property to find out which NumPy data type we are working with.<\/p>\n<p>Below, we shall use NumPy\u2019s <code>dtype<\/code> property to find out which type of elements are in our NumPy array.<\/p>\n<pre class=\"language-python hljs\">import numpy as np\narrs = np.array([[2,4,6],[3,2,4],[6,4,2]])\narr.dtype\n<\/pre>\n<p>Any attempt to use the <code>dtype<\/code> attribute on another non-NumPy Python object will give us an error.<\/p>\n<pre class=\"language-python hljs\">python\nimport numpy as np\npyList =[ \"Listtype\",2]\npyList.dtype\n\nResult;\n\u200b\u200b---------------------------------------------------------------------------\nAttributeError                            Traceback (most recent call last)\n&lt;ipython-input-19-2756eacf407c&gt; in &lt;module&gt;\n      1 arr = \"string type\"\n----&gt; 2 arr.dtype\n\nAttributeError: 'list' object has no attribute 'dtype'\n<\/pre>\n<h2 id=\"useful-functions-numpy\">Useful functions in NumPy<\/h2>\n<p>NumPy arrays are rich with a number of inbuilt functions. In this section, I will introduce you to the functions we\u2019d use most often while working on datasets:<\/p>\n<ul>\n<li>Reshaping an array<\/li>\n<li>Reshaping a vector to a matrix<\/li>\n<li>Reshaping a horizontal vector to vertical<\/li>\n<\/ul>\n<h3>Reshaping an array<\/h3>\n<p>The <code>reshape<\/code> function will enable us to generate random data. It is not only good for rendering arrays to the columns and rows we want, but can also be helpful in converting a row to a column to row. This gives us the flexibility to manipulate our array the way we want it.<\/p>\n<p>In the code snippet below, we have a vector, but we reshape it to a matrix, with an x-dimension and a y-dimension. The first argument in the <code>reshape<\/code> function is the <code>row<\/code>, and the second is the <code>column<\/code>.<\/p>\n<h3>Reshaping a vector to a matrix<\/h3>\n<p>We can use reshape to render our array in the desired shape we want to achieve. This is one of the wonders of NumPy.<\/p>\n<pre class=\"language-python hljs\">python\nimport numpy as np\na = np.arrange(12)\nmatrix = a.reshape(3,4)\nprint(matrix)\n<\/pre>\n<h3>Reshaping a vector from horizontal to vertical<\/h3>\n<p>We can also turn a row into a column or a column into a row. This makes the NumPy array more flexible to use for data manipulation.<\/p>\n<pre class=\"language-python hljs\">python\nimport numpy as np\na = np.arrange(12)\nvertical = a.reshape(12,1)\nprint(vertical)\n<\/pre>\n<h3>Adding more rows and columns<\/h3>\n<p>The code snippet below starts with a one-dimensional array of nine elements, but we reshape it to two dimensions, with three rows and three columns.<\/p>\n<pre class=\"language-python hljs\">python\nimport numpy as np\none_d_array = np.array([2,3,4,5,6,7,8,9,10])\nreshaped_array = one_d_array.reshape(3,3)\nreshaped_array\n<\/pre>\n<h3>Transposing data<\/h3>\n<p>Just as reshaping data is common during data preprocessing, transposing data is also common. In some cases, we have data that\u2019s supposed to be in a particular format, but receive some new data that is not in tandem with the data we have. This is where transposing the new data emerges to resolve the conflicting structure of our data.<\/p>\n<p>We can just transpose the data using the <code><a href=\"https:\/\/docs.scipy.org\/doc\/numpy\/reference\/generated\/numpy.transpose.html\" target=\"_blank\" rel=\"noopener\">np.transpose<\/a><\/code> function to convert it to the proper format that fits the required data.<\/p>\n<figure id=\"attachment_83902\" aria-describedby=\"caption-attachment-83902\" style=\"width: 730px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-83902\" src=\"https:\/\/blog.logrocket.com\/wp-content\/uploads\/2021\/12\/transpose-array-demo.png\" alt=\"Diagram demonstrating how to transpose an array\" width=\"730\" height=\"422\" srcset=\"https:\/\/blog.logrocket.com\/wp-content\/uploads\/2021\/12\/transpose-array-demo.png 730w, https:\/\/blog.logrocket.com\/wp-content\/uploads\/2021\/12\/transpose-array-demo-300x173.png 300w\" sizes=\"auto, (max-width: 730px) 100vw, 730px\" \/><figcaption id=\"caption-attachment-83902\" class=\"wp-caption-text\">Source: <a href=\"https:\/\/numpy.org\" target=\"_blank\" rel=\"noopener\">NumPy<\/a><\/figcaption><\/figure>\n<pre class=\"language-python hljs\">python\nimport numpy as np\narr = np.arrange(12)\narr = np.reshape(arr, (4, 3))\ntransposed_arr = np.transpose(arr)\nprint((arr))\nprint('arr shape: {}'.format(arr.shape))\nprint((transposed_arr))\nprint('new transposed shape: {}'.format(transposed_arr.shape))\n<\/pre>\n<p>Transpose wouldn\u2019t work for a one-dimensional array:<\/p>\n<pre class=\"language-python hljs\">import numpy as np\narr = np.arrange(12)\narr.ndim\ntransposed_arr = np.transpose(arr)\nprint((arr))\n<\/pre>\n<h2>Finding array dimensions and shapes<\/h2>\n<p>It is sometimes important to know the dimensions of our data during preprocessing. Performing mathematical operations on vectors and matrices with no similar dimensions will result in an error. For example, we can get an error from multiplying a 2D array by a 1D array.<\/p>\n<p>If you don\u2019t know the dimensions of your data, you can use the <code>ndim<\/code> attribute to find out.<\/p>\n<pre class=\"language-python hljs\">python\nimport numpy as np\none_d_array = np.array([2,3,4,5,6,7,8,9,10])\nreshaped_array = one_d_array.reshape(3,3)\nreshaped_array.ndim\n<\/pre>\n<p>Using different dimensions gave the error below, hence the importance of knowing the dimensions of our arrays.<\/p>\n<pre class=\"language-python hljs\">python\nimport numpy as np\none_d_array = np.array([2,3,4,5,6,7,8,9,10])\nreshaped_array = one_d_array.reshape(3,3)\nreshaped_array * one_d_array\n\nResult;\n\n\u200b\u200bValueError: operands could not be broadcast together with shapes (3,3) (9,) \n<\/pre>\n<h3>Finding the shape of arrays<\/h3>\n<p>More specifically, you can use the <code>shape<\/code> property to find the number of rows and columns in your array. Imbalances in the shapes can also give us errors when dealing with two different datasets. The code snippet shows how to find the shape of an array:<\/p>\n<pre class=\"language-python hljs\">python\nimport numpy as np\none_d_array = np.array([2,3,4,5,6,7,8,9,10])\nreshaped_array = one_d_array.reshape(3,3)\nreshaped_array.shape\n<\/pre>\n<h3>Generating matrices with the <code>arrange<\/code> and <code>reshape<\/code> functions<\/h3>\n<p>With NumPy, we can easily generate numbers and use <code>reshape<\/code> functions to convert the numbers to any possible rows and columns we want. For example in the code sample below, the <code>arrange<\/code> function generates a single row of <code>1<\/code> to <code>13<\/code>, while the <code>reshape<\/code> function renders the array to three rows and four columns.<\/p>\n<pre class=\"language-python hljs\">python\nimport numpy as np\nmatrix =  np.arrange(1,13).reshape(3,4)\nmatrix\n<\/pre>\n<h2 id=\"arithmetic-operations-numpy\">Arithmetic operations in NumPy<\/h2>\n<p>Data scientists mostly work with vectors and matrices while trying to perform data mining. In order to avoid errors during the preprocessing stage, it is crucial we check our arrays\u2019 dimensions, shapes, and dtypes.<\/p>\n<p>If we didn\u2019t, we would get errors if we tried to perform mathematical operations on these matrices and vectors when their sizes, dimensions, and shapes are not the same.<\/p>\n<p>Checking the <code>dtype<\/code> is to avoid type errors, as I explained in the previous section. But knowing each array\u2019s dimensions and shape safeguards us from getting value errors.<\/p>\n<p>For an <a href=\"https:\/\/hackernoon.com\/what-steps-should-one-take-while-doing-data-preprocessing-502c993e1caa\" target=\"_blank\" rel=\"noopener\">overview of data preprocessing<\/a>, kindly check this HackerNoon post.<\/p>\n<p>Below is an example of two-vector arithmetic:<\/p>\n<pre class=\"language-python hljs\">python \nfrom numpy import array\nx1 = array([20,21,22,23,24])\nx2 = array([21,23,2,2,3])\nx1*x2\n<\/pre>\n<p>We can divide as well:<\/p>\n<pre class=\"language-python hljs\">python \nfrom numpy import array\nx1 = array([20,21,22,23,24])\nx2 = array([21,23,2,2,3])\nx1\/x2\n<\/pre>\n<p>Subtraction of two vectors looks like this:<\/p>\n<pre class=\"language-python hljs\">python \nfrom numpy import array\nx1 = array([20,21,22,23,24])\nx2 = array([21,23,2,2,3])\nx1-x2\n<\/pre>\n<p>This is similar to performing any other mathematical operation, such as subtraction, division, and multiplication.<\/p>\n<p>The addition of two vectors follows this pattern:<\/p>\n<pre class=\"language-python hljs\">z = [z1,z2,z3,z4,z5]\ny = [y1,y2,y3,y4,y5]\nz + y =  z1 + y1, z2 + y2, z3 + y3, z4 + y4, z5 + y5\n\npython\nfrom numpy import array\nz = array([2,3,4,5,6])\ny = array([1,2,3,4,5])\nsum_vectors = z + y\nmultiplication_vectors = z * y\nsum_vectors\nprint(multiplication_vectors)\n<\/pre>\n<p>You can also perform mathematical operations on matrices:<\/p>\n<pre class=\"language-python hljs\">import numpy as np\narr = np.array([[1, 2], [3, 4]])\n# Square root element values\nprint('Square root', arr**0.5)\n# Add 1 to element values\nprint('added one',arr + 1)\n# Subtract element values by 1.2\nprint(arr - 1.2)\n# Double element values\nprint(arr * 2)\n# Halve element values\nprint(arr \/ 2)\n# Integer division (half)\nprint(arr \/\/ 2)\n# Square element values\nprint(arr**2)\n<\/pre>\n<h3><code>sum<\/code> function in NumPy<\/h3>\n<p>In the previous section on mathematical operations, we summed the values between two vectors. There are cases where we can also use the inbuilt function (<a href=\"https:\/\/docs.scipy.org\/doc\/numpy\/reference\/generated\/numpy.sum.html\" target=\"_blank\" rel=\"noopener\">np.sum<\/a>) in NumPy to sum the values within a single array.<\/p>\n<p>The code snippet below shows how to use <code>np.sum<\/code>:<\/p>\n<p>If the <code>np.sum<\/code> axis is equal to <code>0<\/code>, the addition is done along the column; it switches to rows when the axis is equal to <code>1<\/code>. If the axis is not defined, the \u200b\u200boverall sum of the array is returned.<\/p>\n<pre class=\"language-python hljs\">python\n\u200b\u200bimport numpy as np\nsum = np.array([[3, 72, 3],\n                [1, 7, -6],\n                [-2, -9, 8]])\n\nprint(np.sum(sum))\nprint(np.sum(sum, axis=0))\nprint(np.sum(sum, axis=1))\n\nResult;\n\n77\n[ 2 70  5]\n[78  2 -3]\n<\/pre>\n<h2 id=\"statistical-functions-numpy\">Statistical functions in NumPy<\/h2>\n<p>NumPy is also useful to analyze data for its main characteristics and interesting trends. There are a few techniques in NumPy that allow us to quickly inspect data arrays. NumPy comes with some <a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/routines.statistics.html\" target=\"_blank\" rel=\"noopener\">statistical functions<\/a>, but we\u2019ll use the <a href=\"https:\/\/scikit-learn.org\/stable\/\" target=\"_blank\" rel=\"noopener\">scikit-learn<\/a> library \u2014 one of the core libraries for professional-level data analysis.<\/p>\n<p>For example, we can obtain the minimum and maximum values of a NumPy array using its inbuilt <a href=\"https:\/\/docs.scipy.org\/doc\/numpy\/reference\/generated\/numpy.ndarray.min.html\" target=\"_blank\" rel=\"noopener\">min<\/a> and <a href=\"https:\/\/docs.scipy.org\/doc\/numpy\/reference\/generated\/numpy.ndarray.max.html\" target=\"_blank\" rel=\"noopener\">max<\/a> functions. This gives us an initial sense of the data\u2019s range and can alert us to extreme outliers in the data.<\/p>\n<p>The code below shows example usages of the min and max functions.<\/p>\n<pre class=\"language-python hljs\">python\nimport numpy as np\narr = np.array([[0, 72, 3],\n               [1, 3, -60],\n               [-3, -2, 4]])\nprint(arr.min())\nprint(arr.max())\n\nprint(arr.min(axis=0))\nprint(arr.max(axis=-1))\n\nResult;\n-60\n72\n[ -3  -2 -60]\n[72  3  4]\n<\/pre>\n<p>Data scientists tend to work on smaller datasets than machine learning engineers, and their main goal is to analyze the data and quickly extract usable results. Therefore, they focus more on the traditional data inference models found in scikit-learn, rather than deep neural networks.<\/p>\n<p>The scikit-learn library includes tools for data preprocessing and data mining. It is imported in Python via the statement <code>import sklearn<\/code>.<\/p>\n<p>This computes <a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.mean.html#numpy.mean\" target=\"_blank\" rel=\"noopener\">the arithmetic mean<\/a> along the specified axis:<\/p>\n<pre class=\"language-python hljs\">mean(a[,axis,dtype,keepdims,where])\n<\/pre>\n<p>This <a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.std.html#numpy.std\">finds the standard deviation<\/a> in a dataset:<\/p>\n<pre class=\"language-python hljs\">std(a[, axis, dtype, out, ddof, keepdims, where])\n<\/pre>\n<h2 id=\"indexing-numpy-arrays\">Indexing NumPy arrays<\/h2>\n<p>An index is the position of a value. Indexing is aimed at getting a specific value in the array by referring to its index or position. In data science, we make use of indexing a lot because it allows us to select an element from an array, a single row\/column, etc.<\/p>\n<p>While working with an array, we may need to locate a specific row or column from the array. Let\u2019s see how indexing works in NumPy.<\/p>\n<p>The first position index is denoted as 0 which represents the first row.<\/p>\n<pre class=\"language-python hljs\">python\nimport numpy as np\nmatrix =  np.arrange(1,13).reshape(3,4)\nmatrix[0]\n\nNow, let's try getting the third row from the array.\npython\nimport numpy as np\nmatrix[2]\n<\/pre>\n<p>The below gives us a vector from the last row.<\/p>\n<pre class=\"language-python hljs\">python\nimport numpy as np\nmatrix[-1]\n<\/pre>\n<p>Every element, row, and column have an array index position numbering from <code>0<\/code>. It can also be a selection of one or more elements from a vector.<\/p>\n<p>This is as simple as trying to filter a column or rows from a matrix. For example, we can select a single value from several values in the below example. The values are numbered sequentially in the index memory, starting from zero.<\/p>\n<h3>Indexing a vector<\/h3>\n<table class=\"center aligncenter\">\n<tbody>\n<tr>\n<td>index<\/td>\n<td>0<\/td>\n<td>1<\/td>\n<td>2<\/td>\n<td>3<\/td>\n<\/tr>\n<tr>\n<td>value<\/td>\n<td>2<\/td>\n<td>4<\/td>\n<td>5<\/td>\n<td>10<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>For instance, getting a value at index 0 will give us 2, which is a scalar.<\/p>\n<pre class=\"language-python hljs\">python\nimport numpy as np\nvalue =  np.array([2,4,5,10])\nvalue[0]\n<\/pre>\n<h3>Indexing a matrix<\/h3>\n<p>A matrix is more like an array of vectors. A single row or column is referred to as a vector, but when there is more than one row, we have a matrix.<\/p>\n<p>We are getting the position of vectors in the matrix below using square brackets.<\/p>\n<table class=\"center aligncenter\">\n<tbody>\n<tr>\n<td>vector[0]<\/td>\n<td>1<\/td>\n<td>2<\/td>\n<td>3<\/td>\n<\/tr>\n<tr>\n<td>vector[1]<\/td>\n<td>4<\/td>\n<td>5<\/td>\n<td>6<\/td>\n<\/tr>\n<tr>\n<td>vector[2]<\/td>\n<td>7<\/td>\n<td>8<\/td>\n<td>9<\/td>\n<\/tr>\n<tr>\n<td>vector[3]<\/td>\n<td>10<\/td>\n<td>11<\/td>\n<td>12<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre class=\"language-python hljs\">vector[0] =&gt; [1,2,3]\nvector[1] =&gt; [4,5,6]\nvector[2] =&gt; [7,8,9]\nvector[3] =&gt; [10,11,12]\n<\/pre>\n<p>Getting an element of <code>vector[0]<\/code> is done by adding the index of the element.<\/p>\n<pre class=\"language-python hljs\">vector[0,0] =&gt; 1\nvector[0,1] =&gt; 2\nvector[0,2] =&gt; 3\n<\/pre>\n<h2 id=\"selecting-element-matrix\">Selecting an element from the matrix<\/h2>\n<p>This gives us a scalar or element of the second position in the third row.<\/p>\n<pre class=\"language-python hljs\">python\nimport numpy as np\nmatrix[2,1]\n<\/pre>\n<h3>Selecting columns from the matrix<\/h3>\n<p>This selects the first column:<\/p>\n<pre class=\"language-python hljs\">python\nimport numpy as np\nmatrix[:,0]\n<\/pre>\n<p>Select the second column:<\/p>\n<pre class=\"language-python hljs\">python\nimport numpy as np\nmatrix[:,1]\n<\/pre>\n<p>This gets the last column:<\/p>\n<pre class=\"language-python hljs\">python\nimport numpy as np\nmatrix[:,-1]\n<\/pre>\n<h2>Conclusion<\/h2>\n<p>In this article, we learned about the fundamentals of NumPy with essential functions for manipulating NumPy arrays. I hope this helps you gain a basic understanding of Python on your path to becoming a data scientist.<\/p>\n<\/html>\n","protected":false},"excerpt":{"rendered":"<p>Implement a series of useful functions, manipulate arrays and data, and see how NumPy arrays differ from Python lists in this detailed guide.<\/p>\n","protected":false},"author":156415876,"featured_media":83917,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2147999,1],"tags":[2109833],"class_list":["post-83888","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dev","category-uncategorized","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.1.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Introduction to data science with NumPy - LogRocket Blog<\/title>\n<meta name=\"description\" content=\"Implement a series of useful functions, manipulate arrays and data, and see how NumPy arrays differ from Python lists in this detailed guide.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.logrocket.com\/introduction-data-science-numpy\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Introduction to data science with NumPy - LogRocket Blog\" \/>\n<meta property=\"og:description\" content=\"Implement a series of useful functions, manipulate arrays and data, and see how NumPy arrays differ from Python lists in this detailed guide.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.logrocket.com\/introduction-data-science-numpy\/\" \/>\n<meta property=\"og:site_name\" content=\"LogRocket Blog\" \/>\n<meta property=\"article:published_time\" content=\"2021-12-27T15:30:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-06-04T21:13:16+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/blog.logrocket.com\/wp-content\/uploads\/2021\/12\/introduction-data-science-numpy.png\" \/>\n\t<meta property=\"og:image:width\" content=\"668\" \/>\n\t<meta property=\"og:image:height\" content=\"446\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Kola Badmus\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kola Badmus\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"18 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/blog.logrocket.com\/introduction-data-science-numpy\/\",\"url\":\"https:\/\/blog.logrocket.com\/introduction-data-science-numpy\/\",\"name\":\"Introduction to data science with NumPy - LogRocket Blog\",\"isPartOf\":{\"@id\":\"https:\/\/blog.logrocket.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/blog.logrocket.com\/introduction-data-science-numpy\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/blog.logrocket.com\/introduction-data-science-numpy\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/blog.logrocket.com\/wp-content\/uploads\/2021\/12\/introduction-data-science-numpy.png\",\"datePublished\":\"2021-12-27T15:30:07+00:00\",\"dateModified\":\"2024-06-04T21:13:16+00:00\",\"author\":{\"@id\":\"https:\/\/blog.logrocket.com\/#\/schema\/person\/7f1c86e3c0091a3bcc184a9ca221005c\"},\"description\":\"Implement a series of useful functions, manipulate arrays and data, and see how NumPy arrays differ from Python lists in this detailed guide.\",\"breadcrumb\":{\"@id\":\"https:\/\/blog.logrocket.com\/introduction-data-science-numpy\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/blog.logrocket.com\/introduction-data-science-numpy\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/blog.logrocket.com\/introduction-data-science-numpy\/#primaryimage\",\"url\":\"https:\/\/blog.logrocket.com\/wp-content\/uploads\/2021\/12\/introduction-data-science-numpy.png\",\"contentUrl\":\"https:\/\/blog.logrocket.com\/wp-content\/uploads\/2021\/12\/introduction-data-science-numpy.png\",\"width\":668,\"height\":446,\"caption\":\"Introduction to data science with NumPy\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/blog.logrocket.com\/introduction-data-science-numpy\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/blog.logrocket.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Introduction to data science with NumPy\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/blog.logrocket.com\/#website\",\"url\":\"https:\/\/blog.logrocket.com\/\",\"name\":\"LogRocket Blog\",\"description\":\"Resources to Help Product Teams Ship Amazing Digital Experiences\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/blog.logrocket.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/blog.logrocket.com\/#\/schema\/person\/7f1c86e3c0091a3bcc184a9ca221005c\",\"name\":\"Kola Badmus\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/blog.logrocket.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/fd1d936a9252e3fb63b1e46a45e4132cac6c58e5bb32cb0f46a9d24dcb35334f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/fd1d936a9252e3fb63b1e46a45e4132cac6c58e5bb32cb0f46a9d24dcb35334f?s=96&d=mm&r=g\",\"caption\":\"Kola Badmus\"},\"url\":\"https:\/\/blog.logrocket.com\/author\/kolabadmus\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Introduction to data science with NumPy - LogRocket Blog","description":"Implement a series of useful functions, manipulate arrays and data, and see how NumPy arrays differ from Python lists in this detailed guide.","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:\/\/blog.logrocket.com\/introduction-data-science-numpy\/","og_locale":"en_US","og_type":"article","og_title":"Introduction to data science with NumPy - LogRocket Blog","og_description":"Implement a series of useful functions, manipulate arrays and data, and see how NumPy arrays differ from Python lists in this detailed guide.","og_url":"https:\/\/blog.logrocket.com\/introduction-data-science-numpy\/","og_site_name":"LogRocket Blog","article_published_time":"2021-12-27T15:30:07+00:00","article_modified_time":"2024-06-04T21:13:16+00:00","og_image":[{"width":668,"height":446,"url":"https:\/\/blog.logrocket.com\/wp-content\/uploads\/2021\/12\/introduction-data-science-numpy.png","type":"image\/png"}],"author":"Kola Badmus","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kola Badmus","Est. reading time":"18 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/blog.logrocket.com\/introduction-data-science-numpy\/","url":"https:\/\/blog.logrocket.com\/introduction-data-science-numpy\/","name":"Introduction to data science with NumPy - LogRocket Blog","isPartOf":{"@id":"https:\/\/blog.logrocket.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/blog.logrocket.com\/introduction-data-science-numpy\/#primaryimage"},"image":{"@id":"https:\/\/blog.logrocket.com\/introduction-data-science-numpy\/#primaryimage"},"thumbnailUrl":"https:\/\/blog.logrocket.com\/wp-content\/uploads\/2021\/12\/introduction-data-science-numpy.png","datePublished":"2021-12-27T15:30:07+00:00","dateModified":"2024-06-04T21:13:16+00:00","author":{"@id":"https:\/\/blog.logrocket.com\/#\/schema\/person\/7f1c86e3c0091a3bcc184a9ca221005c"},"description":"Implement a series of useful functions, manipulate arrays and data, and see how NumPy arrays differ from Python lists in this detailed guide.","breadcrumb":{"@id":"https:\/\/blog.logrocket.com\/introduction-data-science-numpy\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.logrocket.com\/introduction-data-science-numpy\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blog.logrocket.com\/introduction-data-science-numpy\/#primaryimage","url":"https:\/\/blog.logrocket.com\/wp-content\/uploads\/2021\/12\/introduction-data-science-numpy.png","contentUrl":"https:\/\/blog.logrocket.com\/wp-content\/uploads\/2021\/12\/introduction-data-science-numpy.png","width":668,"height":446,"caption":"Introduction to data science with NumPy"},{"@type":"BreadcrumbList","@id":"https:\/\/blog.logrocket.com\/introduction-data-science-numpy\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.logrocket.com\/"},{"@type":"ListItem","position":2,"name":"Introduction to data science with NumPy"}]},{"@type":"WebSite","@id":"https:\/\/blog.logrocket.com\/#website","url":"https:\/\/blog.logrocket.com\/","name":"LogRocket Blog","description":"Resources to Help Product Teams Ship Amazing Digital Experiences","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.logrocket.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blog.logrocket.com\/#\/schema\/person\/7f1c86e3c0091a3bcc184a9ca221005c","name":"Kola Badmus","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blog.logrocket.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/fd1d936a9252e3fb63b1e46a45e4132cac6c58e5bb32cb0f46a9d24dcb35334f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/fd1d936a9252e3fb63b1e46a45e4132cac6c58e5bb32cb0f46a9d24dcb35334f?s=96&d=mm&r=g","caption":"Kola Badmus"},"url":"https:\/\/blog.logrocket.com\/author\/kolabadmus\/"}]}},"yoast_description":"Implement a series of useful functions, manipulate arrays and data, and see how NumPy arrays differ from Python lists in this detailed guide.","_links":{"self":[{"href":"https:\/\/blog.logrocket.com\/wp-json\/wp\/v2\/posts\/83888","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.logrocket.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.logrocket.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.logrocket.com\/wp-json\/wp\/v2\/users\/156415876"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.logrocket.com\/wp-json\/wp\/v2\/comments?post=83888"}],"version-history":[{"count":11,"href":"https:\/\/blog.logrocket.com\/wp-json\/wp\/v2\/posts\/83888\/revisions"}],"predecessor-version":[{"id":83923,"href":"https:\/\/blog.logrocket.com\/wp-json\/wp\/v2\/posts\/83888\/revisions\/83923"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.logrocket.com\/wp-json\/wp\/v2\/media\/83917"}],"wp:attachment":[{"href":"https:\/\/blog.logrocket.com\/wp-json\/wp\/v2\/media?parent=83888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.logrocket.com\/wp-json\/wp\/v2\/categories?post=83888"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.logrocket.com\/wp-json\/wp\/v2\/tags?post=83888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}