{"id":3408,"date":"2021-12-12T01:17:52","date_gmt":"2021-12-12T01:17:52","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=3408"},"modified":"2022-08-16T02:59:42","modified_gmt":"2022-08-16T02:59:42","slug":"what-is-numpy","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-numpy\/what-is-numpy\/","title":{"rendered":"What is NumPy"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll have a good understanding of NumPy and how it helps you perform calculations fast and efficiently.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-numpy'>Introduction to NumPy <a href=\"#introduction-to-numpy\" class=\"anchor\" id=\"introduction-to-numpy\" title=\"Anchor for Introduction to NumPy\">#<\/a><\/h2>\n\n\n\n<p>NumPy stands for Numerical Python and is pronounced as \/\u02c8n\u028cmpa\u026a\/. Numpy is a Python library that performs numerical calculations. <\/p>\n\n\n\n<p>NumPy is very fast because it is written in the C programming language.<\/p>\n\n\n\n<p>NumPy is built on <a href=\"https:\/\/en.wikipedia.org\/wiki\/Linear_algebra\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">linear algebra<\/a>. It&#8217;s about matrices and vectors and performing the mathematical calculations on them.<\/p>\n\n\n\n<p>The key concept in NumPy is the <em>NumPy array<\/em> data type. A NumPy array may have one or more dimensions:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>One dimension arrays (1D) represent vectors.<\/li><li>Two-dimensional arrays (2D) represent matrices.<\/li><li>And higher dimensional arrays represent tensors.<\/li><\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/08\/what-is-numpy-1024x572.png\" alt=\"numpy\" class=\"wp-image-4542\" width=\"512\" height=\"286\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/08\/what-is-numpy-1024x572.png 1024w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/08\/what-is-numpy-300x168.png 300w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/08\/what-is-numpy-768x429.png 768w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/08\/what-is-numpy.png 1440w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/figure>\n<\/div>\n\n\n<p>Unlike the built-in <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-list\/\">list<\/a> type that can hold the elements of different types, the NumPy arrays allow only one data type for all elements. Therefore, we say that the NumPy array requires homogeneous data values.<\/p>\n\n\n\n<p>A NumPy array can contain either <a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-integers\/\">integer<\/a> or <a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-float\/\">float<\/a> numbers, but not both at the same time. This restriction allows Numpy to speed up the linear algebra calculations.<\/p>\n\n\n\n<p>NumPy supports basic operations such as average, minimum, maximum, standard deviation, variance, and many more. <\/p>\n\n\n\n<p>After mastering NumPy, you&#8217;ll have a powerful tool for data analysis on numerical multi-dimensional data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='what-is-numpy-for'>What is NumPy for <a href=\"#what-is-numpy-for\" class=\"anchor\" id=\"what-is-numpy-for\" title=\"Anchor for What is NumPy for\">#<\/a><\/h2>\n\n\n\n<p>NumPy is an important library for:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Data Science<\/li><li>Machine learning<\/li><li>Signal and image processing<\/li><li>Scientific and engineer computing<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='install-numpy'>Install NumPy <a href=\"#install-numpy\" class=\"anchor\" id=\"install-numpy\" title=\"Anchor for Install NumPy\">#<\/a><\/h2>\n\n\n\n<p>Since NumPy is a third-party package, you need to install it. To install NumPy, you use the following pip command:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">pip install numpy<\/code><\/span><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='import-numpy-as-np'>import NumPy as np <a href=\"#import-numpy-as-np\" class=\"anchor\" id=\"import-numpy-as-np\" title=\"Anchor for import NumPy as np\">#<\/a><\/h2>\n\n\n\n<p>Once NumPy is installed successfully, you can import it to your program like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">import<\/span> numpy<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>By convention, we use an alias for the NumPy library as <code>np<\/code>. Note you can use any aliases. But it&#8217;s a good practice to follow the community convention so that others can understand your code more quickly:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Once you reference the NumPy module, you can use its functions and classes like creating a new array.<\/p>\n\n\n\n<p>For example, the following creates a new NumPy array that contains three temperatures in celsius:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">tc = np.<span class=\"hljs-keyword\">array<\/span>(&#91;<span class=\"hljs-number\">25.5<\/span>, <span class=\"hljs-number\">28.1<\/span>, <span class=\"hljs-number\">30.6<\/span>])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To convert these temperatures from celsius to Kelvin, you use the following:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">tk =  tc * <span class=\"hljs-number\">9<\/span> \/ <span class=\"hljs-number\">5<\/span> + <span class=\"hljs-number\">32<\/span>\n<span class=\"hljs-keyword\">print<\/span>(tk)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JSON \/ JSON with Comments\" data-shcb-language-slug=\"json\"><span><code class=\"hljs language-json\">&#91;<span class=\"hljs-number\">77.9<\/span> <span class=\"hljs-number\">82.58<\/span> <span class=\"hljs-number\">87.08<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JSON \/ JSON with Comments<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">json<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>As you can see from the example, the calculation is much simpler than the following Python code: <\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">tk = &#91;c*<span class=\"hljs-number\">9<\/span>\/<span class=\"hljs-number\">5<\/span> + <span class=\"hljs-number\">32<\/span> <span class=\"hljs-keyword\">for<\/span> c in tc]\n<span class=\"hljs-keyword\">print<\/span>(tk)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"JSON \/ JSON with Comments\" data-shcb-language-slug=\"json\"><span><code class=\"hljs language-json\">&#91;<span class=\"hljs-number\">77.9<\/span>, <span class=\"hljs-number\">82.58<\/span>, <span class=\"hljs-number\">87.08000000000001<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JSON \/ JSON with Comments<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">json<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And the NumPy calculation&#8217;s speed is much faster.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>NumPy stands for Numerical Python. It&#8217;s a Python library for numerical calculation.<\/li><li>Use <code>np<\/code> as the alias for the NumPy module.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"3408\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-numpy\/what-is-numpy\/\"\n\t\t\t\tdata-post-title=\"What is NumPy\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"3408\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-numpy\/what-is-numpy\/\"\n\t\t\t\tdata-post-title=\"What is NumPy\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll understand NumPy and how it helps you perform calculations fast and efficiently.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3406,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3408","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3408","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=3408"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3408\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3406"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=3408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}