<jupyter_start><jupyter_text>Input and Output<jupyter_code>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())<jupyter_output>2021-03-13<jupyter_text>NumPy binary files (NPY, NPZ) Q1. Save x into `temp.npy` and load it.<jupyter_code>x = np.arange(10)
np.save('temp.npy', x)

# 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))<jupyter_output>True<jupyter_text>Q2. Save x and y into a single file 'temp.npz' and load it.<jupyter_code>x = np.arange(10)
y = np.arange(11, 20)
np.savez('temp.npz', x=x, y=y)

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))<jupyter_output>True
True<jupyter_text>Text files Q3. Save x to 'temp.txt' in string format and load it.<jupyter_code>x = np.arange(10).reshape(2, 5)
header = 'num1 num2 num3 num4 num5'
np.savetxt('temp.txt', x, header=header)
np.loadtxt('temp.txt')<jupyter_output><empty_output><jupyter_text>Q4. Save `x`, `y`, and `z` to 'temp.txt' in string format line by line, then load it.<jupyter_code>x = np.arange(10)
y = np.arange(11, 21)
z = np.arange(22, 32)
np.savetxt('temp.txt', (x, y, z))
np.loadtxt('temp.txt')<jupyter_output><empty_output><jupyter_text>Q5. Convert `x` into bytes, and load it as array.<jupyter_code>x = np.array([1, 2, 3, 4])
x_bytes = x.tostring()
x2 = np.frombuffer(x_bytes, dtype=x.dtype)
print(np.array_equal(x, x2))<jupyter_output>True<jupyter_text>Q6. Convert `a` into an ndarray and then convert it into a list again.<jupyter_code>a = [[1, 2], [3, 4]]
x = np.array(a)
a2 = x.tolist()
print(a == a2)<jupyter_output>True<jupyter_text>String formatting¶ Q7. Convert `x` to a string, and revert it.<jupyter_code>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)<jupyter_output>[[0 1 2 3 4]
 [5 6 7 8 9]] 
 <class 'str'><jupyter_text>Text formatting options Q8. Print `x` such that all elements are displayed with precision=1, no suppress.<jupyter_code>import sys
x = np.random.uniform(size=[10, 100])
np.set_printoptions(precision=1, threshold=sys.maxsize, suppress=True)
print(x)<jupyter_output>[[0.6 1.  0.6 0.1 0.6 0.2 0.5 0.9 0.6 0.  0.6 0.2 0.9 0.7 0.  0.4 0.6 0.9
  0.3 0.4 0.8 0.8 0.6 0.3 0.2 0.6 0.2 0.9 0.9 0.7 0.  0.4 1.  0.6 0.8 0.8
  0.1 0.4 0.4 0.1 0.2 1.  0.3 0.  0.6 0.9 0.7 0.1 0.9 0.2 0.9 0.9 0.9 0.6
  1.  0.  1.  1.  0.6 0.4 0.9 0.3 0.6 0.  0.1 0.7 0.9 0.  0.7 0.7 0.8 0.7
  0.5 0.3 0.1 0.6 0.9 0.8 0.2 0.8 0.2 0.6 0.1 0.5 0.8 0.4 1.  0.5 0.3 0.4
  0.8 0.2 0.2 0.5 0.4 0.1 0.8 0.1 0.3 0. ]
 [0.7 0.7 0.6 0.1 0.1 1.  0.6 0.  0.2 0.6 0.7 0.7 0.9 0.9 1.  0.8 0.9 0.9
  0.8 0.8 0.9 0.4 0.8 0.2 0.  0.6 0.6 0.9 0.7 0.7 0.3 0.1 0.3 0.8 0.8 0.8
  0.6 0.7 0.8 0.6 0.  0.6 0.3 0.7 0.1 0.9 0.6 0.2 0.3 0.7 0.4 0.6 0.  0.4
  0.1 0.  0.8 1.  0.  0.2 0.2 0.6 0.5 0.7 0.9 0.5 1.  0.5 0.7 0.1 1.  0.2
  0.8 0.  1.  0.2 0.  1.  0.9 0.6 0.7 0.2 0.2 0.6 0.4 0.5 0.9 0.5 0.6 0.3
  0.3 0.5 0.6 0.8 0.3 0.  0.  0.1 0.2 0.6]
 [0.  0.8 0.5 0.3 0.9 0.6 0.5 0.3 0.4 0.5 0.1 0.9 0.7 0.6 0.1 0.6 0.6 0.5
  0.5 0.5 1.  0.9 0.4 0.1 0.4 0.4 0.9 0.3 0.9 0.9 0.1 0.9 0.4 0.9 0.  0.
  0.7 0.2 0.5 0.3 0.4 0.  0[...]<jupyter_text>Base-n representations Q9. Convert 12 into a binary number in string format.<jupyter_code>np.binary_repr(12)<jupyter_output><empty_output><jupyter_text>Q10. Convert 12 into a hexadecimal number in string format.<jupyter_code>np.base_repr(1100, base=16)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1829652996845426, 'fraction_numerical': 0.14379600420609884, 'mean_word_length': 2.959417273673257, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '612178', 'license': 'MIT', 'n_tokens_mistral': 2171, 'n_tokens_neox': 1919, 'n_words': 504, 'path': '05_Input_and_Output.ipynb', 'repo_name': 'zarkaltair/numpy_exercises', 'size': '3804'}