<jupyter_start><jupyter_text>Numpy experiments Import numpy package.<jupyter_code>import numpy as np<jupyter_output><empty_output><jupyter_text>Reshaping Optionally, the last index given in `reshape` can be `-1`, the shape will be such that the dimensions multiply to the size of the original array.<jupyter_code>a = np.arange(1, 21, dtype=np.int).reshape(4, -1)
a<jupyter_output><empty_output><jupyter_text>In three dimensions, the last dimension can be replaced by `-1`.<jupyter_code>b = a.reshape(2, 5, -1)
b<jupyter_output><empty_output><jupyter_text>Slicing Define 3D data with a "width" of 5, a "height" of 4, and a "depth" of 3.<jupyter_code>a = np.arange(1, 61, dtype=np.int).reshape((3, 4, -1))<jupyter_output><empty_output><jupyter_text>When the last dimension is -1, it is computed automatically if possible, i.e., if the product of the sizes in each dimension is the size of the array.<jupyter_code>a.shape<jupyter_output><empty_output><jupyter_text>When data is viewed as 3D, slice the "front" layer.<jupyter_code>a[0, :, :]<jupyter_output><empty_output><jupyter_text>Slice the middle layer.<jupyter_code>a[1, :, :]<jupyter_output><empty_output><jupyter_text>Slice the back layer.<jupyter_code>a[2, :, :]<jupyter_output><empty_output><jupyter_text>Slice the top layer (first row is front).<jupyter_code>a[:, 0, :]<jupyter_output><empty_output><jupyter_text>Slice the left layer (first row is front).<jupyter_code>a[:, :, 0]<jupyter_output><empty_output><jupyter_text>Introducing new leading dimension.<jupyter_code>a[None].shape<jupyter_output><empty_output><jupyter_text>Check that this is the original matrix, with size of leading dimension equal to 1.<jupyter_code>np.all(a == a[None][0])<jupyter_output><empty_output><jupyter_text>Add additional "last" dimension, check that the result is the top-front row, with each element in an individual dimension 1 subarray.<jupyter_code>a[:, :, :, None][0, 0]<jupyter_output><empty_output><jupyter_text>It is important to realize that slicing doesn't copy, i.e., a slice yields a reference to the original data. Consider the following matrix.<jupyter_code>b = np.arange(1, 21).reshape(4, 5)
b
c = b[1:-1, 1:-1]
c<jupyter_output><empty_output><jupyter_text>Now we modify an element of `c`<jupyter_code>c[0, 0] = 0
c<jupyter_output><empty_output><jupyter_text>Clearly, `c` was changed, but so is `b`.<jupyter_code>b<jupyter_output><empty_output><jupyter_text>Fancy indexing Create a new $4 \times 5$ array.<jupyter_code>a = np.arange(1, 21, dtype=np.int).reshape((4, -1))
a<jupyter_output><empty_output><jupyter_text>Select the second and fourth row.<jupyter_code>a[[1, 3], :]<jupyter_output><empty_output><jupyter_text>Select the first and fourth column.<jupyter_code>a[:, [0, 3]]<jupyter_output><empty_output><jupyter_text>Structured arrays Although strictly speaking not fancy indexing, we can create structured arrays, which you can think of as arrrays of structures. These arrays can be indexed by number, and by structure field name.<jupyter_code>b = np.zeros(10, dtype={'names': ('x', 'y'), 'formats': ('f8', 'f8')})
b<jupyter_output><empty_output><jupyter_text>Each element acts as a tuple, the first field has name `x` and type `np.float`, the second has name `y`, and is also `np.float`. The shape shows it is an 1-D array.<jupyter_code>b.shape<jupyter_output><empty_output><jupyter_text>The type of the elements is perhaps a bit surprising.<jupyter_code>type(b[0])<jupyter_output><empty_output><jupyter_text>We can initialize the `x` and `y` fields of each array element with a single assingment.<jupyter_code>b['x'] = np.random.uniform(size=10)
b['y'] = np.random.uniform(size=10)
b
b['x']<jupyter_output><empty_output><jupyter_text>Using a numerical index gives the corresponding element in the array.<jupyter_code>b[0]<jupyter_output><empty_output><jupyter_text>Accessing elements by field number or field name is equivalent.<jupyter_code>b[4][1] == b[4]['y']<jupyter_output><empty_output><jupyter_text>Stacking Create two matrices, both $4 \times 5$.<jupyter_code>a = np.arange(1, 21, dtype=np.int).reshape((4, -1))
b = np.arange(21, 41, dtype=np.int).reshape((4, -1))
print(a)
print(b)<jupyter_output><empty_output><jupyter_text>Stack matrices horizontally.<jupyter_code>np.hstack((a, b))<jupyter_output><empty_output><jupyter_text>Stack matrices vertically.<jupyter_code>np.vstack((a, b))<jupyter_output><empty_output><jupyter_text>Create a list of 3 arrays, each $4 \times 5$.<jupyter_code>arrays_2d = []
size_2d = 20
for i in range(3):
    upperleft = i*size_2d + 1
    arrays_2d.append(np.arange(upperleft, upperleft + size_2d, dtype=np.int).reshape(4, -1))<jupyter_output><empty_output><jupyter_text>Create a 3D array out of the list of 2D arrays.<jupyter_code>array_3d = np.array(arrays_2d)

array_3d.shape<jupyter_output><empty_output><jupyter_text>Get front plane.<jupyter_code>array_3d[0, :, :]<jupyter_output><empty_output><jupyter_text>Get top plane.<jupyter_code>array_3d[:, 0, :]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13536243492190628, 'fraction_numerical': 0.0210252302763316, 'mean_word_length': 7.8250883392226145, 'pattern_counts': {'":': 0, '<': 129, '<?xml version=': 0, '>': 129, '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': '32', 'dir': 'jupyter-structured-clean-dedup', 'id': '459201', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1927, 'n_tokens_neox': 1874, 'n_words': 437, 'path': 'Python/Numpy/numpy.ipynb', 'repo_name': 'Gjacquenot/training-material', 'size': '4994'}