{"id":37886,"date":"2022-12-16T14:37:13","date_gmt":"2022-12-16T14:37:13","guid":{"rendered":"https:\/\/www.askpython.com\/?p=37886"},"modified":"2022-12-16T14:37:14","modified_gmt":"2022-12-16T14:37:14","slug":"numpy-ones","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-ones","title":{"rendered":"NumPy ones &#8211; A Complete Guide"},"content":{"rendered":"\n<p>Hello and welcome to this tutorial on <strong>Numpy ones<\/strong>. In this tutorial, we will be learning about the <strong>NumPy ones()<\/strong> method and also seeing a lot of examples regarding the same. So let us begin!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What is the NumPy ones method?<\/h2>\n\n\n\n<p>NumPy\u00a0<code>ones<\/code>\u00a0returns a\u00a0<a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/python-numpy-arrays\"><strong>Numpy array<\/strong><\/a>\u00a0of the given shape and data type with all values set to 1.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of NumPy ones<\/h2>\n\n\n\n<p>Let us first have a look at the syntax of NumPy ones.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.ones(shape, dtype=None, order=&#039;C&#039;, like=None)\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Parameter<\/strong><\/td><td><strong>Description<\/strong><\/td><td><strong>Required\/Optional<\/strong><\/td><\/tr><tr><td>shape<\/td><td>The desired shape of the array. It can be an int or a tuple of ints.<\/td><td>Required<\/td><\/tr><tr><td>dtype<\/td><td>The desired data type of the array.<br>The default data type is&nbsp;<strong>float<\/strong>.<\/td><td>Optional<\/td><\/tr><tr><td>order<\/td><td>The desired order in which the multi-dimensional data is to be stored in the memory. It can either be row-major (\u2018C\u2019) or column-major (\u2018F\u2019).<br>The default order is&nbsp;<strong>\u2018C\u2019<\/strong>&nbsp;i.e.&nbsp;<strong>row-major<\/strong>.<\/td><td>Optional<\/td><\/tr><tr><td>like (array_like)<\/td><td>Reference object to allow the creation of arrays that are not NumPy arrays.<\/td><td>Optional<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Returns:<\/strong>&nbsp;<br>An array with the given shape, data type and order filled with only ones.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Examples of using NumPy ones<\/h2>\n\n\n\n<p>Let\u2019s now look at some practical examples of the <code>numpy.ones()<\/code> method.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1-dimensional array using ones<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\none_dim_array = np.ones(4)\nprint(one_dim_array) \n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;1. 1. 1. 1.]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2-dimensional array using zeros<\/h3>\n\n\n\n<p><strong>N x M array<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\ntwo_dim_array = np.ones((4, 2))\nprint(two_dim_array) \n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;&#x5B;1. 1.]\n &#x5B;1. 1.]\n &#x5B;1. 1.]\n &#x5B;1. 1.]]\n<\/pre><\/div>\n\n\n<p><strong>1 x N array<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\none_row_array = np.ones((1, 3))\nprint(one_row_array) \n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;&#x5B;1. 1. 1.]]\n<\/pre><\/div>\n\n\n<p><strong>N x 1 array<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\none_col_array = np.ones((5, 1))\nprint(one_col_array) \n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;&#x5B;1.]\n &#x5B;1.]\n &#x5B;1.]\n &#x5B;1.]\n &#x5B;1.]]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1-dimensional int-type array<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\none_dim_int_array = np.ones(5, dtype=np.int64)\nprint(one_dim_int_array) \n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;1 1 1 1 1]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2-dimensional int-type array<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\ntwo_dim_int_array = np.ones((3, 3), dtype=np.int64)\nprint(two_dim_int_array) \n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;&#x5B;1 1 1]\n &#x5B;1 1 1]\n &#x5B;1 1 1]]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1-dimensional custom data type array<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\ncustom_one_dim_array = np.ones(4, dtype=&#x5B;(&#039;x&#039;, &#039;int&#039;), (&#039;y&#039;, &#039;float&#039;)])\nprint(custom_one_dim_array) \nprint(custom_one_dim_array.dtype) \n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;(1, 1.) (1, 1.) (1, 1.) (1, 1.)]\n&#x5B;(&#039;x&#039;, &#039;&amp;lt;i4&#039;), (&#039;y&#039;, &#039;&amp;lt;f8&#039;)]\n<\/pre><\/div>\n\n\n<p>In this example, we specified the first value as an int and the second as a float.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2-dimensional custom data type array<\/h3>\n\n\n\n<p>We can specify the elements of the array as a tuple and also specify their data types.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\ncustom_two_dim_array = np.ones((2, 3), dtype=&#x5B;(&#039;x&#039;, &#039;float&#039;), (&#039;y&#039;, &#039;int&#039;)])\nprint(custom_two_dim_array) \nprint(custom_two_dim_array.dtype) \n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;&#x5B;(1., 1) (1., 1) (1., 1)]\n &#x5B;(1., 1) (1., 1) (1., 1)]]\n&#x5B;(&#039;x&#039;, &#039;&amp;lt;f8&#039;), (&#039;y&#039;, &#039;&amp;lt;i4&#039;)]\n<\/pre><\/div>\n\n\n<p>Here, the code specifies the first value of the tuple in the array elements to be a float and the second one to be an int.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>That\u2019s all! In this tutorial, we learned about the&nbsp;<strong>Numpy ones<\/strong>&nbsp;method and practiced different types of examples using the same.<br>If you want to learn more about NumPy, feel free to go through our&nbsp;<a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\" target=\"_blank\" rel=\"noreferrer noopener\">NumPy tutorials<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Reference<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.ones.html\" target=\"_blank\" rel=\"noreferrer noopener\">NumPy ones official documentation<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Hello and welcome to this tutorial on Numpy ones. In this tutorial, we will be learning about the NumPy ones() method and also seeing a lot of examples regarding the same. So let us begin! What is the NumPy ones method? NumPy\u00a0ones\u00a0returns a\u00a0Numpy array\u00a0of the given shape and data type with all values set to [&hellip;]<\/p>\n","protected":false},"author":46,"featured_media":37887,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-37886","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/37886","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/46"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=37886"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/37886\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/37887"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=37886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=37886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=37886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}