{"id":903,"date":"2024-02-23T13:23:42","date_gmt":"2024-02-23T13:23:42","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=903"},"modified":"2024-02-23T13:23:44","modified_gmt":"2024-02-23T13:23:44","slug":"numpy-splitting-array","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/02\/23\/numpy-splitting-array\/","title":{"rendered":"NumPy\u00a0Splitting Array"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Splitting NumPy Arrays<\/h2>\n\n\n\n<p>Splitting is reverse operation of Joining.<\/p>\n\n\n\n<p>Joining merges multiple arrays into one and Splitting breaks one array into multiple.<\/p>\n\n\n\n<p>We use&nbsp;<code>array_split()<\/code>&nbsp;for splitting arrays, we pass it the array we want to split and the number of splits.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ExampleGet your own Python Server<\/h3>\n\n\n\n<p>Split the array in 3 parts:<\/p>\n\n\n\n<p>import&nbsp;numpy&nbsp;as&nbsp;np<br><br>arr = np.array([1,&nbsp;2,&nbsp;3,&nbsp;4,&nbsp;5,&nbsp;6])<br><br>newarr =&nbsp;np.array_split(arr,&nbsp;3)<br><br>print(newarr)<\/p>\n\n\n\n<p><a target=\"_blank\" href=\"https:\/\/www.w3schools.com\/python\/numpy\/trypython.asp?filename=demo_numpy_array_split1\" rel=\"noreferrer noopener\">Try it Yourself \u00bb<\/a><\/p>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;The return value is a list containing three arrays.<\/p>\n\n\n\n<p>If the array has less elements than required, it will adjust from the end accordingly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Split the array in 4 parts:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import&nbsp;numpy&nbsp;as&nbsp;np<br><br>arr = np.array(&#91;1,&nbsp;2,&nbsp;3,&nbsp;4,&nbsp;5,&nbsp;6])<br><br>newarr =&nbsp;np.array_split(arr,&nbsp;4)<br><br>print(newarr)<\/code><\/pre>\n\n\n\n<p><a target=\"_blank\" href=\"https:\/\/www.w3schools.com\/python\/numpy\/trypython.asp?filename=demo_numpy_array_split2\" rel=\"noreferrer noopener\">Try it Yourself \u00bb<\/a><\/p>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;We also have the method&nbsp;<code>split()<\/code>&nbsp;available but it will not adjust the elements when elements are less in source array for splitting like in example above,&nbsp;<code>array_split()<\/code>&nbsp;worked properly but&nbsp;<code>split()<\/code>&nbsp;would fail.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Split Into Arrays<\/h2>\n\n\n\n<p>The return value of the&nbsp;<code>array_split()<\/code>&nbsp;method is an array containing each of the split as an array.<\/p>\n\n\n\n<p>If you split an array into 3 arrays, you can access them from the result just like any array element:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Access the splitted arrays:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import&nbsp;numpy&nbsp;as&nbsp;np<br><br>arr = np.array(&#91;1,&nbsp;2,&nbsp;3,&nbsp;4,&nbsp;5,&nbsp;6])<br><br>newarr =&nbsp;np.array_split(arr,&nbsp;3)<br><br>print(newarr&#91;0])<br>print(newarr&#91;1])<br>print(newarr&#91;2])<\/code><\/pre>\n\n\n\n<p><a target=\"_blank\" href=\"https:\/\/www.w3schools.com\/python\/numpy\/trypython.asp?filename=demo_numpy_array_split3\" rel=\"noreferrer noopener\">Try it Yourself \u00bb<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Splitting 2-D Arrays<\/h2>\n\n\n\n<p>Use the same syntax when splitting 2-D arrays.<\/p>\n\n\n\n<p>Use the&nbsp;<code>array_split()<\/code>&nbsp;method, pass in the array you want to split and the number of splits you want to do.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Split the 2-D array into three 2-D arrays.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import&nbsp;numpy&nbsp;as&nbsp;np<br><br>arr = np.array(&#91;&#91;1,&nbsp;2], &#91;3,&nbsp;4], &#91;5,&nbsp;6], &#91;7,&nbsp;8], &#91;9,&nbsp;10], &#91;11,&nbsp;12]])<br><br>newarr = np.array_split(arr,&nbsp;3)<br><br>print(newarr)<\/code><\/pre>\n\n\n\n<p><a target=\"_blank\" href=\"https:\/\/www.w3schools.com\/python\/numpy\/trypython.asp?filename=demo_numpy_array_split4\" rel=\"noreferrer noopener\">Try it Yourself \u00bb<\/a><\/p>\n\n\n\n<p>The example above returns three 2-D arrays.<\/p>\n\n\n\n<p>Let&#8217;s look at another example, this time each element in the 2-D arrays contains 3 elements.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Split the 2-D array into three 2-D arrays.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import&nbsp;numpy&nbsp;as&nbsp;np<br><br>arr = np.array(&#91;&#91;1,&nbsp;2,&nbsp;3], &#91;4,&nbsp;5,&nbsp;6], &#91;7,&nbsp;8,&nbsp;9], &#91;10,&nbsp;11,&nbsp;12], &#91;13,&nbsp;14,&nbsp;15], &#91;16,&nbsp;17,&nbsp;18]])<br><br>newarr = np.array_split(arr,&nbsp;3)<br><br>print(newarr)<\/code><\/pre>\n\n\n\n<p><a target=\"_blank\" href=\"https:\/\/www.w3schools.com\/python\/numpy\/trypython.asp?filename=demo_numpy_array_split5\" rel=\"noreferrer noopener\">Try it Yourself \u00bb<\/a><\/p>\n\n\n\n<p>The example above returns three 2-D arrays.<\/p>\n\n\n\n<p>In addition, you can specify which axis you want to do the split around.<\/p>\n\n\n\n<p>The example below also returns three 2-D arrays, but they are split along the row (axis=1).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Split the 2-D array into three 2-D arrays along rows.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import&nbsp;numpy&nbsp;as&nbsp;np<br><br>arr = np.array(&#91;&#91;1,&nbsp;2,&nbsp;3], &#91;4,&nbsp;5,&nbsp;6], &#91;7,&nbsp;8,&nbsp;9], &#91;10,&nbsp;11,&nbsp;12], &#91;13,&nbsp;14,&nbsp;15], &#91;16,&nbsp;17,&nbsp;18]])<br><br>newarr = np.array_split(arr,&nbsp;3, axis=1)<br><br>print(newarr)<\/code><\/pre>\n\n\n\n<p><a target=\"_blank\" href=\"https:\/\/www.w3schools.com\/python\/numpy\/trypython.asp?filename=demo_numpy_array_split6\" rel=\"noreferrer noopener\">Try it Yourself \u00bb<\/a><\/p>\n\n\n\n<p>An alternate solution is using&nbsp;<code>hsplit()<\/code>&nbsp;opposite of&nbsp;<code>hstack()<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Use the&nbsp;<code>hsplit()<\/code>&nbsp;method to split the 2-D array into three 2-D arrays along rows.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import&nbsp;numpy&nbsp;as&nbsp;np<br><br>arr = np.array(&#91;&#91;1,&nbsp;2,&nbsp;3], &#91;4,&nbsp;5,&nbsp;6], &#91;7,&nbsp;8,&nbsp;9],&nbsp;&#91;10,&nbsp;11,&nbsp;12], &#91;13,&nbsp;14,&nbsp;15], &#91;16,&nbsp;17,&nbsp;18]])<br><br>newarr = np.hsplit(arr,&nbsp;3)<br><br>print(newarr)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Splitting NumPy Arrays Splitting is reverse operation of Joining. Joining merges multiple arrays into one and Splitting breaks one array into multiple. We use&nbsp;array_split()&nbsp;for splitting arrays, we pass it the array we want to split and the number of splits. ExampleGet your own Python Server Split the array in 3 parts: import&nbsp;numpy&nbsp;as&nbsp;np arr = np.array([1,&nbsp;2,&nbsp;3,&nbsp;4,&nbsp;5,&nbsp;6]) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-903","post","type-post","status-publish","format-standard","hentry","category-numpy"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/903","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=903"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/903\/revisions"}],"predecessor-version":[{"id":904,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/903\/revisions\/904"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}