Numpy Savetxt is a method to save an array to a text file or CSV file. In this coding tutorial, I will show you the implementation of the NumPy savetxt() method using the best examples I have compiled.
First of all, let’s import all the necessary libraries required for this tutorial. Here I am using only NumPy library.
import numpy as np
How to Save One Dimensional Numpy array using numpy.savetxt()
In this section, I will create a one-dimensional NumPy array. Then after saving it to both as a text file and CSV file.
array_1d = np.array([10,30,40,20])
Saving to text file
Here I am passing the filename, the array I want to save and delimiter to split the array elements after the comma.
Saving to a CSV File
You can save it to a CSV file by just changing the name of the file.
There is something you should be careful about. You have to pass your 1D Numpy array inside the square bracket. And fmt=”%d” as by default the array will be stored as float type. Here We are using the values of integer type.
You can also add a header and footer argument inside the np.savetxt() method. Just execute the following code.
np.savetxt("array_1d_with_hf.csv",[array_1d],delimiter=",",fmt="%d",header="This is header",footer="This is footer")
Save Two Dimensional Numpy array using numpy.savetxt()
The above example was for one dimensional array. Now let’s save the two-dimensional array as a text and CSV file. Let’s create a Two Dimensional Array.
array_2d = np.array([[10,30,20],[60,50,40],[5,6,7]])
Saving to a text file
Saving to a CSV File
You can also see how I am not the square bracket for array_2d as here it is not required.
How to Save a Structured Numpy array in CSV file?
You can also save a Structured Numpy array to a CSV file. Those Numpy arrays that has a custom data type (dtype) is a structured Numpy array. Below is the code for the Structured Numpy array.
#type of the numpy array structure dtype = [('Name', (np.str_, 10)), ('RollNo', np.int32), ('Marks', np.float64)] strud_array = np.array([("Sahil",15,92),("Abhishek",16,98),("John",17,100)],dtype=dtype)
Here I am defining the name, roll no, and marks with their corresponding type.
Now you can save the strud_array with the header as Name, RollNo, and Marks. It will act as the column name in your CSV File.
np.savetxt('strud_array.csv', strud_array, delimiter=',', fmt=['%s' , '%d', '%f'], header='Name,RollNo,Marks', comments='')
Save more than one NumPy array
In this section, I will show you how you can save more than one Numpy array to both text file and CSV file.
Let’s create three Numpy array.
array1 = np.arange(100,200,10) array2 = np.arange(200,300,10) array3 = np.arange(300,400,10)
Save to text file
Saving to CSV File
Here In both cases, I am passing the set of Numpy array. All arrays will be appended to the next row of the file.
Numpy savetxt method is very useful for saving and retrieving your own dataset. You can manipulate or change any existing dataset and save it. These are examples I have coded for getting a deep understanding. If you have any queries then you can contact us for more information.
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.