Numpy Savetxt, Explained
This article is originally published at https://www.sharpsightlabs.com
This tutorial will show you how to save your Numpy arrays to a text file using Numpy savetxt.
The tutorial explains what the function does, explains the syntax, and shows step-by-step examples of how to use np.savetxt.
If you need something specific, you can click on any of the following links.
Table of Contents:
A Quick Introduction to Numpy Savetxt
As you’re probably aware, Numpy savetxt is a function from the Numpy package.
Numpy has a variety of tools for creating, reshaping, aggregating, and otherwise manipulating numeric data in Python.
But after you’ve wrangled your data, you sometimes need to save it in a format that’s suitable for long-term storage or transfer to other people.
That’s where Numpy savetxt comes in.
Numpy savetxt enables you to save a Numpy array to a text file.
There are quite a few details about the function, however, that depend on the syntax.
So let’s quickly look at the syntax of Numpy savetxt.
The syntax of Numpy Savetxt
In this section, we’ll look at the syntax of np.savetxt.
We’ll look at how it works at a high level, but we’ll also look at some of the optional parameters.
A quick note
Before we jump into the syntax, I want to remind you of one detail.
Whenever we use a Python package, we need to import the package first.
In the case of Numpy, we typically do that with the following code:
import numpy as np
This is important, because when we import Numpy like this, it enables us to call Numpy functions with the prefix ‘np
‘.
Said differently, how exactly we import Numpy will change the syntax slightly, because it changes the prefix that we use for the Numpy namespace.
I just want to remind you of this, because going forward, I’m going to assume that you’ve imported Numpy like I just did in the code above.
np.savetxt syntax
At a high level, the syntax of Numpy savetxt is simple.
Assuming that you’ve imported the Numpy package as I explained above, you can type the function as np.savetxt
.
The first argument to the function is the filename that you want to use for the output text file.
The second argument is the Numpy array that you want to save.
Then, there are some optional parameters that you can use that will modify how exactly the function works.
The Parameters and Inputs of Numpy Savetxt
Numpy savetxt has several arguments and optional parameters.
filename
array
fmt
delimiter
newline
header
footer
comments
encoding
Let’s look at each of them one at a time.
filename
(required)
The filename
argument is the first input to Numpy savetxt.
As noted above, this is the name of the output file.
Importantly, this argument only works as a positional argument. What that means is that the np.savetxt() function assumes that the first input to the function is the name of the output file.
Note that if the name you provide ends in .gz
, Numpy savetxt will automatically save the file in a compressed format.
array
(required)
The array
argument is the data that you want to save.
Most frequently, the input will be a Numpy array.
However, technically, np.savetxt
will accept any “array like” object. So instead of a Numpy array, you can also provide a Python list or similar array-like object.
fmt
The fmt
parameter enables you to specify a format that you want to use for your data in the saved text file.
There are several options for this, including:
- using scientific notation
- specifying if the numbers are integers or floats
- specifying the number of significant digits
- etc
Formats can get rather complicated, as they use a mini-language to specify all of the details.
For more information about format specification, read this.
delimiter
The delimiter parameter allows you to specify a delimiter character that will separate the columns in the saved text file.
By default, this is set to delimiter = ' '
, which will separate the columns with a space.
Alternatively, you can use another character like a comma, which is also very common.
I’ll show you an example of this in the examples section.
newline
The newline
parameter allows you to specify a specific string that designates a new line in the output file.
By default, this is set to newline = '\n'
.
If you set this to another character or string, that string will be included in the file instead of a new line.
header
The header
parameter allows you to add some text to the top of the output file, as a header.
footer
The footer
parameter allows you to add some text to the bottom of the output file, as a footer.
comments
The comments
parameter allows you to specify a character that will appear in front of the header and footer text to show that the text is a comment.
By default, this is set to comments = '# '
.
encoding
The encoding
parameter allows you to specify the encoding that is used to encode the text file.
By default, this is set to encoding = None
, which uses latin1 encoding.
Examples of How to Use Numpy Savetxt
Now that we’ve looked at the syntax and parameters, let’s take a look at some examples of Numpy savetxt.
Examples:
- Save a Numpy array to a text file
- Use a specific delimiter in the output text file
- Use a specific format in the output text file
- Add a header and footer to the output text file
- Use a specific character to designate comments in the header/footer of the output text file
- Change the ‘newline’ character
Run this code first
Before you run the examples, there’s a little bit of setup code that you need to run first.
Specifically, you need to:
- Import Numpy
- Create Numpy array
Let’s do those one at a time.
Import Numpy
First, you need to import Numpy.
You can do that with this code:
import numpy as np
Remember, this allows us to call Numpy functions with the prefix np
.
Create Numpy Array
Next, we’ll create a Numpy array that we can use in our examples.
Specifically, we’ll use the Numpy array function to manually create a 2D numpy array with the numbers 1 to 6:
my_array = np.array([[1,2,3],[4,5,6]])
And let’s print it out.
print(my_array)
OUT:
[[1 2 3] [4 5 6]]
This is a simple 2-dimensional array that we’ll be able to save to a text file with np.savetxt()
.
EXAMPLE 1: Save a Numpy array to a text file
Here, we’ll start with a very simple example.
We’re simply going to save our Numpy array data to a text file, and we’ll use all of the defaults for the optional parameters.
Let’s take a look:
np.savetxt('my_numpy_data.txt', my_array)
If you find the output file on your computer, you should be able to examine it with a text editor (e.g., TextEdit on Mac, or Notepad on Windows).
Here’s what it looks like in the saved text file:
1.000000000000000000e+00 2.000000000000000000e+00 3.000000000000000000e+00 4.000000000000000000e+00 5.000000000000000000e+00 6.000000000000000000e+00
You’ll see that the numeric data has been stored in text format, with scientific notation, spaces between the columns, and new lines for every row.
That’s the default behavior. We’ll change some of those details in the next few examples.
EXAMPLE 2: Use a specific delimiter in the output text file
Next, we’ll use a specific delimiter to separate the columns in the output text file.
You’ll notice that in the previous example, the columns were separated by spaces by default.
Here, we’ll change that so that the columns are separated by commas.
To do that, we’ll use the delimiter
parameter.
Let’s take a look:
np.savetxt('my_numpy_data.txt', my_array, delimiter = ',')
Now, find the text file on your computer, and open it with a text editor (e.g., TextEdit on Mac, or Notepad on Windows).
Here are the contents of the saved text file:
1.000000000000000000e+00,2.000000000000000000e+00,3.000000000000000000e+00 4.000000000000000000e+00,5.000000000000000000e+00,6.000000000000000000e+00
You can see here at the numbers are separated by commas instead of spaces.
EXAMPLE 3: Use a specific format in the output text file
Next, we’re going to use a specific format for the numbers in the output file.
You probably noticed that in examples 1 and 2, the numbers were in scientific notation.
I’m not sure that that’ necessary, since the numbers in our Numpy array are integers.
So, let’s save the data in integer format, in the output array.
To do that, we’ll use the fmt
parameter, and use '%i'
to specify that we want to save the data in integer format:
np.savetxt('my_numpy_data.txt', my_array, fmt = '%i')
Now, find the text file on your computer, and open it with a text editor (e.g., TextEdit on Mac, or Notepad on Windows).
Here are the contents of the saved text file:
1 2 3 4 5 6
You’ll notice that the numbers are stored in integer format, in this case.
To be fair, integers are much simpler to work with. If you’re working with floats, the formatting may need to be more complex. For more complete information on working with string formatting, read this documentation.
EXAMPLE 4: Add a header and footer to the output text file
Here, we’re going to add a header and footer to the output file.
We’ll do that with the header
and footer
parameters:
np.savetxt('my_numpy_data.txt' ,my_array ,header = 'This is header text' ,footer = 'This is footer text' )
Now, find the text file on your computer, and open it with a text editor (e.g., TextEdit on Mac, or Notepad on Windows).
Here are the contents of the saved text file:
# This is header text 1.000000000000000000e+00 2.000000000000000000e+00 3.000000000000000000e+00 4.000000000000000000e+00 5.000000000000000000e+00 6.000000000000000000e+00 # This is footer text
You’ll notice that this has added the specified “header” text at the top of the file and the “footer” text at the bottom of the file.
Additionally, notice that preceding the header and footer text is the ‘#
‘ symbol.
This is intended to denote that these lines of text are comments.
We can also change this “comment character”. Let’s do that in the next example.
EXAMPLE 5: Use a specific character to designate comments in the header/footer of the output text file
In this example, we’ll change the “comment” character that designates lines of comment text in the output file.
We can do that with the comments
character:
np.savetxt('my_numpy_data.txt' ,my_array ,header = 'This is header text' ,footer = 'This is footer text' ,comments = '** ' )
Now, find the text file on your computer, and open it with a text editor (e.g., TextEdit on Mac, or Notepad on Windows).
Here are the contents of the saved text file:
** This is header text 1.000000000000000000e+00 2.000000000000000000e+00 3.000000000000000000e+00 4.000000000000000000e+00 5.000000000000000000e+00 6.000000000000000000e+00 ** This is footer text
Notice that in our code, we specified comments = '** '
.
This has used '** '
as a sort of prefix before our lines of comment text (i.e., the header and footer).
EXAMPLE 6: Change the ‘newline’ character
Finally, let’s change the newline character.
By default, the newline character is '\n'
. This is the traditional newline character for text files and strings, which will literally cause character data to be printed on a new line.
We can encode this differently though with np.savetxt
.
We can choose any character or string of characters to designate a new line of data.
Here, we’ll use ‘//
‘ to designate a new line.
And we’ll specify this with the newline
parameter.
np.savetxt('my_numpy_data.txt' ,my_array ,newline = '//' )
Now, find the text file on your computer, and open it with a text editor (e.g., TextEdit on Mac, or Notepad on Windows).
Here are the contents of the saved text file:
1.000000000000000000e+00 2.000000000000000000e+00 3.000000000000000000e+00//4.000000000000000000e+00 5.000000000000000000e+00 6.000000000000000000e+00//
Remember, the numbers 1, 2, and 3 were on the first row of our array, and 4, 5, 6 were on the second row.
In this text representation, however, you can see that there is a //
between 3 and 4. That’s because we used //
to specify a new line of data.
Keep in mind: when you need to load your data back into Python as a Numpy array (presumably using Numpy loadtxt), you’ll need to specify your newline character in your load code.
Frequently Asked Questions About Numpy Savetext
Now that you’ve learned about Numpy savetxt and seen some examples, let’s review some frequently asked questions.
Frequently asked questions:
Question 1: How do I save my data to an npy file?
Numpy savetxt is actually one of several different functions that you can use to save your Numpy data.
If you want to save your data to an .npy
file – with is a Numpy file storage format – you can use Numpy save.
Question 2: How do I save my data to a compressed format?
If you want to save your file to a compressed format instead of a text file or .npy
file, you can use Numpy savez.
Leave your other questions in the comments below
Do you have other questions about Numpy savetxt?
If so, leave your questions in the comments section below.
Join our course to learn more about Numpy
In this tutorial, I’ve shown you how to use Numpy savetxt.
Numpy savetxt is useful if you need to save a Numpy array in a text file, but if you really want to master numeric data manipulation in Python, you’ll need to learn a lot more Numpy.
That said, if you’re serious about learning Numpy, you should consider joining our premium course called Numpy Mastery.
Numpy Mastery will teach you everything you need to know about Numpy, including:
- How to create Numpy arrays
- How to Numpy axes work
- What the “Numpy random seed” function does
- How to use the Numpy random functions
- How to reshape, split, and combine your Numpy arrays
- Applying mathematical operations on Numpy arrays
- and more …
The course will also provide you with our unique practice system. This practice system will enable you to memorize all of the Numpy syntax that you learn.
If you’re struggled to remember Numpy syntax, this is the course you’ve been looking for.
If you practice like we show you, you’ll memorize all of the critical Numpy syntax in only a few weeks.
Find out more here:
Learn More About Numpy Mastery
Thanks for visiting r-craft.org
This article is originally published at https://www.sharpsightlabs.com
Please visit source website for post related comments.