{"id":4883,"date":"2020-03-16T23:30:22","date_gmt":"2020-03-17T04:30:22","guid":{"rendered":"https:\/\/www.sharpsightlabs.com\/?p=4883"},"modified":"2022-07-11T12:58:46","modified_gmt":"2022-07-11T17:58:46","slug":"numpy-flatten","status":"publish","type":"post","link":"https:\/\/www.sharpsightlabs.com\/blog\/numpy-flatten\/","title":{"rendered":"How to use Numpy flatten"},"content":{"rendered":"
This tutorial will show you how to use the Numpy flatten method. It will explain the syntax of the method and show you how to flatten an array in Numpy.<\/p>\n
The following is a table of contents. If you’re looking for something specific, you can click on any of the links to jump to the appropriate section.<\/p>\n
Table of Contents:<\/strong><\/p>\n Having said that, if you’re new to Numpy, I recommend reading the whole tutorial.<\/p>\n So what does Numpy flatten do?<\/p>\n Very simply: Numpy flatten “flattens” out a Numpy array.<\/p>\n <\/p>\n To understand this though, it helps to know a few things about Numpy.<\/p>\n <\/a><\/p>\n Let’s do a quick review of Numpy and Numpy arrays.<\/p>\n You probably know that Numpy arrays are a type of object that hold numeric data<\/a>. Numpy arrays contain numbers in a row and column form.<\/p>\n Here’s a visual example of a 2-dimentional numpy array.<\/p>\n <\/p>\n Numpy arrays can be 2-dimentional like the array above, but also 1-dimentional or n-dimentional.<\/p>\n We use can Numpy<\/a> functions to create Numpy arrays (i.e., arrays of numeric data). And we can also use Numpy functions and methods to manipulate Numpy arrays.<\/p>\n There are a variety of Numpy functions for creating Numpy arrays. For example, we can create arrays that contain all zeros<\/a> using the np.zeros function. We can create Numpy arrays within a numeric range<\/a> using np.arange. And we can create Numpy arrays out of Python lists using np.array<\/a>. <\/p>\n Those are just a few methods (there are quite a few others).<\/p>\n It’s important to understand that Numpy arrays have a “shape.” <\/p>\n The shape of an array is essentially the number of rows and columns.<\/p>\n For example, an array with a shape of Often, when working with Numpy arrays, we need to reshape the array. That is, we need to re-organize the elements of the array into a new “shape” with a different number of rows and columns.<\/p>\n One common way is by using the Numpy reshape method<\/a>, which enables us to specify the exact number of rows and columns of the output array. The reshape method gives us a lot of control over the output.<\/p>\n But there’s actually another simple way to reshape your array.<\/p>\n Let’s say you want to reshape your array from a 2-dimensional array (with multiple rows and columns) into a 1-dimensional array, with only one row.<\/p>\n There’s an easy way to do that. You can use the Numpy flatten method.<\/p>\n <\/a><\/p>\n So again, what does flatten do?<\/p>\n It just “flattens” out a Numpy array.<\/p>\n Numpy flatten changes the shape of a Numpy array from a multi-dimensional array, to a 1-dimensional array.<\/p>\n <\/p>\n It’s a very simple tool. It really only does one thing, and the syntax is very straightforward.<\/p>\n Let’s take a look.<\/p>\n <\/a><\/p>\n The syntax is very simple, but before we get into it, I need to remind you of one thing.<\/p>\n We’re going to need to use a couple of Numpy functions going forward. <\/p>\n Exactly how we call those functions depends on how we import Numpy.<\/p>\n In the rest of this tutorial, we’re going to assume that you’ve imported Numpy with the following code:<\/p>\n This is the common convention among almost all Python programmers who use Numpy.<\/p>\n When we import Numpy with the code Again, that’s the convention we’ll be using in the rest of this tutorial.<\/p>\n When we call the Numpy flatten method, we need to call it as a method based of of an existing Numpy array.<\/p>\n So let’s say that you have a Numpy array called You can type the name of the array, then use dot notation to call the method.<\/p>\n <\/p>\n So if you have a Numpy array called That’s basically it, with the exception of one parameter.<\/p>\n Let’s quickly talk about that.<\/p>\n The flatten method only has one parameter: the Let’s talk about it.<\/p>\n The Keep in mind that the However, you have a few options for the If you set <\/p>\n (Note that it’s called the This is the default behavior, so if you do not use the If you set <\/p>\n (Note that it’s called the The flatten method returns a new array with the elements collapsed into one dimension.<\/p>\n So remember: the flatten method will not directly reshape your original array. It produces a new array.<\/p>\n <\/a><\/p>\n Ok. Now that we’ve taken a look at the syntax, let’s look at some examples.<\/p>\n Examples:<\/strong><\/p>\n Before we run the examples, we need to import Numpy.<\/p>\n To import Numpy, run this code:<\/p>\n By importing this code, we will be able to use the alias ‘ (Note, this is a common convention among Numpy users.)<\/p>\n Before we run our example, we also need to create a Numpy array.<\/p>\n To do this, we’ll use the Numpy arange function to create an array of integers<\/a>, and we’ll use the Numpy reshape method to reshape it into 2-dimensions<\/a>.<\/p>\n And let’s take a look.<\/p>\n OUT:<\/p>\n This is a simple, 2D array that contains the values 1 to 6.<\/p>\n Ok. Now let’s look at some examples.<\/p>\n <\/a><\/p>\n First, we’ll just do a simple example.<\/p>\n Here, we’re going to flatten a our 2D numpy array, OUT:<\/p>\n So what happened here?<\/p>\n Our input array, When we use the flatten function, it just flattens it out. <\/p>\n <\/p>\n Keep in mind that it does this row-wise by default.<\/p>\n Ultimately, the flatten method produces a new output array that has been “flattened” into 1 dimension.<\/p>\n <\/a><\/p>\n Next, let’s use the “ Here, we’ll set Let’s take a look.<\/p>\n OUT:<\/p>\n When we set <\/p>\n So the output is just a 1D array, with the elements reordered row-wise based on the input array.<\/p>\n (Note that the output here is the same as the output for example 1<\/a>. That’s because if we don’t use the <\/a><\/p>\n Finally, let’s use Numpy flatten with This will flatten the elements of the array column-wise.<\/p>\n OUT:<\/p>\n By setting <\/p>\n Notice that in the output array, the elements are arranged differently from the output in example 2<\/a>.<\/p>\n In example 2, the elements were re-arranged row-wise.<\/p>\n However here, when we set <\/a><\/p>\n Do you still have questions about the Numpy flatten method?<\/p>\n Leave your questions in the comments section below.<\/p>\n The examples you’ve seen in this tutorial should be enough to get you started, but if you’re serious about learning Numpy, you should enroll in our premium course called Numpy Mastery<\/em>.<\/p>\n There’s a lot more to learn about Numpy, and Numpy Mastery<\/em> will teach you everything, including:<\/p>\n Moreover, it will help you completely master<\/em> the syntax within a few weeks. You’ll discover how to become “fluent” in writing Numpy code.<\/p>\n Find out more here:<\/p>\n \nLearn More About Numpy Mastery<\/a><\/p>\n","protected":false},"excerpt":{"rendered":" This tutorial will show you how to use the Numpy flatten method. It will explain the syntax of the method and show you how to flatten an array in Numpy. The following is a table of contents. If you’re looking for something specific, you can click on any of the links to jump to the … Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":5083,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","om_disable_all_campaigns":false,"footnotes":""},"categories":[31,62,58],"tags":[],"publishpress_future_action":{"enabled":false,"date":"2024-05-18 05:57:08","action":"change-status","newStatus":"draft","terms":[],"taxonomy":"category"},"_links":{"self":[{"href":"https:\/\/www.sharpsightlabs.com\/wp-json\/wp\/v2\/posts\/4883"}],"collection":[{"href":"https:\/\/www.sharpsightlabs.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sharpsightlabs.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sharpsightlabs.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sharpsightlabs.com\/wp-json\/wp\/v2\/comments?post=4883"}],"version-history":[{"count":0,"href":"https:\/\/www.sharpsightlabs.com\/wp-json\/wp\/v2\/posts\/4883\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sharpsightlabs.com\/wp-json\/wp\/v2\/media\/5083"}],"wp:attachment":[{"href":"https:\/\/www.sharpsightlabs.com\/wp-json\/wp\/v2\/media?parent=4883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sharpsightlabs.com\/wp-json\/wp\/v2\/categories?post=4883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sharpsightlabs.com\/wp-json\/wp\/v2\/tags?post=4883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}\n
A quick introduction to Numpy Flatten<\/h2>\n
A quick review of Numpy<\/h3>\n
Creating Numpy arrays<\/h3>\n
Numpy arrays have a “shape”<\/h4>\n
(2,3)<\/code> has 2 rows and 3 columns.<\/p>\n
Reshaping Numpy arrays<\/h3>\n
How to Flatten Numpy Arrays<\/h2>\n
The syntax of Numpy flatten<\/h2>\n
A quick note about importing Numpy<\/h4>\n
\r\nimport numpy as np\r\n<\/pre>\n
import numpy as np<\/code>, we can use the prefix
np<\/code> when we call our Numpy functions.<\/p>\n
Numpy Flatten syntax<\/h3>\n
my_array<\/code>.<\/p>\n
my_array<\/code>, you can call the method with the code
my_array.flatten()<\/code>.<\/p>\n
The parameters of Numpy flatten<\/h3>\n
order<\/code> parameter.<\/p>\n
order<\/code> (optional)<\/h5>\n
order<\/code> parameter enables you to specify the order of the observations in the flattened output array.<\/p>\n
order<\/code> parameter is optional. If you don’t use it in your syntax, it will default to
order = 'C'<\/code>.<\/p>\n
order<\/code> parameter. Let’s talk about two of them.<\/p>\n
order = ‘C’<\/h6>\n
order = 'C'<\/code>, the flatten method will flatten the elements out in a row first fashion.<\/p>\n
'C'<\/code> method because this is how data are stored and retrieved in the C programming language.)<\/p>\n
order<\/code> parameter in your syntax, Numpy flatten will automatically flatten your array by row.<\/p>\n
order = ‘F’<\/h6>\n
order = 'F'<\/code>, the flatten method will flatten the elements out in a column first fashion.<\/p>\n
'F'<\/code> method because this is how data are stored and retrieved in the Fortran programming language. I know. It’s old school.)<\/p>\n
The output of Numpy flatten<\/h5>\n
Examples of how to use Numpy Flatten<\/h2>\n
\n
Run this code first to import Numpy<\/h4>\n
\r\nimport numpy as np\r\n<\/pre>\n
np<\/code>‘ in our syntax. We can use that ‘
np<\/code>‘ as a prefix before our Numpy functions.<\/p>\n
Run this code to create a 2D array<\/h5>\n
\r\nmy_array = np.arange(start = 1, stop = 7).reshape([2,3])\r\n<\/pre>\n
\r\nprint(my_array)\r\n<\/pre>\n
\r\n[[1 2 3]\r\n [4 5 6]]\r\n<\/pre>\n
EXAMPLE 1: Use numpy flatten to flatten a 2-d array<\/h3>\n
my_array<\/code>.<\/p>\n
\r\nmy_array.flatten()\r\n<\/pre>\n
\r\narray([1, 2, 3, 4, 5, 6])\r\n<\/pre>\n
Explanation<\/h6>\n
my_array<\/code>, is a 2-dimensional array with 2 rows and 3 columns.<\/p>\n
EXAMPLE 2: Flatten an array by row<\/h3>\n
order<\/code>” parameter. <\/p>\n
order = 'C'<\/code> to explicitly flatten the input array by row. (Remember from the syntax explanation above<\/a> that when we set
order = 'C'<\/code>, it flattens the array row-wise).<\/p>\n
\r\nmy_array.flatten(order = 'C')\r\n<\/pre>\n
\r\narray([1, 2, 3, 4, 5, 6])\r\n<\/pre>\n
Explanation<\/h6>\n
order = 'C'<\/code>, the flatten method flattens the array row-wise.<\/p>\n
order<\/code> parameter, Numpy flatten defaults to
order = 'C'<\/code>.)<\/p>\n
EXAMPLE 3: Flatten an array by column<\/h3>\n
order = 'F'<\/code>.<\/p>\n
\r\nmy_array.flatten(order = 'F')\r\n<\/pre>\n
\r\narray([1, 4, 2, 5, 3, 6])\r\n<\/pre>\n
Explanation<\/h6>\n
order = 'F'<\/code>, we’re instructing the flatten method to flatten out the elements column wise<\/i>.<\/p>\n
order = 'F'<\/code>, the elements have been re-arranged and flattened column-wise.<\/p>\n
Leave your questions in the comments below<\/h3>\n
Join our course to learn more about Numpy<\/h2>\n
\n