{"id":878,"date":"2022-10-25T16:32:00","date_gmt":"2022-10-25T11:02:00","guid":{"rendered":"https:\/\/geekpython.in\/?p=878"},"modified":"2023-08-15T16:04:01","modified_gmt":"2023-08-15T10:34:01","slug":"argmax-function-in-numpy-and-tensorflow","status":"publish","type":"post","link":"https:\/\/geekpython.in\/argmax-function-in-numpy-and-tensorflow","title":{"rendered":"argmax() Function in NumPy and TensorFlow &#8211; Are They Same"},"content":{"rendered":"\n<p><strong>Numpy<\/strong>&nbsp;is a Python library used for working with arrays. It provides multi-dimensional array objects, masked arrays, and matrices. To work with these arrays, NumPy provides some methods to carry out operations like linear algebra, statistical operations, shape manipulation, mathematical and logical operations, and much more.<\/p>\n\n\n\n<p>The array object in NumPy is called&nbsp;<code>ndarray<\/code>, and dimensions are called&nbsp;<code>axes<\/code>.<\/p>\n\n\n\n<p>Whereas&nbsp;<strong>TensorFlow<\/strong>&nbsp;is an open-source library developed by Google primarily to build deep learning and machine learning neural networks.<\/p>\n\n\n\n<p>However, TensorFlow has some modules and methods to handle arrays or tensors in TensorFlow&#8217;s language.<\/p>\n\n\n\n<p>In this article, we will see the&nbsp;<code>argmax()<\/code>&nbsp;function in both NumPy and TensorFlow, and ultimately we&#8217;ll discover if they provide the same functionality to handle arrays.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-definition\"><a href=\"https:\/\/geekpython.in\/argmax-function-in-numpy-and-tensorflow#heading-definition\"><\/a>Definition<\/h2>\n\n\n\n<p>First, we&#8217;ll decipher the definition of the&nbsp;<code>argmax()<\/code>&nbsp;function provided in the docs of both the libraries NumPy and TensorFlow.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-numpyargmax\"><a href=\"https:\/\/geekpython.in\/argmax-function-in-numpy-and-tensorflow#heading-numpyargmax\"><\/a>numpy.argmax<\/h3>\n\n\n\n<p>NumPy provides a function called&nbsp;<strong><em>argmax()<\/em><\/strong>&nbsp;that&nbsp;<strong>returns the indices of the maximum element of the array along an axis<\/strong>.<\/p>\n\n\n\n<p><code>numpy.argmax()<\/code>&nbsp;can be used to get the indices of the maximum values from the 1-dimensional and multi-dimensional arrays.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-tensorflowmathargmax-or-tensorflowargmax\"><a href=\"https:\/\/geekpython.in\/argmax-function-in-numpy-and-tensorflow#heading-tensorflowmathargmax-or-tensorflowargmax\"><\/a>tensorflow.math.argmax or tensorflow.argmax<\/h3>\n\n\n\n<p>TensorFlow has a module named&nbsp;<code>math<\/code>&nbsp;that provides the&nbsp;<code>argmax()<\/code>&nbsp;function, or we can call the&nbsp;<strong><em>argmax()<\/em><\/strong>&nbsp;function directly from the TensorFlow is also used to&nbsp;<strong>return the index of the largest value across the axes of the tensor or array<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-syntax\"><a href=\"https:\/\/geekpython.in\/argmax-function-in-numpy-and-tensorflow#heading-syntax\"><\/a>Syntax<\/h2>\n\n\n\n<p>Let&#8217;s look at the syntax since they both are used for similar actions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-numpyargmax\"><a href=\"https:\/\/geekpython.in\/argmax-function-in-numpy-and-tensorflow#heading-numpyargmax\"><\/a>numpy.argmax<\/h3>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><code>numpy.argmax(a, axis=None, out=None)<\/code><\/p>\n<\/blockquote>\n\n\n\n<p>Parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>a<\/code>&nbsp;&#8211; The array you will work on<\/li>\n\n\n\n<li><code>axis<\/code>&nbsp;&#8211; It is optional. We can specify an axis like 0 or 1 to find the maximum value index horizontally or vertically.<\/li>\n\n\n\n<li><code>out<\/code>&nbsp;&#8211; By default, it is None. It provides a feature to insert the output to the out area, but the array should be of appropriate shape and dtype.<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Return value<\/p>\n\n\n\n<p>An array is returned with the indices of max values with the same shape as&nbsp;<code>a.shape<\/code>&nbsp;with the dimension along the&nbsp;<strong><em>axis<\/em><\/strong>&nbsp;removed.<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-tensorflowargmax\"><a href=\"https:\/\/geekpython.in\/argmax-function-in-numpy-and-tensorflow#heading-tensorflowargmax\"><\/a>tensorflow.argmax<\/h3>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><code>tensorflow.argmax( input, axis=None, output_type=tf.dtypes.int64, name=None )<\/code><\/p>\n<\/blockquote>\n\n\n\n<p>Parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>input<\/code>&nbsp;&#8211; It is a tensor from which we&#8217;ll get the index of the highest values.<\/li>\n\n\n\n<li><code>axis<\/code>&nbsp;&#8211; It is for specifying the axes to reduce the tensor. In the case of vector, we always use&nbsp;<code>axis=0<\/code>.<\/li>\n\n\n\n<li><code>output_type<\/code>&nbsp;&#8211; It defines the dtype in which the returned result should be. The default value is&nbsp;<strong>int64<\/strong>.<\/li>\n\n\n\n<li><code>name<\/code>&nbsp;&#8211; By default it is None. It is used to specify a name for the operation.<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Return value<\/p>\n\n\n\n<p>A tensor is returned with the indices of the highest values along the axes with the type of&nbsp;<strong><em>output_type<\/em><\/strong>.<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-code-example\"><a href=\"https:\/\/geekpython.in\/argmax-function-in-numpy-and-tensorflow#heading-code-example\"><\/a>Code Example<\/h2>\n\n\n\n<p>Let&#8217;s see some examples.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-numpyargmax\"><a href=\"https:\/\/geekpython.in\/argmax-function-in-numpy-and-tensorflow#heading-numpyargmax\"><\/a>numpy.argmax<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Importing numpy as np\nimport numpy as np\n\n# Creating a 2D array of shape 3, 6\narr = np.arange(18).reshape(3, 6)\n\n# Printing out an input array\nprint(\"INPUT ARRAY: \\n\", arr)\n\n# Applying argmax to the input array\nmax_elem_index = np.argmax(arr)\n\n# Printing the max element from the input array\nprint(\"\\nMAX ELEMENT INDEX:\", max_elem_index)<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">INPUT ARRAY: \n [[ 0  1  2  3  4  5]\n [ 6  7  8  9 10 11]\n [12 13 14 15 16 17]]\n\nMAX ELEMENT INDEX: 17<\/pre><\/div>\n\n\n\n<p>Since we didn&#8217;t specify the&nbsp;<strong><em>axis<\/em><\/strong>&nbsp;argument in the&nbsp;<strong><em>argmax()<\/em><\/strong>, the array is flattened or considered a 1D array, so we got the&nbsp;<strong><em>index<\/em><\/strong>&nbsp;of the highest value from the input array.<\/p>\n\n\n\n<p><strong>Finding indices of the highest values along the axis<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import numpy as np\n\n# Creating a 2D array of random integers\ninput_arr = np.random.randint(16, size=(4, 4))\nprint(\"INPUT ARRAY: \\n\", input_arr)\n\n# Printing the indices of the max element along axis 0\nprint(\"\\nIndices of MAX ELEMENT\", np.argmax(input_arr, axis=0))\n\n# Printing the indices of the max element along axis 1\nprint(\"\\nIndices of MAX ELEMENT\", np.argmax(input_arr, axis=1))<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">INPUT ARRAY: \n [[13  3  8  1]\n [13  2  8 15]\n [ 7  8  7  7]\n [ 1  9 11  8]]\n\nIndices of MAX ELEMENT [0 3 3 1]\n\nIndices of MAX ELEMENT [0 3 1 2]<\/pre><\/div>\n\n\n\n<p><strong>Get a detailed explanation of numpy.argmax() function<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-geekpython wp-block-embed-geekpython\"><div class=\"wp-block-embed__wrapper\">\n<div class=\"oceanwp-oembed-wrap clr\"><blockquote class=\"wp-embedded-content\" data-secret=\"zBKX0DzqCb\"><a href=\"https:\/\/geekpython.in\/numpy-argmax-function-in-python\">Understanding numpy.argmax() Function In Python<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Understanding numpy.argmax() Function In Python&#8221; &#8212; GeekPython\" src=\"https:\/\/geekpython.in\/numpy-argmax-function-in-python\/embed#?secret=Wnu9kjcFdy#?secret=zBKX0DzqCb\" data-secret=\"zBKX0DzqCb\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/div>\n<\/div><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-tensorflowargmax\"><a href=\"https:\/\/geekpython.in\/argmax-function-in-numpy-and-tensorflow#heading-tensorflowargmax\"><\/a>tensorflow.argmax<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import tensorflow as tf\n\ninp_tensor = tf.constant([3, 1, 5, 10, 45, 21])\n\nout = tf.argmax(inp_tensor)\nprint(f\"INDEX of highest value: {out}\")\nprint(\"Tensor:\", out)<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">INDEX of highest value: 4\nTensor: tf.Tensor(4, shape=(), dtype=int64)<\/pre><\/div>\n\n\n\n<p>Unlike NumPy, TensorFlow provides additional information like the shape and dtype of the output.<\/p>\n\n\n\n<p><strong>Finding the indices of the highest values in a tensor along the specified axis<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import tensorflow as tf\n\ninp_tensor = tf.constant([[7, 13, 9, 10, 13.5, 21], [9, 2, 8, 14, 45, 21], [92, 3, 14, 92.1, 19, 8]])\n\n# Printing the indices of highest values along axis 0\nprint(f\"INDEX of highest value: {tf.argmax(inp_tensor, 0)}\")\nprint(\"Tensor:\", tf.argmax(inp_tensor, 0), \"\\n\")\n\n# Printing the indices of highest values along axis 1\nprint(f\"INDEX of highest value: {tf.argmax(inp_tensor, 1)}\")\nprint(\"Tensor:\", tf.argmax(inp_tensor, 1))<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">INDEX of highest value: [2 0 2 2 1 0]\nTensor: tf.Tensor([2 0 2 2 1 0], shape=(6,), dtype=int64) \n\nINDEX of highest value: [5 4 3]\nTensor: tf.Tensor([5 4 3], shape=(3,), dtype=int64)<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-conclusion\"><a href=\"https:\/\/geekpython.in\/argmax-function-in-numpy-and-tensorflow#heading-conclusion\"><\/a>Conclusion<\/h2>\n\n\n\n<p>We already discussed that NumPy is primarily for numerical computation, or we often use it for handling complex array operations. In contrast, TensorFlow is used to build machine learning and deep learning neural networks for developing AI-based applications.<\/p>\n\n\n\n<p>Both libraries have a&nbsp;<code>argmax()<\/code>&nbsp;function, and by this article, we meant to look at the function.<\/p>\n\n\n\n<p>After seeing the definition, syntax, and code, we can conclude that there was a slight difference in syntax, but the function&#8217;s fundamentals were the same in both libraries.<\/p>\n\n\n\n<p>Another minor difference was that TensorFlow provides additional info like the output tensor&#8217;s shape and dtype.<\/p>\n\n\n\n<p>Now we got a pretty good idea about the&nbsp;<strong><em>argmax()<\/em><\/strong>&nbsp;function provided by both libraries.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>That&#8217;s all for now<\/strong><\/p>\n\n\n\n<p><strong>Keep Coding\u270c\u270c<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Numpy&nbsp;is a Python library used for working with arrays. It provides multi-dimensional array objects, masked arrays, and matrices. To work with these arrays, NumPy provides some methods to carry out operations like linear algebra, statistical operations, shape manipulation, mathematical and logical operations, and much more. The array object in NumPy is called&nbsp;ndarray, and dimensions are [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":880,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"0","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"off","ocean_gallery_id":[],"footnotes":""},"categories":[2],"tags":[40,12,31],"class_list":["post-878","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-functions","tag-python","tag-python3","entry","has-media"],"_links":{"self":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/878","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/comments?post=878"}],"version-history":[{"count":3,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/878\/revisions"}],"predecessor-version":[{"id":1351,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/878\/revisions\/1351"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media\/880"}],"wp:attachment":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media?parent=878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/categories?post=878"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/tags?post=878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}