<jupyter_start><jupyter_text>Data type (`dtype`)In contrast to built-in Python containers (like lists)  NumPy arrays can store elements of pre-determined type only. All elements of an array must be of the same type. To see the type of array contents you can use the `dtype` attribute.<jupyter_code>import numpy as np
a = np.array([1., 2])
a.dtype<jupyter_output><empty_output><jupyter_text>Quiz: Data typesGuess the data type of the following arrays:```a = np.array([[1, 2],               [2, 3]])b = np.array(['a', 'b', 'c'])c = np.array([1, 2, 'a'])d = np.array([np.dot, np.array])e = np.random.randn(5) > 0f = np.arange(5)g = np.array([[1], [2, 3]])```  Exercise: OverflowConstruct the array ```pythonx = np.array([0, 1, 2, 255], dtype=np.uint8)```Canyou explain the difference obtained by `x + 1` and `x.astype(int) + 1`?  Structured arrays `dtype` may also be a structure of several items as long as they are the same type. You can find out more from the examples in the docs:<jupyter_code>np.info(np.dtype)
rec_array = np.array([(0, 'a'), 
                      (1, 'b'),
                      (2, 'c')], dtype=[('a', np.int32), ('b', '|S1')])<jupyter_output><empty_output><jupyter_text>Structured array is somewhat similar to a table with multiple rows and columns. You can access different fields by their name:<jupyter_code>rec_array['a']
rec_array['b']<jupyter_output><empty_output><jupyter_text>If you index by an integer you get a single row:<jupyter_code>rec_array[0]<jupyter_output><empty_output><jupyter_text>QuizCan you predict the `shape` and `strides` of the following array:```pythonA = np.array([('a', 0),              ('b', 1),              ('c', 2)], dtype=[('name', '|S1'),                                 ('value', np.int8)])```  Exercise: Structured data types*This exercise was proposed by <NAME> (https://python.g-node.org/python-summerschool-2014/_media/numpy_advanced.tar.bz2)*Design a data-type for storing the following record: - Timestamp in nanoseconds (a 64-bit unsigned integer) - Position (x- and y-coordinates, stored as floating point numbers)Use it to represent the following data:```dt = np.dtype()x = np.array([(100, (0, 0.5)),              (200, (0, 10.3)),              (300, (5.5, 15.1))], dtype=dt)```Have a look at the ``np.dtype`` docstring if you need help.After constructing ``x``, try to print all the ``x`` values for which timestampis greater than 100 (hint: something of the form ``y[y > 100]``).  Exercise: Structured file I/O*Modified from exercise by <NAME> (https://python.g-node.org/python-summerschool-2014/_media/numpy_advanced.tar.bz2)*Given the ``data.txt`` file with the following content:<jupyter_code>%%file data.txt
#rank         lemma (10 letters max)      frequency       dispersion
21             they                        1865844         0.96
42             her                         969591          0.91
49             as                          829018          0.95
7              to                          6332195         0.98
63             take                        670745          0.97
14             you                         3085642         0.92
35             go                          1151045         0.93
56             think                       772787          0.91
28             not                         1638883         0.98<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12753277711561384, 'fraction_numerical': 0.0533373063170441, 'mean_word_length': 2.3671013039117352, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 25, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '132871', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1130, 'n_tokens_neox': 1015, 'n_words': 331, 'path': '03 - Dtypes and structured array.ipynb', 'repo_name': 'btel/2016-erlangen-euroscipy-advanced-numpy', 'size': '3382'}