Get startedGet started for free

Saving and loading arrays

1. Saving and loading arrays

This lesson focuses on saving and loading arrays for use elsewhere in our code.

2. RGB arrays

Let's start by introducing a type of 3D array used in image-based machine learning: the RGB array. Each 1D array in the 3D array contains red, green, and blue values which together describe the color of a single pixel. The top-left pixel in this array has a value of 255 for red and zero for green and blue, so the result is a red pixel. Each 2D array represents a row of pixel colors. The collection of all rows of pixels is a 3D array.

3. RGB arrays

Here, RGB colors are mixed. In the top middle pixel, high red and green values create yellow. In the bottom left corner, three zero values create black, and in the top right three 255 values create white.

4. Loading .npy files

Let's load an RGB array and plot it to see what it contains. NumPy arrays can be saved in many formats, including, dot-csv, dot-txt, and pickle files. In terms of speed and storage efficiency, though, NumPy's own dot-npy file type is by far the best. To load a dot-npy file, use the keyword with, followed by Python's open function. Open takes two arguments: the first is the name of the file to open, and the second is the mode to open the file in. To load, pass "rb", which stands for "read binary". Alias the opened file as "f". We'll call the loaded array logo_rgb_array and set it equal to the np-dot-load function with f passed as an argument. Then, we display the image using Matplotlib by passing the array to plt-dot-imshow and displaying the resulting plot with plt-dot-show.

5. Examining RGB data

Let's pull apart the red, green, and blue pixel information from logo_rgb_array by slicing along the third axis.

6. Examining RGB data

Printing the top row of each of the resulting 2D arrays, we see that all values are 255: the logo has white pixels across that entire first row.

7. Updating RGB data

Replacing all elements equal to 255 with the number 50 using the help of np-dot-where will swap this white background with a dark one. Recall that np-dot-where accepts three arguments: a condition, how to change the element if it meets the condition, and how to change the element if it does not.

8. Saving arrays as .npy files

To save dark_logo_array in a dot-npy file, the syntax is similar to loading an array. Pass "wb", which stands for "write binary", to the open function. dark_logo_array is saved in a file called dark_logo-dot-npy using the np-dot-save function. dark_logo-dot-npy does not need to exist in the directory before this code is run: a new dot-npy file will be created if it does not exist or overwritten if it does.

9. If we need help()...

As we advance in NumPy, it's helpful to be familiar with Python's all-purpose help function. If we encounter a function such as np-dot-unique and don't know what it does, we can call help and pass np-dot-unique as an argument without its trailing parentheses. We see that np-dot-unique returns an array comprised of only the unique elements in the array passed to it.

10. numpy.org documentation

The result is the same documentation you can find at numpy-dot-org, and it's faster than searching online.

11. help() with methods

To look up the documentation of a method, prefix it with the object type that the method is called on: in this case, a NumPy n-dimensional array.

12. Let's practice!

Alright, let's get loading!