NumPy File IO

NumPyNumPyBeginner
Practice Now

Introduction

In this lab, you will learn how to use NumPy to read and write arrays to files. NumPy provides several functions for file input and output that make it easy to work with large datasets.

Achievements

  • The savetxt()function
  • The save()function
  • The loadtxt()function
  • The genfromtxt()function
  • The load()function

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) numpy(("`NumPy`")) -.-> numpy/MathandStatisticsGroup(["`Math and Statistics`"]) numpy(("`NumPy`")) -.-> numpy/FileInputOutputGroup(["`File Input/Output`"]) python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") numpy/MathandStatisticsGroup -.-> numpy/rand_num("`Random Numbers`") numpy/FileInputOutputGroup -.-> numpy/text_io("`Text File Input/Output`") numpy/FileInputOutputGroup -.-> numpy/bin_io("`Binary File Input/Output`") subgraph Lab Skills python/tuples -.-> lab-127{{"`NumPy File IO`"}} python/importing_modules -.-> lab-127{{"`NumPy File IO`"}} python/standard_libraries -.-> lab-127{{"`NumPy File IO`"}} python/math_random -.-> lab-127{{"`NumPy File IO`"}} python/numerical_computing -.-> lab-127{{"`NumPy File IO`"}} python/build_in_functions -.-> lab-127{{"`NumPy File IO`"}} numpy/rand_num -.-> lab-127{{"`NumPy File IO`"}} numpy/text_io -.-> lab-127{{"`NumPy File IO`"}} numpy/bin_io -.-> lab-127{{"`NumPy File IO`"}} end

Writing Arrays to Files

NumPy provides several functions for writing arrays to files. The most common are savetxt and save.

Open the Python Shell

Open the Python shell by typing the following command in the terminal.

python3

Import NumPy

NumPy is already installed, you can import it in your Python code:

import numpy as np

Using Savetxt

The savetxt function is used to write arrays to text files. Here is an example:

data = np.random.rand(10, 5)
np.savetxt('data.txt', data, delimiter=',')
  • This will write the contents of data to a text file called data.txt, separating the values by commas.

Using save

The save function is used to write arrays to binary files. Here is an example:

np.save('data.npy', data)
  • This will write the contents of data to a binary file called data.npy.

Reading Arrays from Files

NumPy provides several functions for reading arrays from files. The most common are loadtxt, genfromtxt and load.

Using loadtxt

The loadtxt function is used to read arrays from text files.Here is an example:

data = np.loadtxt('data.txt',delimiter=',')
print(data)
  • This will read the contents of data.txt into a NumPy array, from step 1 we know that the values in data.txt are separated by commas.
  • The code print(data) will print the content read from data.txt.

Using Genfromtxt

The genfromtxt function is similar to loadtxt, but it can handle missing values and other special cases. Here is an example:

data = np.genfromtxt('data.txt', delimiter=',')
print(data)
  • This will read the contents of data.txt into a NumPy array.

Using Load

The load function is used to read the arrays from binary files. Here is an example:

data = np.load('data.npy')
print(data)
  • This will read the contents of data.npy into a NumPy array.

Summary

Congratulations! You have completed the NumPy File IO Lab.

In this lab, you learned how to:

  • Use NumPy's savetxt() and save() functions to write arrays to files.
  • Use NumPy's loadtxt(), genfromtxt() and load() functions to read arrays from files.

Other NumPy Tutorials you may like