np.save: How to Save Numpy Array in CSV File

0
113
Np save | How To Save Numpy Array In Python

Numpy arrays are one of the most efficient data structures for preparing data in Python, and machine learning models like those in the scikit-learn library, and deep learning models like those in the Tensorflow and Keras library, expect input data in the form of NumPy arrays and make predictions in the format of Numpy arrays.

np.save

The np.save() is a mathematical function used to store the input array in a disk file with npy extension(.npy). The np.save() function saves an array to a binary file in the .npy format. The numpy save() function takes the file, arr, allow_pickle, and file_imports as arguments.

Sometimes we have a lot of data in the Numpy arrays that we need to save efficiently, but which we only need to use in another Python program. Therefore, we save the NumPy arrays into the native binary format that is efficient to both save and load.

This is standard for input data that has been prepared, such as cleaned and transformed data, that will need to be used as the basis for testing the range of machine learning models in the future or running many experiments.

The .npy file format is appropriate for the use case and is mere “NumPy format “. This can be achieved using the save() and specifying the filename and the array that is to be saved.

See the following syntax of the numpy save() function.

Syntax

numpy.save(file, arr, allow_pickle=True, fix_imports=True)

Parameters

file:

File or filename to which the data is saved. If the file is a string or Path, the .npy extension will be appended to the file name if it does not already have one. If the file is a file object, then the filename is unchanged.

allow_pickle:

Allow saving object arrays using Python pickles. Reasons for disallowing pickles include security (loading pickled data can execute arbitrary code) and portability (pickled objects may not be loadable on different Python installations). Default: True.

fix_imports: 

Only helpful in forcing objects in object arrays on Python 3 to be pickled in a Python 2 compatible way.

arr: Array data to be saved.

np.save() Function with example

See the following code.

# app.py

import numpy as np

a = np.arange(5)

# a is printed.
print("a is:")
print(a)

# The array is saved in the file npfile.npy
np.save('npfile', a)

print("The array is saved in the file npfile.npy")

Output

python3 app.py
a is:
[0 1 2 3 4]
The array is saved in the file npfile.npy

You can see npfile.npy file in your working directory.

How to read npy file in Python

We saved the numpy array in the .npy file in the above example. Next, we will read the file using the np.load() function and print the content of the npy file in the console.

See the following code.

# app.py

import numpy as np

# The array is loaded into data
data = np.load('npfile.npy')

print("The data is:")
print(data)

# Data is printed from geekfile.npy
print("The data is printed from npfile.npy")

Output

python3 app.py
The data is:
[0 1 2 3 4]
The data is printed from npfile.npy

After running the example, you will see the new file in the directory with ‘npfile.npy ‘.

You cannot see the contents of this file directly with the text editor because it is in binary format.

Saving numpy Array to CSV File (ASCII)

The most standard file format for storing numerical data in Computer files is the comma-separated variable format.

It is most likely that your training data and input data to your models are stored in CSV files.

It can be convenient to save the data to CSV files, such as the predictions from a model.

You can save the NumPy arrays to CSV files using the numpy savetxt() function.

Numpy savetxt() function takes the filename and array as arguments and saves the array into CSV format.

You must also define the delimiter; this is the character used to separate each variable in the file, most commonly a comma. This can be set via the “delimiter” argument.

See the following code.

# app.py

import numpy as np

# define data
data = np.asarray([[0, 1, 2, 3, 4]])

# save to csv file
np.savetxt('npfile.csv', data, delimiter=',')

print("The numpy array is saved in npfile.csv file")

Output

python3 app.py
The numpy array is saved in npfile.csv file

The npfile.csv file is created inside your project directory.

Running the example will define the NumPy array and save it to ‘npfile.csv ‘.

The array has the single row of data with 10 columns. Therefore, we would expect the data to be saved to a CSV file as a single row of data.

After running the above example, we can see the contents of ‘npfile.csv ‘.

The content of the npfile.csv file is the following.

0.000000000000000000e+00,1.000000000000000000e+00,2.000000000000000000e+00,3.000000000000000000e+00,4.000000000000000000e+00

We can see that the data is correctly saved as a single row and that the floating-point numbers in the array were saved with full precision.

Conclusion

The np save() function is used to save numpy arrays in the binary file. We have also seen how to save numpy arrays in the csv file.

That’s it for this tutorial.

See also

Numpy isrealobj()

Numpy expand_dims()

Numpy isreal()

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.