{"id":6890,"date":"2022-08-29T20:00:22","date_gmt":"2022-08-30T01:00:22","guid":{"rendered":"https:\/\/www.sharpsightlabs.com\/?p=6890"},"modified":"2022-08-29T21:29:35","modified_gmt":"2022-08-30T02:29:35","slug":"numpy-load","status":"publish","type":"post","link":"https:\/\/www.sharpsightlabs.com\/blog\/numpy-load\/","title":{"rendered":"Numpy Load, Explained"},"content":{"rendered":"
In this tutorial, I’ll show you how to use Numpy load to load Numpy arrays from stored npy or npz files.<\/p>\n
So I’ll explain what Numpy load does, I’ll explain the syntax of np.load, and I’ll show you step-by-step examples of how to use it.<\/p>\n
If you need something specific, you can click on any of the following links:<\/p>\n
Table of Contents:<\/strong><\/p>\n <\/a><\/p>\n The Numpy load function is pretty straight forward: it “loads” Numpy arrays from Numpy storage files (i.e., That being the case, let’s quickly review stored Numpy files.<\/p>\n As you probably know, Numpy is a data science package for Python<\/a> that is used for numerical data manipulation and analysis.<\/p>\n When we use Numpy, we use it inside our Python working environment.<\/p>\n But sometimes, we need to store our data in an external file that we can put on a computer disk for long term storage. These long-term files that store Numpy array data are Later though, if we want to work with those stored arrays again, we need to re-load those array files from those How do we do this?<\/p>\n We do it with Numpy load.<\/p>\n <\/p>\n So essentially, we put Numpy array data in long-term storage with Numpy save, and we can load it back into our working environment later with Numpy load.<\/p>\n Numpy load is fairly easy to use, but a few details depend on how exactly you use the syntax.<\/p>\n Let’s take a look at it.<\/p>\n <\/a><\/p>\n Here, we’ll look at the exact syntax of Numpy load including some of the details like optional parameters.<\/p>\n One note about the syntax.<\/p>\n Whenever we use a Python package such as Numpy, we need to load it into our environment before we can use it.<\/p>\n Exactly how<\/em> we load a package actually impacts the syntax.<\/p>\n The reason for this is because we often import a package with an alias<\/em>.<\/p>\n In the case of Numpy, we typically import it with the alias Again, this is important, because when we import Numpy like this, it allows us to call Numpy functions with the prefix Everything else going forward in this tutorial assumes that you’ve imported Numpy with the In the simplest case, the syntax for Numpy load is simple.<\/p>\n Assuming that you’ve imported Numpy with the alias Inside the parenthesis, you provide the name of the <\/p>\n The first argument to the function is the name of the file from which you want to load your data.<\/p>\n Then there are a few optional parameters that you can use to modify the behavior of the function.<\/p>\n <\/a><\/p>\n Let’s look quickly at the parameters of Numpy load:<\/p>\n The Keep in mind that this argument is a positional argument. Python assumes that the first input to Numpy load is the name of the file you want to load.<\/p>\n This filename will typically be a The By default, this is set to Other possible arguments to this parameter are: <\/p>\n If you use any of these arguments, Numpy load will memory map the file using the mode that’s specified by the argument that you choose.<\/p>\n Note that according to the official documentation, “memory mapping is especially useful for accessing small fragments of large files without reading the entire file into memory.”<\/p>\n This parameter controls whether the function will be able to load “pickled” arrays stored in an By default, this parameter is set to Loading pickled data can cause security issues, which is one reason why this parameter is set to The By default, this parameter is set to This parameter controls what encoding By default, this is set to Numpy load returns the Numpy arrays that are stored in the file.<\/p>\n As you’ll see in the examples, this can be either a single numpy array, in the case of an Or it can be multiple arrays, in the case of an You’ll see an example of this in example 2<\/a>.<\/p>\n <\/a><\/p>\n Now that you’ve seen the syntax, let’s look at some simple examples of how to use Numpy load.<\/p>\n Examples:<\/strong><\/p>\n Before you run the examples, you’ll need to run some preliminary setup code.<\/p>\n Specifically, you need to import Numpy.<\/p>\n You can do that with this code:<\/p>\n Remember: when we import Numpy with the alias <\/a><\/p>\n In this example, we’re going to load a single numpy array from an This will require a few steps. We will:<\/p>\n Let’s do each of those, one at a time.<\/p>\n First, we’l create a Numpy simple array that we can store, and then load.<\/p>\n To do this, we’ll use the Numpy array function<\/a> to manually create an array with a specific set of values.<\/p>\n We’ll call np.array, and store the array with the name, Now, let’s print it out:<\/p>\n OUT:<\/p>\n This is a simple array with 2 rows and 3 columns. <\/p>\n We’ll be able to save this with Numpy save<\/a>, and then load it with Numpy load.<\/p>\n Now, we’ll save our Numpy array in order to create an Here, we’ve called np.save and created an Now, we’re going to load our Numpy array by calling OUT:<\/p>\n As you can see, when you call np.load, it loads the array stored in the .npy file to your Python environment. Remember: this is the array that we created previously and stored with Numpy save.<\/p>\n Importantly, the way that I’ve called np.load here, the array has simply been sent to the console.<\/p>\n If you want to keep that array with a variable name, you need to pass the output to a variable, like this:<\/p>\n <\/a><\/p>\n In this example, we’re going to load two<\/em> Numpy arrays. This time, we’re going to load our arrays from an Again: this will require multiple steps. We will:<\/p>\n Let’s do it.<\/p>\n For the first step of this example, we’re going to create two Numpy arrays.<\/p>\n Here, we’ll create a 1-dimensional of integers using Numpy arange<\/a>.<\/p>\n We’ll also create an array with the logs of those numbers<\/a> using Numpy log.<\/p>\n And let’s print those arrays out:<\/p>\n OUT:<\/p>\n We’re going to save these arrays with specific names, using Numpy savez<\/a>.<\/p>\n Here, we’re going to use Numpy savez to save both of these Numpy arrays to a single Moreover, we’re going to save those files with specific names<\/em> inside the To learn more about how this works, you can read our tutorial about Numpy savez<\/a>.<\/p>\n And now that we have the arrays stored to an Here, we’ve loaded the When we load an npz file like this, we can inspect the contents using the OUT:<\/p>\n As you can see, there are two arrays in this loaded npz file. These are the Numpy arrays that we stored in the previous section using Numpy save.<\/p>\n To retrieve the contents of the arrays, we can treat OUT:<\/p>\n Here, we’ve used the file names associated with <\/a><\/p>\n Now that you’ve learned about Numpy load and seen some examples, let’s review some frequently asked questions about the function.<\/p>\n Frequently asked questions:<\/strong><\/p>\n <\/a><\/p>\n Numpy load works with To learn more, you can read our tutorial about Numpy loadtxt<\/a>.<\/p>\n Do you have other questions about Numpy load? <\/p>\n If so, leave your questions in the comments section below.<\/p>\n In this tutorial, I’ve shown you how to use Numpy load.<\/p>\n Numpy load is useful if you strictly need to load a Numpy array, but if you really want to master numeric data manipulation in Python, you’ll need to learn a lot more Numpy.<\/p>\n That said, if you’re serious about learning Numpy, you should consider joining our premium course called Numpy Mastery<\/em>.<\/p>\n Numpy Mastery will teach you everything you need to know about Numpy, including:<\/p>\n The course will also provide you with our unique practice system. This practice system will enable you to memorize<\/em> all of the Numpy syntax that you learn.<\/p>\n If you’re struggled to remember Numpy syntax, this is the course you’ve been looking for.<\/p>\n If you practice like we show you, you’ll memorize all of the critical Numpy syntax in only a few weeks.<\/p>\n Find out more here:<\/p>\n <\/p>\n \nLearn More About Numpy Mastery<\/a>\n<\/p>\n <\/strong><\/p>\n","protected":false},"excerpt":{"rendered":" In this tutorial, I’ll show you how to use Numpy load to load Numpy arrays from stored npy or npz files. So I’ll explain what Numpy load does, I’ll explain the syntax of np.load, and I’ll show you step-by-step examples of how to use it. If you need something specific, you can click on any … Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":6908,"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,79,62,58],"tags":[],"publishpress_future_action":{"enabled":false,"date":"2024-05-23 22:06:40","action":"change-status","newStatus":"draft","terms":[],"taxonomy":"category"},"_links":{"self":[{"href":"https:\/\/www.sharpsightlabs.com\/wp-json\/wp\/v2\/posts\/6890"}],"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=6890"}],"version-history":[{"count":0,"href":"https:\/\/www.sharpsightlabs.com\/wp-json\/wp\/v2\/posts\/6890\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sharpsightlabs.com\/wp-json\/wp\/v2\/media\/6908"}],"wp:attachment":[{"href":"https:\/\/www.sharpsightlabs.com\/wp-json\/wp\/v2\/media?parent=6890"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sharpsightlabs.com\/wp-json\/wp\/v2\/categories?post=6890"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sharpsightlabs.com\/wp-json\/wp\/v2\/tags?post=6890"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}\n
A Quick Introduction to Numpy Load<\/h2>\n
.npy<\/code> files and
.npz<\/code> files).<\/p>\n
A quick review of Numpy and stored Numpy files<\/h3>\n
.npy<\/code> and
.npz<\/code> files. We can create
.npy<\/code> and
.npz<\/code> files with Numpy save<\/a> and Numpy savez<\/a>.<\/p>\n
.npy<\/code> or
.npz<\/code> files back into our working environment.<\/p>\n
The syntax of Numpy Load<\/h2>\n
A quick note<\/h4>\n
np<\/code>, like this:<\/p>\n
\r\nimport numpy as np\r\n<\/pre>\n
np<\/code>.<\/p>\n
import<\/code> statement shown above.<\/p>\n
np.load syntax<\/h3>\n
np<\/code>, you type the function as
np.load()<\/code>.<\/p>\n
.npy<\/code> or
.npz<\/code> file that you want to load.<\/p>\n
The Parameters and Inputs of Numpy Load<\/h3>\n
\n
filename<\/code><\/li>\n
mmap_mode<\/code><\/li>\n
allow_pickle<\/code><\/li>\n
fix_imports<\/code><\/li>\n
encoding<\/code><\/li>\n<\/ul>\n
filename<\/code> (required)<\/h6>\n
filename<\/code> argument is the first input to the function, and it is required.<\/p>\n
.npy<\/code> file or an
.npz<\/code> file.<\/p>\n
mmap_mode<\/code><\/h6>\n
mmap_mode<\/code> parameter controls whether or not the function uses memory mapping<\/a>.<\/p>\n
mmap_mode = None<\/code>.<\/p>\n
\n
allow_pickle<\/code><\/h6>\n
npy<\/code> or
npz<\/code> file.<\/p>\n
allow_pickle = False<\/code>. <\/p>\n
False<\/code> by default.<\/p>\n
fix_imports<\/code><\/h6>\n
fix_imports<\/code> parameter is helpful if you attempt to load pickled data that was stored on a Python 2 system into a Python 3 system.<\/p>\n
fix_imports = True<\/code>. <\/p>\n
encoding<\/code><\/h6>\n
np.load<\/code> uses when it reads Python 2 strings.<\/p>\n
encoding='ASCII'<\/code>.<\/p>\n
Numpy Load Output<\/h3>\n
.npy<\/code> file.<\/p>\n
.npz<\/code> file. In this case, you will be able to access the arrays by name, using the stored name of the array as a “key” (much like a dictionary).<\/p>\n
Examples of How to Use Numpy Load<\/h2>\n
\n
.npy<\/code> file<\/a><\/li>\n
Run this code to import Numpy<\/h4>\n
\r\nimport numpy as np\r\n<\/pre>\n
np<\/code>, we can use
np<\/code> as a prefix when we call the function. I explained this in the syntax section above.<\/p>\n
EXAMPLE 1: Load a single Numpy array from an .npy file<\/h3>\n
.npy<\/code> file.<\/p>\n
\n
.npy<\/code> file<\/li>\n
Create Array<\/h4>\n
my_array<\/code>.<\/p>\n
\r\n# CREATE NUMPY ARRAY\r\nmy_array = np.array([[0,2,4],[1,3,5]])\r\n<\/pre>\n
\r\nprint(my_array)\r\n<\/pre>\n
\r\n[[0 2 4]\r\n [1 3 5]]\r\n<\/pre>\n
Create npy file<\/h4>\n
.npy<\/code> file.<\/p>\n
\r\nnp.save('my_array_temp.npy', my_array)\r\n<\/pre>\n
.npy<\/code> file called
my_array_temp.npy<\/code>.<\/p>\n
Load npy file<\/h4>\n
np.load<\/code> on the file we just created,
my_array_temp.npy<\/code>.<\/p>\n
\r\nnp.load('my_array_temp.npy')\r\n<\/pre>\n
\r\narray([[0, 2, 4],\r\n [1, 3, 5]])\r\n<\/pre>\n
\r\nmy_reloaded_array = np.load('my_array_temp.npy')\r\n<\/pre>\n
EXAMPLE 2: Load a multiple Numpy arrays from an .npz file, with specific array names<\/h3>\n
.npz<\/code> file, where we’ve stored the arrays with specific names<\/a>.<\/p>\n
\n
.npz<\/code> file, with specific names<\/em><\/li>\n
Create Arrays<\/h4>\n
\r\ninteger_array = np.arange(start = 1, stop = 6)\r\nlog_array = np.log(integer_array)\r\n<\/pre>\n
\r\nprint(integer_array)\r\nprint(log_array)\r\n<\/pre>\n
\r\n[1 2 3 4 5]\r\n\r\n[0. 0.69314718 1.09861229 1.38629436 1.60943791]\r\n<\/pre>\n
Create npz file<\/h4>\n
.npz<\/code> file.<\/p>\n
.npz<\/code> file.<\/p>\n
\r\nnp.savez('my_arrays_temp.npz', int_arr = integer_array, log_arr = log_array)\r\n<\/pre>\n
Load the Arrays<\/h4>\n
npz<\/code> file, we’ll load the arrays with Numpy load.<\/p>\n
\r\nloaded_arrays = np.load('my_arrays_temp.npz')\r\n<\/pre>\n
npz<\/code> file to the variable called
loaded_arrays<\/code>.<\/p>\n
files<\/code> attribute:<\/p>\n
\r\nloaded_arrays.files\r\n<\/pre>\n
\r\n['int_arr', 'log_arr']\r\n<\/pre>\n
loaded_arrays<\/code> like a container, and use the file names we just printed out as the keys. This will retrieve the stored numpy arrays:<\/p>\n
\r\nloaded_arrays['array_2d']\r\nloaded_arrays['linspace_array_1d']\r\n<\/pre>\n
\r\narray([1, 2, 3, 4, 5])\r\n\r\narray([0., 0.69314718, 1.09861229, 1.38629436, 1.60943791])\r\n<\/pre>\n
loaded_arrays<\/code>, and loaded them by referencing the file names like container keys. Provide the file name and it retrieves the stored Numpy array.<\/p>\n
Frequently asked questions about Numpy Load<\/h2>\n
\n
Question 1: How do you load Numpy data from a text file?<\/h3>\n
.npy<\/code> or
.npz<\/code> files, but if you want to load numeric data that’s stored in a text file, you need to use Numpy loadtxt.<\/p>\n
Leave your other questions in the comments below<\/h5>\n
Join our course to learn more about Numpy<\/h2>\n
\n