{"id":850,"date":"2023-09-21T21:53:31","date_gmt":"2023-09-21T13:53:31","guid":{"rendered":"https:\/\/datascihubs.com\/?p=850"},"modified":"2023-09-21T21:53:33","modified_gmt":"2023-09-21T13:53:33","slug":"simple-ways-to-create-array-with-numpy","status":"publish","type":"post","link":"https:\/\/datascihubs.com\/index.php\/2023\/09\/21\/simple-ways-to-create-array-with-numpy\/","title":{"rendered":"Simple Ways to Create Array with NumPy"},"content":{"rendered":"\n<p class=\"has-text-align-justify\"><strong>NumPy<\/strong>, the powerful numerical Python library, is essential for data manipulation, scientific computing, and data analysis. NumPy arrays provide a high-performance, memory-efficient way to store and process large datasets, making them a fundamental tool in the toolkit of data scientists, engineers, and researchers. They offer a unified data structure for mathematical operations, making it easier to work with multidimensional data, perform complex calculations, and implement algorithms efficiently. Whether you&#8217;re working on machine learning, scientific simulations, or data preprocessing, understanding how to create and use NumPy arrays is a crucial step in harnessing the full potential of Python for data-centric tasks. In this article, we&#8217;ll explore different ways to create array with NumPy easily with some examples.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1498\" height=\"998\" src=\"https:\/\/i0.wp.com\/datascihubs.com\/wp-content\/uploads\/2023\/09\/pexels-photo-3038740.jpeg?resize=1498%2C998&#038;ssl=1\" alt=\"ways to create array\" class=\"wp-image-853\" srcset=\"https:\/\/i0.wp.com\/datascihubs.com\/wp-content\/uploads\/2023\/09\/pexels-photo-3038740.jpeg?w=1880&amp;ssl=1 1880w, https:\/\/i0.wp.com\/datascihubs.com\/wp-content\/uploads\/2023\/09\/pexels-photo-3038740.jpeg?resize=300%2C200&amp;ssl=1 300w, https:\/\/i0.wp.com\/datascihubs.com\/wp-content\/uploads\/2023\/09\/pexels-photo-3038740.jpeg?resize=1024%2C682&amp;ssl=1 1024w, https:\/\/i0.wp.com\/datascihubs.com\/wp-content\/uploads\/2023\/09\/pexels-photo-3038740.jpeg?resize=768%2C512&amp;ssl=1 768w, https:\/\/i0.wp.com\/datascihubs.com\/wp-content\/uploads\/2023\/09\/pexels-photo-3038740.jpeg?resize=1536%2C1024&amp;ssl=1 1536w, https:\/\/i0.wp.com\/datascihubs.com\/wp-content\/uploads\/2023\/09\/pexels-photo-3038740.jpeg?resize=1320%2C880&amp;ssl=1 1320w, https:\/\/i0.wp.com\/datascihubs.com\/wp-content\/uploads\/2023\/09\/pexels-photo-3038740.jpeg?resize=600%2C400&amp;ssl=1 600w\" sizes=\"auto, (max-width: 1498px) 100vw, 1498px\" \/><figcaption class=\"wp-element-caption\">Photo by Vlado Paunovic on <a href=\"https:\/\/www.pexels.com\/photo\/gray-concrete-building-exterior-with-geometric-design-3038740\/\" rel=\"nofollow\">Pexels.com<\/a><\/figcaption><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Table of Contents<\/h4>\n\n\n\n<div class=\"wp-block-aioseo-table-of-contents\"><ul><li><a href=\"#aioseo-convert-existing-data-into-numpy-array\">Convert existing data into NumPy Array<\/a><\/li><li><a href=\"#aioseo-1-1d-array-creation-functions\">1D array creation functions<\/a><\/li><li><a href=\"#aioseo-2-2d-array-creation-functions\">2D array creation functions<\/a><\/li><li><a href=\"#aioseo-3-general-ndarray-creation-functions\">General ndarray creation functions<\/a><ul><\/ul><\/li><\/ul><\/div>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"aioseo-convert-existing-data-into-numpy-array\">Convert existing data into NumPy Array<\/h5>\n\n\n\n<p class=\"has-text-align-justify\">Let&#8217;s get started with the simplest and most used method, which is converting existing data into one. If we have a Python list, we can convert it to NumPy by simply using the &#8216;np.array()&#8217; function. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># covnert Python list to NumPy array\nimport numpy as np\n\npython_list = &#91;1, 2, 3, 4, 5]\nnumpy_array = np.array(&#91;python_list])<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\">In the example above, I&#8217;ve only shown how to convert a 1D python list to NumPy, but you could use the same method for any dimensional data. <\/p>\n\n\n\n<p class=\"has-text-align-justify\">What if we don&#8217;t have a Python List or we want to create a large matrix from scratch? NumPy also provides some built-in functions for array generation. These functions include <\/p>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"aioseo-1-1d-array-creation-functions\">1D array creation functions<\/h5>\n\n\n\n<ul class=\"wp-block-list\">\n<li>np.arrange()<\/li>\n\n\n\n<li>np.linspace()<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code># create an array with evenly spaced values within a given interval\n&gt;&gt;&gt; np.arange(0, 5, 0.5)\narray(&#91;0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5])\n\n\n# create an array with evenly spaced numbers over a specified interval\n&gt;&gt;&gt; np.linspace(0, 5, 5)\narray(&#91;0.  , 1.25, 2.5 , 3.75, 5.  ])<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"aioseo-2-2d-array-creation-functions\">2D array creation functions<\/h5>\n\n\n\n<ul class=\"wp-block-list\">\n<li>np.eye()<\/li>\n\n\n\n<li>np.diag()<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code># create an identity matrix \n&gt;&gt;&gt; np.eye(3)\narray(&#91;&#91;1., 0., 0.],\n       &#91;0., 1., 0.],\n       &#91;0., 0., 1.]])\n\n\n# create 2D array with given diagonal elements\n&gt;&gt;&gt; np.diag(&#91;1, 2, 3])\narray(&#91;&#91;1, 0, 0],\n       &#91;0, 2, 0],\n       &#91;0, 0, 3]])<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"aioseo-3-general-ndarray-creation-functions\">General ndarray creation functions<\/h5>\n\n\n\n<ul class=\"wp-block-list\">\n<li>np.zeros()<\/li>\n\n\n\n<li>np.ones()<\/li>\n\n\n\n<li>np.random()<\/li>\n\n\n\n<li>np.indices()<\/li>\n<\/ul>\n\n\n\n<p class=\"has-text-align-justify\">These functions accept a desired array shape and data type as parameters. For example, np.zeros(2) returns a 1D array with 2 zeros. The default data type is <strong>float64<\/strong>, but you could also change it by changing the <strong>dtype<\/strong> argument. We could also replace it with (a, b) which returns an a-by-b array. You also try np.zeros((1, 2, 3)) and see what it returns (supposed to be a 3D array).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># create an array of given shape and type, filled with zeros\n&gt;&gt;&gt; np.zeros((2, 2))\narray(&#91;&#91;0., 0.],\n       &#91;0., 0.]])\n\n# create an array of given shape and type, filled with ones\n&gt;&gt;&gt; np.ones((2, 3))\narray(&#91;&#91;1., 1.],\n       &#91;1., 1.],\n       &#91;1., 1.]])\n\n# create an array of given shape and type, without initializing entries\n&gt;&gt;&gt; np.empty(&#91;2, 2])\narray(&#91;&#91; -9.74499359e+001,   6.69583040e-309],\n       &#91;  2.13182611e-314,   3.06959433e-309]])  <\/code><\/pre>\n\n\n\n<p>The arrays created above are all 1D, but you could also create different dimensional data by providing the data shape. For example, creating a 2D zero array with <strong><em>&#8220;np.zeros((2, 2))&#8221;<\/em><\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"aioseo-post-you-might-want-to-read\">Other Posts to Read<\/h2>\n\n\n<ul class=\"wp-block-latest-posts__list wp-block-latest-posts\"><li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/datascihubs.com\/index.php\/2024\/02\/12\/sql-join-a-complete-guide-with-examples\/\">SQL JOIN: A Complete Guide With Examples<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/datascihubs.com\/index.php\/2024\/01\/15\/a-quick-guide-to-numpy-functions\/\">A Quick Guide to NumPy Functions<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/datascihubs.com\/index.php\/2024\/01\/13\/the-big-picture-of-structured-query-language\/\">The Big Picture of Structured Query Language<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/datascihubs.com\/index.php\/2023\/09\/21\/simple-ways-to-create-array-with-numpy\/\">Simple Ways to Create Array with NumPy<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/datascihubs.com\/index.php\/2023\/09\/17\/useful-indexing-techniques-in-numpy\/\">Useful Indexing Techniques In NumPy<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>NumPy, the powerful numerical Python library, is essential for data manipulation, scientific computing, and data analysis. NumPy arrays provide a high-performance, memory-efficient way to store and process large datasets, making them a fundamental tool in the toolkit of data scientists, engineers, and researchers. They offer a unified data structure for mathematical operations, making it easier [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":855,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[90,93,28,57,86,91,47],"class_list":["post-850","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-information","tag-create-array","tag-create-numpy-array","tag-data-analysis","tag-data-preprocessing","tag-numpy","tag-numpy-python","tag-python"],"blocksy_meta":[],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/datascihubs.com\/wp-content\/uploads\/2023\/09\/Create-array-in-python.png?fit=1280%2C720&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/datascihubs.com\/index.php\/wp-json\/wp\/v2\/posts\/850","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datascihubs.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datascihubs.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datascihubs.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datascihubs.com\/index.php\/wp-json\/wp\/v2\/comments?post=850"}],"version-history":[{"count":3,"href":"https:\/\/datascihubs.com\/index.php\/wp-json\/wp\/v2\/posts\/850\/revisions"}],"predecessor-version":[{"id":854,"href":"https:\/\/datascihubs.com\/index.php\/wp-json\/wp\/v2\/posts\/850\/revisions\/854"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/datascihubs.com\/index.php\/wp-json\/wp\/v2\/media\/855"}],"wp:attachment":[{"href":"https:\/\/datascihubs.com\/index.php\/wp-json\/wp\/v2\/media?parent=850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datascihubs.com\/index.php\/wp-json\/wp\/v2\/categories?post=850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datascihubs.com\/index.php\/wp-json\/wp\/v2\/tags?post=850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}