{"id":5469,"date":"2020-11-19T17:50:57","date_gmt":"2020-11-19T12:20:57","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=5469"},"modified":"2026-07-13T12:28:52","modified_gmt":"2026-07-13T06:58:52","slug":"numpy-axis","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/numpy-axis\/","title":{"rendered":"NumPy Axis Explained: 0, 1, Negative Axes, and keepdims"},"content":{"rendered":"<p><strong>Quick answer:<\/strong> A NumPy axis identifies one dimension of an array. For a two-dimensional array, axis=0 reduces down rows and returns one value per column, while axis=1 reduces across columns and returns one value per row. Negative axes count from the end, and keepdims=True preserves a size-one dimension so the result can broadcast against the original array.<\/p>\n<figure class=\"pythonpool-article-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-axis.png\" alt=\"Python Pool infographic showing NumPy array shapes, axis 0 and 1 reductions, negative axes, and keepdims\" width=\"1536\" height=\"1024\" loading=\"lazy\" decoding=\"async\"><figcaption>An axis identifies a dimension of an array; reductions remove that dimension unless keepdims=True preserves a broadcastable size of one.<\/figcaption><\/figure>\n<p>A NumPy axis is a dimension of an array. Axes are numbered from left to right in the array shape, so axis 0 is the first dimension, axis 1 is the second dimension, and so on. <a href=\"https:\/\/www.pythonpool.com\/numpy-roll\/\">NumPy roll() Array Shift Guide<\/a> uses the axis parameter to shift values with wraparound along one selected dimension. Most array work should stay vectorized, but <a href=\"https:\/\/www.pythonpool.com\/numpy-nditer\/\">NumPy nditer() for Array Iteration<\/a> shows controlled multidimensional iteration when element-wise access is genuinely required.<\/p>\n<p>The official NumPy documentation defines <a href=\"https:\/\/numpy.org\/doc\/stable\/glossary.html#term-axis\">axis<\/a> in the glossary and documents axis parameters for functions such as <a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.sum.html\">numpy.sum()<\/a> and <a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.expand_dims.html\">numpy.expand_dims()<\/a>.<\/p>\n<p>The easiest way to understand axes is to look at <code>array.shape<\/code>. For a 2D array with shape <code>(3, 4)<\/code>, axis 0 has length 3 and axis 1 has length 4. Many NumPy operations reduce, move, or add one of those dimensions.<\/p>\n<p>When a function reduces an axis, that axis is the direction being collapsed. For example, <code>sum(axis=0)<\/code> collapses rows and leaves one value for each column. <code>sum(axis=1)<\/code> collapses columns and leaves one value for each row.<\/p>\n<p>That language can feel backward at first because the result is described by what remains. A good habit is to say: \u201caxis equals the dimension being operated over.\u201d Then inspect the output shape to confirm the result.<\/p>\n<p>Negative axes count from the end. Axis <code>-1<\/code> means the last dimension, which is often the most convenient way to work with the final coordinate of arrays that may have different ranks.<\/p>\n<p>There is no universal \u201chorizontal axis\u201d or \u201cvertical axis\u201d rule that works for every array. The right interpretation depends on the array shape and the operation. For 2D arrays, axis 0 often lines up with rows and axis 1 often lines up with columns, but the reliable source is still the shape tuple. For a 2D array, <a href=\"https:\/\/www.pythonpool.com\/numpy-fliplr\/\">NumPy fliplr: Flip Arrays Left to Right<\/a> applies the axis concept by reversing columns while preserving row order.<\/p>\n<p>When debugging axis code, print both the input shape and output shape. If the dimension you expected to collapse is still present, the axis value is probably wrong or <code>keepdims=True<\/code> is preserving it intentionally.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #990303;color:#990303\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #990303;color:#990303\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pythonpool.com\/numpy-axis\/#Check_Axis_From_Shape\" >Check Axis From Shape<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.pythonpool.com\/numpy-axis\/#Sum_Along_Axis_0\" >Sum Along Axis 0<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/numpy-axis\/#Sum_Along_Axis_1\" >Sum Along Axis 1<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/numpy-axis\/#Use_Axes_With_3D_Arrays\" >Use Axes With 3D Arrays<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/numpy-axis\/#Use_Negative_Axis_Values\" >Use Negative Axis Values<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/numpy-axis\/#Keep_Dimensions_After_Reduction\" >Keep Dimensions After Reduction<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/numpy-axis\/#Read_The_Shape_First\" >Read The Shape First<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.pythonpool.com\/numpy-axis\/#Use_Negative_Axes\" >Use Negative Axes<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.pythonpool.com\/numpy-axis\/#Preserve_Dimensions_With_keepdims\" >Preserve Dimensions With keepdims<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.pythonpool.com\/numpy-axis\/#Validate_Axis_Inputs\" >Validate Axis Inputs<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.pythonpool.com\/numpy-axis\/#Frequently_Asked_Questions\" >Frequently Asked Questions<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Check_Axis_From_Shape\"><\/span>Check Axis From Shape<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The shape tuple shows the length of each axis.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">import numpy as np\n\ndata = np.array([\n    [1, 2, 3, 4],\n    [5, 6, 7, 8],\n    [9, 10, 11, 12],\n])\n\nprint(data.shape)\n<\/code><\/pre>\n<\/div>\n<p>The shape is <code>(3, 4)<\/code>.<\/p>\n<p>Axis 0 has length 3 because there are three rows.<\/p>\n<p>Axis 1 has length 4 because there are four columns.<\/p>\n<p>For higher-dimensional arrays, keep reading the shape tuple from left to right.<\/p>\n<p>This shape-first approach also works after reshaping. If a reshape changes <code>(12,)<\/code> into <code>(3, 4)<\/code>, the new array has two axes even though the values came from a one-dimensional sequence.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Sum_Along_Axis_0\"><\/span>Sum Along Axis 0<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>axis=0<\/code> collapses the first dimension.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">import numpy as np\n\ndata = np.array([\n    [1, 2, 3],\n    [4, 5, 6],\n])\n\nresult = np.sum(data, axis=0)\n\nprint(result)\n<\/code><\/pre>\n<\/div>\n<p>The result contains one sum for each column.<\/p>\n<p>Rows were collapsed because axis 0 was the dimension being reduced.<\/p>\n<p>The output shape is <code>(3,)<\/code>, matching the column count from the original array.<\/p>\n<p>Another way to say it: each column contributes a result, because rows were combined along axis 0.<\/p>\n<p><!-- Python Pool visual layout repair 2026-07-13 --><\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/axis-dimensions-b160.png\" alt=\"Python Pool infographic showing a two-dimensional NumPy array, rows, columns, and axes\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Array dimensions: A two-dimensional NumPy array, rows, columns, and axes.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Sum_Along_Axis_1\"><\/span>Sum Along Axis 1<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>axis=1<\/code> collapses the second dimension.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">import numpy as np\n\ndata = np.array([\n    [1, 2, 3],\n    [4, 5, 6],\n])\n\nresult = np.sum(data, axis=1)\n\nprint(result)\n<\/code><\/pre>\n<\/div>\n<p>The result contains one sum for each row.<\/p>\n<p>Columns were collapsed because axis 1 was the dimension being reduced.<\/p>\n<p>The output shape is <code>(2,)<\/code>, matching the row count from the original array.<\/p>\n<p>This is the row-sum pattern many readers expect from spreadsheets: each row becomes one total.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Use_Axes_With_3D_Arrays\"><\/span>Use Axes With 3D Arrays<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>A 3D array has axes 0, 1, and 2.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">import numpy as np\n\ndata = np.arange(24).reshape(2, 3, 4)\n\nprint(data.shape)\nprint(np.sum(data, axis=2).shape)\n<\/code><\/pre>\n<\/div>\n<p>The original shape is <code>(2, 3, 4)<\/code>.<\/p>\n<p>Reducing axis 2 collapses the last dimension and returns shape <code>(2, 3)<\/code>.<\/p>\n<p>This is the same rule as 2D arrays: the selected axis is the one removed by the reduction.<\/p>\n<p>For a 3D array, axis 0 might represent batches, axis 1 might represent rows, and axis 2 might represent columns. The names depend on your data model, but the axis numbers still follow the shape order.<\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/axis-reduce-b160.png\" alt=\"Python Pool infographic mapping a reduction across axis zero, axis one, and the full array\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Reduce axis: A reduction across axis zero, axis one, and the full array.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Use_Negative_Axis_Values\"><\/span>Use Negative Axis Values<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Negative axes count backward from the end.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">import numpy as np\n\ndata = np.arange(24).reshape(2, 3, 4)\n\nlast_axis_sum = np.sum(data, axis=-1)\n\nprint(last_axis_sum.shape)\n<\/code><\/pre>\n<\/div>\n<p>Axis <code>-1<\/code> is the last axis, so this example also returns shape <code>(2, 3)<\/code>.<\/p>\n<p>Negative axes are helpful when you care about the last dimension but do not want to hard-code the exact number of dimensions.<\/p>\n<p>Use them carefully in tutorials so readers can still connect the value back to the shape tuple.<\/p>\n<p>In libraries and reusable functions, negative axes can make code more flexible. For example, reducing the last axis can work for both 2D and 3D input as long as the last dimension has the same meaning.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Keep_Dimensions_After_Reduction\"><\/span>Keep Dimensions After Reduction<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>keepdims=True<\/code> leaves the reduced axis in the result with length 1.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">import numpy as np\n\ndata = np.array([\n    [1, 2, 3],\n    [4, 5, 6],\n])\n\nresult = np.sum(data, axis=1, keepdims=True)\n\nprint(result)\nprint(result.shape)\n<\/code><\/pre>\n<\/div>\n<p>This keeps the result two-dimensional with shape <code>(2, 1)<\/code>.<\/p>\n<p>Keeping the dimension can make later broadcasting steps easier.<\/p>\n<p>Without <code>keepdims<\/code>, the reduced dimension disappears. With <code>keepdims<\/code>, that dimension stays in place with length 1, so the result can often broadcast back against the original array.<\/p>\n<p>In short, read axes from the shape tuple, remember that the chosen axis is the dimension being operated over, use negative axes for end-relative indexing, and use <code>keepdims=True<\/code> when preserving rank helps the next operation.<\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/axis-negative-b160.png\" alt=\"Python Pool infographic comparing axis -1, axis -2, rank, and equivalent positive axes\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Negative axes: Axis -1, axis -2, rank, and equivalent positive axes.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Read_The_Shape_First\"><\/span>Read The Shape First<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Axis numbers are positions in the shape tuple, not labels such as x or y. Print the shape and ndim before choosing an axis, especially when code accepts arrays with different ranks.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">import numpy as np\n\nvalues = np.array([[1, 2, 3], [4, 5, 6]])\nprint(values.shape)\nprint(values.ndim)\nprint(values.sum(axis=0))\nprint(values.sum(axis=1))\n<\/code><\/pre>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Use_Negative_Axes\"><\/span>Use Negative Axes<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>axis=-1 means the final dimension and axis=-2 means the dimension before it. This is useful for code that should work for batches with extra leading dimensions.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">import numpy as np\n\nvalues = np.ones((2, 3, 4))\nprint(values.sum(axis=-1).shape)\nprint(values.sum(axis=-2).shape)\n<\/code><\/pre>\n<\/div>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/axis-check-b160.png\" alt=\"Python Pool infographic testing keepdims, shape, broadcasting, one-dimensional input, and errors\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Axis checks: Keepdims, shape, broadcasting, one-dimensional input, and errors.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Preserve_Dimensions_With_keepdims\"><\/span>Preserve Dimensions With keepdims<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>A reduction normally removes the reduced axis. keepdims leaves it at length one, which makes subtraction or division against the original array broadcast in the intended direction.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">import numpy as np\n\nvalues = np.array([[1.0, 2.0], [3.0, 4.0]])\nrow_totals = values.sum(axis=1, keepdims=True)\nnormalized = values \/ row_totals\nprint(row_totals.shape)\nprint(normalized)\n<\/code><\/pre>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Validate_Axis_Inputs\"><\/span>Validate Axis Inputs<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>An axis outside the array rank raises an error. A helper can expose a clear message and keep the reduction operation close to the shape contract rather than hiding the mistake in a later broadcast failure.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">import numpy as np\n\n\ndef mean_along(values, axis):\n    array = np.asarray(values)\n    if not -array.ndim &lt;= axis &lt; array.ndim:\n        raise ValueError(\"axis is outside the array rank\")\n    return array.mean(axis=axis)\n\nprint(mean_along([[1, 2], [3, 4]], 0))\n<\/code><\/pre>\n<\/div>\n<p>NumPy&#8217;s <a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.sum.html\">reduction documentation<\/a> explains axis and keepdims behavior. Related references include <a href=\"https:\/\/www.pythonpool.com\/numpy-amin\/\">minimum reductions<\/a>, <a href=\"https:\/\/www.pythonpool.com\/numpy-conjugate\/\">array operations<\/a>, and <a href=\"https:\/\/www.pythonpool.com\/numpy-reshape-3d-to-2d\/\">reshape and rank changes<\/a>.<\/p>\n<p>For related array reductions, compare <a href=\"https:\/\/www.pythonpool.com\/numpy-amin\/\">minimum reductions<\/a>, <a href=\"https:\/\/www.pythonpool.com\/numpy-conjugate\/\">array operations<\/a>, and <a href=\"https:\/\/www.pythonpool.com\/numpy-reshape-3d-to-2d\/\">reshape and rank changes<\/a> when selecting an axis.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Frequently_Asked_Questions\"><\/span>Frequently Asked Questions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3>What does axis 0 mean in NumPy?<\/h3>\n<p>For a two-dimensional array, axis 0 reduces down rows and returns one result per column.<\/p>\n<h3>What does axis 1 mean?<\/h3>\n<p>Axis 1 reduces across columns and returns one result per row.<\/p>\n<h3>Why use a negative axis?<\/h3>\n<p>Negative axes count from the last dimension, so axis=-1 means the final dimension regardless of array rank.<\/p>\n<h3>What does keepdims do?<\/h3>\n<p>It leaves the reduced dimension at size one so the result can broadcast against the original array.<\/p>\n<p><script type=\"application\/ld+json\">{\"@context\":\"https:\/\/schema.org\",\"@type\":\"FAQPage\",\"mainEntity\":[{\"@type\":\"Question\",\"name\":\"What does axis 0 mean in NumPy?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"For a two-dimensional array, axis 0 reduces down rows and returns one result per column.\"}},{\"@type\":\"Question\",\"name\":\"What does axis 1 mean?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Axis 1 reduces across columns and returns one result per row.\"}},{\"@type\":\"Question\",\"name\":\"Why use a negative axis?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Negative axes count from the last dimension, so axis=-1 means the final dimension regardless of array rank.\"}},{\"@type\":\"Question\",\"name\":\"What does keepdims do?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"It leaves the reduced dimension at size one so the result can broadcast against the original array.\"}}]}<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Understand NumPy axes with shapes, axis 0 and 1 reductions, negative axes, keepdims, and examples that prevent dimension mistakes.<\/p>\n","protected":false},"author":1,"featured_media":33984,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1495],"tags":[2527,2528,2531,2529,1883,2530],"class_list":["post-5469","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy","tag-numpy-apply-along-axis","tag-numpy-array-axis","tag-numpy-axis","tag-numpy-axis-0","tag-numpy-mean-axis","tag-numpy-sum-axis","infinite-scroll-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v28.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>NumPy Axis Explained: 0, 1, Negative Axes, and keepdims<\/title>\n<meta name=\"description\" content=\"Understand NumPy axes with shapes, axis 0 and 1 reductions, negative axes, keepdims, and examples that prevent dimension mistakes.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pythonpool.com\/numpy-axis\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"NumPy Axis Explained: 0, 1, Negative Axes, and keepdims\" \/>\n<meta property=\"og:description\" content=\"Understand NumPy axes with shapes, axis 0 and 1 reductions, negative axes, keepdims, and examples that prevent dimension mistakes.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/numpy-axis\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2020-11-19T12:20:57+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-07-13T06:58:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-axis.png\" \/>\n<meta name=\"author\" content=\"Python Pool\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"Python Pool\" \/>\n<meta name=\"twitter:description\" content=\"Practical Python tutorials, error fixes, code examples, and project guides.\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-axis.png\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:site\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Python Pool\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-axis\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-axis\\\/\"},\"author\":{\"name\":\"Python Pool\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/person\\\/f87448ee54c0ffd2889fbf2408c18998\"},\"headline\":\"NumPy Axis Explained: 0, 1, Negative Axes, and keepdims\",\"datePublished\":\"2020-11-19T12:20:57+00:00\",\"dateModified\":\"2026-07-13T06:58:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-axis\\\/\"},\"wordCount\":1159,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-axis\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/numpy-axis-array-dimensions-guide-pythonpool.png\",\"keywords\":[\"numpy apply along axis\",\"numpy array axis\",\"numpy axis\",\"numpy axis 0\",\"numpy mean axis\",\"numpy sum axis\"],\"articleSection\":[\"Numpy\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.pythonpool.com\\\/numpy-axis\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-axis\\\/\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-axis\\\/\",\"name\":\"NumPy Axis Explained: 0, 1, Negative Axes, and keepdims\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-axis\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-axis\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/numpy-axis-array-dimensions-guide-pythonpool.png\",\"datePublished\":\"2020-11-19T12:20:57+00:00\",\"dateModified\":\"2026-07-13T06:58:52+00:00\",\"description\":\"Understand NumPy axes with shapes, axis 0 and 1 reductions, negative axes, keepdims, and examples that prevent dimension mistakes.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-axis\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pythonpool.com\\\/numpy-axis\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-axis\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/numpy-axis-array-dimensions-guide-pythonpool.png\",\"contentUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/numpy-axis-array-dimensions-guide-pythonpool.png\",\"width\":1350,\"height\":650,\"caption\":\"NumPy axis array dimensions guide\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-axis\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pythonpool.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"NumPy Axis Explained: 0, 1, Negative Axes, and keepdims\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/\",\"name\":\"Python Pool\",\"description\":\"Practical Python tutorials, error fixes, code examples, and project guides.\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pythonpool.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#organization\",\"name\":\"Python Pool\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/aa.png\",\"contentUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/aa.png\",\"width\":452,\"height\":185,\"caption\":\"Python Pool\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/x.com\\\/pythonpool\",\"https:\\\/\\\/www.youtube.com\\\/c\\\/pythonpool\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/person\\\/f87448ee54c0ffd2889fbf2408c18998\",\"name\":\"Python Pool\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/fdd3cb9ad7f560324dfd481989550aa8ffce84388fd253c42beca35c999d3108?s=96&d=wavatar&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/fdd3cb9ad7f560324dfd481989550aa8ffce84388fd253c42beca35c999d3108?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/fdd3cb9ad7f560324dfd481989550aa8ffce84388fd253c42beca35c999d3108?s=96&d=wavatar&r=g\",\"caption\":\"Python Pool\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"NumPy Axis Explained: 0, 1, Negative Axes, and keepdims","description":"Understand NumPy axes with shapes, axis 0 and 1 reductions, negative axes, keepdims, and examples that prevent dimension mistakes.","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:\/\/www.pythonpool.com\/numpy-axis\/","og_locale":"en_US","og_type":"article","og_title":"NumPy Axis Explained: 0, 1, Negative Axes, and keepdims","og_description":"Understand NumPy axes with shapes, axis 0 and 1 reductions, negative axes, keepdims, and examples that prevent dimension mistakes.","og_url":"https:\/\/www.pythonpool.com\/numpy-axis\/","og_site_name":"Python Pool","article_published_time":"2020-11-19T12:20:57+00:00","article_modified_time":"2026-07-13T06:58:52+00:00","og_image":[{"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-axis.png","type":"","width":"","height":""}],"author":"Python Pool","twitter_card":"summary_large_image","twitter_title":"Python Pool","twitter_description":"Practical Python tutorials, error fixes, code examples, and project guides.","twitter_image":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-axis.png","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Python Pool","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/numpy-axis\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/numpy-axis\/"},"author":{"name":"Python Pool","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/f87448ee54c0ffd2889fbf2408c18998"},"headline":"NumPy Axis Explained: 0, 1, Negative Axes, and keepdims","datePublished":"2020-11-19T12:20:57+00:00","dateModified":"2026-07-13T06:58:52+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/numpy-axis\/"},"wordCount":1159,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/numpy-axis\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-axis-array-dimensions-guide-pythonpool.png","keywords":["numpy apply along axis","numpy array axis","numpy axis","numpy axis 0","numpy mean axis","numpy sum axis"],"articleSection":["Numpy"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/numpy-axis\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pythonpool.com\/numpy-axis\/","url":"https:\/\/www.pythonpool.com\/numpy-axis\/","name":"NumPy Axis Explained: 0, 1, Negative Axes, and keepdims","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/numpy-axis\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/numpy-axis\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-axis-array-dimensions-guide-pythonpool.png","datePublished":"2020-11-19T12:20:57+00:00","dateModified":"2026-07-13T06:58:52+00:00","description":"Understand NumPy axes with shapes, axis 0 and 1 reductions, negative axes, keepdims, and examples that prevent dimension mistakes.","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/numpy-axis\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/numpy-axis\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/numpy-axis\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-axis-array-dimensions-guide-pythonpool.png","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-axis-array-dimensions-guide-pythonpool.png","width":1350,"height":650,"caption":"NumPy axis array dimensions guide"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/numpy-axis\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"NumPy Axis Explained: 0, 1, Negative Axes, and keepdims"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Practical Python tutorials, error fixes, code examples, and project guides.","publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonpool.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonpool.com\/#organization","name":"Python Pool","url":"https:\/\/www.pythonpool.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","width":452,"height":185,"caption":"Python Pool"},"image":{"@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/pythonpool","https:\/\/www.youtube.com\/c\/pythonpool"]},{"@type":"Person","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/f87448ee54c0ffd2889fbf2408c18998","name":"Python Pool","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/fdd3cb9ad7f560324dfd481989550aa8ffce84388fd253c42beca35c999d3108?s=96&d=wavatar&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/fdd3cb9ad7f560324dfd481989550aa8ffce84388fd253c42beca35c999d3108?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/fdd3cb9ad7f560324dfd481989550aa8ffce84388fd253c42beca35c999d3108?s=96&d=wavatar&r=g","caption":"Python Pool"}}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/5469","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=5469"}],"version-history":[{"count":45,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/5469\/revisions"}],"predecessor-version":[{"id":41454,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/5469\/revisions\/41454"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/33984"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=5469"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=5469"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=5469"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}