<jupyter_start><jupyter_text>1.3. Introducing the multidimensional array in NumPy for fast array computations<jupyter_code>import random
import numpy as np
n = 10
x = [random.random() for _ in range(n)]
y = [random.random() for _ in range(n)]
x, y
z = [x[i] + y[i] for i in range(n)]
z
%timeit [x[i] + y[i] for i in range(n)]
xa = np.array(x)
ya = np.array(y)
xa
za = xa + ya
za
%timeit xa + ya
%timeit sum(x)  # pure Python
%timeit np.sum(xa)  # NumPy, so much faster
d = [abs(x[i] - y[j])
     for i in range(10)
     for j in range(10)]
d
da = np.abs(xa[:10, np.newaxis] - ya[:10])
da
%timeit [abs(x[i] - y[j]) \
         for i in range(10) \
         for j in range(10)]
%timeit np.abs(xa[:,np.newaxis] - ya)<jupyter_output>2.9 µs ± 75.9 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
{'fraction_non_alphanumeric': 0.1647940074906367, 'fraction_numerical': 0.03495630461922597, 'mean_word_length': 3.265957446808511, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '603189', 'license': 'MIT', 'n_tokens_mistral': 365, 'n_tokens_neox': 342, 'n_words': 114, 'path': 'chapter01_basic/03_numpy.ipynb', 'repo_name': 'shelly77/cookbook-2nd-code', 'size': '801'}