{"id":14755,"date":"2021-07-24T17:57:58","date_gmt":"2021-07-24T12:27:58","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=14755"},"modified":"2026-07-13T12:35:43","modified_gmt":"2026-07-13T07:05:43","slug":"numpy-magnitude","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/numpy-magnitude\/","title":{"rendered":"NumPy Magnitude: Vector Norms, Absolute Values, and Shape"},"content":{"rendered":"<p><strong>Quick answer:<\/strong> NumPy magnitude depends on the mathematical object. Use numpy.abs() for element-wise absolute values, numpy.linalg.norm() for a vector or selected axis, and an explicit axis and keepdims policy for matrices so the output shape matches the rest of the calculation.<\/p>\n<figure class=\"pythonpool-article-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-magnitude.png\" alt=\"Python Pool infographic showing NumPy vector magnitude norm absolute values axis shape and direction\" width=\"1536\" height=\"1024\" loading=\"lazy\" decoding=\"async\"><figcaption>Magnitude describes size, not direction: choose absolute values for elements and a norm with the correct axis for vectors or rows.<\/figcaption><\/figure>\n<p>Vector magnitude is the length of a vector. In NumPy, the most direct way to calculate it is <code>np.linalg.norm()<\/code>. For a vector such as <code>[3, 4]<\/code>, the magnitude is <code>5<\/code> because the length is the square root of <code>3^2 + 4^2<\/code>.<\/p>\n<p>Magnitude is used in geometry, machine learning, physics, similarity calculations, normalization, and graphics. The official <a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.linalg.norm.html\">numpy.linalg.norm documentation<\/a> covers vector and matrix norms, <a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.sqrt.html\">numpy.sqrt<\/a> covers square roots, and <a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.hypot.html\">numpy.hypot<\/a> covers two-dimensional Euclidean distance.<\/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-magnitude\/#Use_numpylinalgnorm\" >Use numpy.linalg.norm<\/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-magnitude\/#Calculate_Magnitude_Manually\" >Calculate Magnitude Manually<\/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-magnitude\/#Use_A_Dot_Product\" >Use A Dot Product<\/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-magnitude\/#Calculate_Row-Wise_Magnitudes\" >Calculate Row-Wise Magnitudes<\/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-magnitude\/#Use_numpyhypot_For_2D_Values\" >Use numpy.hypot For 2D 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-magnitude\/#Calculate_Complex_Magnitude\" >Calculate Complex Magnitude<\/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-magnitude\/#Practical_Guidance\" >Practical Guidance<\/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-magnitude\/#Choose_The_Right_Norm\" >Choose The Right Norm<\/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-magnitude\/#Use_Magnitude_For_Normalization\" >Use Magnitude For Normalization<\/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-magnitude\/#Distinguish_Absolute_Value_From_Norm\" >Distinguish Absolute Value From Norm<\/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-magnitude\/#Select_The_Correct_Norm\" >Select The Correct Norm<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.pythonpool.com\/numpy-magnitude\/#Make_Shape_Changes_Explicit\" >Make Shape Changes Explicit<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.pythonpool.com\/numpy-magnitude\/#Handle_Complex_And_Empty_Data\" >Handle Complex And Empty Data<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.pythonpool.com\/numpy-magnitude\/#Test_Values_And_Geometry\" >Test Values And Geometry<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/www.pythonpool.com\/numpy-magnitude\/#Frequently_Asked_Questions\" >Frequently Asked Questions<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/www.pythonpool.com\/numpy-magnitude\/#How_do_I_calculate_a_vector_magnitude_in_NumPy\" >How do I calculate a vector magnitude in NumPy?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/www.pythonpool.com\/numpy-magnitude\/#What_is_the_difference_between_abs_and_a_norm\" >What is the difference between abs and a norm?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/www.pythonpool.com\/numpy-magnitude\/#How_do_I_calculate_row_magnitudes\" >How do I calculate row magnitudes?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/www.pythonpool.com\/numpy-magnitude\/#Why_is_my_NumPy_magnitude_shape_unexpected\" >Why is my NumPy magnitude shape unexpected?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Use_numpylinalgnorm\"><\/span>Use numpy.linalg.norm<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>np.linalg.norm()<\/code> calculates the Euclidean norm of a one-dimensional array by default.<\/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\nvector = np.array([3, 4])\nmagnitude = np.linalg.norm(vector)\n\nprint(magnitude)\n<\/code><\/pre>\n<\/div>\n<p>The result is <code>5.0<\/code>. This is the standard approach when you want the length of a vector.<\/p>\n<p>Use <code>linalg.norm()<\/code> first unless you have a specific reason to spell out the formula manually.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Calculate_Magnitude_Manually\"><\/span>Calculate Magnitude Manually<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The manual formula squares each component, sums the squares, and takes the square root.<\/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\nvector = np.array([2, 3, 6])\nmagnitude = np.sqrt(np.sum(vector ** 2))\n\nprint(magnitude)\n<\/code><\/pre>\n<\/div>\n<p>This shows exactly what the Euclidean magnitude does. It is useful for learning, debugging, or explaining the calculation.<\/p>\n<p>For production code, the built-in norm function is usually clearer and handles more cases.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Use_A_Dot_Product\"><\/span>Use A Dot Product<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The dot product of a vector with itself is the sum of its squared components. Taking the square root gives the magnitude.<\/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\nvector = np.array([1, 2, 2])\nmagnitude = np.sqrt(np.dot(vector, vector))\n\nprint(magnitude)\n<\/code><\/pre>\n<\/div>\n<p>This returns <code>3.0<\/code>. The dot-product form is common in linear algebra explanations because it connects length to inner products.<\/p>\n<p>It is also easy to compare with formulas for cosine similarity and vector projection.<\/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\/magnitude-vector-b166.png\" alt=\"Python Pool infographic showing vector components, absolute values, length, and NumPy magnitude\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Vector values: Vector components, absolute values, length, and NumPy magnitude.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Calculate_Row-Wise_Magnitudes\"><\/span>Calculate Row-Wise Magnitudes<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>When you have a two-dimensional array, each row may represent one vector. Use the <code>axis<\/code> argument to calculate one magnitude per row.<\/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\nvectors = np.array([\n    [3, 4],\n    [5, 12],\n    [8, 15],\n])\n\nmagnitudes = np.linalg.norm(vectors, axis=1)\n\nprint(magnitudes)\n<\/code><\/pre>\n<\/div>\n<p>The output contains one magnitude for each row. This pattern is useful for batches of points, embeddings, and feature vectors.<\/p>\n<p>Choose the axis carefully. A wrong axis can calculate column lengths when you intended row lengths.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Use_numpyhypot_For_2D_Values\"><\/span>Use numpy.hypot For 2D Values<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>np.hypot(x, y)<\/code> calculates <code>sqrt(x*x + y*y)<\/code> for two components. It works with scalars and arrays.<\/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\nx = np.array([3, 5, 8])\ny = np.array([4, 12, 15])\n\nmagnitudes = np.hypot(x, y)\n\nprint(magnitudes)\n<\/code><\/pre>\n<\/div>\n<p>This is convenient for two-dimensional coordinates, pixel offsets, and geometry problems.<\/p>\n<p>For three or more dimensions, use <code>np.linalg.norm()<\/code> or the manual square-sum formula.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Calculate_Complex_Magnitude\"><\/span>Calculate Complex Magnitude<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>For complex numbers, magnitude is the distance from zero in the complex plane. NumPy&#8217;s absolute value handles that directly.<\/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([3 + 4j, 5 + 12j])\nmagnitudes = np.abs(values)\n\nprint(magnitudes)\n<\/code><\/pre>\n<\/div>\n<p>The results are <code>5<\/code> and <code>13<\/code>. This works because the magnitude of <code>a + bj<\/code> is <code>sqrt(a^2 + b^2)<\/code>.<\/p>\n<p>Use <code>np.abs()<\/code> for complex arrays and <code>np.linalg.norm()<\/code> for vector lengths.<\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/magnitude-norm-b166.png\" alt=\"Python Pool infographic mapping vector components through absolute value, squares, sum, and norm\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Compute norm: Vector components through absolute value, squares, sum, and norm.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Practical_Guidance\"><\/span>Practical Guidance<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Use <code>np.linalg.norm(vector)<\/code> for a single vector. Use <code>np.linalg.norm(array, axis=1)<\/code> for row-wise vector magnitudes. Use <code>np.hypot()<\/code> for two-dimensional component pairs.<\/p>\n<p>Before normalizing a vector, check that its magnitude is not zero. Dividing by zero produces invalid values and can break later numeric steps.<\/p>\n<p>Large arrays should use vectorized NumPy operations instead of Python loops. Vectorized operations are shorter, faster, and easier to verify.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Choose_The_Right_Norm\"><\/span>Choose The Right Norm<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The default norm for a one-dimensional array is the Euclidean norm, also called the L2 norm. It measures straight-line distance from the origin. This is the common choice for geometry, embeddings, and physical vectors.<\/p>\n<p>Other norms answer different questions. The L1 norm sums absolute component values, which can be useful in optimization and sparse-model discussions. The infinity norm reports the largest absolute component. NumPy supports these choices through the <code>ord<\/code> argument.<\/p>\n<p>Do not switch norms only because the result looks convenient. The norm should match the math meaning of the task. If your code says magnitude without qualification, readers usually expect the Euclidean norm.<\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/magnitude-shape-b166.png\" alt=\"Python Pool infographic comparing scalar magnitude, vector norm, matrix norms, axes, and output shape\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Shape semantics: Scalar magnitude, vector norm, matrix norms, axes, and output shape.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Use_Magnitude_For_Normalization\"><\/span>Use Magnitude For Normalization<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Normalization divides a vector by its magnitude to create a unit vector. A unit vector has length one but keeps the same direction. This is useful when direction matters more than raw size.<\/p>\n<p>Always guard against a zero vector before dividing. A zero vector has no direction, so a normalized version is not meaningful. In batch code, create a mask for nonzero magnitudes before division.<\/p>\n<p>Magnitude can also help detect outliers. If one vector has a much larger magnitude than the rest, check whether the source data uses a different scale or contains an input error.<\/p>\n<p>The practical default is to use <code>linalg.norm()<\/code> for clarity, and switch to manual formulas only when the formula itself is the point of the example.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Distinguish_Absolute_Value_From_Norm\"><\/span>Distinguish Absolute Value From Norm<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>np.abs(array) returns one magnitude per element and preserves the array shape. A vector norm combines elements into one length, such as the Euclidean square root of the sum of squares. Using one where the other is intended can produce plausible but incorrect results.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Select_The_Correct_Norm\"><\/span>Select The Correct Norm<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>numpy.linalg.norm(vector) gives the default Euclidean length for a vector. The ord argument selects alternatives such as L1 or infinity norms, while axis chooses whether rows, columns, or another dimension represents the vectors being measured.<\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/magnitude-check-b166.png\" alt=\"Python Pool infographic testing complex values, zero vectors, overflow, dtype, and empty arrays\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Magnitude checks: Complex values, zero vectors, overflow, dtype, and empty arrays.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Make_Shape_Changes_Explicit\"><\/span>Make Shape Changes Explicit<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Reducing an axis removes that dimension by default. Use keepdims=True when the result must broadcast back over the original array, and inspect array.shape before and after the call. Shape assertions are more reliable than relying on visual output.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Handle_Complex_And_Empty_Data\"><\/span>Handle Complex And Empty Data<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Magnitude for complex values is based on distance from zero and should use NumPy&#8217;s documented behavior. Decide how empty vectors, NaN values, integer overflow risk, and zero-length axes should be represented before they reach a downstream normalization step.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Test_Values_And_Geometry\"><\/span>Test Values And Geometry<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Test a unit vector, zero vector, negative values, a matrix with row and column reductions, complex values where relevant, and NaN or empty inputs according to the contract. Assert both numeric tolerance and output shape.<\/p>\n<p>The official <a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.linalg.norm.html\">numpy.linalg.norm reference<\/a> defines norms, ord, axis, and keepdims. The <a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.absolute.html\">numpy.absolute reference<\/a> covers element-wise absolute values. Related guidance includes <a href=\"https:\/\/www.pythonpool.com\/numpy-axis\/\">NumPy axes<\/a> and <a href=\"https:\/\/www.pythonpool.com\/python-testing-framework\/\">shape tests<\/a>.<\/p>\n<p>For related numerical shape decisions, compare <a href=\"https:\/\/www.pythonpool.com\/numpy-standard-deviation\/\">axis reductions<\/a>, <a href=\"https:\/\/www.pythonpool.com\/numpy-axis\/\">NumPy axes<\/a>, and <a href=\"https:\/\/www.pythonpool.com\/python-testing-framework\/\">numeric tests<\/a> when validating a magnitude result.<\/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><span class=\"ez-toc-section\" id=\"How_do_I_calculate_a_vector_magnitude_in_NumPy\"><\/span>How do I calculate a vector magnitude in NumPy?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Use numpy.linalg.norm(vector) for the default Euclidean norm, or supply ord and axis when the mathematical definition requires another norm or layout.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"What_is_the_difference_between_abs_and_a_norm\"><\/span>What is the difference between abs and a norm?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>absolute values operate element by element, while a norm reduces a vector or selected axis to one magnitude.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_do_I_calculate_row_magnitudes\"><\/span>How do I calculate row magnitudes?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Call numpy.linalg.norm(array, axis=1) when each row is a vector and the first dimension indexes observations.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Why_is_my_NumPy_magnitude_shape_unexpected\"><\/span>Why is my NumPy magnitude shape unexpected?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The selected axis and keepdims setting control which dimensions are reduced; inspect array.shape before and after the operation.<\/p>\n<p><script type=\"application\/ld+json\">{\"@context\":\"https:\/\/schema.org\",\"@type\":\"FAQPage\",\"mainEntity\":[{\"@type\":\"Question\",\"name\":\"How do I calculate a vector magnitude in NumPy?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Use numpy.linalg.norm(vector) for the default Euclidean norm, or supply ord and axis when the mathematical definition requires another norm or layout.\"}},{\"@type\":\"Question\",\"name\":\"What is the difference between abs and a norm?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"absolute values operate element by element, while a norm reduces a vector or selected axis to one magnitude.\"}},{\"@type\":\"Question\",\"name\":\"How do I calculate row magnitudes?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Call numpy.linalg.norm(array, axis=1) when each row is a vector and the first dimension indexes observations.\"}},{\"@type\":\"Question\",\"name\":\"Why is my NumPy magnitude shape unexpected?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"The selected axis and keepdims setting control which dimensions are reduced; inspect array.shape before and after the operation.\"}}]}<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Calculate NumPy magnitudes with absolute values and vector norms, choose the correct axis, preserve shapes, and avoid confusing magnitude with direction.<\/p>\n","protected":false},"author":22,"featured_media":33725,"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":[4361,4360,4359,4358],"class_list":["post-14755","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy","tag-numpy-magnitude","tag-numpy-magnitude-of-complex-number","tag-numpy-magnitude-of-vector","tag-numpy-vector-magnitude","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 Magnitude: Vector Norms, Absolute Values, and Shape<\/title>\n<meta name=\"description\" content=\"Calculate NumPy magnitudes with absolute values and vector norms, choose the correct axis, preserve shapes, and avoid confusing magnitude with direction.\" \/>\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-magnitude\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"NumPy Magnitude: Vector Norms, Absolute Values, and Shape\" \/>\n<meta property=\"og:description\" content=\"Calculate NumPy magnitudes with absolute values and vector norms, choose the correct axis, preserve shapes, and avoid confusing magnitude with direction.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/numpy-magnitude\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-24T12:27:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-07-13T07:05:43+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-magnitude.png\" \/>\n<meta name=\"author\" content=\"Priyalakshmi\" \/>\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-magnitude.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=\"Priyalakshmi\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-magnitude\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-magnitude\\\/\"},\"author\":{\"name\":\"Priyalakshmi\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/person\\\/83e6e22a6c461cc1a8445bb473db2f20\"},\"headline\":\"NumPy Magnitude: Vector Norms, Absolute Values, and Shape\",\"datePublished\":\"2021-07-24T12:27:58+00:00\",\"dateModified\":\"2026-07-13T07:05:43+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-magnitude\\\/\"},\"wordCount\":1146,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-magnitude\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/numpy-magnitude-vector-guide-pythonpool.png\",\"keywords\":[\"numpy magnitude\",\"numpy magnitude of complex number\",\"numpy magnitude of vector\",\"numpy vector magnitude\"],\"articleSection\":[\"Numpy\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.pythonpool.com\\\/numpy-magnitude\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-magnitude\\\/\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-magnitude\\\/\",\"name\":\"NumPy Magnitude: Vector Norms, Absolute Values, and Shape\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-magnitude\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-magnitude\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/numpy-magnitude-vector-guide-pythonpool.png\",\"datePublished\":\"2021-07-24T12:27:58+00:00\",\"dateModified\":\"2026-07-13T07:05:43+00:00\",\"description\":\"Calculate NumPy magnitudes with absolute values and vector norms, choose the correct axis, preserve shapes, and avoid confusing magnitude with direction.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-magnitude\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pythonpool.com\\\/numpy-magnitude\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-magnitude\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/numpy-magnitude-vector-guide-pythonpool.png\",\"contentUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/numpy-magnitude-vector-guide-pythonpool.png\",\"width\":1350,\"height\":650,\"caption\":\"Calculate vector magnitude with NumPy guide\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-magnitude\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pythonpool.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"NumPy Magnitude: Vector Norms, Absolute Values, and Shape\"}]},{\"@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\\\/83e6e22a6c461cc1a8445bb473db2f20\",\"name\":\"Priyalakshmi\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/8f1aadb3b77a89fe5776d41fb52aec63200d0e68e92c71cb9aafac7fb4456696?s=96&d=wavatar&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/8f1aadb3b77a89fe5776d41fb52aec63200d0e68e92c71cb9aafac7fb4456696?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/8f1aadb3b77a89fe5776d41fb52aec63200d0e68e92c71cb9aafac7fb4456696?s=96&d=wavatar&r=g\",\"caption\":\"Priyalakshmi\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"NumPy Magnitude: Vector Norms, Absolute Values, and Shape","description":"Calculate NumPy magnitudes with absolute values and vector norms, choose the correct axis, preserve shapes, and avoid confusing magnitude with direction.","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-magnitude\/","og_locale":"en_US","og_type":"article","og_title":"NumPy Magnitude: Vector Norms, Absolute Values, and Shape","og_description":"Calculate NumPy magnitudes with absolute values and vector norms, choose the correct axis, preserve shapes, and avoid confusing magnitude with direction.","og_url":"https:\/\/www.pythonpool.com\/numpy-magnitude\/","og_site_name":"Python Pool","article_published_time":"2021-07-24T12:27:58+00:00","article_modified_time":"2026-07-13T07:05:43+00:00","og_image":[{"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-magnitude.png","type":"","width":"","height":""}],"author":"Priyalakshmi","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-magnitude.png","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Priyalakshmi","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/numpy-magnitude\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/numpy-magnitude\/"},"author":{"name":"Priyalakshmi","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/83e6e22a6c461cc1a8445bb473db2f20"},"headline":"NumPy Magnitude: Vector Norms, Absolute Values, and Shape","datePublished":"2021-07-24T12:27:58+00:00","dateModified":"2026-07-13T07:05:43+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/numpy-magnitude\/"},"wordCount":1146,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/numpy-magnitude\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-magnitude-vector-guide-pythonpool.png","keywords":["numpy magnitude","numpy magnitude of complex number","numpy magnitude of vector","numpy vector magnitude"],"articleSection":["Numpy"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/numpy-magnitude\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pythonpool.com\/numpy-magnitude\/","url":"https:\/\/www.pythonpool.com\/numpy-magnitude\/","name":"NumPy Magnitude: Vector Norms, Absolute Values, and Shape","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/numpy-magnitude\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/numpy-magnitude\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-magnitude-vector-guide-pythonpool.png","datePublished":"2021-07-24T12:27:58+00:00","dateModified":"2026-07-13T07:05:43+00:00","description":"Calculate NumPy magnitudes with absolute values and vector norms, choose the correct axis, preserve shapes, and avoid confusing magnitude with direction.","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/numpy-magnitude\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/numpy-magnitude\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/numpy-magnitude\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-magnitude-vector-guide-pythonpool.png","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-magnitude-vector-guide-pythonpool.png","width":1350,"height":650,"caption":"Calculate vector magnitude with NumPy guide"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/numpy-magnitude\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"NumPy Magnitude: Vector Norms, Absolute Values, and Shape"}]},{"@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\/83e6e22a6c461cc1a8445bb473db2f20","name":"Priyalakshmi","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/8f1aadb3b77a89fe5776d41fb52aec63200d0e68e92c71cb9aafac7fb4456696?s=96&d=wavatar&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/8f1aadb3b77a89fe5776d41fb52aec63200d0e68e92c71cb9aafac7fb4456696?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/8f1aadb3b77a89fe5776d41fb52aec63200d0e68e92c71cb9aafac7fb4456696?s=96&d=wavatar&r=g","caption":"Priyalakshmi"}}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/14755","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\/22"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=14755"}],"version-history":[{"count":96,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/14755\/revisions"}],"predecessor-version":[{"id":41714,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/14755\/revisions\/41714"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/33725"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=14755"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=14755"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=14755"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}