5 Input And Output Solutions
title: "Input and Output" date: 2026-05-24T13:51:46Z
Input and Output
1from __future__ import print_function
2import numpy as np
1author = "kyubyong. https://github.com/Kyubyong/numpy_exercises"
1np.__version__
'1.12.0'
1from datetime import date
2print(date.today())
2017-04-01
NumPy binary files (NPY, NPZ)
Q1. Save x into temp.npy and load it.
1x = np.arange(10)
2np.save('temp.npy', x) # Actually you can omit the extension. If so, it will be added automatically.
3
4# Check if there exists the 'temp.npy' file.
5import os
6if os.path.exists('temp.npy'):
7 x2 = np.load('temp.npy')
8 print(np.array_equal(x, x2))
True
Q2. Save x and y into a single file 'temp.npz' and load it.
1x = np.arange(10)
2y = np.arange(11, 20)
3np.savez('temp.npz', x=x, y=y)
4# 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.
5with np.load('temp.npz') as data:
6 x2 = data['x']
7 y2 = data['y']
8 print(np.array_equal(x, x2))
9 print(np.array_equal(y, y2))
True
True
Text files
Q3. Save x to 'temp.txt' in string format and load it.
1x = np.arange(10).reshape(2, 5)
2header = 'num1 num2 num3 num4 num5'
3np.savetxt('temp.txt', x, fmt="%d", header=header)
4np.loadtxt('temp.txt')
array([[ 0., 1., 2., 3., 4.],
[ 5., 6., 7., 8., 9.]])
Q4. Save x, y, and z to 'temp.txt' in string format line by line, then load it.
1x = np.arange(10)
2y = np.arange(11, 21)
3z = np.arange(22, 32)
4np.savetxt('temp.txt', (x, y, z), fmt='%d')
5np.loadtxt('temp.txt')
array([[ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.],
[ 11., 12., 13., 14., 15., 16., 17., 18., 19., 20.],
[ 22., 23., 24., 25., 26., 27., 28., 29., 30., 31.]])
Q5. Convert x into bytes, and load it as array.
1x = np.array([1, 2, 3, 4])
2x_bytes = x.tostring() # Don't be misled by the function name. What it really does is it returns bytes.
3x2 = np.fromstring(x_bytes, dtype=x.dtype) # returns a 1-D array even if x is not.
4print(np.array_equal(x, x2))
True
Q6. Convert a into an ndarray and then convert it into a list again.
1a = [[1, 2], [3, 4]]
2x = np.array(a)
3a2 = x.tolist()
4print(a == a2)
True
String formatting¶
Q7. Convert x to a string, and revert it.
1x = np.arange(10).reshape(2,5)
2x_str = np.array_str(x)
3print(x_str, "\n", type(x_str))
4x_str = x_str.replace("[", "") # [] must be stripped
5x_str = x_str.replace("]", "")
6x2 = np.fromstring(x_str, dtype=x.dtype, sep=" ").reshape(x.shape)
7assert np.array_equal(x, x2)
[[0 1 2 3 4]
[5 6 7 8 9]]
<class 'str'>
Text formatting options
Q8. Print x such that all elements are displayed with precision=1, no suppress.
1x = np.random.uniform(size=[10,100])
2np.set_printoptions(precision=1, threshold=np.nan, suppress=True)
3print(x)
[[ 0.5 0. 0.8 0.2 0.3 0.2 0.2 1. 0.4 0.8 0.6 0.2 0.5 0.1
0.4 0.1 0.9 0.6 0.1 0.5 0.8 0.8 0.8 0. 0.6 0.8 0.4 0.3
0.8 0.2 0.7 0.7 0.2 1. 0.8 0.1 0.2 0.1 0.3 0.1 0.5 0.9
0.6 0.9 0.6 0.5 0.8 0.3 0.3 0.5 0.1 0.6 0.1 0.3 0.6 0.2
0.4 0.8 0.6 0.4 0.2 0.6 0. 0.3 0.8 0.5 0.7 0.9 0.8 0.6
0.9 0.8 0.4 0.4 0.7 0.8 0. 0.1 0.5 0.4 0.7 1. 0.1 0.2
0.6 0.3 0.9 0.1 0.6 0.4 0.3 0.8 0.3 0.6 0.6 0.3 1. 0.2
0.9 0.2]
[ 0.9 0.2 0.4 0.9 0.5 0.6 0.1 0.7 0. 0. 0.1 0.8 0.8 1. 0.2
0.8 0.3 0.2 1. 0.6 1. 0.3 0.4 0.4 0.7 0.5 0.4 0.8 0.5
0.9 0.3 0.5 0.7 0.4 0.2 0.3 0.9 0. 0.6 0.8 0.3 0.5 0.2
0.3 0. 0.6 0.5 0.2 0.5 0.8 0.2 0.8 0. 0.9 0. 0.7 0.1
0.4 0.2 0.5 0.6 0.2 0.6 0.1 0.1 0. 0.5 0.9 0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.6 0.2 0.7 0.4 0.8 0.2 0.9 1. 1.
0.7 0.9 0.1 0.2 0. 0.5 0.8 0.7 0.6 0.7 0.7 0.5 0.9 0.2
0.2 0.1 0.2 0.1 0.7 1. 0.6 0.3 0.9 1. 0.3 0.3 0.7 0.9
0.5 0.8 0.9 0.7 0.2 0.7 0.3 0.1 0.9 0.2 0.5 0.6 0.3 0.4]]
Base-n representations
Q9. Convert 12 into a binary number in string format.
1out1 = np.binary_repr(12)
2out2 = np.base_repr(12, base=2)
3assert out1 == out2 # But out1 is better because it's much faster.
4print(out1)
1100
Q10. Convert 12 into a hexadecimal number in string format.
1np.base_repr(1100, base=16)
'44C'