Input and Output – SolutionsΒΆ

These solutions demonstrate how to save and load NumPy arrays using binary formats (.npy, .npz), text files, string conversions, and formatting options. Pay attention to the trade-offs between binary and text formats: binary preserves exact types and is fast, while text is human-readable and portable.

from __future__ import print_function
import numpy as np
author = "kyubyong. https://github.com/Kyubyong/numpy_exercises"
np.__version__
from datetime import date
print(date.today())

NumPy Binary Files (NPY, NPZ)ΒΆ

Solutions using np.save(), np.load(), and np.savez() for binary array persistence.

Q1. Save x into temp.npy and load it.

x = np.arange(10)
np.save('temp.npy', x) # Actually you can omit the extension. If so, it will be added automatically.

# Check if there exists the 'temp.npy' file.
import os
if os.path.exists('temp.npy'):
    x2 = np.load('temp.npy')
    print(np.array_equal(x, x2))

Q2. Save x and y into a single file β€˜temp.npz’ and load it.

x = np.arange(10)
y = np.arange(11, 20)
np.savez('temp.npz', x=x, y=y)
# np.savez_compressed('temp.npz', x=x, y=y) # If you want to save x and y into a single file in compressed .npz format.
with np.load('temp.npz') as data:
    x2 = data['x']
    y2 = data['y']
    print(np.array_equal(x, x2))
    print(np.array_equal(y, y2))

Text FilesΒΆ

Solutions using np.savetxt(), np.loadtxt(), .tostring(), .tolist(), and related functions for text-based I/O.

Q3. Save x to β€˜temp.txt’ in string format and load it.

x = np.arange(10).reshape(2, 5)
header = 'num1 num2 num3 num4 num5'
np.savetxt('temp.txt', x, fmt="%d", header=header) 
np.loadtxt('temp.txt')

Q4. Save x, y, and z to β€˜temp.txt’ in string format line by line, then load it.

x = np.arange(10)
y = np.arange(11, 21)
z = np.arange(22, 32)
np.savetxt('temp.txt', (x, y, z), fmt='%d')
np.loadtxt('temp.txt')

Q5. Convert x into bytes, and load it as array.

x = np.array([1, 2, 3, 4])
x_bytes = x.tostring() # Don't be misled by the function name. What it really does is it returns bytes.
x2 = np.fromstring(x_bytes, dtype=x.dtype) # returns a 1-D array even if x is not.
print(np.array_equal(x, x2))

Q6. Convert a into an ndarray and then convert it into a list again.

a = [[1, 2], [3, 4]]
x = np.array(a)
a2 = x.tolist()
print(a == a2)

String FormattingΒΆ

Solutions using np.array_str() and np.fromstring() for string-based array serialization.

Q7. Convert x to a string, and revert it.

x = np.arange(10).reshape(2,5)
x_str = np.array_str(x)
print(x_str, "\n", type(x_str))
x_str = x_str.replace("[", "") # [] must be stripped
x_str = x_str.replace("]", "")
x2 = np.fromstring(x_str, dtype=x.dtype, sep=" ").reshape(x.shape)
assert np.array_equal(x, x2)

Text Formatting OptionsΒΆ

Solutions using np.set_printoptions() to control array display precision and thresholds.

Q8. Print x such that all elements are displayed with precision=1, no suppress.

x = np.random.uniform(size=[10,100])
np.set_printoptions(precision=1, threshold=np.nan, suppress=True)
print(x)

Base-n RepresentationsΒΆ

Solutions using np.binary_repr() and np.base_repr() for number base conversion.

Q9. Convert 12 into a binary number in string format.

out1 = np.binary_repr(12)
out2 = np.base_repr(12, base=2)
assert out1 == out2 # But out1 is better because it's much faster.
print(out1)

Q10. Convert 12 into a hexadecimal number in string format.

np.base_repr(1100, base=16)