{"id":4323,"date":"2022-08-09T10:16:20","date_gmt":"2022-08-09T10:16:20","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=4323"},"modified":"2022-08-10T04:11:43","modified_gmt":"2022-08-10T04:11:43","slug":"numpy-std","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-numpy\/numpy-std\/","title":{"rendered":"NumPy std()"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the numpy <code>std()<\/code> function to calculate the standard deviation.<\/p>\n\n\n\n<p>Standard deviation measures how spread out the elements of an array is. The more spread out elements is, the greater their standard deviation.<\/p>\n\n\n\n<p>Standard deviation is the square root of the variance. To calculate the variance, check out the numpy <a href=\"https:\/\/www.pythontutorial.net\/python-numpy\/numpy-var\/\">var() function tutorial<\/a>.<\/p>\n\n\n\n<p>To calculate standard deviation, you can use the numpy <code>std()<\/code> function as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">numpy.std(a, axis=<span class=\"hljs-literal\">None<\/span>, dtype=<span class=\"hljs-literal\">None<\/span>, out=<span class=\"hljs-literal\">None<\/span>, ddof=<span class=\"hljs-number\">0<\/span>, keepdims=&lt;no value&gt;, *, where=&lt;no value&gt;)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>std()<\/code> function has many parameters but we&#8217;ll focus on only the first one in this tutorial.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='numpy-std-function-example'>NumPy std() function example <a href=\"#numpy-std-function-example\" class=\"anchor\" id=\"numpy-std-function-example\" title=\"Anchor for NumPy std() function example\">#<\/a><\/h2>\n\n\n\n<p>Suppose you have a list of trees with the broadest crown. The first column displays the tree name and the second column shows its corresponding diameter in feet:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Tree Name<\/th><th>Diameter (Feet)<\/th><\/tr><\/thead><tbody><tr><td>Thimmamma Marrimanu<\/td><td>591<\/td><\/tr><tr><td>Monkira Monster<\/td><td>239<\/td><\/tr><tr><td>Oriental Plane Tree at Corsham Court<\/td><td>210<\/td><\/tr><tr><td>Saman de Guere<\/td><td>207<\/td><\/tr><tr><td>The Big Tree<\/td><td>201<\/td><\/tr><tr><td>Shugborough Yew<\/td><td>182<\/td><\/tr><tr><td>Moreton Bay Fig Tree<\/td><td>176<\/td><\/tr><tr><td>The Pechanga Great Oak<\/td><td>176<\/td><\/tr><tr><td>El Gigante<\/td><td>175<\/td><\/tr><tr><td>Benaroon<\/td><td>170<\/td><\/tr><tr><td>The E. O. Hunt Oak<\/td><td>170<\/td><\/tr><tr><td>The Lansdowne Sycamore<\/td><td>169<\/td><\/tr><tr><td>The Glencoe Tree<\/td><td>168<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The following example uses the <code>std()<\/code> function to calculate the standard deviation of the diameters of the above trees:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\n\ndiameters = np.array(&#91;<span class=\"hljs-number\">591<\/span>, <span class=\"hljs-number\">239<\/span>, <span class=\"hljs-number\">210<\/span>, <span class=\"hljs-number\">207<\/span>, <span class=\"hljs-number\">201<\/span>, <span class=\"hljs-number\">182<\/span>,\n                      <span class=\"hljs-number\">176<\/span>, <span class=\"hljs-number\">176<\/span>, <span class=\"hljs-number\">175<\/span>, <span class=\"hljs-number\">170<\/span>, <span class=\"hljs-number\">170<\/span>, <span class=\"hljs-number\">169<\/span>, <span class=\"hljs-number\">168<\/span>, ])\nresult = np.std(diameters)\nprint(round(result, <span class=\"hljs-number\">1<\/span>))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/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-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-number\">109.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\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, create an array that holds the diameters of trees:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">diameters = np.array(&#91;<span class=\"hljs-number\">591<\/span>, <span class=\"hljs-number\">239<\/span>, <span class=\"hljs-number\">210<\/span>, <span class=\"hljs-number\">207<\/span>, <span class=\"hljs-number\">201<\/span>, <span class=\"hljs-number\">182<\/span>,\n                      <span class=\"hljs-number\">176<\/span>, <span class=\"hljs-number\">176<\/span>, <span class=\"hljs-number\">175<\/span>, <span class=\"hljs-number\">170<\/span>, <span class=\"hljs-number\">170<\/span>, <span class=\"hljs-number\">169<\/span>, <span class=\"hljs-number\">168<\/span>, ])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, calculate the standard deviation of diameters using the <code>std()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">result = np.std(diameters)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, round the standard deviation and display it:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">print(round(result, <span class=\"hljs-number\">1<\/span>))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>By using the standard deviation, we have a &#8220;standard&#8221; way of knowing which trees have a normal diameter, and which trees have large or small diameters.<\/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>Use the numpy <code>std()<\/code> function to calculate the standard deviation.<\/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=\"4323\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-numpy\/numpy-std\/\"\n\t\t\t\tdata-post-title=\"NumPy std()\"\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=\"4323\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-numpy\/numpy-std\/\"\n\t\t\t\tdata-post-title=\"NumPy std()\"\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>Summary: in this tutorial, you&#8217;ll learn how to use the numpy std() function to calculate the standard deviation. Standard deviation measures how spread out the elements of an array is. The more spread out elements is, the greater their standard deviation. Standard deviation is the square root of the variance. To calculate the variance, check [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3406,"menu_order":14,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-4323","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4323","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=4323"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4323\/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=4323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}